Skip to content

Commit 16f5ad2

Browse files
Add BaggageLogRecordProcessor
1 parent 8ec5dc1 commit 16f5ad2

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed
Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,49 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
package io.opentelemetry.contrib.baggage.processor;
27

38
import io.opentelemetry.api.baggage.Baggage;
49
import io.opentelemetry.api.common.AttributeKey;
510
import io.opentelemetry.context.Context;
611
import io.opentelemetry.sdk.logs.LogRecordProcessor;
712
import io.opentelemetry.sdk.logs.ReadWriteLogRecord;
8-
913
import java.util.function.Predicate;
1014

1115
/**
12-
* This log record processor copies attributes stored in {@link Baggage} into each newly created log record.
16+
* This log record processor copies attributes stored in {@link Baggage} into each newly created log
17+
* record.
1318
*/
1419
public class BaggageLogRecordProcessor implements LogRecordProcessor {
1520

16-
/**
17-
* Creates a new {@link BaggageLogRecordProcessor} that copies all baggage entries into the newly
18-
* created log record.
19-
*/
20-
public static BaggageLogRecordProcessor allowAllBaggageKeys() {
21-
return new BaggageLogRecordProcessor(baggageKey -> true);
22-
}
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+
}
2328

24-
private final Predicate<String> baggageKeyPredicate;
29+
private final Predicate<String> baggageKeyPredicate;
2530

26-
/**
27-
* Creates a new {@link BaggageLogRecordProcessor} that copies only baggage entries with keys that pass
28-
* the provided filter into the newly created log record.
29-
*/
30-
public BaggageLogRecordProcessor(Predicate<String> baggageKeyPredicate) {
31-
this.baggageKeyPredicate = baggageKeyPredicate;
32-
}
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+
}
3338

34-
@Override
35-
public void onEmit(Context context, ReadWriteLogRecord logRecord) {
36-
Baggage.fromContext(context).forEach((s, baggageEntry) -> {
37-
if (baggageKeyPredicate.test(s)) {
39+
@Override
40+
public void onEmit(Context context, ReadWriteLogRecord logRecord) {
41+
Baggage.fromContext(context)
42+
.forEach(
43+
(s, baggageEntry) -> {
44+
if (baggageKeyPredicate.test(s)) {
3845
logRecord.setAttribute(AttributeKey.stringKey(s), baggageEntry.getValue());
39-
}
40-
});
41-
}
46+
}
47+
});
48+
}
4249
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ private static void addLogRecordProcessor(
5858

5959
sdkLoggerProviderBuilder.addLogRecordProcessor(createBaggageLogRecordProcessor(keys));
6060
}
61+
6162
static BaggageLogRecordProcessor createBaggageLogRecordProcessor(List<String> keys) {
6263
if (keys.size() == 1 && keys.get(0).equals("*")) {
6364
return BaggageLogRecordProcessor.allowAllBaggageKeys();

baggage-processor/src/test/java/io/opentelemetry/contrib/baggage/processor/BaggageProcessorCustomizerTest.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ void test_customizer() {
5454
properties.put("otel.java.experimental.span-attributes.copy-from-baggage.include", "key");
5555
properties.put("otel.java.experimental.log-attributes.copy-from-baggage.include", "key");
5656
// TODO try use
57-
// AttributeAssertion attributeAssertion = OpenTelemetryAssertions.equalTo(AttributeKey.stringKey("key"), "value");
58-
assertCustomizer(
59-
properties,
60-
span -> span.hasAttribute(AttributeKey.stringKey("key"), "value"));
57+
// AttributeAssertion attributeAssertion =
58+
// OpenTelemetryAssertions.equalTo(AttributeKey.stringKey("key"), "value");
59+
assertCustomizer(properties, span -> span.hasAttribute(AttributeKey.stringKey("key"), "value"));
6160
}
6261

6362
private static void assertCustomizer(
@@ -69,7 +68,7 @@ private static void assertCustomizer(
6968
OpenTelemetrySdk sdk = getOpenTelemetrySdk(properties, spanExporter, logExporter);
7069
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
7170
sdk.getTracer("test").spanBuilder("test").startSpan().end();
72-
sdk.getLogsBridge().get("test").logRecordBuilder().setBody ("test").emit();
71+
sdk.getLogsBridge().get("test").logRecordBuilder().setBody("test").emit();
7372
}
7473
// TODO verify log record attributes
7574
await()
@@ -82,10 +81,10 @@ private static void assertCustomizer(
8281
}
8382

8483
private static OpenTelemetrySdk getOpenTelemetrySdk(
85-
Map<String, String> properties, InMemorySpanExporter spanExporter,
84+
Map<String, String> properties,
85+
InMemorySpanExporter spanExporter,
8686
InMemoryLogRecordExporter logRecordExporter) {
87-
SpiHelper spiHelper =
88-
SpiHelper.create(BaggageProcessorCustomizerTest.class.getClassLoader());
87+
SpiHelper spiHelper = SpiHelper.create(BaggageProcessorCustomizerTest.class.getClassLoader());
8988

9089
AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder =
9190
AutoConfiguredOpenTelemetrySdk.builder()
@@ -174,9 +173,11 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_m
174173
}
175174

176175
@Test
177-
public void test_baggageLogRecordProcessor_adds_attributes_to_logRecord(@Mock ReadWriteLogRecord logRecord) {
176+
public void test_baggageLogRecordProcessor_adds_attributes_to_logRecord(
177+
@Mock ReadWriteLogRecord logRecord) {
178178
try (BaggageLogRecordProcessor processor =
179-
BaggageProcessorCustomizer.createBaggageLogRecordProcessor(Collections.singletonList("*"))) {
179+
BaggageProcessorCustomizer.createBaggageLogRecordProcessor(
180+
Collections.singletonList("*"))) {
180181
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
181182
processor.onEmit(Context.current(), logRecord);
182183
verify(logRecord).setAttribute(AttributeKey.stringKey("key"), "value");
@@ -188,7 +189,8 @@ public void test_baggageLogRecordProcessor_adds_attributes_to_logRecord(@Mock Re
188189
public void test_baggageLogRecordProcessor_adds_attributes_to_spans_when_key_filter_matches(
189190
@Mock ReadWriteLogRecord logRecord) {
190191
try (BaggageLogRecordProcessor processor =
191-
BaggageProcessorCustomizer.createBaggageLogRecordProcessor(Collections.singletonList("key"))) {
192+
BaggageProcessorCustomizer.createBaggageLogRecordProcessor(
193+
Collections.singletonList("key"))) {
192194
try (Scope ignore =
193195
Baggage.current().toBuilder()
194196
.put("key", "value")

0 commit comments

Comments
 (0)