@Test void testResetState() { final TraceContext traceContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); traceContext.asChildOf("00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-00"); traceContext.resetState(); assertThat(traceContext.getIncomingTraceParentHeader()).isEqualTo("00-00000000000000000000000000000000-0000000000000000-00"); }
public Transaction startTransaction() { return startTransaction(TraceContext.asRoot(), null); }
@Test void testSetSampled() { final TraceContext traceContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); traceContext.asRootSpan(ConstantSampler.of(false)); assertThat(traceContext.isSampled()).isFalse(); traceContext.setRecorded(true); assertThat(traceContext.isSampled()).isTrue(); traceContext.setRecorded(false); assertThat(traceContext.isSampled()).isFalse(); }
private void serializeTraceContext(TraceContext traceContext, boolean serializeTransactionId) { // errors might only have an id writeHexField("id", traceContext.getId()); if (!traceContext.getTraceId().isEmpty()) { writeHexField("trace_id", traceContext.getTraceId()); } if (serializeTransactionId && !traceContext.getTransactionId().isEmpty()) { writeHexField("transaction_id", traceContext.getTransactionId()); } if (!traceContext.getParentId().isEmpty()) { writeHexField("parent_id", traceContext.getParentId()); } }
public <T> Transaction start(TraceContext.ChildContextCreator<T> childContextCreator, @Nullable T parent, long epochMicros, Sampler sampler) { onStart(); if (parent == null || !childContextCreator.asChildOf(traceContext, parent)) { traceContext.asRootSpan(sampler); } if (epochMicros >= 0) { this.timestamp = epochMicros; } else { this.timestamp = traceContext.getClock().getEpochMicros(); } return this; }
private void assertValid(String s) { final TraceContext traceContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); assertThat(traceContext.asChildOf(s)).isTrue(); } }
@Test void outgoingHeader() { final TraceContext traceContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); final String header = "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-03"; assertThat(traceContext.asChildOf(header)).isTrue(); assertThat(traceContext.getOutgoingTraceParentHeader().toString()) .isEqualTo("00-0af7651916cd43dd8448eb211c80319c-" + traceContext.getId().toString() + "-03"); }
@Test void parseFromTraceParentHeaderUnsupportedFlag() { final TraceContext traceContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); final String header = "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-03"; assertThat(traceContext.asChildOf(header)).isTrue(); assertThat(traceContext.isSampled()).isTrue(); assertThat(traceContext.getOutgoingTraceParentHeader().toString()).endsWith("-03"); }
@Test void testRandomValue() { final TraceContext traceContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); traceContext.asRootSpan(ConstantSampler.of(true)); assertThat(traceContext.getTraceId().isEmpty()).isFalse(); assertThat(traceContext.getParentId().isEmpty()).isTrue(); assertThat(traceContext.getId().isEmpty()).isFalse(); }
@Test void outgoingHeaderRootSpan() { final TraceContext traceContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); traceContext.asRootSpan(ConstantSampler.of(true)); assertThat(traceContext.isSampled()).isTrue(); assertThat(traceContext.getOutgoingTraceParentHeader().toString()).hasSize(55); assertThat(traceContext.getOutgoingTraceParentHeader().toString()).startsWith("00-"); assertThat(traceContext.getOutgoingTraceParentHeader().toString()).endsWith("-01"); }
@Test void parseFromTraceParentHeader_notSampled() { final TraceContext traceContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); final String header = "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-00"; assertThat(traceContext.asChildOf(header)).isTrue(); assertThat(traceContext.isSampled()).isFalse(); assertThat(traceContext.getIncomingTraceParentHeader()).isEqualTo(header); }
@Test void testTransactionWithParentReference() { final String traceContextHeader = "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01"; final Transaction transaction = tracerImpl.startTransaction(TraceContext.fromTraceparentHeader(), traceContextHeader, ConstantSampler.of(false), 0); // the traced flag in the header overrides the sampler assertThat(transaction.isSampled()).isTrue(); assertThat(transaction.getTraceContext().getParentId().toString()).isEqualTo("b9c7c989f97918e1"); assertThat(transaction.getTraceContext().getTraceId().toString()).isEqualTo("0af7651916cd43dd8448eb211c80319c"); transaction.end(1); assertThat(reporter.getTransactions()).hasSize(1); assertThat(reporter.getSpans()).hasSize(0); }
@Override public boolean isChildOf(TraceContextHolder parent) { return parent.getTraceContext().getTraceId().equals(traceId) && parent.getTraceContext().getId().equals(parentId); }
@Test void testStartSpanAfterTransactionHasEnded() { final Transaction transaction = tracerImpl.startTransaction(TraceContext.asRoot(), null); final TraceContext transactionTraceContext = transaction.getTraceContext().copy(); transaction.end(); transaction.resetState(); tracerImpl.activate(transactionTraceContext); try { assertThat(tracerImpl.getActive()).isEqualTo(transactionTraceContext); final Span span = tracerImpl.startSpan(TraceContext.fromActive(), tracerImpl); assertThat(span).isNotNull(); try (Scope scope = span.activateInScope()) { assertThat(tracerImpl.currentTransaction()).isNull(); assertThat(tracerImpl.getActive()).isSameAs(span); } finally { span.end(); } } finally { tracerImpl.deactivate(transactionTraceContext); } assertThat(tracerImpl.getActive()).isNull(); } }
@Override public Span createSpan() { return tracer.startSpan(fromParent(), this); }
public boolean isChildOf(AbstractSpan<?> parent) { return traceContext.isChildOf(parent.traceContext); }
@Test void testRecordException() { tracerImpl.captureException(new Exception("test")); assertThat(reporter.getErrors()).hasSize(1); ErrorCapture error = reporter.getFirstError(); assertThat(error.getException()).isNotNull(); assertThat(error.getTraceContext().hasContent()).isFalse(); assertThat(error.getTransactionInfo().isSampled()).isFalse(); }
/** * Creates a reference to a {@link TraceContext} * * @return {@code this}, for chaining * @param traceContext parent trace context */ public ErrorCapture asChildOf(TraceContext traceContext) { this.traceContext.asChildOf(traceContext); return this; }
@Override public String toString() { return getOutgoingTraceParentHeader().toString(); }
@Test void testSpanTypeSerialization() throws IOException { Span span = new Span(mock(ElasticApmTracer.class)); span.getTraceContext().asRootSpan(ConstantSampler.of(true)); span.withType("template.jsf.render.view"); JsonNode spanJson = objectMapper.readTree(serializer.toJsonString(span)); assertThat(spanJson.get("type").textValue()).isEqualTo("template_jsf_render_view"); span.withType("template").withSubtype("jsf.lifecycle").withAction("render.view"); spanJson = objectMapper.readTree(serializer.toJsonString(span)); assertThat(spanJson.get("type").textValue()).isEqualTo("template.jsf_lifecycle.render_view"); span = new Span(mock(ElasticApmTracer.class)); span.getTraceContext().asRootSpan(ConstantSampler.of(true)); span.withType("template").withAction("jsf.render"); spanJson = objectMapper.readTree(serializer.toJsonString(span)); assertThat(spanJson.get("type").textValue()).isEqualTo("template..jsf_render"); span = new Span(mock(ElasticApmTracer.class)); span.getTraceContext().asRootSpan(ConstantSampler.of(true)); span.withType("template").withSubtype("jsf.render"); spanJson = objectMapper.readTree(serializer.toJsonString(span)); assertThat(spanJson.get("type").textValue()).isEqualTo("template.jsf_render"); span = new Span(mock(ElasticApmTracer.class)); span.getTraceContext().asRootSpan(ConstantSampler.of(true)); span.withSubtype("jsf").withAction("render"); spanJson = objectMapper.readTree(serializer.toJsonString(span)); assertThat(spanJson.get("type").isNull()).isTrue(); System.out.println(spanJson); }