Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -3,7 +3,7 @@
package com.azure.core.amqp.implementation;

import com.azure.core.amqp.exception.AmqpException;
import com.azure.core.implementation.tracing.ProcessKind;
import com.azure.core.util.tracing.ProcessKind;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.tracing.Tracer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import com.azure.core.amqp.exception.AmqpException;
import com.azure.core.amqp.exception.ErrorCondition;
import com.azure.core.implementation.tracing.ProcessKind;
import com.azure.core.util.tracing.ProcessKind;
import com.azure.core.util.Context;
import com.azure.core.util.tracing.Tracer;
import org.junit.After;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.core.implementation.tracing;
package com.azure.core.util.tracing;

/**
* Contains constants common AMQP protocol process calls.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

package com.azure.core.util.tracing;

import com.azure.core.implementation.tracing.ProcessKind;
import com.azure.core.util.Context;

/**
Expand Down Expand Up @@ -58,9 +57,14 @@ public interface Tracer {
* new span will be added as a child, otherwise the span will be created and added to the context and any downstream
* start calls will use the created span as the parent.
*
* <p><strong>Code Samples</strong></p>
* <p>Starts a tracing span with provided method name and explicit parent span</p>
* {@codesnippet com.azure.core.util.tracing.start#string-context}
*
* @param methodName Name of the method triggering the span creation.
* @param context Additional metadata that is passed through the call stack.
* @return An updated context object.
* @return The updated {@link Context} object containing the returned span.
* @throws NullPointerException if {@code methodName} or {@code context} is {@code null}.
*/
Context start(String methodName, Context context);

Expand All @@ -71,30 +75,56 @@ public interface Tracer {
* new span will be added as a child, otherwise the span will be created and added to the context and any downstream
* start calls will use the created span as the parent.
*
* Sets additional request attributes on the created span for the given {@link ProcessKind} SEND.
Comment thread
samvaity marked this conversation as resolved.
Outdated
* Returns the diagnostic Id and span context of the returned span for the given {@link ProcessKind} RECEIVE.
* Creates a new tracing span with remote parent and returns that scope when the given {@link ProcessKind} PROCESS.
*
* <p><strong>Code Samples</strong></p>
* <p>Starts a tracing span with provided method name and AMQP operation SEND</p>
* {@codesnippet com.azure.core.util.tracing.start#string-context-processKind-SEND}
*
* <p>Starts a tracing span with provided method name and AMQP operation RECEIVE</p>
* {@codesnippet com.azure.core.util.tracing.start#string-context-processKind-RECEIVE}
*
* <p>Starts a tracing span with provided method name and AMQP operation PROCESS</p>
* {@codesnippet com.azure.core.util.tracing.start#string-context-processKind-PROCESS}
*
* @param methodName Name of the method triggering the span creation.
* @param context Additional metadata that is passed through the call stack.
* @param processKind AMQP message process kind.
* @return An updated context object.
* @param processKind AMQP operation kind.
* @return The updated {@link Context} object containing the returned span.
* @throws NullPointerException if {@code methodName} or {@code context} or {@code processKind} is {@code null}.
*/
Context start(String methodName, Context context, ProcessKind processKind);

/**
* Completes the current tracing span.
*
* <p><strong>Code Samples</strong></p>
* <p>Completes the tracing span present in the context, with the corresponding OpenCensus status for the given
* response status code</p>
* {@codesnippet com.azure.core.util.tracing.end#int-throwable-context}
*
* @param responseCode Response status code if the span is in a HTTP call context.
* @param error Potential throwable that happened during the span.
* @param context Additional metadata that is passed through the call stack.
* @throws NullPointerException if {@code context} is {@code null}.
*/
void end(int responseCode, Throwable error, Context context);

/**
* Completes the current tracing span.
*
* @param errorCondition the error message that occurred during the call.
* <p><strong>Code Samples</strong></p>
* <p>Completes the tracing span with the corresponding OpenCensus status for the given status message</p>
* {@codesnippet com.azure.core.util.tracing.end#string-throwable-context}
*
* @param statusMessage the error or success message that occurred during the call.
* @param error Potential throwable that happened during the span.
* @param context Additional metadata that is passed through the call stack.
* @throws NullPointerException if {@code context} is {@code null}.
*/
void end(String errorCondition, Throwable error, Context context);
void end(String statusMessage, Throwable error, Context context);

/**
* Adds metadata to the current span. The {@code context} is checked for having span information, if no span
Expand All @@ -103,62 +133,48 @@ public interface Tracer {
* @param key Name of the metadata.
* @param value Value of the metadata.
* @param context Additional metadata that is passed through the call stack.
* @throws NullPointerException if {@code key} or {@code value} or {@code context} is {@code null}.
*/
void setAttribute(String key, String value, Context context);

/*
* Adds metadata to the current span. The {@code context} is checked for having span information, if no span
* information is found in the context no metadata is added.
*
* @param key Name of the metadata.
* @param value Value of the metadata.
* @param context Additional metadata that is passed through the call stack.
*/
// void setAttribute(String key, long value, Context context);

/*
* Adds metadata to the current span. The {@code context} is checked for having span information, if no span
* information is found in the context no metadata is added.
*
* @param key Name of the metadata.
* @param value Value of the metadata.
* @param context Additional metadata that is passed through the call stack.
*/
// void setAttribute(String key, double value, Context context);

/*
* Adds metadata to the current span. The {@code context} is checked for having span information, if no span
* information is found in the context no metadata is added.
*
* @param key Name of the metadata.
* @param value Value of the metadata.
* @param context Additional metadata that is passed through the call stack.
*/
// void setAttribute(String key, boolean value, Context context);

/**
* Configures the name for spans that are created.
*
* <p><strong>Code Samples</strong></p>
* <p>Retrieve the span name of the returned span</p>
* {@codesnippet com.azure.core.util.tracing.setSpanName#string-context}
*
* @param spanName Name to give the next span.
* @param context Additional metadata that is passed through the call stack.
* @return An updated context object.
* @return The updated {@link Context} object containing the name of the returned span.
* @throws NullPointerException if {@code spanName} or {@code context} is {@code null}.
*/
Context setSpanName(String spanName, Context context);

/**
* Adds a link to the tracing span.
* Provides a way to link multiple tracing spans.
* Used in batching operations to relate multiple requests under a single batch.
*
* <p><strong>Code Samples</strong></p>
* <p>Link multiple spans using their span context information</p>
* {@codesnippet com.azure.core.util.tracing.addLink#context}
*
* @param context Additional metadata that is passed through the call stack.
* @throws NullPointerException if {@code context} is {@code null}.
*/
void addLink(Context context);

/**
* Extracts the span's {@link Context} from the given event's diagnostic id.
* Extracts the span's context as {@link Context} from upstream.
*
* <p><strong>Code Samples</strong></p>
* <p>Extracts the corresponding span context information from a valid diagnostic id</p>
* {@codesnippet com.azure.core.util.tracing.extractContext#string-context}
*
* @param diagnosticId Unique identifier for the trace information of the span.
* @param context Additional metadata that is passed through the call stack.
* @return An updated context object.
* @return The updated {@link Context} object containing the span context.
* @throws NullPointerException if {@code diagnosticId} or {@code context} is {@code null}.
*/
Context extractContext(String diagnosticId, Context context);
}
15 changes: 0 additions & 15 deletions sdk/core/azure-core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,6 @@
com.azure.storage.blob, // FIXME this should not be a long-term solution
com.azure.storage.file, // FIXME this should not be a long-term solution
com.azure.storage.queue; // FIXME this should not be a long-term solution
exports com.azure.core.implementation.tracing to
com.azure.core.management, // FIXME this should not be a long-term solution
com.azure.core.test, // FIXME this should not be a long-term solution
com.azure.core.amqp, // FIXME this should not be a long-term solution
com.azure.http.netty, // FIXME this should not be a long-term solution
com.azure.messaging.eventhubs, // FIXME this should not be a long-term solution
com.azure.messaging.eventhubs.checkpointstore.blob, // FIXME this should not be a long-term solution
com.azure.identity, // FIXME this should not be a long-term solution
com.azure.security.keyvault.keys, // FIXME this should not be a long-term solution
com.azure.security.keyvault.secrets, // FIXME this should not be a long-term solution
com.azure.storage.common, // FIXME this should not be a long-term solution
com.azure.core.tracing.opencensus, // FIXME this should not be a long-term solution
com.azure.storage.blob, // FIXME this should not be a long-term solution
com.azure.storage.file, // FIXME this should not be a long-term solution
com.azure.storage.queue; // FIXME this should not be a long-term solution
exports com.azure.core.implementation.exception to
com.azure.core.management, // FIXME this should not be a long-term solution
com.azure.core.test, // FIXME this should not be a long-term solution
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.util.tracing;

import com.azure.core.util.Context;

import static com.azure.core.util.tracing.Tracer.OPENCENSUS_SPAN_NAME_KEY;
import static com.azure.core.util.tracing.Tracer.ENTITY_PATH;
import static com.azure.core.util.tracing.Tracer.OPENCENSUS_SPAN_KEY;
import static com.azure.core.util.tracing.Tracer.HOST_NAME;
import static com.azure.core.util.tracing.Tracer.SPAN_CONTEXT;
import static com.azure.core.util.tracing.Tracer.DIAGNOSTIC_ID_KEY;

/**
* Contains code snippets when generating javadocs through doclets for {@link Tracer}.
*/
public class TracerJavaDocCodeSnippets {
final Tracer tracer = new TracerImplementation();

/**
* Code snippet for {@link Tracer#start(String, Context, ProcessKind)} and {@link Tracer#start(String, Context)}
*/
public void startTracingSpan() {
// BEGIN: com.azure.core.util.tracing.start#string-context
// pass the current tracing span context to the calling method
Context traceContext = new Context(OPENCENSUS_SPAN_KEY, "<user-current-span>");
// start a new tracing span with the given method name and explicit parent span
Context updatedContext = tracer.start("azure.keyvault.secrets/setsecret", traceContext);
System.out.printf("Span returned in the context object: %s%n",
updatedContext.getData(OPENCENSUS_SPAN_KEY).get().getClass());
Comment thread
samvaity marked this conversation as resolved.
Outdated
// END: com.azure.core.util.tracing.start#string-context

// BEGIN: com.azure.core.util.tracing.start#string-context-processKind-SEND
// pass the current tracing span and request metadata to the calling method
Context sendContext = new Context(OPENCENSUS_SPAN_KEY, "<user-current-span>")
.addData(ENTITY_PATH, "entity-path").addData(HOST_NAME, "hostname");

// start a new tracing span with explicit parent, sets the request attributes on the span and sets the span
// kind to client when process kind SEND
Context updatedSendContext = tracer.start("azure.eventhubs.send", sendContext, ProcessKind.SEND);
System.out.printf("Span returned in the context object: %s%n",
updatedSendContext.getData(OPENCENSUS_SPAN_KEY).get());
// END: com.azure.core.util.tracing.start#string-context-processKind-SEND

// BEGIN: com.azure.core.util.tracing.start#string-context-processKind-RECEIVE
// start a new tracing span with explicit parent, sets the diagnostic Id (traceparent headers) on the current
// context when process kind RECEIVE
Context updatedReceiveContext = tracer.start("azure.eventhubs.receive", traceContext,
ProcessKind.RECEIVE);
System.out.printf("Diagnostic Id: {} %s%n", (String) updatedReceiveContext.getData(DIAGNOSTIC_ID_KEY).get());
Comment thread
samvaity marked this conversation as resolved.
Outdated
// END: com.azure.core.util.tracing.start#string-context-processKind-RECEIVE

// BEGIN: com.azure.core.util.tracing.start#string-context-processKind-PROCESS
// start a new tracing span with remote parent and uses the span in the current context to return a scope
// when process kind PROCESS
Context processContext = new Context(OPENCENSUS_SPAN_KEY, "<user-current-span>")
.addData(SPAN_CONTEXT, "<user-current-span-context>");
Context updatedProcessContext = tracer.start("azure.eventhubs.process", processContext,
ProcessKind.PROCESS);
System.out.printf("Scope: {} %s%n", updatedProcessContext.getData("scope").get().getClass());
Comment thread
samvaity marked this conversation as resolved.
Outdated
// END: com.azure.core.util.tracing.start#string-context-processKind-PROCESS
}

/**
* Code snippet for {@link Tracer#end(int, Throwable, Context)} and {@link Tracer#end(String, Throwable, Context)}
*/
public void endTracingSpan() {
// BEGIN: com.azure.core.util.tracing.end#int-throwable-context
// context containing the current tracing span to end
Context traceContext = new Context(OPENCENSUS_SPAN_KEY, "<user-current-span>");

// completes the tracing span with the passed response status code
tracer.end(200, null, traceContext);
// END: com.azure.core.util.tracing.end#int-throwable-context

// BEGIN: com.azure.core.util.tracing.end#string-throwable-context
// context containing the current tracing span to end
// completes the tracing span with the passed status message
tracer.end("success", null, traceContext);
// END: com.azure.core.util.tracing.end#string-throwable-context
}

/**
* Code snippet for {@link Tracer#setSpanName(String, Context)}
*/
public void setSpanName() {
// BEGIN: com.azure.core.util.tracing.setSpanName#string-context
// Sets the span name of the returned span on the context object, with key OPENCENSUS_SPAN_NAME_KEY
Context context = tracer.setSpanName("test-span-method", Context.NONE);
System.out.printf("Span name: %s%n", (String) context.getData(OPENCENSUS_SPAN_NAME_KEY).get());
// END: com.azure.core.util.tracing.setSpanName#string-context
}

/**
* Code snippet for {@link Tracer#addLink(Context)}
*/
public void addLink() {
// BEGIN: com.azure.core.util.tracing.addLink#context
// use the parent context containing the current tracing span to start a child span
Context parentContext = new Context(OPENCENSUS_SPAN_KEY, "<user-current-span>");
// use the returned span context information of the current tracing span to link
Context spanContext = tracer.start("test.method", parentContext, ProcessKind.RECEIVE);

// Adds a link between multiple span's using the span context information of the Span
// For each event processed, add a link with the created spanContext
tracer.addLink(spanContext);
// END: com.azure.core.util.tracing.addLink#context
}

/**
* Code snippet for {@link Tracer#extractContext(String, Context)}
*/
public void extractContext() {
// BEGIN: com.azure.core.util.tracing.extractContext#string-context
// Extracts the span context information from the passed diagnostic Id that can be used for linking spans.
Context spanContext = tracer.extractContext("valid-diagnostic-id", Context.NONE);
System.out.printf("Span context of the current tracing span: %s%n", spanContext.getData(SPAN_CONTEXT).get());
// END: com.azure.core.util.tracing.extractContext#string-context
}

//Noop Tracer
private static final class TracerImplementation implements Tracer {
@Override
public Context start(String methodName, Context context) {
return null;
}

@Override
public Context start(String methodName, Context context, ProcessKind processKind) {
return null;
}

@Override
public void end(int responseCode, Throwable error, Context context) {

}

@Override
public void end(String errorCondition, Throwable error, Context context) {

}

@Override
public void setAttribute(String key, String value, Context context) {

}

@Override
public Context setSpanName(String spanName, Context context) {
return null;
}

@Override
public void addLink(Context context) {

}

@Override
public Context extractContext(String diagnosticId, Context context) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.azure.core.amqp.implementation.MessageSerializer;
import com.azure.core.amqp.implementation.TracerProvider;
import com.azure.core.annotation.Immutable;
import com.azure.core.implementation.tracing.ProcessKind;
import com.azure.core.util.tracing.ProcessKind;
import com.azure.core.implementation.util.ImplUtils;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import com.azure.core.amqp.exception.AmqpException;
import com.azure.core.amqp.implementation.TracerProvider;
import com.azure.core.implementation.tracing.ProcessKind;
import com.azure.core.util.tracing.ProcessKind;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.messaging.eventhubs.CloseReason;
Expand Down
Loading