@Override public Transaction createInstance() { return new Transaction(ElasticApmTracer.this); } });
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); }
@Override public void addTag(String key, String value) { if (!isSampled()) { return; } getContext().getTags().put(key, value); }
@Test void testSamplingNone() throws IOException { config.getConfig(CoreConfiguration.class).getSampleRate().update(0.0, SpyConfiguration.CONFIG_SOURCE_NAME); Transaction transaction = tracerImpl.startTransaction().withType("request"); try (Scope scope = transaction.activateInScope()) { transaction.setUser("1", "jon.doe@example.com", "jondoe"); Span span = tracerImpl.getActive().createSpan(); try (Scope spanScope = span.activateInScope()) { span.end(); } transaction.end(); } // we do report non-sampled transactions (without the context) assertThat(reporter.getTransactions()).hasSize(1); assertThat(reporter.getSpans()).hasSize(0); assertThat(reporter.getFirstTransaction().getContext().getUser().getEmail()).isNull(); assertThat(reporter.getFirstTransaction().getType()).isEqualTo("request"); }
public static void fillTransaction(Transaction t) { t.start(TraceContext.asRoot(), null, (long) 0, ConstantSampler.of(true)); t.setName("GET /api/types"); t.withType("request"); t.withResult("success"); TransactionContext context = t.getContext(); Request request = context.getRequest(); request.withHttpVersion("1.1");
private Transaction createTransactionWithRequiredValues() { Transaction t = new Transaction(mock(ElasticApmTracer.class)); t.start(TraceContext.asRoot(), null, (long) 0, ConstantSampler.of(true)); t.withType("type"); t.getContext().getRequest().withMethod("GET"); t.getContext().getRequest().getUrl().appendToFull("http://localhost:8080/foo/bar"); return t; }
private void innerRecordExceptionWithTrace(boolean sampled) { reporter.reset(); Transaction transaction = tracerImpl.startTransaction(TraceContext.asRoot(), null, ConstantSampler.of(sampled), -1); transaction.withType("test-type"); try (Scope scope = transaction.activateInScope()) { transaction.getContext().getRequest() .addHeader("foo", "bar") .withMethod("GET") .getUrl() .withPathname("/foo"); tracerImpl.currentTransaction().captureException(new Exception("from transaction")); ErrorCapture error = validateError(transaction, sampled, transaction); assertThat(error.getContext().getRequest().getHeaders().get("foo")).isEqualTo("bar"); reporter.reset(); Span span = transaction.createSpan().activate(); span.captureException(new Exception("from span")); validateError(span, sampled, transaction); span.deactivate().end(); transaction.end(); } }
@Test void testTimestamps() { final Transaction transaction = tracerImpl.startTransaction(TraceContext.fromTraceparentHeader(), null, ConstantSampler.of(true), 0); final Span span = transaction.createSpan(10); span.end(20); transaction.end(30); assertThat(transaction.getTimestamp()).isEqualTo(0); assertThat(transaction.getDuration()).isEqualTo(0.03); assertThat(span.getTimestamp()).isEqualTo(10); assertThat(span.getDuration()).isEqualTo(0.01); }
@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); }
@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); }
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); } }
@Test void testNullHeaders() throws IOException { Transaction transaction = new Transaction(mock(ElasticApmTracer.class)); transaction.getContext().getRequest().addHeader("foo", (String) null); transaction.getContext().getRequest().addHeader("baz", (Enumeration<String>) null); transaction.getContext().getRequest().getHeaders().add("bar", null); JsonNode jsonNode = objectMapper.readTree(serializer.toJsonString(transaction)); System.out.println(jsonNode); // calling addHeader with a null value ignores the header assertThat(jsonNode.get("context").get("request").get("headers").get("foo")).isNull(); assertThat(jsonNode.get("context").get("request").get("headers").get("baz")).isNull(); // should a null value sneak in, it should not break assertThat(jsonNode.get("context").get("request").get("headers").get("bar").isNull()).isTrue(); }
@Test void testErrorSerialization() throws IOException { ElasticApmTracer tracer = MockTracer.create(); Transaction transaction = new Transaction(tracer); ErrorCapture error = new ErrorCapture(tracer).asChildOf(transaction.getTraceContext()).withTimestamp(5000); error.setTransactionSampled(true); error.setTransactionType("test-type"); error.setException(new Exception("test")); error.getContext().getTags().put("foo", "bar"); String errorJson = serializer.toJsonString(error); System.out.println("errorJson = " + errorJson); JsonNode errorTree = objectMapper.readTree(errorJson); assertThat(errorTree.get("timestamp").longValue()).isEqualTo(5000); assertThat(errorTree.get("culprit").textValue()).startsWith(this.getClass().getName()); JsonNode context = errorTree.get("context"); assertThat(context.get("tags").get("foo").textValue()).isEqualTo("bar"); JsonNode exception = errorTree.get("exception"); assertThat(exception.get("message").textValue()).isEqualTo("test"); assertThat(exception.get("stacktrace")).isNotNull(); assertThat(exception.get("type").textValue()).isEqualTo(Exception.class.getName()); assertThat(errorTree.get("transaction").get("sampled").booleanValue()).isTrue(); assertThat(errorTree.get("transaction").get("type").textValue()).isEqualTo("test-type"); }
@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 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(); } }
@Test void resetState() { final Transaction transaction = new Transaction(mock(ElasticApmTracer.class)); TransactionUtils.fillTransaction(transaction); transaction.resetState(); assertThat(jsonSerializer.toJsonString(transaction)).isEqualTo(jsonSerializer.toJsonString(new Transaction(mock(ElasticApmTracer.class)))); } }
@Test void testBodyBuffer() throws IOException { final Transaction transaction = createTransactionWithRequiredValues(); final CharBuffer bodyBuffer = transaction.getContext().getRequest().withBodyBuffer(); IOUtils.decodeUtf8Bytes("{f".getBytes(StandardCharsets.UTF_8), bodyBuffer); IOUtils.decodeUtf8Bytes(new byte[]{0, 0, 'o', 'o', 0}, 2, 2, bodyBuffer); IOUtils.decodeUtf8Byte((byte) '}', bodyBuffer); bodyBuffer.flip(); final String content = serializer.toJsonString(transaction); System.out.println(content); final JsonNode transactionJson = objectMapper.readTree(content); assertThat(transactionJson.get("context").get("request").get("body").textValue()).isEqualTo("{foo}"); transaction.resetState(); assertThat((Object) transaction.getContext().getRequest().getBodyBuffer()).isNull(); }
/** * 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; }
@Test void testTraceMethod() { TestClass.traceMe(); assertThat(reporter.getTransactions()).hasSize(1); assertThat(reporter.getFirstTransaction().getName().toString()).isEqualTo("TestClass#traceMe"); assertThat(reporter.getSpans()).hasSize(1); assertThat(reporter.getFirstSpan().getName().toString()).isEqualTo("TestClass#traceMeToo"); }