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; }
@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(); }
@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 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 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); }
@Test void testPropagateTransactionIdForUnsampledSpan() { final TraceContext rootContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); rootContext.asRootSpan(ConstantSampler.of(false)); final TraceContext childContext = TraceContext.with64BitId(mock(ElasticApmTracer.class)); childContext.asChildOf(rootContext); assertThat(childContext.getOutgoingTraceParentHeader().toString()).doesNotContain(childContext.getId().toString()); assertThat(childContext.getOutgoingTraceParentHeader().toString()).contains(rootContext.getId().toString()); }