@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 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(); }
@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")); }
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 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); }
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(); } }
Request request = context.getRequest(); request.withHttpVersion("1.1"); request.withMethod("POST");
private TransactionContext createContext() { TransactionContext context = new TransactionContext(); Request request = context.getRequest(); request.withHttpVersion("1.1"); request.withMethod("POST");