Skip to content

Commit fea5c4d

Browse files
committed
Implement reading of simple key-value Logstash JSON Marker attributes
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]>
1 parent 3b0e7b8 commit fea5c4d

File tree

9 files changed

+444
-35
lines changed

9 files changed

+444
-35
lines changed
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Settings for the Logback Appender instrumentation
22

3-
| System property | Type | Default | Description |
4-
|----------------------------------------------------------------------------------------|---------|---------|-----------------------------------------------------------------------------------------------------------------------------------------------|
5-
| `otel.instrumentation.logback-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
6-
| `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. |
7-
| `otel.instrumentation.logback-appender.experimental.capture-marker-attribute` | Boolean | `false` | Enable the capture of Logback markers as attributes. |
8-
| `otel.instrumentation.logback-appender.experimental.capture-key-value-pair-attributes` | Boolean | `false` | Enable the capture of Logback key value pairs as attributes. |
9-
| `otel.instrumentation.logback-appender.experimental.capture-logger-context-attributes` | Boolean | `false` | Enable the capture of Logback logger context properties as attributes. |
10-
| `otel.instrumentation.logback-appender.experimental.capture-arguments` | Boolean | `false` | Enable the capture of Logback logger arguments. |
11-
| `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. |
3+
| System property | Type | Default | Description |
4+
|----------------------------------------------------------------------------------------|---------|---------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
5+
| `otel.instrumentation.logback-appender.experimental-log-attributes` | Boolean | `false` | Enable the capture of experimental log attributes `thread.name` and `thread.id`. |
6+
| `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. |
7+
| `otel.instrumentation.logback-appender.experimental.capture-marker-attribute` | Boolean | `false` | Enable the capture of Logback markers as attributes. |
8+
| `otel.instrumentation.logback-appender.experimental.capture-key-value-pair-attributes` | Boolean | `false` | Enable the capture of Logback key value pairs as attributes. |
9+
| `otel.instrumentation.logback-appender.experimental.capture-logger-context-attributes` | Boolean | `false` | Enable the capture of Logback logger context properties as attributes. |
10+
| `otel.instrumentation.logback-appender.experimental.capture-arguments` | Boolean | `false` | Enable the capture of Logback logger arguments. |
11+
| `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 |
12+
| `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. |
1213

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

instrumentation/logback/logback-appender-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/logback/appender/v1_0/LogbackSingletons.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public final class LogbackSingletons {
3939
boolean captureArguments =
4040
config.getBoolean(
4141
"otel.instrumentation.logback-appender.experimental.capture-arguments", false);
42+
boolean captureLogstashAttributes =
43+
config.getBoolean(
44+
"otel.instrumentation.logback-appender.experimental.capture-logstash-attributes",
45+
false);
4246
List<String> captureMdcAttributes =
4347
config.getList(
4448
"otel.instrumentation.logback-appender.experimental.capture-mdc-attributes",
@@ -53,6 +57,7 @@ public final class LogbackSingletons {
5357
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
5458
.setCaptureLoggerContext(captureLoggerContext)
5559
.setCaptureArguments(captureArguments)
60+
.setCaptureLogstashAttributes(captureLogstashAttributes)
5661
.build();
5762
}
5863

instrumentation/logback/logback-appender-1.0/library/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ The available settings are:
100100
| `captureMarkerAttribute` | Boolean | `false` | Enable the capture of Logback markers as attributes. |
101101
| `captureKeyValuePairAttributes` | Boolean | `false` | Enable the capture of Logback key value pairs as attributes. |
102102
| `captureLoggerContext` | Boolean | `false` | Enable the capture of Logback logger context properties as attributes. |
103-
| `captureArguments` | Boolean | `false` | Enable the capture of Logback logger arguments. |
103+
| `captureArguments` | Boolean | `false` | Enable the capture of Logback logger arguments. |
104+
| `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. |
104105
| `captureMdcAttributes` | String | | Comma separated list of MDC attributes to capture. Use the wildcard character `*` to capture all attributes. |
105106
| `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. |
106107

instrumentation/logback/logback-appender-1.0/library/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ dependencies {
1919
strictly("2.0.0")
2020
}
2121
}
22+
compileOnly("net.logstash.logback:logstash-logback-encoder") {
23+
version {
24+
strictly("3.0")
25+
}
26+
}
2227

2328
if (findProperty("testLatestDeps") as Boolean) {
2429
testImplementation("ch.qos.logback:logback-classic:+")
@@ -75,6 +80,7 @@ testing {
7580
if (latestDepTest) {
7681
implementation("ch.qos.logback:logback-classic:+")
7782
implementation("org.slf4j:slf4j-api:+")
83+
implementation("net.logstash.logback:logstash-logback-encoder:+")
7884
} else {
7985
implementation("ch.qos.logback:logback-classic") {
8086
version {
@@ -86,6 +92,11 @@ testing {
8692
strictly("2.0.0")
8793
}
8894
}
95+
implementation("net.logstash.logback:logstash-logback-encoder") {
96+
version {
97+
strictly("3.0")
98+
}
99+
}
89100
}
90101
}
91102
}

instrumentation/logback/logback-appender-1.0/library/src/main/java/io/opentelemetry/instrumentation/logback/appender/v1_0/OpenTelemetryAppender.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class OpenTelemetryAppender extends UnsynchronizedAppenderBase<ILoggingEv
3434
private boolean captureKeyValuePairAttributes = false;
3535
private boolean captureLoggerContext = false;
3636
private boolean captureArguments = false;
37+
private boolean captureLogstashAttributes = false;
3738
private List<String> captureMdcAttributes = emptyList();
3839

3940
private volatile OpenTelemetry openTelemetry;
@@ -81,6 +82,7 @@ public void start() {
8182
.setCaptureKeyValuePairAttributes(captureKeyValuePairAttributes)
8283
.setCaptureLoggerContext(captureLoggerContext)
8384
.setCaptureArguments(captureArguments)
85+
.setCaptureLogstashAttributes(captureLogstashAttributes)
8486
.build();
8587
eventsToReplay = new ArrayBlockingQueue<>(numLogsCapturedBeforeOtelInstall);
8688
super.start();
@@ -175,6 +177,15 @@ public void setCaptureArguments(boolean captureArguments) {
175177
this.captureArguments = captureArguments;
176178
}
177179

180+
/**
181+
* Sets whether the Logstash attributes should be set to logs.
182+
*
183+
* @param captureLogstashAttributes To enable or disable capturing Logstash attributes
184+
*/
185+
public void setCaptureLogstashAttributes(boolean captureLogstashAttributes) {
186+
this.captureLogstashAttributes = captureLogstashAttributes;
187+
}
188+
178189
/** Configures the {@link MDC} attributes that will be copied to logs. */
179190
public void setCaptureMdcAttributes(String attributes) {
180191
if (attributes != null) {

0 commit comments

Comments
 (0)