/** * The result of the transaction. HTTP status code for HTTP-related transactions. */ public Transaction withResult(@Nullable String result) { if (!isSampled()) { return this; } this.result = result; return this; }
public Transaction withName(@Nullable String name) { if (!isSampled()) { return this; } setName(name); return this; }
@Override public void addTag(String key, String value) { if (!isSampled()) { return; } getContext().getTags().put(key, value); }
@Override public void doEnd(long epochMicros) { if (!isSampled()) { context.resetState(); } if (type == null) { type = "custom"; } this.tracer.endTransaction(this); }
public void setUser(String id, String email, String username) { if (!isSampled()) { return; } getContext().getUser().withId(id).withEmail(email).withUsername(username); }
@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); }
@Test void testDisable() { when(config.getConfig(CoreConfiguration.class).isActive()).thenReturn(false); Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { assertThat(tracerImpl.currentTransaction()).isSameAs(transaction); assertThat(transaction.isSampled()).isFalse(); Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { assertThat(tracerImpl.getActive()).isSameAs(span); assertThat(tracerImpl.getActive()).isNotNull(); } assertThat(tracerImpl.getActive()).isSameAs(transaction); transaction.end(); } assertThat(tracerImpl.currentTransaction()).isNull(); assertThat(reporter.getTransactions()).isEmpty(); assertThat(reporter.getSpans()).isEmpty(); }
@Test void testEnableDropSpans() { when(tracerImpl.getConfig(CoreConfiguration.class).getTransactionMaxSpans()).thenReturn(1); Transaction transaction = tracerImpl.startTransaction(); try (Scope scope = transaction.activateInScope()) { Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { assertThat(span.isSampled()).isTrue(); span.end(); } Span span2 = tracerImpl.getActive().createSpan(); try (Scope spanScope = span2.activateInScope()) { assertThat(span2.isSampled()).isFalse(); span2.end(); } transaction.end(); } assertThat(reporter.getFirstTransaction().isSampled()).isTrue(); assertThat(reporter.getFirstTransaction().getSpanCount().getDropped().get()).isEqualTo(1); assertThat(reporter.getFirstTransaction().getSpanCount().getStarted().get()).isEqualTo(1); assertThat(reporter.getSpans()).hasSize(1); }
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 serializeTransaction(final Transaction transaction) { jw.writeByte(OBJECT_START); writeTimestamp(transaction.getTimestamp()); writeField("name", transaction.getName()); serializeTraceContext(transaction.getTraceContext(), false); writeField("type", transaction.getType()); writeField("duration", transaction.getDuration()); writeField("result", transaction.getResult()); serializeContext(transaction.getContext()); serializeSpanCount(transaction.getSpanCount()); writeLastField("sampled", transaction.isSampled()); jw.writeByte(OBJECT_END); }