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

add config support for BaggageSpanProcessor #1330

Merged
merged 7 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions baggage-processor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ Pattern pattern = Pattern.compile("^key.+");
new BaggageSpanProcessor(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.

| Property | Description |
|-------------------------------------------|-------------------------------------------------------------------------------------------------|
| otel.traces.baggage-to-attributes.include | Add baggage items to span attributes, e.g. `key1,key2` or `*` to add all baggage items as keys. |
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved

## Component owners

- [Mike Golsmith](https://github.com/MikeGoldsmith), Honeycomb
- [Gregor Zeitlinger](https://github.com/zeitlinger), Grafana
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved

Learn more about component owners in [component_owners.yml](../.github/component_owners.yml).
2 changes: 2 additions & 0 deletions baggage-processor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ otelJava.moduleName.set("io.opentelemetry.contrib.baggage.processor")
dependencies {
api("io.opentelemetry:opentelemetry-api")
api("io.opentelemetry:opentelemetry-sdk")
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")

testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
testImplementation("org.mockito:mockito-inline")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.trace.SdkTracerProviderBuilder;
import java.util.List;

public class BaggageSpanProcessorCustomizer implements AutoConfigurationCustomizerProvider {
@Override
public void customize(AutoConfigurationCustomizer autoConfigurationCustomizer) {
autoConfigurationCustomizer.addTracerProviderCustomizer(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a moment I thought this wouldn't work because this customizers are applied after the batch exporter is registered, but then I realized that order of registration doesn't matter for span processors which want to take advantage of modifying the span on start.

We would need to make changes in the SDK if this was a BaggageLogRecordProcessor: Either providing a hook to customize the logger provider before batch processor is added, or by extending SdkLoggerProviderBuilder with a method to insert a processor at the front of the queue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either providing a hook to customize the logger provider before batch processor is added

I think this is a great idea - would make sense for all signals

BTW, I've made the test end-to-end to cover that "onStast" is actually called

(sdkTracerProviderBuilder, config) -> {
addSpanProcessor(sdkTracerProviderBuilder, config);
return sdkTracerProviderBuilder;
});
}

private static void addSpanProcessor(
SdkTracerProviderBuilder sdkTracerProviderBuilder, ConfigProperties config) {
List<String> keys = config.getList("otel.traces.baggage-to-attributes.include");

if (keys.isEmpty()) {
return;
}

sdkTracerProviderBuilder.addSpanProcessor(createProcessor(keys));
}

static BaggageSpanProcessor createProcessor(List<String> keys) {
if (keys.size() == 1 && keys.get(0).equals("*")) {
return new BaggageSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys);
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved
}
return new BaggageSpanProcessor(keys::contains);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.opentelemetry.contrib.baggage.processor.BaggageSpanProcessorCustomizer
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.contrib.baggage.processor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
import java.util.Collections;
import java.util.Map;
import java.util.function.BiFunction;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class BaggageSpanProcessorCustomizerTest {

@Test
void test_customizer() {
assertCustomizer(Collections.emptyMap(), 0);
assertCustomizer(
Collections.singletonMap("otel.traces.baggage-to-attributes.include", "key"), 1);
}

private static void assertCustomizer(
zeitlinger marked this conversation as resolved.
Show resolved Hide resolved
Map<String, String> properties, int addedSpanProcessorTimes) {
AutoConfigurationCustomizer customizer = Mockito.mock(AutoConfigurationCustomizer.class);
new BaggageSpanProcessorCustomizer().customize(customizer);
@SuppressWarnings("unchecked")
ArgumentCaptor<BiFunction<SdkTracerProviderBuilder, ConfigProperties, SdkTracerProviderBuilder>>
captor = ArgumentCaptor.forClass(BiFunction.class);
verify(customizer, times(1)).addTracerProviderCustomizer(captor.capture());

SdkTracerProviderBuilder builder = Mockito.mock(SdkTracerProviderBuilder.class);
SdkTracerProviderBuilder apply =
captor.getValue().apply(builder, DefaultConfigProperties.createFromMap(properties));
assertThat(apply).isSameAs(builder);

verify(builder, times(addedSpanProcessorTimes)).addSpanProcessor(Mockito.any());
}

@Test
public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSpan span) {
try (BaggageSpanProcessor processor =
BaggageSpanProcessorCustomizer.createProcessor(Collections.singletonList("*"))) {
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
processor.onStart(Context.current(), span);
verify(span).setAttribute("key", "value");
}
}
}

@Test
public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_matches(
@Mock ReadWriteSpan span) {
try (BaggageSpanProcessor processor =
BaggageSpanProcessorCustomizer.createProcessor(Collections.singletonList("key"))) {
try (Scope ignore =
Baggage.current().toBuilder()
.put("key", "value")
.put("other", "value")
.build()
.makeCurrent()) {
processor.onStart(Context.current(), span);
verify(span).setAttribute("key", "value");
verify(span, Mockito.never()).setAttribute("other", "value");
}
}
}
}
Loading