raskasa / metrics-okhttp

An OkHttp HTTP client wrapper providing Metrics instrumentation of connection pools, request durations and rates, and other useful information.
Apache License 2.0
23 stars 9 forks source link

Refactor EventListener API usage. #74

Closed raskasa closed 3 years ago

raskasa commented 3 years ago

OkHttp previewed a new EventListener API in v3.9. It became stable in 3.11.

In a previous change, we took a first stab at using the new API to record some new metrics.

This change takes a second stab at using the new API.

We introduce an InstrumentedEventListener that records metrics derived from the EventListener's events. The instrumented listener ensures it properly delegates to any event listener that was previously configured; this is required as only one event listener can be configured on a client.

With this change, the following metrics are now being collected:

okhttp3.EventListener.calls-duration
okhttp3.EventListener.calls-end
okhttp3.EventListener.calls-failed
okhttp3.EventListener.calls-start
okhttp3.EventListener.connections-acquired
okhttp3.EventListener.connections-duration
okhttp3.EventListener.connections-end
okhttp3.EventListener.connections-failed
okhttp3.EventListener.connections-released
okhttp3.EventListener.connections-start
okhttp3.EventListener.dns-duration
okhttp3.EventListener.dns-end
okhttp3.EventListener.dns-start

We avoid introducing metrics for more granular things like request header parsing, response body parsing, and HTTPS connection setup. Those will be added upon request.

Some of these new EventListener metrics duplicate the purpose of some existing OkHttpClient metrics. This is fine for now. In the future, we may shift 100% of the metrics to the EventListener API if we can.

This change is easier to review commit-by-commit.

Closes #58