Skip to content
Merged
9 changes: 5 additions & 4 deletions instrumentation/jboss-logmanager/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Settings for the JBoss Log Manager instrumentation

| System property | Type | Default | Description |
|-----------------------------------------------------------------------------|---------|---------|--------------------------------------------------------------------------------------------------------------|
| `otel.instrumentation.jboss-logmanager.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
| `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. |
| System property | Type | Default | Description |
|-----------------------------------------------------------------------------|---------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `otel.instrumentation.jboss-logmanager.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
| `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. |
| `otel.instrumentation.jboss-logmanager.experimental.capture-event-name` | Boolean | `false` | Enable the capture of the log event name from the `event.name` attribute (captured via one of the above means). When true, the `event.name` attribute will be used as the log event name, and the `event.name` attribute will be removed. |
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ if (latestDepTest) {
tasks.withType<Test>().configureEach {
// TODO run tests both with and without experimental log attributes
jvmArgs("-Dotel.instrumentation.jboss-logmanager.experimental.capture-mdc-attributes=*")
jvmArgs("-Dotel.instrumentation.jboss-logmanager.experimental.capture-event-name=true")
jvmArgs("-Dotel.instrumentation.jboss-logmanager.experimental-log-attributes=true")
jvmArgs("-Dotel.instrumentation.java-util-logging.experimental-log-attributes=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder;
import io.opentelemetry.api.logs.LogRecordBuilder;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.internal.cache.Cache;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes;
import java.util.List;
Expand All @@ -30,7 +26,8 @@ public final class LoggingEventMapper {

public static final LoggingEventMapper INSTANCE = new LoggingEventMapper();

private static final Cache<String, AttributeKey<String>> mdcAttributeKeys = Cache.bounded(100);
// copied from EventIncubatingAttributes
private static final String EVENT_NAME = "event.name";

private final List<String> captureMdcAttributes;

Expand All @@ -41,6 +38,11 @@ public final class LoggingEventMapper {
// cached as an optimization
private final boolean captureAllMdcAttributes;

private final boolean captureEventName =
AgentInstrumentationConfig.get()
.getBoolean(
"otel.instrumentation.jboss-logmanager.experimental.capture-event-name", false);

private LoggingEventMapper() {
this.captureMdcAttributes =
AgentInstrumentationConfig.get()
Expand Down Expand Up @@ -75,53 +77,52 @@ public void capture(Logger logger, ExtLogRecord record) {
builder.setSeverityText(level.toString());
}

AttributesBuilder attributes = Attributes.builder();

Throwable throwable = record.getThrown();
if (throwable != null) {
// this cast is safe within java agent instrumentation
((ExtendedLogRecordBuilder) builder).setException(throwable);
}
captureMdcAttributes(attributes);
captureMdcAttributes(builder);

if (captureExperimentalAttributes) {
Thread currentThread = Thread.currentThread();
attributes.put(ThreadIncubatingAttributes.THREAD_NAME, currentThread.getName());
attributes.put(ThreadIncubatingAttributes.THREAD_ID, currentThread.getId());
builder.setAttribute(ThreadIncubatingAttributes.THREAD_NAME, currentThread.getName());
builder.setAttribute(ThreadIncubatingAttributes.THREAD_ID, currentThread.getId());
}

builder.setAllAttributes(attributes.build());

builder.setContext(Context.current());

builder.setTimestamp(record.getMillis(), MILLISECONDS);
builder.emit();
}

private void captureMdcAttributes(AttributesBuilder attributes) {
private void captureMdcAttributes(LogRecordBuilder builder) {

Map<String, String> context = MDC.copy();

if (captureAllMdcAttributes) {
if (context != null) {
for (Map.Entry<String, String> entry : context.entrySet()) {
attributes.put(
getMdcAttributeKey(String.valueOf(entry.getKey())), String.valueOf(entry.getValue()));
setAttributeMaybeEventName(builder, entry.getKey(), String.valueOf(entry.getValue()));
}
}
return;
}

for (String key : captureMdcAttributes) {
Object value = context.get(key);
if (value != null) {
attributes.put(key, value.toString());
}
setAttributeMaybeEventName(builder, key, value.toString());
}
}

public static AttributeKey<String> getMdcAttributeKey(String key) {
return mdcAttributeKeys.computeIfAbsent(key, AttributeKey::stringKey);
private void setAttributeMaybeEventName(LogRecordBuilder builder, String key, String value) {
if (value != null) {
if (captureEventName && key.equals(EVENT_NAME)) {
builder.setEventName(value);
} else {
builder.setAttribute(key, value);
}
}
}

private static Severity levelToSeverity(java.util.logging.Level level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,13 @@ private static void performLogging(
void testMdc() {
MDC.put("key1", "val1");
MDC.put("key2", "val2");
MDC.put("event.name", "MyEventName");
try {
logger.info("xyz");
} finally {
MDC.remove("key1");
MDC.remove("key2");
MDC.remove("event.name");
}

testing.waitAndAssertLogRecords(
Expand All @@ -216,6 +218,7 @@ void testMdc() {
.hasInstrumentationScope(InstrumentationScopeInfo.builder("abc").build())
.hasSeverity(Severity.INFO)
.hasSeverityText("INFO")
.hasEventName("MyEventName")
.hasAttributesSatisfyingExactly(
equalTo(AttributeKey.stringKey("key1"), "val1"),
equalTo(AttributeKey.stringKey("key2"), "val2"),
Expand Down
15 changes: 8 additions & 7 deletions instrumentation/log4j/log4j-appender-2.17/javaagent/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Settings for the Log4j Appender instrumentation

| System property | Type | Default | Description |
|-----------------------------------------------------------------------------------|---------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------|
| `otel.instrumentation.log4j-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
| `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. |
| `otel.instrumentation.log4j-appender.experimental.capture-map-message-attributes` | Boolean | `false` | Enable the capture of `MapMessage` attributes. |
| `otel.instrumentation.log4j-appender.experimental.capture-marker-attribute` | Boolean | `false` | Enable the capture of Log4j markers as attributes. |
| `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. |
| System property | Type | Default | Description |
|-----------------------------------------------------------------------------------|---------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `otel.instrumentation.log4j-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
| `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. |
| `otel.instrumentation.log4j-appender.experimental.capture-map-message-attributes` | Boolean | `false` | Enable the capture of `MapMessage` attributes. |
| `otel.instrumentation.log4j-appender.experimental.capture-marker-attribute` | Boolean | `false` | Enable the capture of Log4j markers as attributes. |
| `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. |
| `otel.instrumentation.log4j-appender.experimental.capture-event-name` | Boolean | `false` | Enable the capture of the log event name from the `event.name` attribute (captured via one of the above means). When true, the `event.name` attribute will be used as the log event name, and the `event.name` attribute will be removed. |

[source code attributes]: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/general/attributes.md#source-code-attributes
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public final class Log4jHelper {
List<String> captureContextDataAttributes =
config.getList(
"otel.instrumentation.log4j-appender.experimental.capture-mdc-attributes", emptyList());
boolean captureEventName =
config.getBoolean(
"otel.instrumentation.log4j-appender.experimental.capture-event-name", false);

mapper =
new LogEventMapper<>(
Expand All @@ -60,7 +63,8 @@ public final class Log4jHelper {
captureCodeAttributes,
captureMapMessageAttributes,
captureMarkerAttribute,
captureContextDataAttributes);
captureContextDataAttributes,
captureEventName);
}

public static void capture(
Expand Down
17 changes: 9 additions & 8 deletions instrumentation/log4j/log4j-appender-2.17/library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ Setting can be configured as XML attributes, for example:

The available settings are:

| XML Attribute | Type | Default | Description |
|------------------------------------|---------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `captureExperimentalAttributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
| `captureCodeAttributes` | Boolean | `false` | Enable the capture of [source code attributes]. Note that capturing source code attributes at logging sites might add a performance overhead. |
| `captureMapMessageAttributes` | Boolean | `false` | Enable the capture of `MapMessage` attributes. |
| `captureMarkerAttribute` | Boolean | `false` | Enable the capture of Log4j markers as attributes. |
| `captureContextDataAttributes` | String | | Comma separated list of context data attributes to capture. Use the wildcard character `*` to capture all attributes. |
| `numLogsCapturedBeforeOtelInstall` | Integer | 1000 | Log telemetry is emitted after the initialization of the OpenTelemetry Log4j appender with an OpenTelemetry object. This setting allows you to modify the size of the cache used to replay the first logs. |
| XML Attribute | Type | Default | Description |
|------------------------------------|---------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `captureExperimentalAttributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
| `captureCodeAttributes` | Boolean | `false` | Enable the capture of [source code attributes]. Note that capturing source code attributes at logging sites might add a performance overhead. |
| `captureMapMessageAttributes` | Boolean | `false` | Enable the capture of `MapMessage` attributes. |
| `captureMarkerAttribute` | Boolean | `false` | Enable the capture of Log4j markers as attributes. |
| `captureContextDataAttributes` | String | | Comma separated list of context data attributes to capture. Use the wildcard character `*` to capture all attributes. |
| `captureEventName` | Boolean | `false` | Enable the capture of the log event name from the `event.name` attribute (captured via one of the above means). When true, the `event.name` attribute will be used as the log event name, and the `event.name` attribute will be removed. |
| `numLogsCapturedBeforeOtelInstall` | Integer | 1000 | Log telemetry is emitted after the initialization of the OpenTelemetry Log4j appender with an OpenTelemetry object. This setting allows you to modify the size of the cache used to replay the first logs. |

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