@Override public void addTag(String key, String value) { if (!isSampled()) { return; } getContext().getTags().put(key, value); }
@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"); }
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 serializeContext(final TransactionContext context) { writeFieldName("context"); jw.writeByte(OBJECT_START); if (context.getUser().hasContent()) { serializeUser(context.getUser()); jw.writeByte(COMMA); } serializeRequest(context.getRequest()); serializeResponse(context.getResponse()); // TODO custom context writeFieldName("tags"); final Map<String, String> value = context.getTags(); serializeTags(value); jw.writeByte(OBJECT_END); jw.writeByte(COMMA); }
.withEmail("foo@example.com"); context.getTags().put("organization_uuid", "9f0e9d64-c185-4d21-a6f4-4673ed561ec8"); context.getCustom().put("my_key", 1); context.getCustom().put("some_other_value", "foo bar");