@Override public void addTag(String key, String value) { context.getTags().put(key, value); }
@Override public void resetState() { super.resetState(); context.resetState(); stacktrace = null; type = null; subtype = null; action = null; originator = null; }
private void serializeSpanContext(SpanContext context) { writeFieldName("context"); jw.writeByte(OBJECT_START); boolean spanContextWritten = false; Db db = context.getDb(); if (db.hasContent()) { serializeDbContext(db); spanContextWritten = true; } Http http = context.getHttp(); if (http.hasContent()) { if (spanContextWritten) { jw.writeByte(COMMA); } serializeHttpContext(http); spanContextWritten = true; } Map<String, String> tags = context.getTags(); if (!tags.isEmpty()) { if (spanContextWritten) { jw.writeByte(COMMA); } writeFieldName("tags"); serializeTags(tags); } jw.writeByte(OBJECT_END); jw.writeByte(COMMA); }
@Test void resetState() { Span span = new Span(mock(ElasticApmTracer.class)) .withName("SELECT FROM product_types") .withType("db") .withSubtype("postgresql") .withAction("query"); span.getContext().getDb() .withInstance("customers") .withStatement("SELECT * FROM product_types WHERE user_id=?") .withType("sql") .withUser("readonly_user"); span.resetState(); assertThat(span.getContext().hasContent()).isFalse(); assertThat((CharSequence) span.getName()).isNullOrEmpty(); assertThat(span.getType()).isNull(); assertThat(span.getSubtype()).isNull(); assertThat(span.getAction()).isNull(); } }
.withSubtype("postgresql") .withAction("query"); span.getContext().getDb() .withInstance("customers") .withStatement("SELECT * FROM product_types WHERE user_id=?") .withSubtype("http") .withAction("apache-httpclient"); span.getContext().getHttp() .withUrl("http://test.elastic.co/test-service") .withMethod("POST")
private void serializeSpan(final Span span) { jw.writeByte(OBJECT_START); writeField("name", span.getName()); writeTimestamp(span.getTimestamp()); serializeTraceContext(span.getTraceContext(), true); writeField("duration", span.getDuration()); if (span.getStacktrace() != null) { serializeStacktrace(span.getStacktrace().getStackTrace()); } if (span.getContext().hasContent()) { serializeSpanContext(span.getContext()); } serializeSpanType(span); jw.writeByte(OBJECT_END); }
private void validateJsonStructure(TransactionPayload payload) throws IOException { JsonNode serializedSpans = getSerializedSpans(payload); validateDbSpanSchema(serializedSpans, true); validateHttpSpanSchema(serializedSpans); for (Span span : payload.getSpans()) { if (span.getType() != null && span.getType().equals("db")) { span.getContext().getTags().clear(); validateDbSpanSchema(getSerializedSpans(payload), false); break; } } }
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); } }