Skip to content

Instantly share code, notes, and snippets.

@carlosalberto
Created September 4, 2020 14:40
Show Gist options
  • Save carlosalberto/ef2c8f4762aa7cfee249984c17a83058 to your computer and use it in GitHub Desktop.
Save carlosalberto/ef2c8f4762aa7cfee249984c17a83058 to your computer and use it in GitHub Desktop.
import opentelemetry.context.Scope;
import opentelemetry.trace.Tracer;
import opentelemetry.trace.Span;
# No imports on Brave
// Start a new trace or a span within an existing trace representing an operation
Span span = tracer.spanBuilder("encode").startSpan();
// Put the span in "scope" so that downstream code such as loggers can see trace IDs
try (Scope scope = tracer.withSpan(span)) {
return encoder.encode();
} catch (RuntimeException | Error e) {
span.recordException(e); // Unless you handle exceptions, you might not know the operation failed!
throw e;
} finally {
span.end(); // note the scope is independent of the span. Always finish a span.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment