Skip to content

Instantly share code, notes, and snippets.

@mageddo
Last active October 24, 2018 05:01
Show Gist options
  • Select an option

  • Save mageddo/0314f0fc907b821a2319e42cb12c2d7f to your computer and use it in GitHub Desktop.

Select an option

Save mageddo/0314f0fc907b821a2319e42cb12c2d7f to your computer and use it in GitHub Desktop.
Zipkin brave example
compile group: 'io.zipkin.zipkin2', name: 'zipkin', version: '2.11.7'
compile group: 'io.zipkin.brave', name: 'brave', version: '5.4.3'
compile group: 'io.zipkin.reporter2', name: 'zipkin-sender-okhttp3', version: '2.7.10'
var sender = OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans")
.toBuilder()
.connectTimeout(10000)
.readTimeout(10000)
.writeTimeout(10000)
.build()
;
var spanReporter = AsyncReporter
.builder(sender)
.closeTimeout(5, TimeUnit.SECONDS)
.build();
// Create a tracing component with the service name you want to see in Zipkin.
var tracing = Tracing.newBuilder()
.localServiceName("my-service")
.spanReporter(spanReporter)
.build();
// Tracing exposes objects you might need, most importantly the tracer
var tracer = tracing.tracer();
// tracer
// .startScopedSpan("xyz")
// .tag("size", "9")
// .tag("name", "Maria")
// .annotate("some-annotation")
// .error(new Exception("some unbelievable error"))
// .finish();
//
// tracer.
tracer.withSpanInScope(tracer.nextSpan().name("skin-buy").start());
tracer.startScopedSpan("rule1Check").tag("x", "y").finish();
Thread.sleep(300);
tracer.startScopedSpan("rule2Check").tag("x", "y").finish();
Thread.sleep(50);
tracer.startScopedSpan("rule3Check").tag("x", "y").finish();
Thread.sleep(250);
tracer.startScopedSpan("rule4Check").tag("x", "y").finish();
Thread.sleep(100);
tracer.startScopedSpan("rule5Check").tag("x", "y").finish();
Thread.sleep(30);
tracer.currentSpan().finish();
// example 2
tracer.withSpanInScope(tracer.nextSpan().name("price-sync").tag("bskItemCode", "14895514112BSL543100268").start());
tracer.startScopedSpan("skin-buy").tag("bskItemCode", "14895514112BSL543100268").tag("status", "started").finish();
Thread.sleep(30);
tracer.startScopedSpan("skin-buy").tag("bskItemCode", "14895514112BSL543100268").tag("status", "success").tag("rule", "min-profit-met").finish();
Thread.sleep(50);
tracer.startScopedSpan("iem-negotiator").tag("bskItemCode", "14895514112BSL543100268").tag("status", "negotiated").tag("value", "10.99").finish();
Thread.sleep(250);
tracer.startScopedSpan("item-negotiator").tag("bskItemCode", "14895514112BSL543100268").tag("status", "failed").tag("rule", "same-price-rule").finish();
tracer.currentSpan().finish();
// example 2.1 -- nao precisa passar o id do item toda vez dado que as spans estão amarradas ao pai
tracer.withSpanInScope(
tracer
.nextSpan()
.name("price-sync")
.tag("app", "CSGO")
.tag("bskItemCode", "14895514112BSL543100268")
.start()
);
tracer.startScopedSpan("skin-buy")
.tag("status", "started")
.finish();
Thread.sleep(30);
tracer.startScopedSpan("skin-buy")
.tag("status", "success")
.tag("rule", "min-profit-met")
.finish();
Thread.sleep(50);
tracer.startScopedSpan("iem-negotiator")
.tag("status", "negotiated")
.tag("value", "10.99")
.finish();
Thread.sleep(250);
tracer.startScopedSpan("item-negotiator")
.tag("status", "failed")
.tag("rule", "same-price-rule")
.finish();
// example 3 referring an previous parent
tracer.withSpanInScope(tracer.joinSpan(TraceContext.newBuilder().traceId(-2792739479104120024L).spanId(6636156556104264803L).build()));
tracer.startScopedSpan("rule1Check").tag("bskItemCode", "14895514112BSL543100268").finish();
Thread.sleep(30);
tracer.startScopedSpan("rule2Check").tag("bskItemCode", "14895514112BSL543100268").finish();
Thread.sleep(50);
tracer.startScopedSpan("rule3Check").tag("bskItemCode", "14895514112BSL543100268").finish();
Thread.sleep(250);
tracer.startScopedSpan("rule4Check").tag("bskItemCode", "14895514112BSL543100268").finish();
Thread.sleep(10);
tracer.startScopedSpan("rule5Check").tag("bskItemCode", LocalDateTime.now().toString()).finish();
Thread.sleep(30);
tracer.currentSpan().finish();
tracing.close();
spanReporter.close();
sender.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment