Skip to content

Commit

Permalink
Implement reading of simple key-value Logstash JSON Marker attributes
Browse files Browse the repository at this point in the history
Supported are MapEntriesAppendingMarker and SingleFieldAppendingMarker
(i.e. ObjectAppendingMarker and RawJsonAppendingMarker) only. The attribute
value is sent either as boolean, long, double or String or typed-array
with respective values. The generic types (Object[], Collection) is
converted to String array with values converted with String.valueOf()
method.

Typically the Logstash markers are added to logs via Markers.append(),
Markers.appendEntries(), Markers.appendArray() and Markers.appendRaw()
methods.

Signed-off-by: Oldřich Jedlička <[email protected]>
  • Loading branch information
oldium committed Nov 1, 2024
1 parent 3cf37dd commit c4b7dd5
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 35 deletions.
19 changes: 10 additions & 9 deletions instrumentation/logback/logback-appender-1.0/javaagent/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Settings for the Logback Appender instrumentation

| System property | Type | Default | Description |
|----------------------------------------------------------------------------------------|---------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------|
| `otel.instrumentation.logback-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
| `otel.instrumentation.logback-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.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-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.logback-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
| `otel.instrumentation.logback-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.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-logstash-attributes` | Boolean | `false` | Enable the capture of Logstash attributes, supported are those added to logs via `Markers.append()`, `Markers.appendEntries()`, `Markers.appendArray()` and `Markers.appendRaw()` methods |
| `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. |

[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 @@ -39,6 +39,10 @@ public final class LogbackSingletons {
boolean captureArguments =
config.getBoolean(
"otel.instrumentation.logback-appender.experimental.capture-arguments", false);
boolean captureLogstashAttributes =
config.getBoolean(
"otel.instrumentation.logback-appender.experimental.capture-logstash-attributes",
false);
List<String> captureMdcAttributes =
config.getList(
"otel.instrumentation.logback-appender.experimental.capture-mdc-attributes",
Expand All @@ -53,6 +57,7 @@ public final class LogbackSingletons {
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
.setCaptureLoggerContext(captureLoggerContext)
.setCaptureArguments(captureArguments)
.setCaptureLogstashAttributes(captureLogstashAttributes)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ 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. |
| `captureArguments` | Boolean | `false` | Enable the capture of Logback logger arguments. |
| `captureLogstashAttributes` | Boolean | `false` | Enable the capture of Logstash attributes, supported are those added to logs via `Markers.append()`, `Markers.appendEntries()`, `Markers.appendArray()` and `Markers.appendRaw()` methods. |
| `captureMdcAttributes` | String | | Comma separated list of MDC attributes to capture. Use the wildcard character `*` to capture all attributes. |
| `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 @@ -19,6 +19,11 @@ dependencies {
strictly("2.0.0")
}
}
compileOnly("net.logstash.logback:logstash-logback-encoder") {
version {
strictly("3.0")
}
}

if (findProperty("testLatestDeps") as Boolean) {
testImplementation("ch.qos.logback:logback-classic:+")
Expand Down Expand Up @@ -75,6 +80,7 @@ testing {
if (latestDepTest) {
implementation("ch.qos.logback:logback-classic:+")
implementation("org.slf4j:slf4j-api:+")
implementation("net.logstash.logback:logstash-logback-encoder:+")
} else {
implementation("ch.qos.logback:logback-classic") {
version {
Expand All @@ -86,6 +92,11 @@ testing {
strictly("2.0.0")
}
}
implementation("net.logstash.logback:logstash-logback-encoder") {
version {
strictly("3.0")
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv
private boolean captureKeyValuePairAttributes = false;
private boolean captureLoggerContext = false;
private boolean captureArguments = false;
private boolean captureLogstashAttributes = false;
private List<String> captureMdcAttributes = emptyList();

private volatile OpenTelemetry openTelemetry;
Expand Down Expand Up @@ -81,6 +82,7 @@ public void start() {
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
.setCaptureLoggerContext(captureLoggerContext)
.setCaptureArguments(captureArguments)
.setCaptureLogstashAttributes(captureLogstashAttributes)
.build();
eventsToReplay = new ArrayBlockingQueue<>(numLogsCapturedBeforeOtelInstall);
super.start();
Expand Down Expand Up @@ -175,6 +177,15 @@ public void setCaptureArguments(boolean captureArguments) {
this.captureArguments = captureArguments;
}

/**
* Sets whether the Logstash attributes should be set to logs.
*
* @param captureLogstashAttributes To enable or disable capturing Logstash attributes
*/
public void setCaptureLogstashAttributes(boolean captureLogstashAttributes) {
this.captureLogstashAttributes = captureLogstashAttributes;
}

/** Configures the {@link MDC} attributes that will be copied to logs. */
public void setCaptureMdcAttributes(String attributes) {
if (attributes != null) {
Expand Down
Loading

0 comments on commit c4b7dd5

Please sign in to comment.