Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
| `otel.instrumentation.logback-appender.experimental.capture-marker-attribute` | Boolean | `false` | Enable the capture of Logback markers as attributes. |
| `otel.instrumentation.logback-appender.experimental.capture-key-value-pair-attributes` | Boolean | `false` | Enable the capture of Logback key value pairs as attributes. |
| `otel.instrumentation.logback-appender.experimental.capture-logger-context-attributes` | Boolean | `false` | Enable the capture of Logback logger context properties as attributes. |
| `otel.instrumentation.logback-appender.experimental.capture-arguments` | Boolean | `false` | Enable the capture of Logback logger arguments. |
| `otel.instrumentation.logback-appender.experimental.capture-template` | Boolean | `false` | Enable the capture of Logback log event message template. |
| `otel.instrumentation.logback-appender.experimental.capture-arguments` | Boolean | `false` | Enable the capture of Logback log event arguments. |
| `otel.instrumentation.logback-appender.experimental.capture-logstash-marker-attributes` | Boolean | `false` | Enable the capture of Logstash markers, supported are those added to logs via `Markers.append()`, `Markers.appendEntries()`, `Markers.appendArray()` and `Markers.appendRaw()` methods. |
| `otel.instrumentation.logback-appender.experimental.capture-logstash-structured-arguments` | Boolean | `false` | Enable the capture of Logstash StructuredArguments as attributes (e.g., `StructuredArguments.v()` and `StructuredArguments.keyValue()`). |
| `otel.instrumentation.logback-appender.experimental.capture-mdc-attributes` | String | | Comma separated list of MDC attributes to capture. Use the wildcard character `*` to capture all attributes. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public final class LogbackSingletons {
config.getBoolean(
"otel.instrumentation.logback-appender.experimental.capture-logger-context-attributes",
false);
boolean captureTemplate =
config.getBoolean(
"otel.instrumentation.logback-appender.experimental.capture-template", false);
boolean captureArguments =
config.getBoolean(
"otel.instrumentation.logback-appender.experimental.capture-arguments", false);
Expand Down Expand Up @@ -66,6 +69,7 @@ public final class LogbackSingletons {
.setCaptureMarkerAttribute(captureMarkerAttribute)
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
.setCaptureLoggerContext(captureLoggerContext)
.setCaptureTemplate(captureTemplate)
.setCaptureArguments(captureArguments)
.setCaptureLogstashMarkerAttributes(captureLogstashMarkerAttributes)
.setCaptureLogstashStructuredArguments(captureLogstashStructuredArguments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ The available settings are:
| `captureMarkerAttribute` | Boolean | `false` | Enable the capture of Logback markers as attributes. |
| `captureKeyValuePairAttributes` | Boolean | `false` | Enable the capture of Logback key value pairs as attributes. |
| `captureLoggerContext` | Boolean | `false` | Enable the capture of Logback logger context properties as attributes. |
| `captureArguments` | Boolean | `false` | Enable the capture of Logback logger arguments. |
| `captureTemplate` | Boolean | `false` | Enable the capture of Logback log event message template. |
| `captureArguments` | Boolean | `false` | Enable the capture of Logback log event arguments. |
| `captureLogstashMarkerAttributes` | Boolean | `false` | Enable the capture of Logstash markers, supported are those added to logs via `Markers.append()`, `Markers.appendEntries()`, `Markers.appendArray()` and `Markers.appendRaw()` methods. |
| `captureLogstashStructuredArguments` | Boolean | `false` | Enable the capture of Logstash StructuredArguments as attributes (e.g., `StructuredArguments.v()` and `StructuredArguments.keyValue()`). |
| `captureLogstashStructuredArguments` | Boolean | `false` | Enable the capture of Logstash StructuredArguments as attributes (e.g., `StructuredArguments.v()` and `StructuredArguments.keyValue()`). |
| `captureMdcAttributes` | String | | Comma separated list of MDC attributes to capture. Use the wildcard character `*` to capture all attributes. |
| `captureEventName` | Boolean | `false` | Enable moving the `event.name` attribute (captured by one of the other mechanisms of capturing attributes) to the log event name. |
| `numLogsCapturedBeforeOtelInstall` | Integer | 1000 | Log telemetry is emitted after the initialization of the OpenTelemetry Logback appender with an OpenTelemetry object. This setting allows you to modify the size of the cache used to replay the first logs. thread.id attribute is not captured. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv
private boolean captureMarkerAttribute = false;
private boolean captureKeyValuePairAttributes = false;
private boolean captureLoggerContext = false;
private boolean captureTemplate = false;
private boolean captureArguments = false;
private boolean captureLogstashMarkerAttributes = false;
private boolean captureLogstashStructuredArguments = false;
Expand Down Expand Up @@ -88,6 +89,7 @@ public void start() {
.setCaptureMarkerAttribute(captureMarkerAttribute)
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
.setCaptureLoggerContext(captureLoggerContext)
.setCaptureTemplate(captureTemplate)
.setCaptureArguments(captureArguments)
.setCaptureLogstashMarkerAttributes(captureLogstashMarkerAttributes)
.setCaptureLogstashStructuredArguments(captureLogstashStructuredArguments)
Expand Down Expand Up @@ -177,6 +179,15 @@ public void setCaptureLoggerContext(boolean captureLoggerContext) {
this.captureLoggerContext = captureLoggerContext;
}

/**
* Sets whether the message template should be captured in logs
*
* @param captureTemplate whether the message template should be captured in logs
*/
public void setCaptureTemplate(boolean captureTemplate) {
this.captureTemplate = captureTemplate;
}

/**
* Sets whether the arguments should be set to logs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public final class LoggingEventMapper {
private final boolean captureMarkerAttribute;
private final boolean captureKeyValuePairAttributes;
private final boolean captureLoggerContext;
private final boolean captureTemplate;
private final boolean captureArguments;
private final boolean captureLogstashMarkerAttributes;
private final boolean captureLogstashStructuredArguments;
Expand All @@ -96,6 +97,7 @@ private LoggingEventMapper(Builder builder) {
this.captureMarkerAttribute = builder.captureMarkerAttribute;
this.captureKeyValuePairAttributes = builder.captureKeyValuePairAttributes;
this.captureLoggerContext = builder.captureLoggerContext;
this.captureTemplate = builder.captureTemplate;
this.captureArguments = builder.captureArguments;
this.captureLogstashMarkerAttributes = builder.captureLogstashMarkerAttributes;
this.captureLogstashStructuredArguments = builder.captureLogstashStructuredArguments;
Expand Down Expand Up @@ -215,10 +217,14 @@ private void mapLoggingEvent(
captureLoggerContext(builder, loggingEvent.getLoggerContextVO().getPropertyMap());
}

if (captureTemplate) {
captureTemplate(builder, loggingEvent);
}

if (captureArguments
&& loggingEvent.getArgumentArray() != null
&& loggingEvent.getArgumentArray().length > 0) {
captureArguments(builder, loggingEvent.getMessage(), loggingEvent.getArgumentArray());
captureArguments(builder, loggingEvent.getArgumentArray());
}

if (supportsLogstashMarkers && captureLogstashMarkerAttributes) {
Expand Down Expand Up @@ -265,8 +271,11 @@ void captureMdcAttributes(LogRecordBuilder builder, Map<String, String> mdcPrope
}
}

void captureArguments(LogRecordBuilder builder, String message, Object[] arguments) {
builder.setAttribute(LOG_BODY_TEMPLATE, message);
private static void captureTemplate(LogRecordBuilder builder, ILoggingEvent loggingEvent) {
builder.setAttribute(LOG_BODY_TEMPLATE, loggingEvent.getMessage());
}

private static void captureArguments(LogRecordBuilder builder, Object[] arguments) {
builder.setAttribute(
LOG_BODY_PARAMETERS,
Arrays.stream(arguments).map(String::valueOf).collect(Collectors.toList()));
Expand Down Expand Up @@ -679,6 +688,7 @@ public static final class Builder {
private boolean captureMarkerAttribute;
private boolean captureKeyValuePairAttributes;
private boolean captureLoggerContext;
private boolean captureTemplate;
private boolean captureArguments;
private boolean captureLogstashMarkerAttributes;
private boolean captureLogstashStructuredArguments;
Expand Down Expand Up @@ -722,6 +732,12 @@ public Builder setCaptureLoggerContext(boolean captureLoggerContext) {
return this;
}

@CanIgnoreReturnValue
public Builder setCaptureTemplate(boolean captureTemplate) {
this.captureTemplate = captureTemplate;
return this;
}

@CanIgnoreReturnValue
public Builder setCaptureArguments(boolean captureArguments) {
this.captureArguments = captureArguments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ void keyValue() {
.hasResource(resource)
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("log message 1")
.hasTotalAttributeCount(codeAttributesLogCount() + 8) // 8 key value pairs
.hasTotalAttributeCount(
codeAttributesLogCount() + 9) // 8 key value pairs + 1 template
.hasEventName("MyEventName")
.hasAttributesSatisfying(
equalTo(AttributeKey.stringKey("string key"), "string value"),
Expand All @@ -70,7 +71,8 @@ void keyValue() {
equalTo(AttributeKey.longKey("int key"), 3),
equalTo(AttributeKey.longKey("long key"), 4),
equalTo(AttributeKey.doubleKey("float key"), 5.0),
equalTo(AttributeKey.doubleKey("double key"), 6.0)));
equalTo(AttributeKey.doubleKey("double key"), 6.0),
equalTo(AttributeKey.stringKey("log.body.template"), "log message 1")));
}

@Test
Expand All @@ -90,15 +92,16 @@ void multipleMarkers() {
.hasResource(resource)
.hasInstrumentationScope(instrumentationScopeInfo)
.hasBody("log message 1")
.hasTotalAttributeCount(codeAttributesLogCount() + 1) // 1 marker
.hasTotalAttributeCount(codeAttributesLogCount() + 2) // 1 marker + 1 template
.hasAttributesSatisfying(
equalTo(
AttributeKey.stringArrayKey("logback.marker"),
Arrays.asList(markerName1, markerName2))));
Arrays.asList(markerName1, markerName2)),
equalTo(AttributeKey.stringKey("log.body.template"), "log message 1")));
}

@Test
void arguments() {
void argumentsAndTemplate() {
logger
.atInfo()
.setMessage("log message {} and {}, bool {}, long {}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<captureCodeAttributes>true</captureCodeAttributes>
<captureMarkerAttribute>true</captureMarkerAttribute>
<captureKeyValuePairAttributes>true</captureKeyValuePairAttributes>
<captureTemplate>true</captureTemplate>
<captureArguments>true</captureArguments>
<captureLogstashMarkerAttributes>true</captureLogstashMarkerAttributes>
<captureLogstashStructuredArguments>true</captureLogstashStructuredArguments>
Expand Down
Loading