-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[baggage-processor] Add BaggageLogRecordProcessor (#1576)
Co-authored-by: Gregor Zeitlinger <[email protected]>
- Loading branch information
1 parent
f4ccd2a
commit 9ad072c
Showing
6 changed files
with
233 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...r/src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageLogRecordProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.baggage.processor; | ||
|
||
import io.opentelemetry.api.baggage.Baggage; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.logs.LogRecordProcessor; | ||
import io.opentelemetry.sdk.logs.ReadWriteLogRecord; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* This log record processor copies attributes stored in {@link Baggage} into each newly created log | ||
* record. | ||
*/ | ||
public class BaggageLogRecordProcessor implements LogRecordProcessor { | ||
|
||
/** | ||
* Creates a new {@link BaggageLogRecordProcessor} that copies all baggage entries into the newly | ||
* created log record. | ||
*/ | ||
public static BaggageLogRecordProcessor allowAllBaggageKeys() { | ||
return new BaggageLogRecordProcessor(baggageKey -> true); | ||
} | ||
|
||
private final Predicate<String> baggageKeyPredicate; | ||
|
||
/** | ||
* Creates a new {@link BaggageLogRecordProcessor} that copies only baggage entries with keys that | ||
* pass the provided filter into the newly created log record. | ||
*/ | ||
public BaggageLogRecordProcessor(Predicate<String> baggageKeyPredicate) { | ||
this.baggageKeyPredicate = baggageKeyPredicate; | ||
} | ||
|
||
@Override | ||
public void onEmit(Context context, ReadWriteLogRecord logRecord) { | ||
Baggage.fromContext(context) | ||
.forEach( | ||
(s, baggageEntry) -> { | ||
if (baggageKeyPredicate.test(s)) { | ||
logRecord.setAttribute(AttributeKey.stringKey(s), baggageEntry.getValue()); | ||
} | ||
}); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
.../src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageProcessorCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.baggage.processor; | ||
|
||
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; | ||
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; | ||
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; | ||
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; | ||
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder; | ||
import java.util.List; | ||
|
||
public class BaggageProcessorCustomizer implements AutoConfigurationCustomizerProvider { | ||
@Override | ||
public void customize(AutoConfigurationCustomizer autoConfigurationCustomizer) { | ||
autoConfigurationCustomizer | ||
.addTracerProviderCustomizer( | ||
(sdkTracerProviderBuilder, config) -> { | ||
addSpanProcessor(sdkTracerProviderBuilder, config); | ||
return sdkTracerProviderBuilder; | ||
}) | ||
.addLoggerProviderCustomizer( | ||
(sdkLoggerProviderBuilder, config) -> { | ||
addLogRecordProcessor(sdkLoggerProviderBuilder, config); | ||
return sdkLoggerProviderBuilder; | ||
}); | ||
} | ||
|
||
private static void addSpanProcessor( | ||
SdkTracerProviderBuilder sdkTracerProviderBuilder, ConfigProperties config) { | ||
List<String> keys = | ||
config.getList("otel.java.experimental.span-attributes.copy-from-baggage.include"); | ||
|
||
if (keys.isEmpty()) { | ||
return; | ||
} | ||
|
||
sdkTracerProviderBuilder.addSpanProcessor(createBaggageSpanProcessor(keys)); | ||
} | ||
|
||
static BaggageSpanProcessor createBaggageSpanProcessor(List<String> keys) { | ||
if (keys.size() == 1 && keys.get(0).equals("*")) { | ||
return BaggageSpanProcessor.allowAllBaggageKeys(); | ||
} | ||
return new BaggageSpanProcessor(keys::contains); | ||
} | ||
|
||
private static void addLogRecordProcessor( | ||
SdkLoggerProviderBuilder sdkLoggerProviderBuilder, ConfigProperties config) { | ||
List<String> keys = | ||
config.getList("otel.java.experimental.log-attributes.copy-from-baggage.include"); | ||
|
||
if (keys.isEmpty()) { | ||
return; | ||
} | ||
|
||
sdkLoggerProviderBuilder.addLogRecordProcessor(createBaggageLogRecordProcessor(keys)); | ||
} | ||
|
||
static BaggageLogRecordProcessor createBaggageLogRecordProcessor(List<String> keys) { | ||
if (keys.size() == 1 && keys.get(0).equals("*")) { | ||
return BaggageLogRecordProcessor.allowAllBaggageKeys(); | ||
} | ||
return new BaggageLogRecordProcessor(keys::contains); | ||
} | ||
} |
42 changes: 0 additions & 42 deletions
42
.../main/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessorCustomizer.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...A-INF/services/io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
io.opentelemetry.contrib.baggage.processor.BaggageSpanProcessorCustomizer | ||
io.opentelemetry.contrib.baggage.processor.BaggageProcessorCustomizer |
Oops, something went wrong.