Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.zalando.riptide.opentelemetry;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;

import java.util.Collection;
import java.util.Optional;

import static java.util.Collections.singleton;
import static org.zalando.riptide.opentelemetry.span.ExtensionAttributes.FLOW_ID;

public final class FlowIdPropagator implements TextMapPropagator {

public static final String FLOW_ID_HEADER = "X-Flow-ID";

@Override
public Collection<String> fields() {
return singleton(FLOW_ID_HEADER);
}

@Override
public <C> void inject(final Context context, final C carrier, final TextMapSetter<C> setter) {
Optional.ofNullable(Baggage.fromContext(context).getEntryValue(FLOW_ID.getKey()))
.ifPresent(flowId -> setter.set(carrier, FLOW_ID_HEADER, flowId));
}

@Override
public <C> Context extract(final Context context, final C carrier, final TextMapGetter<C> getter) {
return context;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,23 @@ public class OpenTelemetryPlugin implements Plugin {
private final Tracer tracer;
private final TextMapPropagator propagator;
private final SpanDecorator spanDecorator;
private final boolean propagateFlowId;

public OpenTelemetryPlugin(final SpanDecorator... decorators) {
this(GlobalOpenTelemetry.get(), decorators);
this(GlobalOpenTelemetry.get(), false, decorators);
}

public OpenTelemetryPlugin(final boolean propagateFlowId, final SpanDecorator... decorators) {
this(GlobalOpenTelemetry.get(), propagateFlowId, decorators);
}

public OpenTelemetryPlugin(final OpenTelemetry telemetry, final SpanDecorator... decorators) {
this(telemetry, CompositeSpanDecorator.composite(
this(telemetry, false, decorators);
}

public OpenTelemetryPlugin(final OpenTelemetry telemetry, final boolean propagateFlowId,
final SpanDecorator... decorators) {
this(telemetry, propagateFlowId, CompositeSpanDecorator.composite(
new HttpMethodSpanDecorator(),
new HttpStatusCodeSpanDecorator(),
new ErrorSpanDecorator(),
Expand All @@ -56,15 +66,20 @@ public OpenTelemetryPlugin(final OpenTelemetry telemetry, final SpanDecorator...
));
}

private OpenTelemetryPlugin(final OpenTelemetry telemetry, final SpanDecorator spanDecorator) {
private OpenTelemetryPlugin(final OpenTelemetry telemetry, final boolean propagateFlowId,
final SpanDecorator spanDecorator) {
this.telemetry = telemetry;
this.tracer = telemetry.getTracer("riptide-opentelemetry");
this.propagator = telemetry.getPropagators().getTextMapPropagator();
final TextMapPropagator propagator = telemetry.getPropagators().getTextMapPropagator();
this.propagator = propagateFlowId
? TextMapPropagator.composite(propagator, new FlowIdPropagator())
: propagator;
this.spanDecorator = spanDecorator;
this.propagateFlowId = propagateFlowId;
}

public OpenTelemetryPlugin withSpanDecorators(final SpanDecorator... decorators) {
return new OpenTelemetryPlugin(telemetry, CompositeSpanDecorator.composite(decorators));
return new OpenTelemetryPlugin(telemetry, propagateFlowId, CompositeSpanDecorator.composite(decorators));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,36 @@
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.trace.Span;
import org.zalando.riptide.RequestArguments;

import java.util.Optional;

import lombok.RequiredArgsConstructor;
import org.zalando.riptide.opentelemetry.FlowIdPropagator;

import static org.zalando.riptide.opentelemetry.span.ExtensionAttributes.FLOW_ID;

@RequiredArgsConstructor
public class FlowIdSpanDecorator implements SpanDecorator {

public static final String FLOW_ID_HEADER = "X-Flow-ID";
/**
* @deprecated Flow ID propagation is handled by {@code FlowIdPropagator}.
*/
@Deprecated
public static final String FLOW_ID_HEADER = FlowIdPropagator.FLOW_ID_HEADER;

public FlowIdSpanDecorator() {
// default constructor
}

private final boolean propagateFlowId;
/**
* @deprecated Flow ID propagation is configured on {@code OpenTelemetryPlugin}; this decorator only adds the
* Flow ID span attribute.
*/
@Deprecated
public FlowIdSpanDecorator(final boolean propagateFlowId) {
this();
}

@Override
public void onRequest(Span span, RequestArguments arguments) {
Optional.ofNullable(Baggage.current().getEntryValue(FLOW_ID.getKey()))
.ifPresent(flowId -> {
span.setAttribute(FLOW_ID, flowId);

if (propagateFlowId) {
arguments.withHeader(FLOW_ID_HEADER, flowId);
}
});
final String flowId = Baggage.current().getEntryValue(FLOW_ID.getKey());
if (flowId != null) {
span.setAttribute(FLOW_ID, flowId);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.zalando.riptide.opentelemetry;

import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.EventData;
Expand All @@ -30,6 +32,7 @@
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.zalando.riptide.*;
import org.zalando.riptide.opentelemetry.span.HttpHostSpanDecorator;
import org.zalando.riptide.opentelemetry.span.ExtensionAttributes;
import org.zalando.riptide.opentelemetry.span.SpanDecorator;
import org.zalando.riptide.opentelemetry.span.StaticSpanDecorator;

Expand Down Expand Up @@ -82,7 +85,7 @@ class OpenTelemetryPluginTest {
.setConnectionManager(cm)
.build()))
.baseUrl(getBaseUrl(server))
.plugin(new OpenTelemetryPlugin(otelTesting.getOpenTelemetry(),
.plugin(new OpenTelemetryPlugin(otelTesting.getOpenTelemetry(), true,
environmentDecorator))
.build();

Expand Down Expand Up @@ -250,6 +253,23 @@ void shouldObtainTracerFromGlobalTelemetry() {
assertThat(plugin.getTracer(), notNullValue());
}

@Test
void shouldPropagateFlowIdHeader() {
server.enqueue(textMockResponse("Hello, world!"));

final Context context = Baggage.builder()
.put(ExtensionAttributes.FLOW_ID.getKey(), "flow-id-value")
.build()
.storeInContext(Context.current());

try (final Scope ignored = context.makeCurrent()) {
unit.get("/").call(pass()).join();
}

verify(server, 1, "/", "GET", headers ->
assertThat(headers.get("X-Flow-ID"), is("flow-id-value")));
}

@Test
void shouldActivateScope() throws IOException {
// when running in java agent, the downstream http client is also instrumented
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
class FlowIdSpanDecoratorTest {

private static final String FLOW_ID = "flow-id-value";
private static final String FLOW_ID_HEADER = "X-Flow-ID";

private Span span;
private RequestArguments arguments;

Expand All @@ -24,24 +22,8 @@ void setUp() {
}

@Test
void shouldSetFlowIdAttributeAndPropagateHeaderWhenEnabled() {
final FlowIdSpanDecorator decorator = new FlowIdSpanDecorator(true);
final Baggage baggage = mock(Baggage.class);
when(baggage.getEntryValue(ExtensionAttributes.FLOW_ID.getKey())).thenReturn(FLOW_ID);

try (MockedStatic<Baggage> mockedBaggage = mockStatic(Baggage.class)) {
mockedBaggage.when(Baggage::current).thenReturn(baggage);

decorator.onRequest(span, arguments);

verify(span).setAttribute(ExtensionAttributes.FLOW_ID, FLOW_ID);
verify(arguments).withHeader(FLOW_ID_HEADER, FLOW_ID);
}
}

@Test
void shouldSetFlowIdAttributeWithoutPropagatingHeaderWhenDisabled() {
final FlowIdSpanDecorator decorator = new FlowIdSpanDecorator(false);
void shouldSetFlowIdAttribute() {
final FlowIdSpanDecorator decorator = new FlowIdSpanDecorator();
final Baggage baggage = mock(Baggage.class);
when(baggage.getEntryValue(ExtensionAttributes.FLOW_ID.getKey())).thenReturn(FLOW_ID);

Expand All @@ -51,13 +33,12 @@ void shouldSetFlowIdAttributeWithoutPropagatingHeaderWhenDisabled() {
decorator.onRequest(span, arguments);

verify(span).setAttribute(ExtensionAttributes.FLOW_ID, FLOW_ID);
verify(arguments, never()).withHeader(FLOW_ID_HEADER, FLOW_ID);
}
}

@Test
void shouldDoNothingWhenFlowIdIsAbsent() {
final FlowIdSpanDecorator decorator = new FlowIdSpanDecorator(true);
final FlowIdSpanDecorator decorator = new FlowIdSpanDecorator();
final Baggage baggage = mock(Baggage.class);
when(baggage.getEntryValue(ExtensionAttributes.FLOW_ID.getKey())).thenReturn(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static Plugin create(final RiptideProperties.Client client) {
if (client.getRetry().getEnabled()) {
decorators.add(new RetrySpanDecorator());
}
decorators.add(new FlowIdSpanDecorator(clientTelemetryConfig.getPropagateFlowId()));
return new OpenTelemetryPlugin(decorators.toArray(new SpanDecorator[0]));
decorators.add(new FlowIdSpanDecorator());
return new OpenTelemetryPlugin(clientTelemetryConfig.getPropagateFlowId(),
decorators.toArray(new SpanDecorator[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class OpenTelemetryPluginFactoryTest {

private static final Field SPAN_DECORATOR_FIELD = ReflectionUtils.findField(OpenTelemetryPlugin.class, "spanDecorator");
private static final Field DECORATORS_FIELD = ReflectionUtils.findField(CompositeSpanDecorator.class, "decorators");
private static final Field PROPAGATE_FLOW_ID_FIELD = ReflectionUtils.findField(FlowIdSpanDecorator.class, "propagateFlowId");
private static final Field PROPAGATE_FLOW_ID_FIELD = ReflectionUtils.findField(OpenTelemetryPlugin.class, "propagateFlowId");

static {
SPAN_DECORATOR_FIELD.setAccessible(true);
Expand Down Expand Up @@ -87,7 +87,7 @@ void shouldAlwaysAddFlowIdSpanDecorator(
"true, flow id propagation should be enabled",
"false, flow id propagation should be enabled be disabled"
})
void shouldAddFlowIdHeaderWhenPropagateFlowIdEnabled(
void shouldConfigureFlowIdHeaderPropagation(
final boolean isFlowIdPropagationEnabled,
final String message
) throws IllegalAccessException {
Expand All @@ -97,18 +97,7 @@ void shouldAddFlowIdHeaderWhenPropagateFlowIdEnabled(
.isNotNull()
.isInstanceOf(OpenTelemetryPlugin.class);

final Optional<CompositeSpanDecorator> innerCompositeDecorator = getDecorator(
(SpanDecorator) SPAN_DECORATOR_FIELD.get(plugin),
CompositeSpanDecorator.class
);

assertThat(innerCompositeDecorator).isPresent();

final Optional<FlowIdSpanDecorator> decoratorCandidate = getDecorator(innerCompositeDecorator.get(), FlowIdSpanDecorator.class);
assertThat(decoratorCandidate).isNotEmpty();

final FlowIdSpanDecorator decorator = decoratorCandidate.get();
assertThat(PROPAGATE_FLOW_ID_FIELD.getBoolean(decorator))
assertThat(PROPAGATE_FLOW_ID_FIELD.getBoolean(plugin))
.withFailMessage(message)
.isEqualTo(isFlowIdPropagationEnabled);
}
Expand Down
Loading