io.opentracing.util.ThreadLocalScopeManager Java Examples

The following examples show how to use io.opentracing.util.ThreadLocalScopeManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MicroProfileOpenTracingTCKDeploymentPackager.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public Archive<?> generateDeployment(final TestDeployment testDeployment,
                                     final Collection<ProtocolArchiveProcessor> processors) {

    final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "microprofile-opentracing.war")
                                            .merge(testDeployment.getApplicationArchive());

    // opentracing-api jar added by Geronimo Impl. Also added by TCK causes issues with same classes in different Classloaders
    final Map<ArchivePath, Node> content = webArchive.getContent(object -> object.get().matches(".*opentracing-api.*jar.*"));
    content.forEach((archivePath, node) -> webArchive.delete(archivePath));

    // TCK expects a MockTracer. Check org/eclipse/microprofile/opentracing/tck/application/TracerWebService.java:133
    webArchive.addAsLibrary(jarLocation(MockTracer.class));
    webArchive.addAsLibrary(jarLocation(ThreadLocalScopeManager.class));
    webArchive.addAsWebInfResource("META-INF/beans.xml");
    webArchive.addClass(MicroProfileOpenTracingTCKTracer.class);

    System.out.println(webArchive.toString(true));

    return super.generateDeployment(
            new TestDeployment(null, webArchive, testDeployment.getAuxiliaryArchives()), processors);
}
 
Example #2
Source File: Sample01.java    From java-span-reporter with Apache License 2.0 5 votes vote down vote up
private static Tracer provideTracerByConstructor() throws Exception {
    Tracer backend = new MockTracer();
    Reporter reporter = new Slf4jReporter(LoggerFactory.getLogger("tracer"), true);
    //ScopeManager scopeManager = new ThreadLocalScopeManager();
    ScopeManager scopeManager = new ThreadLocalScopeManager();
    return new TracerR(backend, reporter, scopeManager);
}
 
Example #3
Source File: LogTracer.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public Tracer getTracer(String serviceName) {
    boolean LogEnabled = Boolean.valueOf(configuration.getFirstProperty(TracingConstants.LOG_ENABLED));
    if (LogEnabled) {
        Tracer tracer = NoopTracerFactory.create();
        Reporter reporter = new TracingReporter(LogFactory.getLog(TracingConstants.TRACER));
        Tracer tracerR = new TracerR(tracer, reporter, new ThreadLocalScopeManager());
        GlobalTracer.register(tracerR);
        return tracerR;
    }
    return null;
}
 
Example #4
Source File: ZipkinTracer.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
@Override
public Tracer getTracer(String serviceName) {
    String hostname = configuration.getFirstProperty(TracingConstants.ZIPKIN_CONFIG_HOST) != null ?
            configuration.getFirstProperty(TracingConstants.ZIPKIN_CONFIG_HOST)
            : TracingConstants.ZIPKIN_DEFAULT_HOST;

    int port = configuration.getFirstProperty(TracingConstants.ZIPKIN_CONFIG_PORT) != null ?
            Integer.parseInt(configuration.getFirstProperty(TracingConstants.ZIPKIN_CONFIG_PORT))
            : TracingConstants.ZIPKIN_DEFAULT_PORT;

    boolean tracerLogEnabled =
            Boolean.parseBoolean(configuration.getFirstProperty(TracingConstants.CONFIG_TRACER_LOG_ENABLED) != null ?
            configuration.getFirstProperty(TracingConstants.CONFIG_TRACER_LOG_ENABLED)
            : TracingConstants.DEFAULT_TRACER_LOG_ENABLED);

    OkHttpSender sender = OkHttpSender.create("http://" + hostname + ":" + port + TracingConstants.ZIPKIN_API_CONTEXT);
    Tracer tracer = BraveTracer.create(Tracing.newBuilder()
            .localServiceName(serviceName)
            .spanReporter(AsyncReporter.builder(sender).build())
            .propagationFactory(ExtraFieldPropagation.newFactory(B3Propagation.FACTORY, TracingConstants.REQUEST_ID))
            .build());

    if (tracerLogEnabled) {
        Reporter reporter = new TracingReporter(LogFactory.getLog(TracingConstants.TRACER));
        Tracer tracerR = new TracerR(tracer, reporter, new ThreadLocalScopeManager());
        GlobalTracer.register(tracerR);
        return tracerR;
    } else {
        GlobalTracer.register(tracer);
        return tracer;
    }
}
 
Example #5
Source File: AbstractClientTest.java    From java-jaxrs with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultConfiguration() {
    MockTracer mockTracer = new MockTracer(new ThreadLocalScopeManager(), Propagator.TEXT_MAP);
    GlobalTracer.registerIfAbsent(mockTracer);

    Client client = ClientBuilder.newClient()
            .register(ClientTracingFeature.class);

    Response response = client.target(url("/hello"))
            .request()
            .get();
    response.close();
    assertNoActiveSpan();
    Assert.assertEquals(1, mockTracer.finishedSpans().size());
}
 
Example #6
Source File: AbstractJettyTest.java    From java-web-servlet-filter with Apache License 2.0 5 votes vote down vote up
@Before
public void beforeTest() throws Exception {
    mockTracer = Mockito.spy(new MockTracer(new ThreadLocalScopeManager(), MockTracer.Propagator.TEXT_MAP));

    ServletContextHandler servletContext = new ServletContextHandler();
    servletContext.setContextPath(contextPath);
    servletContext.addServlet(TestServlet.class, "/hello");
    
    ServletHolder asyncServletHolder = new ServletHolder(new AsyncServlet(mockTracer));
    servletContext.addServlet(asyncServletHolder, "/async");
    asyncServletHolder.setAsyncSupported(true);
    servletContext.addServlet(AsyncImmediateExitServlet.class, "/asyncImmediateExit")
            .setAsyncSupported(true);

    ServletHolder timeoutServletHolder = new ServletHolder(new AsyncTimeoutServlet());
    timeoutServletHolder.setAsyncSupported(true);
    servletContext.addServlet(timeoutServletHolder, "/asyncTimeout");

    servletContext.addServlet(new ServletHolder(new LocalSpanServlet(mockTracer)), "/localSpan");
    servletContext.addServlet(new ServletHolder(new CurrentSpanServlet(mockTracer)), "/currentSpan");
    servletContext.addServlet(ExceptionServlet.class, "/servletException");

    servletContext.addFilter(new FilterHolder(tracingFilter()), "/*", EnumSet.of(DispatcherType.REQUEST,
            DispatcherType.FORWARD, DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.INCLUDE));
    servletContext.addFilter(ErrorFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));

    initServletContext(servletContext);

    jettyServer = new Server(0);
    jettyServer.setHandler(servletContext);
    jettyServer.start();
    serverPort = ((ServerConnector)jettyServer.getConnectors()[0]).getLocalPort();
}
 
Example #7
Source File: NoContextJettyTest.java    From java-web-servlet-filter with Apache License 2.0 5 votes vote down vote up
@Before
public void beforeTest() throws Exception {
	mockTracer = Mockito.spy(new MockTracer(new ThreadLocalScopeManager(), MockTracer.Propagator.TEXT_MAP));

	ServletHandler servletHandler = new ServletHandler();
	servletHandler.addServletWithMapping(TestServlet.class, "/hello");

	servletHandler.addFilterWithMapping(new FilterHolder(tracingFilter()), "/*", EnumSet.of(DispatcherType.REQUEST,
			DispatcherType.FORWARD, DispatcherType.ASYNC, DispatcherType.ERROR, DispatcherType.INCLUDE));

	jettyServer = new Server(0);
	jettyServer.setHandler(servletHandler);
	jettyServer.start();
	serverPort = ((ServerConnector) jettyServer.getConnectors()[0]).getLocalPort();
}
 
Example #8
Source File: QuarkusJaegerTracer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private ScopeManager getScopeManager() {
    ScopeManager scopeManager = new ThreadLocalScopeManager();
    if (logTraceContext) {
        scopeManager = new MDCScopeManager(scopeManager);
    }
    return scopeManager;
}
 
Example #9
Source File: NestedMdcScopesTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
public void mdcIsRestoredCorrectly() {
    ThreadLocalScopeManager threadLocalScopeManager = new ThreadLocalScopeManager();
    MDCScopeManager mdcScopeManager = new MDCScopeManager(threadLocalScopeManager);

    assertNull(mdcScopeManager.active());
    assertNull(threadLocalScopeManager.active());
    assertNull(MDC.get("traceId"));

    JaegerSpanContext span = new JaegerSpanContext(1, 1, 1, 1, Byte.valueOf("0"));
    Scope scope = mdcScopeManager.activate(new TestSpan(span), true);
    assertSame(span, threadLocalScopeManager.active().span().context());
    assertEquals("10000000000000001", MDC.get("traceId"));

    JaegerSpanContext subSpan = new JaegerSpanContext(2, 2, 2, 1, Byte.valueOf("0"));
    Scope subScope = mdcScopeManager.activate(new TestSpan(subSpan), true);
    assertSame(subSpan, threadLocalScopeManager.active().span().context());
    assertEquals("20000000000000002", MDC.get("traceId"));

    subScope.close();

    assertSame(span, threadLocalScopeManager.active().span().context());
    assertEquals("10000000000000001", MDC.get("traceId"));

    scope.close();

    assertNull(mdcScopeManager.active());
    assertNull(threadLocalScopeManager.active());
    assertNull(MDC.get("traceId"));
}
 
Example #10
Source File: CustomSpanDecoratorAutoConfigurationTest.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Bean
public MockTracer tracer() {
    return new MockTracer(new ThreadLocalScopeManager());
}
 
Example #11
Source File: DisabledRestTemplateTracingAutoConfigurationTest.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Bean
public MockTracer tracer() {
    return new MockTracer(new ThreadLocalScopeManager());
}
 
Example #12
Source File: RestTemplatePostProcessingConfigurationTest.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Bean
public MockTracer tracer() {
    return new MockTracer(new ThreadLocalScopeManager());
}
 
Example #13
Source File: DefaultLoggerTracer.java    From java-span-reporter with Apache License 2.0 4 votes vote down vote up
/**
 * No args constructor used by Service Loader
 */
public DefaultLoggerTracer() {
    super(new MockTracer(), new Slf4jReporter(LoggerFactory.getLogger("tracer"), true), new ThreadLocalScopeManager());
}
 
Example #14
Source File: ProxyMockTracer.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
public ProxyMockTracer(final Tracer tracer) {
  super(new ThreadLocalScopeManager(), Propagator.TEXT_MAP);
  this.realTracer = tracer;
}
 
Example #15
Source File: LoggerTracerModule.java    From java-span-reporter with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
protected ScopeManager scopeManager(@Named("backend") Tracer tracer) {
    return (tracer.scopeManager() == null) ? tracer.scopeManager() : new ThreadLocalScopeManager();
}
 
Example #16
Source File: AsyncRestTemplatePostProcessingConfigurationTest.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Bean
public MockTracer tracer() {
    return new MockTracer(new ThreadLocalScopeManager());
}
 
Example #17
Source File: WebClientTracingAutoConfigurationTest.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Bean
public MockTracer tracer() {
    return new MockTracer(new ThreadLocalScopeManager());
}
 
Example #18
Source File: MockTracer.java    From opentracing-java with Apache License 2.0 4 votes vote down vote up
public MockTracer() {
    this(new ThreadLocalScopeManager(), Propagator.TEXT_MAP);
}
 
Example #19
Source File: MockTracer.java    From opentracing-java with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new MockTracer that passes through any calls to inject() and/or extract().
 */
public MockTracer(Propagator propagator) {
    this(new ThreadLocalScopeManager(), propagator);
}
 
Example #20
Source File: JaegerTracer.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Override
public Tracer getTracer(String serviceName) {
    String hostname = configuration.getFirstProperty(TracingConstants.JAEGER_CONFIG_HOST) != null ?
            configuration.getFirstProperty(TracingConstants.JAEGER_CONFIG_HOST)
            : TracingConstants.JAEGER_DEFAULT_HOST;

    int port = configuration.getFirstProperty(TracingConstants.JAEGER_CONFIG_PORT) != null ?
            Integer.parseInt(configuration.getFirstProperty(TracingConstants.JAEGER_CONFIG_PORT))
            : TracingConstants.JAEGER_DEFAULT_PORT;

    String samplerType = configuration.getFirstProperty(TracingConstants.CONFIG_SAMPLER_TYPE) != null ?
            configuration.getFirstProperty(TracingConstants.CONFIG_SAMPLER_TYPE)
            : TracingConstants.DEFAULT_SAMPLER_TYPE;

    float samplerParam = configuration.getFirstProperty(TracingConstants.CONFIG_SAMPLER_PARAM) != null ?
            Float.parseFloat(configuration.getFirstProperty(TracingConstants.CONFIG_SAMPLER_PARAM))
            : TracingConstants.DEFAULT_SAMPLER_PARAM;

    int reporterFlushInterval =
            configuration.getFirstProperty(TracingConstants.CONFIG_REPORTER_FLUSH_INTERVAL) != null ?
            Integer.parseInt(configuration.getFirstProperty(TracingConstants.CONFIG_REPORTER_FLUSH_INTERVAL))
            : TracingConstants.DEFAULT_REPORTER_FLUSH_INTERVAL;

    int reporterBufferSize = configuration.getFirstProperty(TracingConstants.CONFIG_REPORTER_BUFFER_SIZE) != null ?
            Integer.parseInt(configuration.getFirstProperty(TracingConstants.CONFIG_REPORTER_BUFFER_SIZE))
            : TracingConstants.DEFAULT_REPORTER_BUFFER_SIZE;

    boolean tracerLogEnabled =
            Boolean.parseBoolean(configuration.getFirstProperty(TracingConstants.CONFIG_TRACER_LOG_ENABLED) != null ?
            configuration.getFirstProperty(TracingConstants.CONFIG_TRACER_LOG_ENABLED)
            : TracingConstants.DEFAULT_TRACER_LOG_ENABLED);

    Configuration.SamplerConfiguration samplerConfig = new Configuration.SamplerConfiguration()
            .withType(samplerType)
            .withParam(samplerParam);
    Configuration.SenderConfiguration senderConfig = new Configuration.SenderConfiguration()
            .withAgentHost(hostname)
            .withAgentPort(port);
    Configuration.ReporterConfiguration reporterConfig = new Configuration.ReporterConfiguration()
            .withLogSpans(true)
            .withFlushInterval(reporterFlushInterval)
            .withMaxQueueSize(reporterBufferSize)
            .withSender(senderConfig);

    Tracer tracer = new Configuration(serviceName).withSampler(samplerConfig)
            .withReporter(reporterConfig).getTracer();

    if (tracerLogEnabled) {
        Reporter reporter = new TracingReporter(LogFactory.getLog(TracingConstants.TRACER));
        Tracer tracerR = new TracerR(tracer, reporter, new ThreadLocalScopeManager());
        GlobalTracer.register(tracerR);
        return tracerR;
    } else {
        GlobalTracer.register(tracer);
        return tracer;
    }
}
 
Example #21
Source File: MockTracer.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new MockTracer that passes through any calls to inject() and/or extract().
 */
public MockTracer(Propagator propagator) {
    this(new ThreadLocalScopeManager(), propagator);
}
 
Example #22
Source File: MockTracer.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
public MockTracer() {
    this(new ThreadLocalScopeManager(), Propagator.TEXT_MAP);
}