Search in sources :

Example 6 with QueryRequest

use of zipkin.storage.QueryRequest in project zipkin by openzipkin.

the class CassandraSpanStoreTest method searchingByAnnotationShouldFilterBeforeLimiting.

@Test
public void searchingByAnnotationShouldFilterBeforeLimiting() {
    long now = System.currentTimeMillis();
    int queryLimit = 2;
    Endpoint endpoint = TestObjects.LOTS_OF_SPANS[0].annotations.get(0).endpoint;
    BinaryAnnotation ba = BinaryAnnotation.create("host.name", "host1", endpoint);
    int nbTraceFetched = queryLimit * storage.indexFetchMultiplier;
    IntStream.range(0, nbTraceFetched).forEach(i -> accept(TestObjects.LOTS_OF_SPANS[i++].toBuilder().timestamp(now - (i * 1000)).build()));
    // Add two traces with the binary annotation we're looking for
    IntStream.range(nbTraceFetched, nbTraceFetched + 2).forEach(i -> accept(TestObjects.LOTS_OF_SPANS[i++].toBuilder().timestamp(now - (i * 1000)).addBinaryAnnotation(ba).build()));
    QueryRequest queryRequest = QueryRequest.builder().addBinaryAnnotation(ba.key, new String(ba.value, Util.UTF_8)).serviceName(endpoint.serviceName).limit(queryLimit).build();
    assertThat(store().getTraces(queryRequest)).hasSize(queryLimit);
}
Also used : BinaryAnnotation(zipkin.BinaryAnnotation) Endpoint(zipkin.Endpoint) QueryRequest(zipkin.storage.QueryRequest) Endpoint(zipkin.Endpoint) Test(org.junit.Test) SpanStoreTest(zipkin.storage.SpanStoreTest)

Example 7 with QueryRequest

use of zipkin.storage.QueryRequest 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)

Example 8 with QueryRequest

use of zipkin.storage.QueryRequest in project zipkin by openzipkin.

the class HttpSpanStore method getTraces.

@Override
public List<List<Span>> getTraces(QueryRequest request) {
    HttpUrl.Builder url = baseUrl.newBuilder("/api/v1/traces");
    maybeAddQueryParam(url, "serviceName", request.serviceName);
    maybeAddQueryParam(url, "spanName", request.spanName);
    maybeAddQueryParam(url, "annotationQuery", request.toAnnotationQuery());
    maybeAddQueryParam(url, "minDuration", request.minDuration);
    maybeAddQueryParam(url, "maxDuration", request.maxDuration);
    maybeAddQueryParam(url, "endTs", request.endTs);
    maybeAddQueryParam(url, "lookback", request.lookback);
    maybeAddQueryParam(url, "limit", request.limit);
    Response response = call(new Request.Builder().url(url.build()).build());
    return Codec.JSON.readTraces(responseBytes(response));
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) QueryRequest(zipkin.storage.QueryRequest) HttpUrl(okhttp3.HttpUrl)

Example 9 with QueryRequest

use of zipkin.storage.QueryRequest in project zipkin by openzipkin.

the class ZipkinDispatcherTest method toQueryRequest_parseBinaryAnnotations_withSlash.

@Test
public void toQueryRequest_parseBinaryAnnotations_withSlash() {
    HttpUrl url = baseUrl.newBuilder().addQueryParameter("serviceName", "myService").addQueryParameter("annotationQuery", "http.path=/sessions").build();
    QueryRequest request = ZipkinDispatcher.toQueryRequest(url);
    assertThat(request.binaryAnnotations).hasSize(1).containsEntry(TraceKeys.HTTP_PATH, "/sessions");
}
Also used : QueryRequest(zipkin.storage.QueryRequest) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Example 10 with QueryRequest

use of zipkin.storage.QueryRequest in project zipkin by openzipkin.

the class ZipkinDispatcherTest method toQueryRequest_parseBinaryAnnotations.

@Test
public void toQueryRequest_parseBinaryAnnotations() {
    HttpUrl url = baseUrl.newBuilder().addQueryParameter("serviceName", "myService").addQueryParameter("annotationQuery", "http.status_code=500").build();
    QueryRequest request = ZipkinDispatcher.toQueryRequest(url);
    assertThat(request.binaryAnnotations).hasSize(1).containsEntry(TraceKeys.HTTP_STATUS_CODE, "500");
}
Also used : QueryRequest(zipkin.storage.QueryRequest) HttpUrl(okhttp3.HttpUrl) Test(org.junit.Test)

Aggregations

QueryRequest (zipkin.storage.QueryRequest)10 Test (org.junit.Test)8 HttpUrl (okhttp3.HttpUrl)6 Callback (zipkin.storage.Callback)3 Arrays.asList (java.util.Arrays.asList)2 ExecutionException (java.util.concurrent.ExecutionException)2 Consumer (java.util.function.Consumer)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Is.isA (org.hamcrest.core.Is.isA)2 Before (org.junit.Before)2 Rule (org.junit.Rule)2 ExpectedException (org.junit.rules.ExpectedException)2 Matchers.any (org.mockito.Matchers.any)2 Matchers.eq (org.mockito.Matchers.eq)2 Mock (org.mockito.Mock)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 MockitoJUnit (org.mockito.junit.MockitoJUnit)2 MockitoRule (org.mockito.junit.MockitoRule)2 Answer (org.mockito.stubbing.Answer)2 BinaryAnnotation (zipkin.BinaryAnnotation)2