Provides OpenTelemetry instrumentation for NATS Client.
Replace OPENTELEMETRY_VERSION with the latest
release.
For Maven, add to your pom.xml dependencies:
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-nats-2.17</artifactId>
<version>OPENTELEMETRY_VERSION</version>
</dependency>
</dependencies>For Gradle, add to your dependencies:
implementation("io.opentelemetry.instrumentation:opentelemetry-nats-2.17:OPENTELEMETRY_VERSION")The instrumentation library provides the class NatsTelemetry that has a builder
method and allows the creation of an instance of the Connection to provide
OpenTelemetry-based instrumentation:
import io.nats.client.Connection;
import io.nats.client.Nats;
import io.nats.client.Options;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.nats.v2_17.NatsTelemetry;
import java.io.IOException;
public class OpenTelemetryNatsConnection {
private OpenTelemetry openTelemetry;
private NatsTelemetry telemetry;
public OpenTelemetryNatsConnection(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
this.telemetry = NatsTelemetry.create(openTelemetry);
}
public Connection newConnection() throws IOException, InterruptedException {
return newConnection(Options.builder());
}
public Connection newConnection(Options.Builder options) throws IOException, InterruptedException {
return telemetry.createConnection(options.build(), Nats::connect);
}
}