Search in sources :

Example 91 with Callback

use of zipkin2.Callback in project zipkin by openzipkin.

the class ITActiveMQCollector method skipsOnSpanStorageException.

/**
 * Guards against errors that leak from storage, such as InvalidQueryException
 */
@Test
public void skipsOnSpanStorageException() throws Exception {
    collector.close();
    AtomicInteger counter = new AtomicInteger();
    consumer = (input) -> new Call.Base<Void>() {

        @Override
        protected Void doExecute() {
            throw new AssertionError();
        }

        @Override
        protected void doEnqueue(Callback<Void> callback) {
            if (counter.getAndIncrement() == 1) {
                callback.onError(new RuntimeException("storage fell over"));
            } else {
                receivedSpans.add(spans);
                callback.onSuccess(null);
            }
        }

        @Override
        public Call<Void> clone() {
            throw new AssertionError();
        }
    };
    activemq.pushMessage(collector.queue, PROTO3.encodeList(spans));
    // tossed on error
    activemq.pushMessage(collector.queue, PROTO3.encodeList(spans));
    activemq.pushMessage(collector.queue, PROTO3.encodeList(spans));
    collector = builder().storage(buildStorage(consumer)).build().start();
    assertThat(receivedSpans.take()).containsExactlyElementsOf(spans);
    // the only way we could read this, is if the malformed span was skipped.
    assertThat(receivedSpans.take()).containsExactlyElementsOf(spans);
    assertThat(activemqMetrics.messages()).isEqualTo(3);
    // storage failure not message failure
    assertThat(activemqMetrics.messagesDropped()).isZero();
    assertThat(activemqMetrics.bytes()).isEqualTo(PROTO3.encodeList(spans).length * 3);
    assertThat(activemqMetrics.spans()).isEqualTo(spans.size() * 3);
    // only one dropped
    assertThat(activemqMetrics.spansDropped()).isEqualTo(spans.size());
}
Also used : Call(zipkin2.Call) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 92 with Callback

use of zipkin2.Callback in project zipkin by openzipkin.

the class ITRabbitMQCollector method skipsOnSpanStorageException.

/**
 * Guards against errors that leak from storage, such as InvalidQueryException
 */
@Test
void skipsOnSpanStorageException() throws Exception {
    AtomicInteger counter = new AtomicInteger();
    consumer = (input) -> new Call.Base<Void>() {

        @Override
        protected Void doExecute() {
            throw new AssertionError();
        }

        @Override
        protected void doEnqueue(Callback<Void> callback) {
            if (counter.getAndIncrement() == 1) {
                callback.onError(new RuntimeException("storage fell over"));
            } else {
                receivedSpans.add(spans);
                callback.onSuccess(null);
            }
        }

        @Override
        public Call<Void> clone() {
            throw new AssertionError();
        }
    };
    final StorageComponent storage = buildStorage(consumer);
    RabbitMQCollector.Builder builder = builder("storage_exception").storage(storage);
    produceSpans(THRIFT.encodeList(spans), builder.queue);
    // tossed on error
    produceSpans(THRIFT.encodeList(spans), builder.queue);
    produceSpans(THRIFT.encodeList(spans), builder.queue);
    try (RabbitMQCollector collector = builder.build()) {
        collector.start();
        assertThat(receivedSpans.take()).containsExactlyElementsOf(spans);
        // the only way we could read this, is if the malformed span was skipped.
        assertThat(receivedSpans.take()).containsExactlyElementsOf(spans);
    }
    assertThat(rabbitmqMetrics.messages()).isEqualTo(3);
    // storage failure isn't a message failure
    assertThat(rabbitmqMetrics.messagesDropped()).isZero();
    assertThat(rabbitmqMetrics.bytes()).isEqualTo(THRIFT.encodeList(spans).length * 3);
    assertThat(rabbitmqMetrics.spans()).isEqualTo(spans.size() * 3);
    // only one dropped
    assertThat(rabbitmqMetrics.spansDropped()).isEqualTo(spans.size());
}
Also used : Call(zipkin2.Call) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StorageComponent(zipkin2.storage.StorageComponent) ForwardingStorageComponent(zipkin2.storage.ForwardingStorageComponent) Test(org.junit.jupiter.api.Test)

Example 93 with Callback

use of zipkin2.Callback in project zipkin by openzipkin.

the class ScribeSpanConsumer method Log.

@Override
public void Log(List<LogEntry> messages, AsyncMethodCallback<ResultCode> resultHandler) {
    metrics.incrementMessages();
    List<Span> spans = new ArrayList<>();
    int byteCount = 0;
    try {
        for (LogEntry logEntry : messages) {
            if (!category.equals(logEntry.category))
                continue;
            byte[] bytes = logEntry.message.getBytes(StandardCharsets.ISO_8859_1);
            // finagle-zipkin uses mime encoding
            bytes = Base64.getMimeDecoder().decode(bytes);
            byteCount += bytes.length;
            spans.add(SpanBytesDecoder.THRIFT.decodeOne(bytes));
        }
    } catch (RuntimeException e) {
        metrics.incrementMessagesDropped();
        resultHandler.onError(e);
        return;
    } finally {
        metrics.incrementBytes(byteCount);
    }
    collector.accept(spans, new Callback<Void>() {

        @Override
        public void onSuccess(Void value) {
            resultHandler.onComplete(ResultCode.OK);
        }

        @Override
        public void onError(Throwable t) {
            Exception error = t instanceof Exception ? (Exception) t : new RuntimeException(t);
            resultHandler.onError(error);
        }
    }, CommonPools.blockingTaskExecutor());
}
Also used : ArrayList(java.util.ArrayList) Span(zipkin2.Span) LogEntry(zipkin2.collector.scribe.generated.LogEntry)

Example 94 with Callback

use of zipkin2.Callback in project zipkin by openzipkin.

the class ITSpanStore method consumer_implementsCall_submit.

@Test
protected void consumer_implementsCall_submit(TestInfo testInfo) throws Exception {
    String testSuffix = testSuffix(testInfo);
    Span span = spanBuilder(testSuffix).build();
    Call<Void> call = storage.spanConsumer().accept(asList(span));
    // Ensure the implementation didn't accidentally do I/O at assembly time.
    assertGetTraceReturnsEmpty(span.traceId());
    CountDownLatch latch = new CountDownLatch(1);
    Callback<Void> callback = new Callback<Void>() {

        @Override
        public void onSuccess(Void value) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable t) {
            latch.countDown();
        }
    };
    call.enqueue(callback);
    latch.await();
    blockWhileInFlight();
    assertGetTraceReturns(span);
    assertThatThrownBy(() -> call.enqueue(callback)).isInstanceOf(IllegalStateException.class);
    // no problem to clone a call
    call.clone().execute();
}
Also used : Callback(zipkin2.Callback) CountDownLatch(java.util.concurrent.CountDownLatch) TestObjects.newClientSpan(zipkin2.TestObjects.newClientSpan) Span(zipkin2.Span) Test(org.junit.jupiter.api.Test)

Example 95 with Callback

use of zipkin2.Callback in project OkHttp3 by MrZhousf.

the class HttpHelper method doRequestAsync.

/**
 * 异步请求
 */
void doRequestAsync(final OkHttpHelper helper) {
    if (httpInfo == null)
        return;
    final HttpInfo info = httpInfo;
    final BaseCallback callback = helper.getCallback();
    Request request = helper.getRequest();
    String url = info.getUrl();
    if (!checkUrl(url)) {
        // 主线程回调
        Message msg = new CallbackMessage(OkMainHandler.RESPONSE_CALLBACK, callback, retInfo(info, HttpInfo.CheckURL), requestTag, null).build();
        OkMainHandler.getInstance().sendMessage(msg);
        return;
    }
    request = request == null ? buildRequest(info, helper.getRequestType(), helper.getProgressCallback()) : request;
    showUrlLog(request);
    Call call = httpClient.newCall(request);
    BaseActivityLifecycleCallbacks.putCall(requestTag, call);
    call.enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            // 主线程回调
            int code = HttpInfo.CheckNet;
            if (e instanceof UnknownHostException) {
                if (!helperInfo.getOkHttpUtil().isNetworkAvailable()) {
                    code = HttpInfo.CheckNet;
                } else {
                    code = HttpInfo.CheckURL;
                }
            } else if (e instanceof SocketTimeoutException) {
                code = HttpInfo.ConnectionTimeOut;
                if (null != e.getMessage()) {
                    if (e.getMessage().contains("failed to connect to"))
                        code = HttpInfo.ConnectionTimeOut;
                    if (e.getMessage().equals("timeout"))
                        code = HttpInfo.WriteAndReadTimeOut;
                }
            }
            Message msg = new CallbackMessage(OkMainHandler.RESPONSE_CALLBACK, callback, retInfo(info, code, "[" + e.getMessage() + "]"), requestTag, call).build();
            OkMainHandler.getInstance().sendMessage(msg);
        }

        @Override
        public void onResponse(Call call, Response res) throws IOException {
            // 主线程回调
            Message msg = new CallbackMessage(OkMainHandler.RESPONSE_CALLBACK, callback, dealResponse(helper, res), requestTag, call).build();
            OkMainHandler.getInstance().sendMessage(msg);
        }
    });
}
Also used : Call(okhttp3.Call) CallbackMessage(com.okhttplib.bean.CallbackMessage) Message(android.os.Message) UnknownHostException(java.net.UnknownHostException) CallbackMessage(com.okhttplib.bean.CallbackMessage) Request(okhttp3.Request) IOException(java.io.IOException) HttpInfo(com.okhttplib.HttpInfo) Response(okhttp3.Response) ProgressCallback(com.okhttplib.callback.ProgressCallback) Callback(okhttp3.Callback) BaseCallback(com.okhttplib.callback.BaseCallback) SocketTimeoutException(java.net.SocketTimeoutException) BaseCallback(com.okhttplib.callback.BaseCallback)

Aggregations

Callback (okhttp3.Callback)173 IOException (java.io.IOException)137 Call (okhttp3.Call)132 Response (okhttp3.Response)132 Request (okhttp3.Request)110 Callback (retrofit2.Callback)42 Call (retrofit2.Call)41 Test (org.junit.Test)39 Response (retrofit2.Response)39 RequestBody (okhttp3.RequestBody)37 OkHttpClient (okhttp3.OkHttpClient)34 File (java.io.File)27 Context (android.content.Context)24 JSONObject (org.json.JSONObject)20 FormBody (okhttp3.FormBody)19 ArrayList (java.util.ArrayList)18 View (android.view.View)16 Intent (android.content.Intent)14 TextView (android.widget.TextView)14 GsonBuilder (com.google.gson.GsonBuilder)14