-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
114 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/org/wildcraft/sample/HelloManualChained.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |