Copying attributes between spans within a trace #29504
Unanswered
bmbferreira
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Using custom SpanProcessor maybe work: DemoSpanProcessor package me.test;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.data.SpanData;
public class DemoSpanProcessor implements SpanProcessor {
@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
Span parentSpan = Span.fromContext(parentContext);
if (parentSpan instanceof ReadableSpan) {
SpanData parentSpanData = ((ReadableSpan) span).toSpanData();
parentSpanData.getAttributes().forEach((key, value) -> {
if (span.getAttribute(key) == null) {
span.setAttribute((AttributeKey<Object>) key, value);
}
});
}
}
@Override
public boolean isStartRequired() {return true;}
@Override
public void onEnd(ReadableSpan span) {}
@Override
public boolean isEndRequired() {return false;}
} then use it to init OpenTelemetry : protected OpenTelemetry newOpenTelemetry() {
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(new DemoSpanProcessor())
.build();
return OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.buildAndRegisterGlobal();
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! I wonder if it would be possible with the opentelemetry collector to copy specific attributes between spans within a single trace. I know we have a processor that allows to group spans by trace but I wonder what should I use to then "propagate" a specific attribute to all the spans within that trace.
For example I know that some of the spans on the trace will have an attribute named "user.email". I would like to copy that specific attribute to all the spans of the trace.
Is this possible to do? Thank you.
Beta Was this translation helpful? Give feedback.
All reactions