4

We have selected to use Jaeger API is used for tracing. There , we have setup the Jaeger locally using docker as mentioned below.

sudo docker run -d --name jaeger \
  -p 5775:5775/udp \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 14268:14268 \
  jaegertracing/all-in-one:latest

In the ServletContextListener, we created new cofigurations like below.

@WebListener
public class TracingContextListener implements ServletContextListener {

      @Inject
      private io.opentracing.Tracer tracer;

        public void contextInitialized(ServletContextEvent servletContextEvent) {
             GlobalTracer.register(tracer);
        }

        public void contextDestroyed(ServletContextEvent servletContextEvent) {

        }

        @Produces
        @Singleton
        public static io.opentracing.Tracer jaegerTracer() {
          return new Configuration("MyApplication", new Configuration.SamplerConfiguration(
              ProbabilisticSampler.TYPE, 1),
              new Configuration.ReporterConfiguration())
              .getTracer();
        }
}

Now this works fine and I can see the tracing in http://localhost:16686

Problem : I want to make the Jager setup in an external environment and connect from another application server (application server is running on wildfly 10 docker under host mode). In future that Jaeger instance may used by more than one server instances for tracings.

After looking at the source and various references as mentioned below I tried below options. But it connects to local always. I tried various ports like 5775, 6831, 6832 also but result was same.

  return new Configuration("MyApplication", new Configuration.SamplerConfiguration(
          ProbabilisticSampler.TYPE, 1, "server2.mycompany.com:5778"),
          new Configuration.ReporterConfiguration())
          .getTracer();

Further I tried setting JAEGER_ENDPOINT and JAEGER_SAMPLER_MANAGER_HOST_PORT as environment variables also. But failed.

In one reference I found that "Jaeger client libraries expect jaeger-agent process to run locally on each host...".

Is that means I can't use it in cebtrelized manner and I need to setup Jaeger in each application server instances? Otherwise how to do that?

ironwood
  • 8,936
  • 15
  • 65
  • 114

2 Answers2

3

BINGO!

We need to setup the ReporterConfigurations as below. previously my ones were default ones that's why it always connected to local.

return new Configuration("MyApplication", 
        new Configuration.SamplerConfiguration(ProbabilisticSampler.TYPE, 1, "server2.mycompany.com:5778"),
        new Configuration.ReporterConfiguration(false, "server2.mycompany.com",6831,1000,100))
        .getTracer();

Even better, you can create the Configuration from Environment as below providing the environment variables as below

return Configuration.fromEnv().getTracer();

You can provide this when you run the docker container

-e JAVA_OPTS="

 -DJAEGER_SAMPLER_TYPE=probabilistic
 -DJAEGER_SAMPLER_PARAM=1
 -DJAEGER_SAMPLER_MANAGER_HOST_PORT=server2.mycompany.com:5778
 -DJAEGER_REPORTER_LOG_SPANS=false
 -DJAEGER_AGENT_HOST=server2.mycompany.com
 -DJAEGER_AGENT_PORT=6831
 -DJAEGER_REPORTER_FLUSH_INTERVAL=1000
 -DJAEGER_REPORTER_MAX_QUEUE_SIZE=100
 -DJAEGER_SERVICE_NAME=MyApplicationNameX 

" ....

ironwood
  • 8,936
  • 15
  • 65
  • 114
1

Step 1 : First we need to configure remote host address and port.

    private static final int JAEGER_PORT = HOST_PORT;
    private static final String JAEGER_HOST = "HOST_IP";

Step 2 : configure sender configuration and pass the remote host and port in withAgentHost, withAgentPort.

SenderConfiguration senderConfig = Configuration.SenderConfiguration.fromEnv() .withAgentHost(JAEGER_HOST) .withAgentPort(JAEGER_PORT);

Step 3 : Pass sender configuration in reporter configuration

Configuration.ReporterConfiguration reporterConfig = Configuration.ReporterConfiguration.fromEnv() .withLogSpans(true) .withSender(senderConfig);

package com.studies.StudyService;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import io.jaegertracing.Configuration;
import io.jaegertracing.Configuration.SenderConfiguration;
import io.jaegertracing.internal.samplers.ProbabilisticSampler;
import io.opentracing.Tracer;

@SpringBootApplication
//@EnableDiscoveryClient

public class StudyServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudyServiceApplication.class, args);
    }
    /* Configure sender host and port details */
        private static final int JAEGER_PORT = HOST_PORT;
        private static final String JAEGER_HOST = "HOST_IP";
    /* End */
    @Bean
    public Tracer getTracer() {

        Configuration.SamplerConfiguration samplerConfig = Configuration.SamplerConfiguration.fromEnv()
                .withType(ProbabilisticSampler.TYPE).withParam(1);

        /* Update default sender configuration with custom host and port */
            SenderConfiguration senderConfig = Configuration.SenderConfiguration.fromEnv()
                    .withAgentHost(JAEGER_HOST)
                    .withAgentPort(JAEGER_PORT);
        /* End */

        Configuration.ReporterConfiguration reporterConfig = Configuration.ReporterConfiguration.fromEnv()
                .withLogSpans(true)
                .withSender(senderConfig);

        Configuration config = new Configuration("Service_Name").withSampler(samplerConfig)
                .withReporter(reporterConfig);

        return config.getTracer();
    }
}
Nijin
  • 11
  • 2
  • 2
    Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See also Explaining entirely code-based answers: https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers – borchvm Dec 04 '19 at 07:01
  • Updated answer with proper comments – Nijin Dec 04 '19 at 11:43