@Test void testBodyBufferCopy() throws IOException { final Transaction transaction = createTransactionWithRequiredValues(); final CharBuffer bodyBuffer = transaction.getContext().getRequest().withBodyBuffer(); IOUtils.decodeUtf8Bytes("{foo}".getBytes(StandardCharsets.UTF_8), bodyBuffer); bodyBuffer.flip(); Transaction copy = createTransactionWithRequiredValues(); copy.getContext().copyFrom(transaction.getContext()); assertThat(objectMapper.readTree(serializer.toJsonString(copy)).get("context")) .isEqualTo(objectMapper.readTree(serializer.toJsonString(transaction)).get("context")); }
@Test void testCopyFromDoNotCopyTags() { TransactionContext context = new TransactionContext(); context.getTags().put("foo", "bar"); TransactionContext copyOfContext = new TransactionContext(); copyOfContext.copyFrom(context); assertThat(copyOfContext.getTags()).isEmpty(); }
@Test void testCopyFromDoNotCopyCustom() { TransactionContext context = new TransactionContext(); context.getCustom().put("foo", "bar"); TransactionContext copyOfContext = new TransactionContext(); copyOfContext.copyFrom(context); assertThat(copyOfContext.getCustom()).isEmpty(); }
@Test void testCopyFrom() { TransactionContext context = createContext(); TransactionContext copyOfContext = new TransactionContext(); copyOfContext.copyFrom(context); assertThat(toJson(context)).isEqualTo(toJson(copyOfContext)); }
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); } }