Skip to content

Feat/http client metrics#2

Open
rag594 wants to merge 2 commits intomainfrom
feat/http-client-metrics
Open

Feat/http client metrics#2
rag594 wants to merge 2 commits intomainfrom
feat/http-client-metrics

Conversation

@rag594
Copy link
Owner

@rag594 rag594 commented Jun 1, 2025

No description provided.

This commit introduces OpenTelemetry metrics collection for the HTTP client.

The following metrics are now collected:
- http.client.request.count (Counter): Total number of HTTP requests.
- http.client.request.duration (Histogram): Duration of HTTP requests.
- http.client.request.errors (Counter): Number of HTTP requests that resulted in an error.

Metrics can be enabled or disabled using the `WithMetricsEnabled` option when creating a new `HTTPClient`.

An example demonstrating how to use the metrics feature has been added in `example/httpMetrics/main.go`.

Unit tests have been added to verify the correctness of the metrics collection.
This commit refactors the internal implementation of OpenTelemetry metrics
in the HTTP client to be more scalable and maintainable.

Key changes:
- Introduced a `MetricConfig` struct and a default list of metric configurations.
- The `HTTPClient` now stores initialized metric instruments in a map
  (`instruments`) keyed by metric name, instead of individual struct fields.
- Metric initialization in `NewHTTPClient` is now driven by iterating
  through the `defaultMetricConfigs`.
- Metric recording in the `Do` method now retrieves instruments from the
  `instruments` map.

This change does not alter your user-facing API for enabling metrics or
the names and attributes of the metrics themselves. Existing tests and
documentation remain valid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant