forked from open-telemetry/opentelemetry-java-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalOpenTelemetryNativeInstrumentationUsage.java
More file actions
47 lines (37 loc) · 1.53 KB
/
GlobalOpenTelemetryNativeInstrumentationUsage.java
File metadata and controls
47 lines (37 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package otel;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
public class GlobalOpenTelemetryNativeInstrumentationUsage {
public static void globalOpenTelemetryUsage(OpenTelemetry openTelemetry) {
// Initialized with OpenTelemetry from java agent if present, otherwise no-op implementation.
MyClient client1 = new MyClientBuilder().build();
// Initialized with an explicit OpenTelemetry instance, overriding the java agent instance.
MyClient client2 = new MyClientBuilder().setOpenTelemetry(openTelemetry).build();
}
/**
* An example library with native OpenTelemetry instrumentation, initialized via {@link
* MyClientBuilder}.
*/
public static class MyClient {
private final OpenTelemetry openTelemetry;
private MyClient(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}
// ... library methods omitted
}
/** Builder for {@link MyClient}. */
public static class MyClientBuilder {
// OpenTelemetry defaults to the GlobalOpenTelemetry instance if set, e.g. by the java agent or
// by the application, else to a no-op implementation.
private OpenTelemetry openTelemetry = GlobalOpenTelemetry.getOrNoop();
/** Explicitly set the OpenTelemetry instance to use. */
public MyClientBuilder setOpenTelemetry(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
return this;
}
/** Build the client. */
public MyClient build() {
return new MyClient(openTelemetry);
}
}
}