Skip to content

Commit 9ad072c

Browse files
[baggage-processor] Add BaggageLogRecordProcessor (#1576)
Co-authored-by: Gregor Zeitlinger <[email protected]>
1 parent f4ccd2a commit 9ad072c

File tree

6 files changed

+233
-80
lines changed

6 files changed

+233
-80
lines changed

baggage-processor/README.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,63 @@
11
# OpenTelemetry Baggage Span Processor
22

3-
The BaggageSpanProcessor reads entries stored in Baggage from the parent context
4-
and adds the baggage keys and values to the `Span` as attributes on start.
3+
The `BaggageSpanProcessor` and `BaggageLogRecordPRocessor` read entries stored in Baggage from the
4+
parent context and adds the baggage keys and values to the `Span`, respectively `LogRecord`, as
5+
attributes on start, respectively emit.
56

6-
Add this span processor to a tracer provider.
7+
Add these span and log processors to the tracer and logger providers.
78

89
Warning!
910

1011
To repeat: a consequence of adding data to Baggage is that the keys and values
11-
will appear in all outgoing trace context headers from the application.
12+
will appear in all outgoing trace and log context headers from the application.
1213

1314
Do not put sensitive information in Baggage.
1415

1516
## Usage
1617

17-
Add the span processor when configuring the tracer provider.
18+
Add the span and log processor when configuring the tracer and logger providers.
1819

19-
To configure the span processor to copy all baggage entries during configuration:
20+
To configure the span and log processors to copy all baggage entries during configuration:
2021

2122
```java
2223
import io.opentelemetry.contrib.baggage.processor;
2324
// ...
2425

25-
Tracer tracer = SdkTracerProvider.builder()
26-
.addSpanProcessor(new BaggageSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys))
27-
.build()
26+
TracerProvider tracerProvider = SdkTracerProvider.builder()
27+
.addSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys())
28+
.build();
29+
30+
LoggerProvider loggerProvider = SdkLoggerProvider.builder()
31+
.addLogRecordProcessor(BaggageLogRecordProcessor.allowAllBaggageKeys())
32+
.build();
2833
```
2934

3035
Alternatively, you can provide a custom baggage key predicate to select which baggage keys you want to copy.
3136

3237
For example, to only copy baggage entries that start with `my-key`:
3338

3439
```java
35-
new BaggageSpanProcessor(baggageKey -> baggageKey.startsWith("my-key"))
40+
new BaggageSpanProcessor(baggageKey -> baggageKey.startsWith("my-key"));
41+
new BaggageLogRecordProcessor(baggageKey -> baggageKey.startsWith("my-key"));
3642
```
3743

3844
For example, to only copy baggage entries that match the regex `^key.+`:
3945

4046
```java
4147
Pattern pattern = Pattern.compile("^key.+");
42-
new BaggageSpanProcessor(baggageKey -> pattern.matcher(baggageKey).matches())
48+
new BaggageSpanProcessor(baggageKey -> pattern.matcher(baggageKey).matches());
49+
new BaggageLogRecordProcessor(baggageKey -> pattern.matcher(baggageKey).matches());
4350
```
4451

4552
## Usage with SDK auto-configuration
4653

47-
If you are using the OpenTelemetry SDK auto-configuration, you can add the span processor this
48-
library to configure the span processor.
54+
If you are using the OpenTelemetry SDK auto-configuration, you can add the span and log baggage
55+
processors through configuration.
4956

50-
| Property | Description |
51-
|------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|
52-
| otel.java.experimental.span-attributes.copy-from-baggage.include | Add baggage entries as span attributes, e.g. `key1,key2` or `*` to add all baggage items as keys. |
57+
| Property | Description |
58+
|--------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|
59+
| `otel.java.experimental.span-attributes.copy-from-baggage.include` | Add baggage entries as span attributes, e.g. `key1,key2` or `*` to add all baggage items as keys. |
60+
| `otel.java.experimental.log-attributes.copy-from-baggage.include` | Add baggage entries as log attributes, e.g. `key1,key2` or `*` to add all baggage items as keys. |
5361

5462
## Component owners
5563

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.baggage.processor;
7+
8+
import io.opentelemetry.api.baggage.Baggage;
9+
import io.opentelemetry.api.common.AttributeKey;
10+
import io.opentelemetry.context.Context;
11+
import io.opentelemetry.sdk.logs.LogRecordProcessor;
12+
import io.opentelemetry.sdk.logs.ReadWriteLogRecord;
13+
import java.util.function.Predicate;
14+
15+
/**
16+
* This log record processor copies attributes stored in {@link Baggage} into each newly created log
17+
* record.
18+
*/
19+
public class BaggageLogRecordProcessor implements LogRecordProcessor {
20+
21+
/**
22+
* Creates a new {@link BaggageLogRecordProcessor} that copies all baggage entries into the newly
23+
* created log record.
24+
*/
25+
public static BaggageLogRecordProcessor allowAllBaggageKeys() {
26+
return new BaggageLogRecordProcessor(baggageKey -> true);
27+
}
28+
29+
private final Predicate<String> baggageKeyPredicate;
30+
31+
/**
32+
* Creates a new {@link BaggageLogRecordProcessor} that copies only baggage entries with keys that
33+
* pass the provided filter into the newly created log record.
34+
*/
35+
public BaggageLogRecordProcessor(Predicate<String> baggageKeyPredicate) {
36+
this.baggageKeyPredicate = baggageKeyPredicate;
37+
}
38+
39+
@Override
40+
public void onEmit(Context context, ReadWriteLogRecord logRecord) {
41+
Baggage.fromContext(context)
42+
.forEach(
43+
(s, baggageEntry) -> {
44+
if (baggageKeyPredicate.test(s)) {
45+
logRecord.setAttribute(AttributeKey.stringKey(s), baggageEntry.getValue());
46+
}
47+
});
48+
}
49+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.contrib.baggage.processor;
7+
8+
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
9+
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
10+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
11+
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
12+
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
13+
import java.util.List;
14+
15+
public class BaggageProcessorCustomizer implements AutoConfigurationCustomizerProvider {
16+
@Override
17+
public void customize(AutoConfigurationCustomizer autoConfigurationCustomizer) {
18+
autoConfigurationCustomizer
19+
.addTracerProviderCustomizer(
20+
(sdkTracerProviderBuilder, config) -> {
21+
addSpanProcessor(sdkTracerProviderBuilder, config);
22+
return sdkTracerProviderBuilder;
23+
})
24+
.addLoggerProviderCustomizer(
25+
(sdkLoggerProviderBuilder, config) -> {
26+
addLogRecordProcessor(sdkLoggerProviderBuilder, config);
27+
return sdkLoggerProviderBuilder;
28+
});
29+
}
30+
31+
private static void addSpanProcessor(
32+
SdkTracerProviderBuilder sdkTracerProviderBuilder, ConfigProperties config) {
33+
List<String> keys =
34+
config.getList("otel.java.experimental.span-attributes.copy-from-baggage.include");
35+
36+
if (keys.isEmpty()) {
37+
return;
38+
}
39+
40+
sdkTracerProviderBuilder.addSpanProcessor(createBaggageSpanProcessor(keys));
41+
}
42+
43+
static BaggageSpanProcessor createBaggageSpanProcessor(List<String> keys) {
44+
if (keys.size() == 1 && keys.get(0).equals("*")) {
45+
return BaggageSpanProcessor.allowAllBaggageKeys();
46+
}
47+
return new BaggageSpanProcessor(keys::contains);
48+
}
49+
50+
private static void addLogRecordProcessor(
51+
SdkLoggerProviderBuilder sdkLoggerProviderBuilder, ConfigProperties config) {
52+
List<String> keys =
53+
config.getList("otel.java.experimental.log-attributes.copy-from-baggage.include");
54+
55+
if (keys.isEmpty()) {
56+
return;
57+
}
58+
59+
sdkLoggerProviderBuilder.addLogRecordProcessor(createBaggageLogRecordProcessor(keys));
60+
}
61+
62+
static BaggageLogRecordProcessor createBaggageLogRecordProcessor(List<String> keys) {
63+
if (keys.size() == 1 && keys.get(0).equals("*")) {
64+
return BaggageLogRecordProcessor.allowAllBaggageKeys();
65+
}
66+
return new BaggageLogRecordProcessor(keys::contains);
67+
}
68+
}

baggage-processor/src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessorCustomizer.java

Lines changed: 0 additions & 42 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
io.opentelemetry.contrib.baggage.processor.BaggageSpanProcessorCustomizer
1+
io.opentelemetry.contrib.baggage.processor.BaggageProcessorCustomizer

0 commit comments

Comments
 (0)