Skip to content

Commit

Permalink
Hello Manual Chained
Browse files Browse the repository at this point in the history
Hello Manual Chained
  • Loading branch information
narendranss committed Jan 2, 2020
1 parent 2be9c6d commit 1a4b1c0
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 26 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
Hello World Client Program for Jaeger
Hello World Client Program for Jaeger

1. HelloManual sample submits 3 traces to jaeger agent.
2. HelloManualChained sample has the following feature
1. Tries to Inject the Span ID
2. Tries to Chain the method calls to parent span based on Span ID
i.e., If parent span id is already present, it will not create new parent span but will try link the child to the
parent despite it coming from any thread.

The second sample is a hack as of now. Usually as per Jaeger all "linked spans" belong to same thread. Jaeger also introduce a concept called scope with that in mind.

A sample related to Jaeger scope is present in below path.
https://github.com/yurishkuro/opentracing-tutorial/blob/master/java/src/main/java/lesson02/solution/HelloActive.java
26 changes: 1 addition & 25 deletions src/main/java/org/wildcraft/sample/HelloManual.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,7 @@ private HelloManual(Tracer tracer) {
}

private void sayHello(String helloTo) {
byte flagSampled = 1;
JaegerSpanContext spanContext = jaegerObjectFactory.createSpanContext(0, 150, 150, 0, flagSampled, new HashMap<String, String>(),null);

Reference reference = new Reference(spanContext, References.CHILD_OF);

List<Reference> references = new ArrayList<>();
references.add(reference);

long startTimeNanoTicks = 0;
boolean computeDurationViaNanoTicks = false;
long startTimeMicroseconds =0;

if (startTimeMicroseconds == 0) {
startTimeMicroseconds = clock.currentTimeMicros();
if (!clock.isMicrosAccurate()) {
startTimeNanoTicks = clock.currentNanoTicks();
computeDurationViaNanoTicks = true;
}
}

System.out.println("startTimeMicroseconds: "+startTimeMicroseconds);
System.out.println("startTimeNanoTicks: "+startTimeNanoTicks);
Span span = jaegerObjectFactory.createSpan((JaegerTracer) tracer, "say-manual-hello", spanContext, 1577104503120000L, 33383308737599L, computeDurationViaNanoTicks, tags, references);

//Span span = tracer.buildSpan("say-manual-hello").start();
Span span = tracer.buildSpan("say-manual-hello").start();
span.setTag("hello-to", helloTo);
span.setTag("hello-fin", helloTo);

Expand Down
100 changes: 100 additions & 0 deletions src/main/java/org/wildcraft/sample/HelloManualChained.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package org.wildcraft.sample;

import com.google.common.collect.ImmutableMap;
import io.jaegertracing.internal.JaegerObjectFactory;
import io.jaegertracing.internal.JaegerSpanContext;
import io.jaegertracing.internal.JaegerTracer;
import io.jaegertracing.internal.Reference;
import io.jaegertracing.internal.clock.Clock;
import io.jaegertracing.internal.clock.SystemClock;
import io.opentracing.References;
import io.opentracing.Span;
import io.opentracing.Tracer;
import org.wildcraft.util.Tracing;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HelloManualChained {

private final Tracer tracer;

private final JaegerObjectFactory jaegerObjectFactory = new JaegerObjectFactory();

private final Clock clock = new SystemClock();

private final Map<String, Object> tags = new HashMap<String, Object>();

private HelloManualChained(Tracer tracer) {
this.tracer = tracer;
}

private void sayHello(String helloTo) {
byte flagSampled = 1;
JaegerSpanContext spanContext = jaegerObjectFactory.createSpanContext(0, 150, 150, 0, flagSampled, new HashMap<String, String>(),null);

Reference reference = new Reference(spanContext, References.CHILD_OF);

List<Reference> references = new ArrayList<>();
references.add(reference);

long startTimeNanoTicks = 0;
boolean computeDurationViaNanoTicks = false;
long startTimeMicroseconds =0;

if (startTimeMicroseconds == 0) {
startTimeMicroseconds = clock.currentTimeMicros();
if (!clock.isMicrosAccurate()) {
startTimeNanoTicks = clock.currentNanoTicks();
computeDurationViaNanoTicks = true;
}
}

System.out.println("startTimeMicroseconds: "+startTimeMicroseconds);
System.out.println("startTimeNanoTicks: "+startTimeNanoTicks);
Span span = jaegerObjectFactory.createSpan((JaegerTracer) tracer, "say-manual-hello", spanContext, 1577104503120000L, 33383308737599L, computeDurationViaNanoTicks, tags, references);

//Span span = tracer.buildSpan("say-manual-hello").start();
span.setTag("hello-to", helloTo);
span.setTag("hello-fin", helloTo);

String helloStr = formatString(span, helloTo);
printHello(span, helloStr);

span.finish();
}

private String formatString(Span rootSpan, String helloTo) {
Span span = tracer.buildSpan("formatString").asChildOf(rootSpan).start();
try {
String helloStr = String.format("Hello, %s!", helloTo);
span.log(ImmutableMap.of("event", "string-format", "value", helloStr));
return helloStr;
} finally {
span.finish();
}
}

private void printHello(Span rootSpan, String helloStr) {
Span span = tracer.buildSpan("printHello").asChildOf(rootSpan).start();
try {
System.out.println(helloStr);
span.log(ImmutableMap.of("event", "println"));
} finally {
span.finish();
}
}

public static void main(String[] args) {
if (args.length != 1) {
throw new IllegalArgumentException("Expecting one argument");
}

String helloTo = args[0];
try (JaegerTracer tracer = Tracing.init("hello-usertraceid-world")) {
new HelloManualChained(tracer).sayHello(helloTo);
}
}
}

0 comments on commit 1a4b1c0

Please sign in to comment.