@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"); }
@Override public boolean isChildOf(TraceContextHolder parent) { return parent.getTraceContext().getTraceId().equals(traceId) && parent.getTraceContext().getId().equals(parentId); }
@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 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()); }
public void captureException(long epochMicros, @Nullable Throwable e, @Nullable TraceContextHolder<?> active) { if (e != null) { ErrorCapture error = errorPool.createInstance(); error.withTimestamp(epochMicros); error.setException(e); Transaction currentTransaction = currentTransaction(); if (currentTransaction != null) { error.setTransactionType(currentTransaction.getType()); error.setTransactionSampled(currentTransaction.isSampled()); } if (active != null) { if (active instanceof Transaction) { Transaction transaction = (Transaction) active; // The error might have occurred in a different thread than the one the transaction was recorded // That's why we have to ensure the visibility of the transaction properties error.getContext().copyFrom(transaction.getContextEnsureVisibility()); } else if (active instanceof Span) { Span span = (Span) active; error.getContext().getTags().putAll(span.getContext().getTags()); } error.asChildOf(active.getTraceContext()); } else { error.getTraceContext().getId().setToRandomValue(); } reporter.report(error); } }
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()); } }