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 onCreate callback to WebFluxSpanDecorator #141

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 The OpenTracing Authors
* Copyright 2016-2021 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -126,8 +126,8 @@ public void testDecoratedProviderIsUsed() {
Awaitility.await().until(reportedSpansSize(), IsEqual.equalTo(1));

assertThat(mockTracer.finishedSpans()).hasSize(1);
assertThat(Mockito.mockingDetails(mockDecorator1).getInvocations()).hasSize(2);
assertThat(Mockito.mockingDetails(mockDecorator2).getInvocations()).hasSize(2);
assertThat(Mockito.mockingDetails(mockDecorator1).getInvocations()).hasSize(3);
assertThat(Mockito.mockingDetails(mockDecorator2).getInvocations()).hasSize(3);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors. Copyright 2019 The OpenTracing Authors.
* Copyright 2013-2021 the original author or authors. Copyright 2019 The OpenTracing Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,8 @@
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.tag.Tags;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.CoreSubscriber;
Expand All @@ -36,6 +38,9 @@
* @author Csaba Kos
*/
class TracingOperator extends MonoOperator<Void, Void> {

private static final Log LOG = LogFactory.getLog(TracingOperator.class);

private final Tracer tracer;
private final ServerWebExchange exchange;
private final List<WebFluxSpanDecorator> spanDecorators;
Expand Down Expand Up @@ -70,8 +75,17 @@ public void subscribe(final CoreSubscriber<? super Void> subscriber) {
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
.start();

exchange.getAttributes().put(TracingWebFilter.SERVER_SPAN_CONTEXT, span.context());

for (WebFluxSpanDecorator spanDecorator : spanDecorators) {
try {
spanDecorator.onCreate(exchange, span);
} catch (RuntimeException exDecorator) {
LOG.error("Exception during decorating span", exDecorator);
}
}

try (final Scope scope = tracer.scopeManager().activate(span)) {
exchange.getAttributes().put(TracingWebFilter.SERVER_SPAN_CONTEXT, span.context());
source.subscribe(new TracingSubscriber(subscriber, exchange, context, span, spanDecorators));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2019 The OpenTracing Authors
* Copyright 2016-2021 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -34,8 +34,17 @@
public interface WebFluxSpanDecorator {

/**
* Decorate span before {@code .onSubscribe()} is called. This is called right after span in created. Span
* context is already present in request attributes with name {@link TracingWebFilter#SERVER_SPAN_CONTEXT}.
* Decorate span right after its creation.
* Span context is already present in request attributes with name {@link TracingWebFilter#SERVER_SPAN_CONTEXT}.
*
* @param exchange web exchange
* @param span span to decorate
*/
default void onCreate(ServerWebExchange exchange, Span span) { }

/**
* Decorate span when {@code .onSubscribe()} is called.
* Span context is already present in request attributes with name {@link TracingWebFilter#SERVER_SPAN_CONTEXT}.
*
* @param exchange web exchange
* @param span span to decorate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2020 The OpenTracing Authors
* Copyright 2016-2021 The OpenTracing Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
Expand All @@ -20,6 +20,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpMethod;
Expand All @@ -38,8 +39,8 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verifyNoMoreInteractions;

@RunWith(MockitoJUnitRunner.class)
public class TracingSubscriberTest {
Expand Down Expand Up @@ -91,9 +92,12 @@ public void testSpanIsFinishedWhenMonoHasCompleted() throws InterruptedException
assertEquals(SignalType.ON_COMPLETE, finalSignalType.get());

Span finishedSpan = tracer.finishedSpans().get(0);
verify(spanDecorator).onRequest(exchange, finishedSpan);
verify(spanDecorator).onResponse(exchange, finishedSpan);
verify(spanDecorator, never()).onError(any(ServerWebExchange.class), any(Throwable.class), any(Span.class));

InOrder inOrder = inOrder(spanDecorator);
inOrder.verify(spanDecorator).onCreate(exchange, finishedSpan);
inOrder.verify(spanDecorator).onRequest(exchange, finishedSpan);
inOrder.verify(spanDecorator).onResponse(exchange, finishedSpan);
verifyNoMoreInteractions(spanDecorator);
}

@Test
Expand Down Expand Up @@ -122,9 +126,12 @@ public void testSpanIsFinishedWhenMonoHasError() throws InterruptedException {
assertEquals(SignalType.ON_ERROR, finalSignalType.get());

Span finishedSpan = tracer.finishedSpans().get(0);
verify(spanDecorator).onRequest(exchange, finishedSpan);
verify(spanDecorator).onError(eq(exchange), any(Throwable.class), eq(finishedSpan));
verify(spanDecorator, never()).onResponse(any(ServerWebExchange.class), any(Span.class));

InOrder inOrder = inOrder(spanDecorator);
inOrder.verify(spanDecorator).onCreate(exchange, finishedSpan);
inOrder.verify(spanDecorator).onRequest(exchange, finishedSpan);
inOrder.verify(spanDecorator).onError(eq(exchange), any(Throwable.class), eq(finishedSpan));
verifyNoMoreInteractions(spanDecorator);
}

@Test
Expand Down Expand Up @@ -168,8 +175,10 @@ public void testSpanIsFinishedWhenMonoHasCanceled() throws InterruptedException
assertEquals(SignalType.CANCEL, finalSignalType.get());

Span finishedSpan = tracer.finishedSpans().get(0);
verify(spanDecorator).onRequest(exchange, finishedSpan);
verify(spanDecorator, never()).onError(any(ServerWebExchange.class), any(Throwable.class), any(Span.class));
verify(spanDecorator, never()).onResponse(any(ServerWebExchange.class), any(Span.class));

InOrder inOrder = inOrder(spanDecorator);
inOrder.verify(spanDecorator).onCreate(exchange, finishedSpan);
inOrder.verify(spanDecorator).onRequest(exchange, finishedSpan);
verifyNoMoreInteractions(spanDecorator);
}
}