use of io.opencensus.trace.Tracer in project instrumentation-java by census-instrumentation.
the class JaegerExporterHandlerIntegrationTest method exportToJaeger.
@Test
public void exportToJaeger() throws IOException, InterruptedException {
Tracer tracer = Tracing.getTracer();
final long startTimeInMicros = MILLISECONDS.toMicros(currentTimeMillis());
final long startNanoTime = System.nanoTime();
SpanBuilder spanBuilder = tracer.spanBuilder(SPAN_NAME).setRecordEvents(true).setSampler(Samplers.alwaysSample());
int spanDurationInMillis = new Random().nextInt(10) + 1;
Scope scopedSpan = spanBuilder.startScopedSpan();
try {
tracer.getCurrentSpan().addAnnotation(START_PROCESSING_VIDEO);
// Fake work.
Thread.sleep(spanDurationInMillis);
tracer.getCurrentSpan().putAttribute("foo", AttributeValue.stringAttributeValue("bar"));
tracer.getCurrentSpan().addAnnotation(FINISHED_PROCESSING_VIDEO);
} catch (Exception e) {
tracer.getCurrentSpan().addAnnotation("Exception thrown when processing video.");
tracer.getCurrentSpan().setStatus(Status.UNKNOWN);
logger.error(e.getMessage());
} finally {
scopedSpan.close();
}
final long durationInMicros = NANOSECONDS.toMicros(System.nanoTime() - startNanoTime);
final long endTimeInMicros = startTimeInMicros + durationInMicros;
// Shutdown the export component to force a flush. This will cause problems if multiple tests
// are added in this class, but this is not the case for the moment.
Tracing.getExportComponent().shutdown();
JaegerTraceExporter.unregister();
logger.info("Wait for Jaeger to process the span...");
long timeWaitingForSpansToBeExportedInMillis = 1100L;
Thread.sleep(timeWaitingForSpansToBeExportedInMillis);
// Get traces recorded by Jaeger:
HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(tracesForServiceEndpoint(SERVICE_NAME)));
HttpResponse response = request.execute();
String body = response.parseAsString();
assertWithMessage("Response was: " + body).that(response.getStatusCode()).isEqualTo(200);
JsonObject result = new JsonParser().parse(body).getAsJsonObject();
// Pretty-print for debugging purposes:
logger.debug(new GsonBuilder().setPrettyPrinting().create().toJson(result));
assertThat(result).isNotNull();
assertThat(result.get("total").getAsInt()).isEqualTo(0);
assertThat(result.get("limit").getAsInt()).isEqualTo(0);
assertThat(result.get("offset").getAsInt()).isEqualTo(0);
assertThat(result.get("errors").getAsJsonNull()).isEqualTo(JsonNull.INSTANCE);
JsonArray data = result.get("data").getAsJsonArray();
assertThat(data).isNotNull();
assertThat(data.size()).isEqualTo(1);
JsonObject trace = data.get(0).getAsJsonObject();
assertThat(trace).isNotNull();
assertThat(trace.get("traceID").getAsString()).matches("[a-z0-9]{1,32}");
JsonArray spans = trace.get("spans").getAsJsonArray();
assertThat(spans).isNotNull();
assertThat(spans.size()).isEqualTo(1);
JsonObject span = spans.get(0).getAsJsonObject();
assertThat(span).isNotNull();
assertThat(span.get("traceID").getAsString()).matches("[a-z0-9]{1,32}");
assertThat(span.get("spanID").getAsString()).matches("[a-z0-9]{1,16}");
assertThat(span.get("flags").getAsInt()).isEqualTo(1);
assertThat(span.get("operationName").getAsString()).isEqualTo(SPAN_NAME);
assertThat(span.get("references").getAsJsonArray()).isEmpty();
assertThat(span.get("startTime").getAsLong()).isAtLeast(startTimeInMicros);
assertThat(span.get("startTime").getAsLong()).isAtMost(endTimeInMicros);
assertThat(span.get("duration").getAsLong()).isAtLeast(MILLISECONDS.toMicros(spanDurationInMillis));
assertThat(span.get("duration").getAsLong()).isAtMost(durationInMicros);
JsonArray tags = span.get("tags").getAsJsonArray();
assertThat(tags.size()).isEqualTo(2);
JsonObject tag = tags.get(0).getAsJsonObject();
assertThat(tag.get("key").getAsString()).isEqualTo("foo");
assertThat(tag.get("type").getAsString()).isEqualTo("string");
assertThat(tag.get("value").getAsString()).isEqualTo("bar");
JsonObject statusTag = tags.get(1).getAsJsonObject();
assertThat(statusTag.get("key").getAsString()).isEqualTo(JaegerExporterHandler.STATUS_CODE);
assertThat(statusTag.get("type").getAsString()).isEqualTo("int64");
assertThat(statusTag.get("value").getAsLong()).isEqualTo(0);
JsonArray logs = span.get("logs").getAsJsonArray();
assertThat(logs.size()).isEqualTo(2);
JsonObject log1 = logs.get(0).getAsJsonObject();
long ts1 = log1.get("timestamp").getAsLong();
assertThat(ts1).isAtLeast(startTimeInMicros);
assertThat(ts1).isAtMost(endTimeInMicros);
JsonArray fields1 = log1.get("fields").getAsJsonArray();
assertThat(fields1.size()).isEqualTo(1);
JsonObject field1 = fields1.get(0).getAsJsonObject();
assertThat(field1.get("key").getAsString()).isEqualTo("message");
assertThat(field1.get("type").getAsString()).isEqualTo("string");
assertThat(field1.get("value").getAsString()).isEqualTo(START_PROCESSING_VIDEO);
JsonObject log2 = logs.get(1).getAsJsonObject();
long ts2 = log2.get("timestamp").getAsLong();
assertThat(ts2).isAtLeast(startTimeInMicros);
assertThat(ts2).isAtMost(endTimeInMicros);
assertThat(ts2).isAtLeast(ts1);
JsonArray fields2 = log2.get("fields").getAsJsonArray();
assertThat(fields2.size()).isEqualTo(1);
JsonObject field2 = fields2.get(0).getAsJsonObject();
assertThat(field2.get("key").getAsString()).isEqualTo("message");
assertThat(field2.get("type").getAsString()).isEqualTo("string");
assertThat(field2.get("value").getAsString()).isEqualTo(FINISHED_PROCESSING_VIDEO);
assertThat(span.get("processID").getAsString()).isEqualTo("p1");
assertThat(span.get("warnings").getAsJsonNull()).isEqualTo(JsonNull.INSTANCE);
JsonObject processes = trace.get("processes").getAsJsonObject();
assertThat(processes.size()).isEqualTo(1);
JsonObject p1 = processes.get("p1").getAsJsonObject();
assertThat(p1.get("serviceName").getAsString()).isEqualTo(SERVICE_NAME);
assertThat(p1.get("tags").getAsJsonArray().size()).isEqualTo(0);
assertThat(trace.get("warnings").getAsJsonNull()).isEqualTo(JsonNull.INSTANCE);
}
use of io.opencensus.trace.Tracer in project beam by apache.
the class BeamFnMapTaskExecutor method execute.
@Override
public void execute() throws Exception {
Tracer tracer = Tracing.getTracer();
SpanBuilder builder = tracer.spanBuilder("MapTaskExecutor.Span").setRecordEvents(true);
// Start the progress tracker before execution (which blocks until execution is finished).
try (Scope unused = builder.startScopedSpan();
AutoCloseable unused2 = progressTrackerCloseable(progressTracker)) {
tracer.getCurrentSpan().addAnnotation("About to execute");
super.execute();
tracer.getCurrentSpan().addAnnotation("Done with execute");
}
}
Aggregations