Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ default <TReq, TResp, TParams> List<TReq> callFormat(
default <TResp> TResp runWithContext(ContextView reactorCtx, Supplier<TResp> inner) {
return inner.get();
}

default void shutdown() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ public static void register(Tracer tracer) {
}
}

public static void resetToNoop() {
Tracer previousTracer = TracerRegistry.tracer;
TracerRegistry.tracer = new NoopTracer();
disableTracingHook();
previousTracer.shutdown();
}

public static Tracer get() {
return tracer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2024-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.agentscope.core.tracing;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@DisplayName("TracerRegistry Tests")
class TracerRegistryTest {

@AfterEach
void tearDown() {
TracerRegistry.resetToNoop();
}

@Test
@DisplayName("resetToNoop() should shutdown current tracer and restore noop tracer")
void resetToNoopShouldShutdownCurrentTracer() {
CloseCountingTracer tracer = new CloseCountingTracer();
TracerRegistry.register(tracer);

TracerRegistry.resetToNoop();

assertEquals(1, tracer.shutdownCount());
assertInstanceOf(NoopTracer.class, TracerRegistry.get());
}

static class CloseCountingTracer implements Tracer {
private final AtomicInteger shutdownCount = new AtomicInteger();

@Override
public void shutdown() {
shutdownCount.incrementAndGet();
}

int shutdownCount() {
return shutdownCount.get();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static void shutdown() {
config = null;
client = null;
wsClient = null;
TracerRegistry.resetToNoop();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,16 @@
public class TelemetryTracer implements Tracer {

private final io.opentelemetry.api.trace.Tracer tracer;
private final SdkTracerProvider sdkTracerProvider;

public TelemetryTracer(io.opentelemetry.api.trace.Tracer tracer) {
this(tracer, null);
}

private TelemetryTracer(
io.opentelemetry.api.trace.Tracer tracer, SdkTracerProvider sdkTracerProvider) {
this.tracer = tracer;
this.sdkTracerProvider = sdkTracerProvider;
}

@Override
Expand Down Expand Up @@ -226,6 +233,13 @@ public <TResp> TResp runWithContext(ContextView reactorCtx, Supplier<TResp> inne
return otelContext.wrapSupplier(inner).get();
}

@Override
public void shutdown() {
if (sdkTracerProvider != null) {
sdkTracerProvider.close();
}
}

public static Builder builder() {
return new Builder();
}
Expand Down Expand Up @@ -294,14 +308,15 @@ public TelemetryTracer build() {
exporterBuilder.addHeader(entry.getKey(), entry.getValue());
}

TracerProvider tracerProvider =
SdkTracerProvider tracerProvider =
SdkTracerProvider.builder()
.addSpanProcessor(
BatchSpanProcessor.builder(exporterBuilder.build()).build())
.setSampler(Sampler.alwaysOn())
.build();

return new TelemetryTracer(tracerProvider.get(INSTRUMENTATION_NAME, Version.VERSION));
return new TelemetryTracer(
tracerProvider.get(INSTRUMENTATION_NAME, Version.VERSION), tracerProvider);
}
}
}
Loading