Line data Source code
1 : // Licensed under the Apache License, Version 2.0
2 : // Copyright 2025, Michael Bushe, All rights reserved.
3 :
4 : import 'dart:io' as io;
5 :
6 : import 'env_constants.dart';
7 : import 'env_from_define.dart';
8 : import 'environment_service.dart';
9 :
10 : /// IO implementation of the environment service.
11 : ///
12 : /// This implementation is used on all platforms except web (Dart VM, Flutter
13 : /// native, and Flutter desktop).
14 : ///
15 : /// Environment variable lookup priority:
16 : /// 1. String.fromEnvironment (--dart-define values)
17 : /// 2. io.Platform.environment (system environment variables)
18 : ///
19 : /// The implementation uses String.fromEnvironment with empty string as the
20 : /// default value. If the result is empty, it falls back to checking
21 : /// io.Platform.environment. This allows --dart-define values to take
22 : /// precedence over system environment variables, as per OpenTelemetry
23 : /// specification recommendations.
24 : class EnvironmentService implements EnvironmentServiceInterface {
25 219 : static final EnvironmentService _instance = EnvironmentService._();
26 :
27 : /// The singleton instance of the EnvironmentService.
28 146 : static EnvironmentService get instance => _instance;
29 :
30 73 : EnvironmentService._();
31 :
32 : /// Gets the value of an environment variable.
33 : ///
34 : /// Returns the value from the first available source:
35 : /// 1. String.fromEnvironment if defined via --dart-define
36 : /// 2. io.Platform.environment (system environment variables)
37 : /// 3. null if not found
38 : ///
39 : /// Note: String.fromEnvironment only works for compile-time constants that
40 : /// are in our [supportedEnvVars] set. This is a limitation of the Dart
41 : /// language when using --dart-define.
42 : ///
43 : /// @param key The name of the environment variable to retrieve
44 : /// @return The value of the environment variable, or null if not found
45 73 : @override
46 : String? getValue(String key) {
47 : // Priority 1: String.fromEnvironment (--dart-define values)
48 : // Only check if this is a known environment variable
49 : String? value;
50 73 : if (supportedEnvVars.contains(key)) {
51 73 : final fromEnvironment = getFromEnvironment(key);
52 73 : if (fromEnvironment != null && fromEnvironment.isNotEmpty) {
53 : value = fromEnvironment;
54 : }
55 : }
56 :
57 : // Priority 2: Platform environment variables
58 146 : value ??= io.Platform.environment[key];
59 :
60 : // Handle comma-separated values for --define compatibility
61 : // The --define flag cannot handle commas in values, so we use semicolons
62 : // as a delimiter for these specific variables and convert them back
63 : if (value != null) {
64 : switch (key) {
65 73 : case otelResourceAttributes:
66 73 : case otelPropagators:
67 73 : case otelExporterOtlpHeaders:
68 73 : case otelExporterOtlpTracesHeaders:
69 73 : case otelExporterOtlpMetricsHeaders:
70 73 : case otelExporterOtlpLogsHeaders:
71 0 : value = value.replaceAll(';', ',');
72 : break;
73 : }
74 : }
75 :
76 : return value;
77 : }
78 : }
|