Skip to content
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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions baggage-processor/README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,63 @@
# OpenTelemetry Baggage Span Processor

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

Add this span processor to a tracer provider.
Add these span and log processors to the tracer and logger providers.

Warning!

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

Do not put sensitive information in Baggage.

## Usage

Add the span processor when configuring the tracer provider.
Add the span and log processor when configuring the tracer and logger providers.

To configure the span processor to copy all baggage entries during configuration:
To configure the span and log processors to copy all baggage entries during configuration:

```java
import io.opentelemetry.contrib.baggage.processor;
// ...

Tracer tracer = SdkTracerProvider.builder()
.addSpanProcessor(new BaggageSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys))
.build()
TracerProvider tracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys())
.build();

LoggerProvider loggerProvider = SdkLoggerProvider.builder()
.addLogRecordProcessor(BaggageLogRecordProcessor.allowAllBaggageKeys())
.build();
```

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

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

```java
new BaggageSpanProcessor(baggageKey -> baggageKey.startsWith("my-key"))
new BaggageSpanProcessor(baggageKey -> baggageKey.startsWith("my-key"));
new BaggageLogRecordProcessor(baggageKey -> baggageKey.startsWith("my-key"));
```

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

```java
Pattern pattern = Pattern.compile("^key.+");
new BaggageSpanProcessor(baggageKey -> pattern.matcher(baggageKey).matches())
new BaggageSpanProcessor(baggageKey -> pattern.matcher(baggageKey).matches());
new BaggageLogRecordProcessor(baggageKey -> pattern.matcher(baggageKey).matches());
```

## Usage with SDK auto-configuration

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

| Property | Description |
|------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|
| 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. |
| Property | Description |
|--------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|
| `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. |
| `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. |

## Component owners

Expand Down
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());
}
});
}
}
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("*")) {
Copy link
Member

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

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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.opentelemetry.contrib.baggage.processor.BaggageSpanProcessorCustomizer
io.opentelemetry.contrib.baggage.processor.BaggageProcessorCustomizer
Loading