Line data Source code
1 : // Copyright The OpenTelemetry Authors
2 : // SPDX-License-Identifier: Apache-2.0
3 :
4 : // Native (VM / Flutter mobile / Flutter desktop) implementations of the
5 : // resource detectors that read from `dart:io`. Imported only on
6 : // non-web platforms via the conditional export in `native_detectors.dart`.
7 :
8 : import 'dart:io' as io;
9 :
10 : import 'package:dartastic_opentelemetry_api/dartastic_opentelemetry_api.dart';
11 :
12 : import 'resource.dart';
13 : import 'resource_detector.dart';
14 :
15 : /// Detects process-related resource information.
16 : ///
17 : /// Populates resource attributes with information about the current process
18 : /// (executable name, command line, runtime). Native-only — `dart:io` is
19 : /// not available in the browser.
20 : ///
21 : /// Semantic conventions:
22 : /// https://opentelemetry.io/docs/specs/semconv/resource/process/
23 : class ProcessResourceDetector implements ResourceDetector {
24 53 : @override
25 : Future<Resource> detect() async {
26 : if (OTelFactory.otelFactory == null) {
27 1 : throw StateError('OTel initialize must be called first.');
28 : }
29 53 : return ResourceCreate.create(
30 106 : OTelFactory.otelFactory!.attributesFromMap({
31 53 : 'process.executable.name': io.Platform.executable,
32 106 : 'process.command_line': io.Platform.executableArguments.join(' '),
33 : 'process.runtime.name': 'dart',
34 53 : 'process.runtime.version': io.Platform.version,
35 106 : 'process.num_threads': io.Platform.numberOfProcessors.toString(),
36 : }),
37 : );
38 : }
39 : }
40 :
41 : /// Detects host-related resource information.
42 : ///
43 : /// Populates resource attributes with information about the host machine
44 : /// (hostname, architecture, OS details). Native-only — `dart:io` is not
45 : /// available in the browser.
46 : ///
47 : /// Semantic conventions:
48 : /// https://opentelemetry.io/docs/specs/semconv/resource/host/
49 : class HostResourceDetector implements ResourceDetector {
50 52 : @override
51 : Future<Resource> detect() async {
52 : if (OTelFactory.otelFactory == null) {
53 1 : throw StateError('OTel initialize must be called first.');
54 : }
55 52 : final attributes = <String, Object>{
56 52 : 'host.name': io.Platform.localHostname,
57 52 : 'host.arch': io.Platform.localHostname,
58 52 : 'host.processors': io.Platform.numberOfProcessors,
59 52 : 'host.os.name': io.Platform.operatingSystem,
60 52 : 'host.locale': io.Platform.localeName,
61 : };
62 :
63 52 : if (io.Platform.isLinux) {
64 52 : attributes['os.type'] = 'linux';
65 0 : } else if (io.Platform.isWindows) {
66 0 : attributes['os.type'] = 'windows';
67 0 : } else if (io.Platform.isMacOS) {
68 0 : attributes['os.type'] = 'macos';
69 0 : } else if (io.Platform.isAndroid) {
70 0 : attributes['os.type'] = 'android';
71 0 : } else if (io.Platform.isIOS) {
72 0 : attributes['os.type'] = 'ios';
73 : }
74 :
75 104 : attributes['os.version'] = io.Platform.operatingSystemVersion;
76 :
77 52 : return ResourceCreate.create(
78 52 : OTelFactory.otelFactory!.attributesFromMap(attributes),
79 : );
80 : }
81 : }
|