Skip to content

Commit 69e74ca

Browse files
laurittrask
andauthored
Rename common logging properties (#18851)
Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
1 parent a5e6882 commit 69e74ca

22 files changed

Lines changed: 254 additions & 117 deletions

File tree

docs/instrumentation-list.yaml

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9877,20 +9877,20 @@ libraries:
98779877
Enables adding baggage entries to the Log4j ThreadContext, prefixed with "baggage.".
98789878
type: boolean
98799879
default: false
9880-
- name: otel.instrumentation.common.logging.trace-id
9881-
declarative_name: java.common.logging.trace_id
9880+
- name: otel.instrumentation.common.logging.trace-id-key
9881+
declarative_name: java.common.logging.trace_id_key
98829882
description: |
98839883
Specifies the key name used to store the trace ID in the logging context.
98849884
type: string
98859885
default: trace_id
9886-
- name: otel.instrumentation.common.logging.span-id
9887-
declarative_name: java.common.logging.span_id
9886+
- name: otel.instrumentation.common.logging.span-id-key
9887+
declarative_name: java.common.logging.span_id_key
98889888
description: |
98899889
Specifies the key name used to store the span ID in the logging context.
98909890
type: string
98919891
default: span_id
9892-
- name: otel.instrumentation.common.logging.trace-flags
9893-
declarative_name: java.common.logging.trace_flags
9892+
- name: otel.instrumentation.common.logging.trace-flags-key
9893+
declarative_name: java.common.logging.trace_flags_key
98949894
description: |
98959895
Specifies the key name used to store the trace flags in the logging context.
98969896
type: string
@@ -9919,20 +9919,20 @@ libraries:
99199919
Enables adding baggage entries to the Log4j ThreadContext, prefixed with "baggage.".
99209920
type: boolean
99219921
default: false
9922-
- name: otel.instrumentation.common.logging.trace-id
9923-
declarative_name: java.common.logging.trace_id
9922+
- name: otel.instrumentation.common.logging.trace-id-key
9923+
declarative_name: java.common.logging.trace_id_key
99249924
description: |
99259925
Specifies the key name used to store the trace ID in the logging context.
99269926
type: string
99279927
default: trace_id
9928-
- name: otel.instrumentation.common.logging.span-id
9929-
declarative_name: java.common.logging.span_id
9928+
- name: otel.instrumentation.common.logging.span-id-key
9929+
declarative_name: java.common.logging.span_id_key
99309930
description: |
99319931
Specifies the key name used to store the span ID in the logging context.
99329932
type: string
99339933
default: span_id
9934-
- name: otel.instrumentation.common.logging.trace-flags
9935-
declarative_name: java.common.logging.trace_flags
9934+
- name: otel.instrumentation.common.logging.trace-flags-key
9935+
declarative_name: java.common.logging.trace_flags_key
99369936
description: |
99379937
Specifies the key name used to store the trace flags in the logging context.
99389938
type: string
@@ -9955,20 +9955,20 @@ libraries:
99559955
javaagent_target_versions:
99569956
- log4j:log4j:[1.2,)
99579957
configurations:
9958-
- name: otel.instrumentation.common.logging.trace-id
9959-
declarative_name: java.common.logging.trace_id
9958+
- name: otel.instrumentation.common.logging.trace-id-key
9959+
declarative_name: java.common.logging.trace_id_key
99609960
description: |
99619961
Specifies the key name used to store the trace ID in the logging context.
99629962
type: string
99639963
default: trace_id
9964-
- name: otel.instrumentation.common.logging.span-id
9965-
declarative_name: java.common.logging.span_id
9964+
- name: otel.instrumentation.common.logging.span-id-key
9965+
declarative_name: java.common.logging.span_id_key
99669966
description: |
99679967
Specifies the key name used to store the span ID in the logging context.
99689968
type: string
99699969
default: span_id
9970-
- name: otel.instrumentation.common.logging.trace-flags
9971-
declarative_name: java.common.logging.trace_flags
9970+
- name: otel.instrumentation.common.logging.trace-flags-key
9971+
declarative_name: java.common.logging.trace_flags_key
99729972
description: |
99739973
Specifies the key name used to store the trace flags in the logging context.
99749974
type: string
@@ -10076,20 +10076,20 @@ libraries:
1007610076
Enables adding baggage entries to the Logback MDC, prefixed with "baggage.".
1007710077
type: boolean
1007810078
default: false
10079-
- name: otel.instrumentation.common.logging.trace-id
10080-
declarative_name: java.common.logging.trace_id
10079+
- name: otel.instrumentation.common.logging.trace-id-key
10080+
declarative_name: java.common.logging.trace_id_key
1008110081
description: |
1008210082
Specifies the key name used to store the trace ID in the logging context.
1008310083
type: string
1008410084
default: trace_id
10085-
- name: otel.instrumentation.common.logging.span-id
10086-
declarative_name: java.common.logging.span_id
10085+
- name: otel.instrumentation.common.logging.span-id-key
10086+
declarative_name: java.common.logging.span_id_key
1008710087
description: |
1008810088
Specifies the key name used to store the span ID in the logging context.
1008910089
type: string
1009010090
default: span_id
10091-
- name: otel.instrumentation.common.logging.trace-flags
10092-
declarative_name: java.common.logging.trace_flags
10091+
- name: otel.instrumentation.common.logging.trace-flags-key
10092+
declarative_name: java.common.logging.trace_flags_key
1009310093
description: |
1009410094
Specifies the key name used to store the trace flags in the logging context.
1009510095
type: string

instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/config/internal/CommonConfig.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@
1313
import java.util.HashSet;
1414
import java.util.List;
1515
import java.util.Set;
16+
import java.util.concurrent.ConcurrentHashMap;
17+
import java.util.logging.Logger;
1618

1719
/**
1820
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
1921
* any time.
2022
*/
2123
public final class CommonConfig {
24+
private static final Logger logger = Logger.getLogger(CommonConfig.class.getName());
25+
private static final Set<String> warnedDeprecatedProperties = ConcurrentHashMap.newKeySet();
2226

2327
private final List<String> clientRequestHeaders;
2428
private final List<String> clientResponseHeaders;
@@ -89,12 +93,58 @@ public CommonConfig(OpenTelemetry openTelemetry) {
8993
.getBoolean("emit_experimental_telemetry/development", false);
9094
v3Preview = commonConfig.getBoolean("v3_preview", false);
9195
userConfig = new UserConfig(commonConfig, v3Preview);
96+
DeclarativeConfigProperties logging = commonConfig.get("logging");
9297
loggingTraceIdKey =
93-
commonConfig.get("logging").getString("trace_id", LoggingContextConstants.TRACE_ID);
98+
getConfig(
99+
logging,
100+
"trace_id_key",
101+
"trace_id",
102+
"otel.instrumentation.common.logging.trace-id-key",
103+
"otel.instrumentation.common.logging.trace-id",
104+
LoggingContextConstants.TRACE_ID);
94105
loggingSpanIdKey =
95-
commonConfig.get("logging").getString("span_id", LoggingContextConstants.SPAN_ID);
106+
getConfig(
107+
logging,
108+
"span_id_key",
109+
"span_id",
110+
"otel.instrumentation.common.logging.span-id-key",
111+
"otel.instrumentation.common.logging.span-id",
112+
LoggingContextConstants.SPAN_ID);
96113
loggingTraceFlagsKey =
97-
commonConfig.get("logging").getString("trace_flags", LoggingContextConstants.TRACE_FLAGS);
114+
getConfig(
115+
logging,
116+
"trace_flags_key",
117+
"trace_flags",
118+
"otel.instrumentation.common.logging.trace-flags-key",
119+
"otel.instrumentation.common.logging.trace-flags",
120+
LoggingContextConstants.TRACE_FLAGS);
121+
}
122+
123+
private static String getConfig(
124+
DeclarativeConfigProperties config,
125+
String newDeclarativeKey,
126+
String oldDeclarativeKey,
127+
String newProperty,
128+
String oldProperty,
129+
String defaultValue) {
130+
String value = config.getString(newDeclarativeKey);
131+
if (value != null) {
132+
return value;
133+
}
134+
value = config.getString(oldDeclarativeKey);
135+
if (value != null) {
136+
if (warnedDeprecatedProperties.add(oldProperty)) {
137+
logger.warning(
138+
"The "
139+
+ oldProperty
140+
+ " setting and the equivalent declarative configuration property"
141+
+ " are deprecated and will be removed in 3.0. Use "
142+
+ newProperty
143+
+ " or equivalent declarative configuration instead.");
144+
}
145+
return value;
146+
}
147+
return defaultValue;
98148
}
99149

100150
public List<String> getClientRequestHeaders() {

instrumentation/log4j/log4j-appender-2.17/library/src/main/java/io/opentelemetry/instrumentation/log4j/appender/v2_17/OpenTelemetryAppender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ private Context getContext(
389389
String traceId = contextDataAccessor.getValue(contextData, contextDataKeys.getTraceIdKey());
390390
String spanId = contextDataAccessor.getValue(contextData, contextDataKeys.getSpanIdKey());
391391
String traceFlags =
392-
contextDataAccessor.getValue(contextData, contextDataKeys.getTraceFlags());
392+
contextDataAccessor.getValue(contextData, contextDataKeys.getTraceFlagsKey());
393393
if (traceId != null && spanId != null && traceFlags != null) {
394394
warnIfUsingLegacyContextDataForAsyncLoggers(event);
395395
return Context.root()

instrumentation/log4j/log4j-appender-2.17/library/src/test/java/io/opentelemetry/instrumentation/log4j/appender/v2_17/OpenTelemetryAppenderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void logWithSpanContextFromContextData() {
164164
StringMap contextData = ContextDataFactory.createContextData();
165165
contextData.putValue(contextDataKeys.getTraceIdKey(), traceId);
166166
contextData.putValue(contextDataKeys.getSpanIdKey(), spanId);
167-
contextData.putValue(contextDataKeys.getTraceFlags(), traceFlags);
167+
contextData.putValue(contextDataKeys.getTraceFlagsKey(), traceFlags);
168168

169169
StatusMessageCollector statusMessages = new StatusMessageCollector();
170170
StatusLogger.getLogger().registerListener(statusMessages);

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/javaagent/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
| ----------------------------------------------------- | ------- | ------------- | ------------------------------------------------------------------ |
55
| `otel.instrumentation.log4j-context-data.add-baggage` | Boolean | `false` | Enable exposing baggage attributes through MDC. |
66
| `otel.instrumentation.common.mdc.resource-attributes` | String | | Comma separated list of resource attributes to expose through MDC. |
7-
| `otel.instrumentation.common.logging.trace-id` | String | `trace_id` | Customize MDC key name for the trace id. |
8-
| `otel.instrumentation.common.logging.span-id` | String | `span_id` | Customize MDC key name for the span id. |
9-
| `otel.instrumentation.common.logging.trace-flags` | String | `trace_flags` | Customize MDC key name for the trace flags. |
7+
| `otel.instrumentation.common.logging.trace-id-key` | String | `trace_id` | Customize MDC key name for the trace id. |
8+
| `otel.instrumentation.common.logging.span-id-key` | String | `span_id` | Customize MDC key name for the span id. |
9+
| `otel.instrumentation.common.logging.trace-flags-key` | String | `trace_flags` | Customize MDC key name for the trace flags. |

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/javaagent/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ testing {
6868
targets {
6969
all {
7070
testTask.configure {
71-
jvmArgs("-Dotel.instrumentation.common.logging.trace-id=trace_id_test")
72-
jvmArgs("-Dotel.instrumentation.common.logging.span-id=span_id_test")
73-
jvmArgs("-Dotel.instrumentation.common.logging.trace-flags=trace_flags_test")
71+
jvmArgs("-Dotel.instrumentation.common.logging.trace-id-key=trace_id_test")
72+
jvmArgs("-Dotel.instrumentation.common.logging.span-id-key=span_id_test")
73+
jvmArgs("-Dotel.instrumentation.common.logging.trace-flags-key=trace_flags_test")
7474
jvmArgs("-Dlog4j2.enable.threadlocals=true")
7575
}
7676
}

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ will be added to the context when a log statement is made when a span is active:
4444

4545
These keys can be customized using the following system properties or environment variables:
4646

47-
| System property | Environment variable |
48-
| ------------------------------------------------- | ------------------------------------------------- |
49-
| `otel.instrumentation.common.logging.trace-id` | `OTEL_INSTRUMENTATION_COMMON_LOGGING_TRACE_ID` |
50-
| `otel.instrumentation.common.logging.span-id` | `OTEL_INSTRUMENTATION_COMMON_LOGGING_SPAN_ID` |
51-
| `otel.instrumentation.common.logging.trace-flags` | `OTEL_INSTRUMENTATION_COMMON_LOGGING_TRACE_FLAGS` |
47+
| System property | Environment variable |
48+
| ----------------------------------------------------- | ----------------------------------------------------- |
49+
| `otel.instrumentation.common.logging.trace-id-key` | `OTEL_INSTRUMENTATION_COMMON_LOGGING_TRACE_ID_KEY` |
50+
| `otel.instrumentation.common.logging.span-id-key` | `OTEL_INSTRUMENTATION_COMMON_LOGGING_SPAN_ID_KEY` |
51+
| `otel.instrumentation.common.logging.trace-flags-key` | `OTEL_INSTRUMENTATION_COMMON_LOGGING_TRACE_FLAGS_KEY` |
5252

5353
If the `otel.instrumentation.log4j-context-data.add-baggage` system property (or the
5454
`OTEL_INSTRUMENTATION_LOG4J_CONTEXT_DATA_ADD_BAGGAGE` environment variable) is set to `true`,

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ tasks {
3434
filter {
3535
includeTestsMatching("LibraryLog4j2LoggingKeysTest")
3636
}
37-
jvmArgs("-Dotel.instrumentation.common.logging.trace-id=trace_id_test")
38-
jvmArgs("-Dotel.instrumentation.common.logging.span-id=span_id_test")
39-
jvmArgs("-Dotel.instrumentation.common.logging.trace-flags=trace_flags_test")
37+
jvmArgs("-Dotel.instrumentation.common.logging.trace-id-key=trace_id_test")
38+
jvmArgs("-Dotel.instrumentation.common.logging.span-id-key=span_id_test")
39+
jvmArgs("-Dotel.instrumentation.common.logging.trace-flags-key=trace_flags_test")
4040
}
4141

4242
named("check") {

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/OpenTelemetryContextDataProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Map<String, String> supplyContextData() {
8080
SpanContext spanContext = currentSpan.getSpanContext();
8181
contextData.put(contextDataKeys.getTraceIdKey(), spanContext.getTraceId());
8282
contextData.put(contextDataKeys.getSpanIdKey(), spanContext.getSpanId());
83-
contextData.put(contextDataKeys.getTraceFlags(), spanContext.getTraceFlags().asHex());
83+
contextData.put(contextDataKeys.getTraceFlagsKey(), spanContext.getTraceFlags().asHex());
8484

8585
if (Configuration.baggageEnabled) {
8686
Baggage baggage = Baggage.fromContext(context);

instrumentation/log4j/log4j-context-data/log4j-context-data-2.17/library-autoconfigure/src/main/java/io/opentelemetry/instrumentation/log4j/contextdata/v2_17/internal/ContextDataKeys.java

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,104 @@
1010
import io.opentelemetry.instrumentation.api.incubator.config.internal.DeclarativeConfigUtil;
1111
import io.opentelemetry.instrumentation.api.incubator.log.LoggingContextConstants;
1212
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil;
13+
import java.util.Set;
14+
import java.util.concurrent.ConcurrentHashMap;
15+
import java.util.logging.Logger;
1316

1417
/**
1518
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
1619
* any time.
1720
*/
1821
public final class ContextDataKeys {
22+
private static final Logger logger = Logger.getLogger(ContextDataKeys.class.getName());
23+
private static final Set<String> warnedDeprecatedProperties = ConcurrentHashMap.newKeySet();
1924

2025
private final String traceIdKey;
2126
private final String spanIdKey;
22-
private final String traceFlags;
27+
private final String traceFlagsKey;
2328

24-
@SuppressWarnings("deprecation") // using deprecated config property
2529
public static ContextDataKeys create(OpenTelemetry openTelemetry) {
2630
DeclarativeConfigProperties logging =
2731
DeclarativeConfigUtil.getInstrumentationConfig(openTelemetry, "common").get("logging");
28-
return new ContextDataKeys(
29-
logging.getString(
32+
String traceIdKey =
33+
getConfig(
34+
logging,
35+
"trace_id_key",
3036
"trace_id",
31-
ConfigPropertiesUtil.getString(
32-
"otel.instrumentation.common.logging.trace-id", LoggingContextConstants.TRACE_ID)),
33-
logging.getString(
37+
"otel.instrumentation.common.logging.trace-id-key",
38+
"otel.instrumentation.common.logging.trace-id",
39+
LoggingContextConstants.TRACE_ID);
40+
String spanIdKey =
41+
getConfig(
42+
logging,
43+
"span_id_key",
3444
"span_id",
35-
ConfigPropertiesUtil.getString(
36-
"otel.instrumentation.common.logging.span-id", LoggingContextConstants.SPAN_ID)),
37-
logging.getString(
45+
"otel.instrumentation.common.logging.span-id-key",
46+
"otel.instrumentation.common.logging.span-id",
47+
LoggingContextConstants.SPAN_ID);
48+
String traceFlagsKey =
49+
getConfig(
50+
logging,
51+
"trace_flags_key",
3852
"trace_flags",
39-
ConfigPropertiesUtil.getString(
40-
"otel.instrumentation.common.logging.trace-flags",
41-
LoggingContextConstants.TRACE_FLAGS)));
53+
"otel.instrumentation.common.logging.trace-flags-key",
54+
"otel.instrumentation.common.logging.trace-flags",
55+
LoggingContextConstants.TRACE_FLAGS);
56+
return new ContextDataKeys(traceIdKey, spanIdKey, traceFlagsKey);
4257
}
4358

44-
private ContextDataKeys(String traceIdKey, String spanIdKey, String traceFlags) {
59+
@SuppressWarnings("deprecation") // using deprecated ConfigPropertiesUtil
60+
private static String getConfig(
61+
DeclarativeConfigProperties config,
62+
String newDeclarativeKey,
63+
String oldDeclarativeKey,
64+
String newProperty,
65+
String oldProperty,
66+
String defaultValue) {
67+
String value = config.getString(newDeclarativeKey);
68+
if (value != null) {
69+
return value;
70+
}
71+
value = config.getString(oldDeclarativeKey);
72+
if (value != null) {
73+
logDeprecationWarning(
74+
oldProperty,
75+
"The "
76+
+ oldProperty
77+
+ " setting and the equivalent declarative configuration property"
78+
+ " are deprecated and will be removed in 3.0. Use "
79+
+ newProperty
80+
+ " or equivalent declarative configuration instead.");
81+
return value;
82+
}
83+
value = ConfigPropertiesUtil.getString(newProperty);
84+
if (value != null) {
85+
return value;
86+
}
87+
value = ConfigPropertiesUtil.getString(oldProperty);
88+
if (value != null) {
89+
logDeprecationWarning(
90+
oldProperty,
91+
"The '"
92+
+ oldProperty
93+
+ "' system property is deprecated and will be removed in 3.0. Use '"
94+
+ newProperty
95+
+ "' instead.");
96+
return value;
97+
}
98+
return defaultValue;
99+
}
100+
101+
private static void logDeprecationWarning(String deprecatedProperty, String message) {
102+
if (warnedDeprecatedProperties.add(deprecatedProperty)) {
103+
logger.warning(message);
104+
}
105+
}
106+
107+
private ContextDataKeys(String traceIdKey, String spanIdKey, String traceFlagsKey) {
45108
this.traceIdKey = traceIdKey;
46109
this.spanIdKey = spanIdKey;
47-
this.traceFlags = traceFlags;
110+
this.traceFlagsKey = traceFlagsKey;
48111
}
49112

50113
public String getTraceIdKey() {
@@ -55,7 +118,7 @@ public String getSpanIdKey() {
55118
return spanIdKey;
56119
}
57120

58-
public String getTraceFlags() {
59-
return traceFlags;
121+
public String getTraceFlagsKey() {
122+
return traceFlagsKey;
60123
}
61124
}

0 commit comments

Comments
 (0)