private void serializeRequest(final Request request) { if (request.hasContent()) { writeFieldName("request"); jw.writeByte(OBJECT_START); writeField("method", request.getMethod()); writeField("headers", request.getHeaders()); writeField("cookies", request.getCookies()); // only one of those can be non-empty if (!request.getFormUrlEncodedParameters().isEmpty()) { writeField("body", request.getFormUrlEncodedParameters()); } else if (request.getBodyBuffer() != null && request.getBodyBuffer().length() > 0) { writeFieldName("body"); jw.writeString(request.getBodyBuffer()); jw.writeByte(COMMA); } if (request.getUrl().hasContent()) { serializeUrl(request.getUrl()); } if (request.getSocket().hasContent()) { serializeSocket(request.getSocket()); } writeLastField("http_version", request.getHttpVersion()); jw.writeByte(OBJECT_END); jw.writeByte(COMMA); } }
public void copyFrom(Request other) { this.postParams.copyFrom(other.postParams); this.headers.copyFrom(other.headers); this.httpVersion = other.httpVersion; this.method = other.method; this.socket.copyFrom(other.socket); this.url.copyFrom(other.url); this.cookies.copyFrom(other.cookies); if (other.bodyBuffer != null) { final CharBuffer otherBuffer = other.getBodyBuffer(); final CharBuffer thisBuffer = this.withBodyBuffer(); for (int i = 0; i < otherBuffer.length(); i++) { thisBuffer.append(otherBuffer.charAt(i)); } thisBuffer.flip(); } }
@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(); }
private TransactionContext createContext() { TransactionContext context = new TransactionContext(); Request request = context.getRequest(); request.withHttpVersion("1.1"); request.withMethod("POST"); request.withBodyBuffer().append("Hello World").flip(); request.getUrl() .withProtocol("https") .appendToFull("https://www.example.com/p/a/t/h?query=string#hash") .withPathname("/p/a/t/h") .withSearch("?query=string"); request.getSocket() .withEncrypted(true) .withRemoteAddress("12.53.12.1"); request.addHeader("user-agent", "Mozilla Chrome Edge"); request.addHeader("content-type", "text/html"); request.addHeader("cookie", "c1=v1; c2=v2"); request.addHeader("some-other-header", "foo"); request.addHeader("array", "foo, bar, baz"); request.getCookies().add("c1", "v1"); request.getCookies().add("c2", "v2");
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(); } }
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; }
@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")); }
public void copyFrom(TransactionContext other) { response.copyFrom(other.response); request.copyFrom(other.request); user.copyFrom(other.user); }
request.withHttpVersion("1.1"); request.withMethod("POST"); request.withBodyBuffer().append("Hello World").flip(); request.getUrl() .withProtocol("https") .appendToFull("https://www.example.com/p/a/t/h?query=string#hash") .withPathname("/p/a/t/h") .withSearch("?query=string"); request.getSocket() .withEncrypted(true) .withRemoteAddress("12.53.12.1"); request.addHeader("user-agent", "Mozilla Chrome Edge"); request.addHeader("content-type", "text/html"); request.addHeader("cookie", "c1=v1; c2=v2"); request.addHeader("some-other-header", "foo"); request.addHeader("array", "foo, bar, baz"); request.getCookies().add("c1", "v1"); request.getCookies().add("c2", "v2");
@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(); }