Search in sources :

Example 1 with Codec

use of zipkin.Codec in project zipkin by openzipkin.

the class ZipkinDispatcher method dispatch.

@Override
public MockResponse dispatch(RecordedRequest request) {
    HttpUrl url = server.url(request.getPath());
    if (request.getMethod().equals("GET")) {
        if (url.encodedPath().equals("/health")) {
            return new MockResponse().setBody("OK\n");
        } else if (url.encodedPath().equals("/api/v1/services")) {
            return jsonResponse(Codec.JSON.writeStrings(store.getServiceNames()));
        } else if (url.encodedPath().equals("/api/v1/spans")) {
            String serviceName = url.queryParameter("serviceName");
            return jsonResponse(Codec.JSON.writeStrings(store.getSpanNames(serviceName)));
        } else if (url.encodedPath().equals("/api/v1/dependencies")) {
            Long endTs = maybeLong(url.queryParameter("endTs"));
            Long lookback = maybeLong(url.queryParameter("lookback"));
            List<DependencyLink> result = store.getDependencies(endTs, lookback);
            return jsonResponse(Codec.JSON.writeDependencyLinks(result));
        } else if (url.encodedPath().equals("/api/v1/traces")) {
            QueryRequest queryRequest = toQueryRequest(url);
            return jsonResponse(Codec.JSON.writeTraces(store.getTraces(queryRequest)));
        } else if (url.encodedPath().startsWith("/api/v1/trace/")) {
            String traceIdHex = url.encodedPath().replace("/api/v1/trace/", "");
            long traceIdHigh = traceIdHex.length() == 32 ? lowerHexToUnsignedLong(traceIdHex, 0) : 0L;
            long traceIdLow = lowerHexToUnsignedLong(traceIdHex);
            List<Span> trace = url.queryParameterNames().contains("raw") ? store.getRawTrace(traceIdHigh, traceIdLow) : store.getTrace(traceIdHigh, traceIdLow);
            if (trace != null)
                return jsonResponse(Codec.JSON.writeSpans(trace));
        }
    } else if (request.getMethod().equals("POST")) {
        if (url.encodedPath().equals("/api/v1/spans")) {
            metrics.incrementMessages();
            byte[] body = request.getBody().readByteArray();
            String encoding = request.getHeader("Content-Encoding");
            if (encoding != null && encoding.contains("gzip")) {
                try {
                    Buffer result = new Buffer();
                    GzipSource source = new GzipSource(new Buffer().write(body));
                    while (source.read(result, Integer.MAX_VALUE) != -1) ;
                    body = result.readByteArray();
                } catch (IOException e) {
                    metrics.incrementMessagesDropped();
                    return new MockResponse().setResponseCode(400).setBody("Cannot gunzip spans");
                }
            }
            String type = request.getHeader("Content-Type");
            Codec codec = type != null && type.contains("/x-thrift") ? Codec.THRIFT : Codec.JSON;
            final MockResponse result = new MockResponse();
            consumer.acceptSpans(body, codec, new Callback<Void>() {

                @Override
                public void onSuccess(Void value) {
                    result.setResponseCode(202);
                }

                @Override
                public void onError(Throwable t) {
                    String message = t.getMessage();
                    result.setBody(message).setResponseCode(message.startsWith("Cannot store") ? 500 : 400);
                }
            });
            return result;
        }
    } else {
        // unsupported method
        return new MockResponse().setResponseCode(405);
    }
    return new MockResponse().setResponseCode(404);
}
Also used : Buffer(okio.Buffer) MockResponse(okhttp3.mockwebserver.MockResponse) QueryRequest(zipkin.storage.QueryRequest) IOException(java.io.IOException) HttpUrl(okhttp3.HttpUrl) GzipSource(okio.GzipSource) Codec(zipkin.Codec) Callback(zipkin.storage.Callback) Util.lowerHexToUnsignedLong(zipkin.internal.Util.lowerHexToUnsignedLong) List(java.util.List)

Aggregations

IOException (java.io.IOException)1 List (java.util.List)1 HttpUrl (okhttp3.HttpUrl)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 Buffer (okio.Buffer)1 GzipSource (okio.GzipSource)1 Codec (zipkin.Codec)1 Util.lowerHexToUnsignedLong (zipkin.internal.Util.lowerHexToUnsignedLong)1 Callback (zipkin.storage.Callback)1 QueryRequest (zipkin.storage.QueryRequest)1