Skip to content

Commit 265b5ea

Browse files
authored
Implement otel.event.name (#16220)
1 parent 0d52841 commit 265b5ea

23 files changed

Lines changed: 535 additions & 159 deletions

File tree

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Settings for the JBoss Log Manager instrumentation
22

3-
| System property | Type | Default | Description |
4-
|-----------------------------------------------------------------------------|---------|---------|------------------------------------------------------------------------------------------------------------------------------------|
5-
| `otel.instrumentation.jboss-logmanager.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
6-
| `otel.instrumentation.jboss-logmanager.experimental.capture-mdc-attributes` | String | | Comma separated list of MDC attributes to capture. Use the wildcard character `*` to capture all attributes. |
7-
| `otel.instrumentation.jboss-logmanager.experimental.capture-event-name` | Boolean | `false` | Enable moving the `event.name` attribute (captured by one of the other mechanisms of capturing attributes) to the log event name. |
3+
| System property | Type | Default | Description |
4+
|-----------------------------------------------------------------------------|---------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------|
5+
| `otel.instrumentation.jboss-logmanager.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
6+
| `otel.instrumentation.jboss-logmanager.experimental.capture-mdc-attributes` | String | | Comma separated list of MDC attributes to capture. Use the wildcard character `*` to capture all attributes. |
7+
| `otel.instrumentation.jboss-logmanager.experimental.capture-event-name` | Boolean | `false` | **Deprecated.** Enable moving the `event.name` attribute (captured by one of the other mechanisms of capturing attributes) to the log event name. |
8+
9+
The `otel.event.name` key is supported in MDC entries. When present, its value is used as the log event name and is not emitted as an attribute.

instrumentation/jboss-logmanager/jboss-logmanager-appender-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jbosslogmanager/appender/v1_1/LoggingEventMapper.java

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.javaagent.instrumentation.jbosslogmanager.appender.v1_1;
77

8+
import static io.opentelemetry.semconv.incubating.OtelIncubatingAttributes.OTEL_EVENT_NAME;
89
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_ID;
910
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_NAME;
1011
import static java.util.Collections.emptyList;
@@ -26,6 +27,9 @@
2627

2728
public final class LoggingEventMapper {
2829

30+
private static final java.util.logging.Logger logger =
31+
java.util.logging.Logger.getLogger(LoggingEventMapper.class.getName());
32+
2933
public static final LoggingEventMapper INSTANCE = new LoggingEventMapper();
3034

3135
private static final Cache<String, AttributeKey<String>> mdcAttributeKeys = Cache.bounded(100);
@@ -53,6 +57,11 @@ private LoggingEventMapper() {
5357
.getScalarList("capture_mdc_attributes/development", String.class, emptyList());
5458
this.captureAllMdcAttributes =
5559
captureMdcAttributes.size() == 1 && captureMdcAttributes.get(0).equals("*");
60+
if (captureEventName) {
61+
logger.warning(
62+
"The otel.instrumentation.jboss-logmanager.experimental.capture-event-name setting is"
63+
+ " deprecated and will be removed in a future version.");
64+
}
5665
}
5766

5867
public void capture(Logger logger, ExtLogRecord record) {
@@ -100,37 +109,47 @@ public void capture(Logger logger, ExtLogRecord record) {
100109
private void captureMdcAttributes(LogRecordBuilder builder) {
101110

102111
Map<String, String> context = MDC.copy();
112+
if (context == null) {
113+
return;
114+
}
115+
116+
// otel.event.name takes priority over event.name
117+
String otelEventName = context.get(OTEL_EVENT_NAME.getKey());
118+
if (otelEventName != null) {
119+
builder.setEventName(otelEventName);
120+
} else if (captureEventName) {
121+
String eventName = context.get(EVENT_NAME.getKey());
122+
if (eventName != null) {
123+
builder.setEventName(eventName);
124+
}
125+
}
103126

104127
if (captureAllMdcAttributes) {
105-
if (context != null) {
106-
for (Map.Entry<String, String> entry : context.entrySet()) {
107-
setAttributeOrEventName(builder, getMdcAttributeKey(entry.getKey()), entry.getValue());
128+
for (Map.Entry<String, String> entry : context.entrySet()) {
129+
String key = entry.getKey();
130+
if (!OTEL_EVENT_NAME.getKey().equals(key)
131+
&& !(captureEventName && EVENT_NAME.getKey().equals(key))) {
132+
builder.setAttribute(getMdcAttributeKey(key), entry.getValue());
108133
}
109134
}
110135
return;
111136
}
112137

113138
for (String key : captureMdcAttributes) {
114-
Object value = context.get(key);
115-
setAttributeOrEventName(builder, getMdcAttributeKey(key), value);
139+
if (!OTEL_EVENT_NAME.getKey().equals(key)
140+
&& !(captureEventName && EVENT_NAME.getKey().equals(key))) {
141+
String value = context.get(key);
142+
if (value != null) {
143+
builder.setAttribute(getMdcAttributeKey(key), value);
144+
}
145+
}
116146
}
117147
}
118148

119149
public static AttributeKey<String> getMdcAttributeKey(String key) {
120150
return mdcAttributeKeys.computeIfAbsent(key, AttributeKey::stringKey);
121151
}
122152

123-
private void setAttributeOrEventName(
124-
LogRecordBuilder builder, AttributeKey<String> key, Object value) {
125-
if (value != null) {
126-
if (captureEventName && key.equals(EVENT_NAME)) {
127-
builder.setEventName(value.toString());
128-
} else {
129-
builder.setAttribute(key, value.toString());
130-
}
131-
}
132-
}
133-
134153
private static Severity levelToSeverity(java.util.logging.Level level) {
135154
int levelInt = level.intValue();
136155
if (levelInt >= Level.FATAL.intValue()) {

instrumentation/jboss-logmanager/jboss-logmanager-appender-1.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/jbosslogmanager/appender/v1_1/JbossLogmanagerTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
import org.jboss.logmanager.LogContext;
3232
import org.jboss.logmanager.Logger;
3333
import org.jboss.logmanager.MDC;
34-
import org.junit.jupiter.api.Test;
3534
import org.junit.jupiter.api.extension.RegisterExtension;
3635
import org.junit.jupiter.params.ParameterizedTest;
3736
import org.junit.jupiter.params.provider.Arguments;
3837
import org.junit.jupiter.params.provider.MethodSource;
38+
import org.junit.jupiter.params.provider.ValueSource;
3939

4040
class JbossLogmanagerTest {
4141

@@ -193,17 +193,18 @@ private static void performLogging(
193193
}
194194
}
195195

196-
@Test
197-
void testMdc() {
196+
@ParameterizedTest
197+
@ValueSource(strings = {"event.name", "otel.event.name"})
198+
void testMdc(String eventNameProperty) {
198199
MDC.put("key1", "val1");
199200
MDC.put("key2", "val2");
200-
MDC.put("event.name", "MyEventName");
201+
MDC.put(eventNameProperty, "MyEventName");
201202
try {
202203
logger.info("xyz");
203204
} finally {
204205
MDC.remove("key1");
205206
MDC.remove("key2");
206-
MDC.remove("event.name");
207+
MDC.remove(eventNameProperty);
207208
}
208209

209210
testing.waitAndAssertLogRecords(
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Settings for the Log4j Appender instrumentation
22

3-
| System property | Type | Default | Description |
4-
|-----------------------------------------------------------------------------------|---------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------|
5-
| `otel.instrumentation.log4j-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
6-
| `otel.instrumentation.log4j-appender.experimental.capture-code-attributes` | Boolean | `false` | Enable the capture of [source code attributes]. Note that capturing source code attributes at logging sites might add a performance overhead. |
7-
| `otel.instrumentation.log4j-appender.experimental.capture-mdc-attributes` | String | | Comma separated list of context data attributes to capture. Use the wildcard character `*` to capture all attributes. |
8-
| `otel.instrumentation.log4j-appender.experimental.capture-event-name` | Boolean | `false` | Enable moving the `event.name` attribute (captured by one of the other mechanisms of capturing attributes) to the log event name. |
3+
| System property | Type | Default | Description |
4+
|-----------------------------------------------------------------------------------|---------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
5+
| `otel.instrumentation.log4j-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
6+
| `otel.instrumentation.log4j-appender.experimental.capture-code-attributes` | Boolean | `false` | Enable the capture of [source code attributes]. Note that capturing source code attributes at logging sites might add a performance overhead. |
7+
| `otel.instrumentation.log4j-appender.experimental.capture-mdc-attributes` | String | | Comma separated list of context data attributes to capture. Use the wildcard character `*` to capture all attributes. |
8+
| `otel.instrumentation.log4j-appender.experimental.capture-event-name` | Boolean | `false` | **Deprecated.** Enable moving the `event.name` attribute (captured by one of the other mechanisms of capturing attributes) to the log event name. |
9+
10+
The `otel.event.name` key is supported in MDC entries. When present, its value is used as the log event name and is not emitted as an attribute.
911

1012
[source code attributes]: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/attributes.md#source-code-attributes

instrumentation/log4j/log4j-appender-1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/log4j/appender/v1_2/LogEventMapper.java

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static io.opentelemetry.semconv.CodeAttributes.CODE_FILE_PATH;
1111
import static io.opentelemetry.semconv.CodeAttributes.CODE_FUNCTION_NAME;
1212
import static io.opentelemetry.semconv.CodeAttributes.CODE_LINE_NUMBER;
13+
import static io.opentelemetry.semconv.incubating.OtelIncubatingAttributes.OTEL_EVENT_NAME;
1314
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_ID;
1415
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_NAME;
1516
import static java.util.Collections.emptyList;
@@ -26,13 +27,16 @@
2627
import java.util.Hashtable;
2728
import java.util.List;
2829
import java.util.Map;
30+
import java.util.logging.Logger;
2931
import org.apache.log4j.Category;
3032
import org.apache.log4j.MDC;
3133
import org.apache.log4j.Priority;
3234
import org.apache.log4j.spi.LocationInfo;
3335

3436
public final class LogEventMapper {
3537

38+
private static final Logger logger = Logger.getLogger(LogEventMapper.class.getName());
39+
3640
private static final Cache<String, AttributeKey<String>> mdcAttributeKeys = Cache.bounded(100);
3741

3842
public static final LogEventMapper INSTANCE = new LogEventMapper();
@@ -69,6 +73,11 @@ private LogEventMapper() {
6973
.collect(toMap(attr -> attr, LogEventMapper::getMdcAttributeKey));
7074
this.captureAllMdcAttributes =
7175
captureMdcAttributes.size() == 1 && captureMdcAttributes.get(0).equals("*");
76+
if (captureEventName) {
77+
logger.warning(
78+
"The otel.instrumentation.log4j-appender.experimental.capture-event-name setting is"
79+
+ " deprecated and will be removed in a future version.");
80+
}
7281
}
7382

7483
boolean captureCodeAttributes =
@@ -162,38 +171,51 @@ public void capture(
162171
private void captureMdcAttributes(LogRecordBuilder builder) {
163172

164173
Hashtable<?, ?> context = MDC.getContext();
174+
if (context == null) {
175+
return;
176+
}
177+
178+
// otel.event.name takes priority over event.name
179+
Object otelEventName = context.get(OTEL_EVENT_NAME.getKey());
180+
if (otelEventName instanceof String) {
181+
builder.setEventName((String) otelEventName);
182+
} else if (captureEventName) {
183+
Object eventName = context.get(EVENT_NAME.getKey());
184+
if (eventName != null) {
185+
builder.setEventName(eventName.toString());
186+
}
187+
}
165188

166189
if (captureAllMdcAttributes) {
167-
if (context != null) {
168-
for (Map.Entry<?, ?> entry : context.entrySet()) {
169-
setAttributeOrEventName(
170-
builder, getMdcAttributeKey(String.valueOf(entry.getKey())), entry.getValue());
190+
for (Map.Entry<?, ?> entry : context.entrySet()) {
191+
String key = String.valueOf(entry.getKey());
192+
if (!OTEL_EVENT_NAME.getKey().equals(key)
193+
&& !(captureEventName && EVENT_NAME.getKey().equals(key))) {
194+
Object value = entry.getValue();
195+
if (value != null) {
196+
builder.setAttribute(getMdcAttributeKey(key), value.toString());
197+
}
171198
}
172199
}
173200
return;
174201
}
175202

176203
for (Map.Entry<String, AttributeKey<String>> entry : captureMdcAttributes.entrySet()) {
177-
Object value = context.get(entry.getKey());
178-
setAttributeOrEventName(builder, entry.getValue(), value);
204+
String key = entry.getKey();
205+
if (!OTEL_EVENT_NAME.getKey().equals(key)
206+
&& !(captureEventName && EVENT_NAME.getKey().equals(key))) {
207+
Object value = context.get(key);
208+
if (value != null) {
209+
builder.setAttribute(entry.getValue(), value.toString());
210+
}
211+
}
179212
}
180213
}
181214

182215
private static AttributeKey<String> getMdcAttributeKey(String key) {
183216
return mdcAttributeKeys.computeIfAbsent(key, AttributeKey::stringKey);
184217
}
185218

186-
private void setAttributeOrEventName(
187-
LogRecordBuilder builder, AttributeKey<String> key, Object value) {
188-
if (value != null) {
189-
if (captureEventName && key.equals(EVENT_NAME)) {
190-
builder.setEventName(value.toString());
191-
} else {
192-
builder.setAttribute(key, value.toString());
193-
}
194-
}
195-
}
196-
197219
private static Severity levelToSeverity(Priority level) {
198220
int lev = level.toInt();
199221
if (lev <= TRACE_INT) {

instrumentation/log4j/log4j-appender-1.2/javaagent/src/test/java/io/opentelemetry/instrumentation/log4j/appender/v1_2/Log4j1Test.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,22 @@ private static void test(
169169
}
170170
}
171171

172-
@Test
173-
void testMdc() {
172+
private static Stream<Arguments> eventNameProperties() {
173+
return Stream.of(Arguments.of("event.name"), Arguments.of("otel.event.name"));
174+
}
175+
176+
@ParameterizedTest
177+
@MethodSource("eventNameProperties")
178+
void testMdc(String eventNameProperty) {
174179
MDC.put("key1", "val1");
175180
MDC.put("key2", "val2");
176-
MDC.put("event.name", "MyEventName");
181+
MDC.put(eventNameProperty, "MyEventName");
177182
try {
178183
logger.info("xyz");
179184
} finally {
180185
MDC.remove("key1");
181186
MDC.remove("key2");
182-
MDC.remove("event.name");
187+
MDC.remove(eventNameProperty);
183188
}
184189

185190
List<AttributeAssertion> assertions =
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Settings for the Log4j Appender instrumentation
22

3-
| System property | Type | Default | Description |
4-
|-----------------------------------------------------------------------------------|---------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------|
5-
| `otel.instrumentation.log4j-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
6-
| `otel.instrumentation.log4j-appender.experimental.capture-code-attributes` | Boolean | `false` | Enable the capture of [source code attributes]. Note that capturing source code attributes at logging sites might add a performance overhead. |
7-
| `otel.instrumentation.log4j-appender.experimental.capture-map-message-attributes` | Boolean | `false` | Enable the capture of `MapMessage` attributes. |
8-
| `otel.instrumentation.log4j-appender.experimental.capture-marker-attribute` | Boolean | `false` | Enable the capture of Log4j markers as attributes. |
9-
| `otel.instrumentation.log4j-appender.experimental.capture-mdc-attributes` | String | | Comma separated list of context data attributes to capture. Use the wildcard character `*` to capture all attributes. |
10-
| `otel.instrumentation.log4j-appender.experimental.capture-event-name` | Boolean | `false` | Enable moving the `event.name` attribute (captured by one of the other mechanisms of capturing attributes) to the log event name. |
3+
| System property | Type | Default | Description |
4+
|-----------------------------------------------------------------------------------|---------|---------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|
5+
| `otel.instrumentation.log4j-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
6+
| `otel.instrumentation.log4j-appender.experimental.capture-code-attributes` | Boolean | `false` | Enable the capture of [source code attributes]. Note that capturing source code attributes at logging sites might add a performance overhead. |
7+
| `otel.instrumentation.log4j-appender.experimental.capture-map-message-attributes` | Boolean | `false` | Enable the capture of `MapMessage` attributes. |
8+
| `otel.instrumentation.log4j-appender.experimental.capture-marker-attribute` | Boolean | `false` | Enable the capture of Log4j markers as attributes. |
9+
| `otel.instrumentation.log4j-appender.experimental.capture-mdc-attributes` | String | | Comma separated list of context data attributes to capture. Use the wildcard character `*` to capture all attributes. |
10+
| `otel.instrumentation.log4j-appender.experimental.capture-event-name` | Boolean | `false` | **Deprecated.** Enable moving the `event.name` attribute (captured by one of the other mechanisms of capturing attributes) to the log event name. |
11+
12+
The `otel.event.name` key is supported in `MapMessage` entries and context data entries. When present, its value is used as the log event name and is not emitted as an attribute.
1113

1214
[source code attributes]: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/attributes.md#source-code-attributes

0 commit comments

Comments
 (0)