Java Code Examples for io.opentracing.util.GlobalTracer#registerIfAbsent()

The following examples show how to use io.opentracing.util.GlobalTracer#registerIfAbsent() . 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: KafkaProducerExample.java    From client-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    KafkaProducerConfig config = KafkaProducerConfig.fromEnv();
    Properties props = KafkaProducerConfig.createProperties(config);

    if (System.getenv("JAEGER_SERVICE_NAME") != null)   {
        Tracer tracer = Configuration.fromEnv().getTracer();
        GlobalTracer.registerIfAbsent(tracer);

        props.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingProducerInterceptor.class.getName());
    }

    KafkaProducer producer = new KafkaProducer(props);
    log.info("Sending {} messages ...", config.getMessageCount());
    for (long i = 0; i < config.getMessageCount(); i++) {
        log.info("Sending messages \"" + config.getMessage() + " - {}\"", i);
        producer.send(new ProducerRecord(config.getTopic(),  "\"" + config.getMessage()  + " - " + i + "\""));
        Thread.sleep(config.getDelay());
    }
    log.info("{} messages sent ...", config.getMessageCount());
    producer.close();
}
 
Example 2
Source File: KafkaConsumerExample.java    From client-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    KafkaConsumerConfig config = KafkaConsumerConfig.fromEnv();
    Properties props = KafkaConsumerConfig.createProperties(config);
    int receivedMsgs = 0;

    if (System.getenv("JAEGER_SERVICE_NAME") != null)   {
        Tracer tracer = Configuration.fromEnv().getTracer();
        GlobalTracer.registerIfAbsent(tracer);

        props.put(ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, TracingConsumerInterceptor.class.getName());
    }

    boolean commit = !Boolean.parseBoolean(config.getEnableAutoCommit());
    KafkaConsumer consumer = new KafkaConsumer(props);
    consumer.subscribe(Collections.singletonList(config.getTopic()));

    while (receivedMsgs < config.getMessageCount()) {
        ConsumerRecords<String, String> records = consumer.poll(Long.MAX_VALUE);
        for (ConsumerRecord<String, String> record : records) {
            log.info("Received message:");
            log.info("\tpartition: {}", record.partition());
            log.info("\toffset: {}", record.offset());
            log.info("\tvalue: {}", record.value());
            receivedMsgs++;
        }
        if (commit) {
            consumer.commitSync();
        }
    }
}
 
Example 3
Source File: TracingInterceptorsTest.java    From java-grpc with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  GlobalTracerTestUtil.resetGlobalTracer();
  clientTracer.reset();
  serverTracer.reset();
  // register server tracer to verify active span on server side
  GlobalTracer.registerIfAbsent(serverTracer);
}
 
Example 4
Source File: TracingDriverTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testExplicitTracer() {
  Tracer tracer = new MockTracer();
  GlobalTracer.registerIfAbsent(tracer);
  Tracer tracer2 = new MockTracer();
  TracingDriver tracingDriver = new TracingDriver();
  tracingDriver.setTracer(tracer2);
  assertEquals(tracer2, tracingDriver.getTracer());
}
 
Example 5
Source File: OpenTracingTracingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
protected void run() {
    final Tracer tracer = new JaegerTracer.Builder("tracer-jaxws")
        .withSampler(new ConstSampler(true))
        .withReporter(REPORTER)
        .build();
    GlobalTracer.registerIfAbsent(tracer);

    final JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
    sf.setServiceClass(BookStore.class);
    sf.setAddress("http://localhost:" + PORT);
    sf.getFeatures().add(new OpenTracingFeature(tracer));
    server = sf.create();
}
 
Example 6
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 7
Source File: TracingAgent.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Agent entry point
 *
 * @param agentArgs The type of tracing which should be initialized
 */
public static void premain(String agentArgs) {
    if ("jaeger".equals(agentArgs)) {
        String jaegerServiceName = System.getenv("JAEGER_SERVICE_NAME");

        if (jaegerServiceName != null) {
            LOGGER.info("Initializing Jaeger tracing with service name {}", jaegerServiceName);
            Tracer tracer = Configuration.fromEnv().getTracer();
            GlobalTracer.registerIfAbsent(tracer);
        } else {
            LOGGER.error("Jaeger tracing cannot be initialized because JAEGER_SERVICE_NAME environment variable is not defined");
        }
    }
}
 
Example 8
Source File: Server.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Server() throws Exception {
    org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server(9000);

    // Register and map the dispatcher servlet
    final ServletHolder servletHolder = new ServletHolder(new CXFNonSpringJaxrsServlet());
    final ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.addServlet(servletHolder, "/*");

    servletHolder.setInitParameter("javax.ws.rs.Application",
        CatalogApplication.class.getName());

    final Tracer tracer = new Configuration("tracer-server")
        .withSampler(new SamplerConfiguration().withType(ConstSampler.TYPE).withParam(1))
        .withReporter(new ReporterConfiguration().withSender(
            new SenderConfiguration() {
                @Override
                public Sender getSender() {
                    return new Slf4jLogSender();
                }
            }
        ))
        .getTracer();
    GlobalTracer.registerIfAbsent(tracer);
    
    server.setHandler(context);
    server.start();
    server.join();
}
 
Example 9
Source File: AllInOne.java    From batfish with Apache License 2.0 5 votes vote down vote up
private void initTracer() {
  Configuration config =
      new Configuration(_settings.getServiceName())
          .withSampler(new SamplerConfiguration().withType("const").withParam(1))
          .withReporter(
              new ReporterConfiguration()
                  .withSender(
                      SenderConfiguration.fromEnv()
                          .withAgentHost(_settings.getTracingAgentHost())
                          .withAgentPort(_settings.getTracingAgentPort()))
                  .withLogSpans(false));
  GlobalTracer.registerIfAbsent(config.getTracer());
}
 
Example 10
Source File: Client.java    From batfish with Apache License 2.0 5 votes vote down vote up
private void initTracer() {
  Configuration config =
      new Configuration(_settings.getServiceName())
          .withSampler(new SamplerConfiguration().withType("const").withParam(1))
          .withReporter(
              new ReporterConfiguration()
                  .withSender(
                      SenderConfiguration.fromEnv()
                          .withAgentHost(_settings.getTracingAgentHost())
                          .withAgentPort(_settings.getTracingAgentPort()))
                  .withLogSpans(false));
  GlobalTracer.registerIfAbsent(config.getTracer());
}
 
Example 11
Source File: Main.java    From batfish with Apache License 2.0 5 votes vote down vote up
private static void initTracer() {
  Configuration config =
      new Configuration(_settings.getServiceName())
          .withSampler(new SamplerConfiguration().withType("const").withParam(1))
          .withReporter(
              new ReporterConfiguration()
                  .withSender(
                      SenderConfiguration.fromEnv()
                          .withAgentHost(_settings.getTracingAgentHost())
                          .withAgentPort(_settings.getTracingAgentPort()))
                  .withLogSpans(false));
  GlobalTracer.registerIfAbsent(config.getTracer());
}
 
Example 12
Source File: Driver.java    From batfish with Apache License 2.0 5 votes vote down vote up
private static void initTracer() {
  io.jaegertracing.Configuration config =
      new io.jaegertracing.Configuration(_mainSettings.getServiceName())
          .withSampler(new SamplerConfiguration().withType("const").withParam(1))
          .withReporter(
              new ReporterConfiguration()
                  .withSender(
                      SenderConfiguration.fromEnv()
                          .withAgentHost(_mainSettings.getTracingAgentHost())
                          .withAgentPort(_mainSettings.getTracingAgentPort()))
                  .withLogSpans(false));
  GlobalTracer.registerIfAbsent(config.getTracer());
}
 
Example 13
Source File: TracingConfiguration.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public TracingConfiguration(Tracer tracer) {
    this.tracer = tracer;
    GlobalTracer.registerIfAbsent(tracer);
}
 
Example 14
Source File: OpenTracingTracingTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public void tearDown() throws Exception {
    server.destroy();
    GlobalTracer.registerIfAbsent(NoopTracerFactory.create());
}
 
Example 15
Source File: TracerProvider.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
public TracerProvider(Tracer tracer) {
  this.tracer = tracer;
  GlobalTracer.registerIfAbsent(tracer);
}
 
Example 16
Source File: JaxrsApplication.java    From java-jaxrs with Apache License 2.0 4 votes vote down vote up
public JaxrsApplication() {
    GlobalTracer.registerIfAbsent(mockTracer);
}
 
Example 17
Source File: TracingKafkaTest.java    From java-kafka-client with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void init() {
  GlobalTracer.registerIfAbsent(mockTracer);
}
 
Example 18
Source File: InstallOpenTracingTracerRuleTest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void beforeClass() {
  GlobalTracer.registerIfAbsent(tracer = new MockTracer(Propagator.TEXT_MAP));
}