Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Okhttp Events didn't send all events, only Receive CallStart Event #8492

Closed
shaokunW-hotstar opened this issue Jul 26, 2024 · 0 comments
Closed
Labels
bug Bug in existing code

Comments

@shaokunW-hotstar
Copy link

shaokunW-hotstar commented Jul 26, 2024

I follow the instructions in https://square.github.io/okhttp/features/events/

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.binder.okhttp3.OkHttpConnectionPoolMetrics;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;
import okhttp3.EventListener;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

@Slf4j
public class Test {
  private static final OkHttpClient httpClient = httpClient();
  private static final Counter counter = Metrics.counter("ok.http.counter");

  public static void main(String[] args) throws InterruptedException {
    Request request = new Request.Builder()
      .url("https://127.0.0.1:8080")
      .build();
    httpClient.newCall(request).enqueue(new Callback() {
      @Override
      public void onFailure(@NotNull Call call, @NotNull IOException e) {
        log.error("failure", e);
        counter.increment();
      }

      @Override
      public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
        log.info("code={}", response.code());
        counter.increment();
      }
    });

    Thread.sleep(2000);
  }


  private static OkHttpClient httpClient() {
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequests(3000);
    dispatcher.setMaxRequestsPerHost(3000);

    ConnectionPool connectionPool = new ConnectionPool(5, 5, TimeUnit.MINUTES);
    new OkHttpConnectionPoolMetrics(connectionPool).bindTo(Metrics.globalRegistry);
    return new OkHttpClient.Builder()
      .dispatcher(dispatcher)
      .eventListenerFactory(PrintingEventListener.FACTORY)
      .connectionPool(new ConnectionPool(5, 5, TimeUnit.MINUTES)) // Configure the pool size and timeout
      .build();
  }

  public class PrintingEventListener extends EventListener {
    public static final Factory FACTORY = new Factory() {
      final AtomicLong nextCallId = new AtomicLong(1L);

      @Override
      public EventListener create(Call call) {
        long callId = nextCallId.getAndIncrement();
        System.out.printf("%04d %s%n", callId, call.request().url());
        return new com.hotstar.adtech.blaze.simulator.PrintingEventListener(callId, System.nanoTime());
      }
    };

    final long callId;
    final long callStartNanos;

    public PrintingEventListener(long callId, long callStartNanos) {
      this.callId = callId;
      this.callStartNanos = callStartNanos;
    }

    private void printEvent(String name) {
      long elapsedNanos = System.nanoTime() - callStartNanos;
      System.out.printf("%04d %.3f %s%n", callId, elapsedNanos / 1000000000d, name);
    }

    @Override
    public void callStart(Call call) {
      printEvent("callStart");
    }

    @Override
    public void callEnd(Call call) {
      printEvent("callEnd");
    }

  }
}

Expected to see callEnd in stdout
Screenshot 2024-07-26 at 16 26 24

Dependency

[INFO] +- com.squareup.okhttp3:okhttp:jar:4.12.0:compile
[INFO] |  +- com.squareup.okio:okio:jar:3.6.0:compile
[INFO] |  |  \- com.squareup.okio:okio-jvm:jar:3.6.0:compile
[INFO] |  |     \- org.jetbrains.kotlin:kotlin-stdlib-common:jar:1.6.21:compile
[INFO] |  \- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.6.21:compile
[INFO] |     +- org.jetbrains.kotlin:kotlin-stdlib:jar:1.6.21:compile
@shaokunW-hotstar shaokunW-hotstar added the bug Bug in existing code label Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code
Projects
None yet
Development

No branches or pull requests

1 participant