6
6
package io .opentelemetry .instrumentation .spring .autoconfigure .internal ;
7
7
8
8
import io .opentelemetry .sdk .autoconfigure .spi .ConfigurationException ;
9
- import java .util .HashMap ;
10
- import java .util .Map ;
11
- import javax .annotation .Nullable ;
12
9
import org .springframework .core .env .Environment ;
13
10
14
11
/**
18
15
public class EarlyConfig {
19
16
private EarlyConfig () {}
20
17
21
- private static final Map <String , String > DECLARATIVE_CONFIG_PROPERTY_MAPPING = new HashMap <>();
22
-
23
- static {
24
- DECLARATIVE_CONFIG_PROPERTY_MAPPING .put ("otel.sdk.disabled" , "otel.disabled" );
25
- DECLARATIVE_CONFIG_PROPERTY_MAPPING .put (
26
- "otel.instrumentation.%s.enabled" , "otel.instrumentation/development.java.%s.enabled" );
27
- }
28
-
29
18
public static boolean otelEnabled (Environment environment ) {
30
- return !environment .getProperty (
31
- translatePropertyName (environment , "otel.sdk.disabled" , null ), Boolean .class , false );
19
+ boolean disabled =
20
+ environment .getProperty (
21
+ getPropertyName (environment , "otel.sdk.disabled" , "otel.disabled" ),
22
+ Boolean .class ,
23
+ false );
24
+ return !disabled ;
32
25
}
33
26
34
27
public static boolean isDeclarativeConfig (Environment environment ) {
@@ -57,44 +50,33 @@ public static boolean isDefaultEnabled(Environment environment) {
57
50
}
58
51
}
59
52
60
- public static String translatePropertyName (
61
- Environment environment , String name , @ Nullable String arg ) {
53
+ public static String translatePropertyName (Environment environment , String name ) {
62
54
if (isDeclarativeConfig (environment )) {
63
- String key = declarativeConfigKey (name );
64
- if (arg != null ) {
65
- key = String .format (key , arg );
55
+ if (name .startsWith ("otel.instrumentation." )) {
56
+ return String .format (
57
+ "otel.instrumentation/development.java.%s" ,
58
+ name .substring ("otel.instrumentation." .length ()))
59
+ .replace ('-' , '_' );
66
60
}
67
- return key .replace ('-' , '_' );
68
- } else {
69
- String key = name ;
70
- if (arg != null ) {
71
- key = String .format (key , arg );
72
- }
73
- return key ;
74
- }
75
- }
76
61
77
- private static String declarativeConfigKey (String name ) {
78
- String value = DECLARATIVE_CONFIG_PROPERTY_MAPPING .get (name );
79
- if (value != null ) {
80
- return value ;
81
- }
82
- if (name .startsWith ("otel.instrumentation." )) {
83
- return String .format (
84
- "otel.instrumentation/development.java.%s" ,
85
- name .substring ("otel.instrumentation." .length ()));
62
+ throw new IllegalStateException (
63
+ "No mapping found for property name: " + name + ". Please report this bug." );
64
+ } else {
65
+ return name ;
86
66
}
87
-
88
- throw new IllegalStateException (
89
- "No mapping found for property name: " + name + ". Please report this bug." );
90
67
}
91
68
92
69
public static boolean isInstrumentationEnabled (
93
70
Environment environment , String name , boolean defaultValue ) {
94
- Boolean explicit =
71
+ String property =
95
72
environment .getProperty (
96
- translatePropertyName (environment , "otel.instrumentation.%s.enabled" , name ),
97
- Boolean .class );
73
+ String .format (
74
+ getPropertyName (
75
+ environment ,
76
+ "otel.instrumentation.%s.enabled" ,
77
+ "otel.instrumentation/development.java.%s.enabled" )),
78
+ name );
79
+ Boolean explicit = environment .getProperty (property , Boolean .class );
98
80
if (explicit != null ) {
99
81
return explicit ;
100
82
}
@@ -103,4 +85,9 @@ public static boolean isInstrumentationEnabled(
103
85
}
104
86
return isDefaultEnabled (environment );
105
87
}
88
+
89
+ private static String getPropertyName (
90
+ Environment environment , String propertyBased , String declarative ) {
91
+ return isDeclarativeConfig (environment ) ? declarative : propertyBased ;
92
+ }
106
93
}
0 commit comments