io.github.middlewarelabs.agentapmjava.MwTracer Maven / Gradle / Ivy
package io.github.middlewarelabs.agentapmjava;
// import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
// import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
// import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME;
// import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.resources.Resource;
import java.util.concurrent.TimeUnit;
public class MwTracer {
public static final String OTEL_RESOURCE_ATTRIBUTES_KEY = "otel.resource.attributes";
public static String OTEL_RESOURCE_ATTRIBUTES_VALUE = "service.name=HelloWorldJavaService";
public static final String INSTRUMENTATION_LIBRARY_NAME = "instrumentation-library-name";
public static final String INSTRUMENTATION_VERSION = "1.0.0";
static Tracer tracer;
public static Tracer track(String project, String service) throws InterruptedException {
OTEL_RESOURCE_ATTRIBUTES_VALUE = "service.name=" + service + ",project.name=" + project ;
/*this will make sure that a proper service.name attribute is set on all the
spans/metrics.*/
System.setProperty(OTEL_RESOURCE_ATTRIBUTES_KEY, OTEL_RESOURCE_ATTRIBUTES_VALUE);
/*tracer must be acquired, which is responsible for creating spans and interacting with the Context*/
tracer = getTracer();
return tracer;
}
public static void childMethod(Span parentSpan) {
tracer = getTracer();
/*setParent(...) is not required, `Span.current()` is automatically added as the parent*/
Span childSpan = tracer.spanBuilder("childSpan").setParent(Context.current().with(parentSpan))
.startSpan();
// logger.info("In child method. TraceID : {}", childSpan.getSpanContext().getTraceIdAsHexString());
/*put the span into the current Context*/
try (Scope scope = childSpan.makeCurrent()) {
Thread.sleep(1000);
} catch (Throwable throwable) {
childSpan.setStatus(StatusCode.ERROR, "Something wrong with the child span");
} finally {
childSpan.end();
}
}
public static Tracer getTracer() {
if (tracer == null) {
OtlpGrpcSpanExporter spanExporter = OtlpGrpcSpanExporter.builder()
.setTimeout(2, TimeUnit.SECONDS)
.setEndpoint("http://localhost:9319")
.build();
BatchSpanProcessor spanProcessor = BatchSpanProcessor.builder(spanExporter).setScheduleDelay(100, TimeUnit.MILLISECONDS). build();
Resource serviceNameResource = Resource
.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "test-service"));
SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(spanProcessor)
.setResource(Resource.getDefault().merge(serviceNameResource)).build();
OpenTelemetrySdk opentelemetrysdk = OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).buildAndRegisterGlobal();;
// Tracer tracer = opentelemetrysdk.getTracer("test");
Runtime.getRuntime().addShutdownHook(new Thread(tracerProvider::shutdown));
tracer = opentelemetrysdk.getTracer(INSTRUMENTATION_LIBRARY_NAME, INSTRUMENTATION_VERSION);
}
return tracer;
}
}