Skip to content

Commit

Permalink
Clarify advice on using global TracerProvider (#3703)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Dec 22, 2023
1 parent ac46f6b commit 862b922
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions content/en/docs/concepts/instrumentation/libraries.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,11 @@ considerations to help you decide how to minimize dependency hell:
### Getting a tracer

All application configuration is hidden from your library through the Tracer
API. Libraries should obtain tracer from
[global `TracerProvider`](/docs/specs/otel/trace/api/#get-a-tracer) by default.

```java
private static final Tracer tracer = GlobalOpenTelemetry.getTracer("demo-db-client", "0.1.0-beta1");
```

It's useful for libraries to have an API that allows applications to pass
instances of `TracerProvider` explicitly which enables better dependency
injection and simplifies testing.
API. Libraries may allow applications to pass instances of `TracerProvider` to
facilitate dependency injection and ease of testing, or obtain it from
[global `TracerProvider`](/docs/specs/otel/trace/api/#get-a-tracer).
OpenTelemetry language implementations may have different preferences for
passing instances or accessing the global based on what's idiomatic.

When obtaining the tracer, provide your library (or tracing plugin) name and
version - they show up on the telemetry and help users process and filter
Expand All @@ -167,7 +162,15 @@ and outcome of library calls. Which calls to trace:
**Instrumentation example:**

```java
private static final Tracer tracer = GlobalOpenTelemetry.getTracer("demo-db-client", "0.1.0-beta1");
private static Tracer tracer = getTracer(TracerProvider.noop());

public static void setTracerProvider(TracerProvider tracerProvider) {
tracer = getTracer(tracerProvider);
}

private static Tracer getTracer(TracerProvider tracerProvider) {
return tracerProvider.getTracer("demo-db-client", "0.1.0-beta1");
}

private Response selectWithTracing(Query query) {
// check out conventions for guidance on span names and attributes
Expand Down

0 comments on commit 862b922

Please sign in to comment.