-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[baggage-processor] Add BaggageLogRecordProcessor #1576
Merged
trask
merged 8 commits into
open-telemetry:main
from
cyrille-leclerc:add-baggage-log-record-processor
Dec 12, 2024
+233
−80
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8ec5dc1
Add BaggageLogRecordProcessor
cyrille-leclerc 16f5ad2
Add BaggageLogRecordProcessor
cyrille-leclerc f00c37c
add simple log test
zeitlinger 1036ffc
Merge pull request #1 from zeitlinger/add-baggage-log-record-processo…
cyrille-leclerc 0bf5874
WIP
cyrille-leclerc 194a640
Add docs
cyrille-leclerc bffb85f
Add docs
cyrille-leclerc 6a233ad
Update baggage-processor/README.md
cyrille-leclerc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extract
isWildcard
method to be reused for logger