Skip to content

Commit

Permalink
Add support to disable tracing for HTTP requests (#18993)
Browse files Browse the repository at this point in the history
* Add support to disable tracing for HTTP requests

* Use getOrDefault

* Fix default

* Fix checkstyle
  • Loading branch information
srnagar authored Feb 5, 2021
1 parent ef2a853 commit e5fd329
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.core.util.serializer.SerializerEncoding;
import com.azure.core.util.tracing.Tracer;
import com.azure.core.util.tracing.TracerProxy;
import reactor.core.Exceptions;
import reactor.core.publisher.Flux;
Expand Down Expand Up @@ -180,13 +181,14 @@ static Flux<ByteBuffer> validateLength(final HttpRequest request) {
/**
* Starts the tracing span for the current service call, additionally set metadata attributes on the span by passing
* additional context information.
*
* @param method Service method being called.
* @param context Context information about the current service call.
*
* @return The updated context containing the span context.
*/
private Context startTracingSpan(Method method, Context context) {
if (!TracerProxy.isTracingEnabled()) {
boolean disableTracing = (boolean) context.getData(Tracer.DISABLE_TRACING_KEY).orElse(false);
if (!TracerProxy.isTracingEnabled() || disableTracing) {
return context;
}
String spanName = String.format("%s.%s", interfaceParser.getServiceName(), method.getName());
Expand Down Expand Up @@ -545,8 +547,9 @@ private static void endTracingSpan(Signal<HttpDecodedResponse> signal) {
// Get the context that was added to the mono, this will contain the information needed to end the span.
reactor.util.context.Context context = signal.getContext();
Optional<Context> tracingContext = context.getOrEmpty("TRACING_CONTEXT");
boolean disableTracing = context.getOrDefault(Tracer.DISABLE_TRACING_KEY, false);

if (!tracingContext.isPresent()) {
if (!tracingContext.isPresent() || disableTracing) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public interface Tracer {
*/
String MESSAGE_ENQUEUED_TIME = "x-opt-enqueued-time";

/**
* Key for {@link Context} which disables tracing for the request associated with the current context.
*/
String DISABLE_TRACING_KEY = "disable-tracing";

/**
* Creates a new tracing span.
* <p>
Expand Down

0 comments on commit e5fd329

Please sign in to comment.