Search in sources :

Example 1 with ErrorPayload

use of co.elastic.apm.impl.error.ErrorPayload in project apm-agent-java by elastic.

the class ApmServerHttpPayloadSender method sendPayload.

@Override
public void sendPayload(final Payload payload) {
    logger.debug("Sending payload with {} elements to APM server {}", payload.getPayloadObjects().size(), reporterConfiguration.getServerUrl());
    final String path;
    if (payload instanceof ErrorPayload) {
        path = "/v1/errors";
    } else {
        path = "/v1/transactions";
    }
    final Request.Builder builder = new Request.Builder().url(reporterConfiguration.getServerUrl() + path).header("User-Agent", getUserAgent(payload));
    if (reporterConfiguration.getSecretToken() != null) {
        builder.header("Authorization", "Bearer " + reporterConfiguration.getSecretToken());
    }
    if (useGzip(payload)) {
        builder.header("Content-Encoding", "gzip");
    }
    Request request = builder.post(new RequestBody() {

        @Override
        public MediaType contentType() {
            return MEDIA_TYPE_JSON;
        }

        @Override
        public void writeTo(BufferedSink sink) throws IOException {
            if (useGzip(payload)) {
                GzipSink gzipSink = new GzipSink(sink);
                gzipSink.deflater().setLevel(GZIP_COMPRESSION_LEVEL);
                sink = Okio.buffer(gzipSink);
            }
            payloadSerializer.serializePayload(sink, payload);
            sink.close();
            payload.recycle();
        }
    }).build();
    try {
        Response response = httpClient.newCall(request).execute();
        int statusCode = response.code();
        logger.debug("APM server responded with status code {}", statusCode);
        if (statusCode >= 400) {
            droppedTransactions += payload.getPayloadObjects().size();
            if (response.body() != null) {
                logger.debug(response.body().string());
            }
        }
        response.close();
    } catch (IOException e) {
        logger.debug("Sending payload to APM server failed", e);
        droppedTransactions += payload.getPayloadObjects().size();
    }
}
Also used : Response(okhttp3.Response) ErrorPayload(co.elastic.apm.impl.error.ErrorPayload) GzipSink(okio.GzipSink) Request(okhttp3.Request) BufferedSink(okio.BufferedSink) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Aggregations

ErrorPayload (co.elastic.apm.impl.error.ErrorPayload)1 IOException (java.io.IOException)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1 BufferedSink (okio.BufferedSink)1 GzipSink (okio.GzipSink)1