You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(opensearchtransport): collect per-request metrics by default (#896)
Per-request counters (requests, failures, responses-by-status) are now
collected by default, independent of EnableMetrics. The metrics struct
is always allocated and wired into the connection pools; EnableMetrics
now sets only a `detailed` flag that gates the expensive
detailed-metrics path (per-connection enumeration and the
per-connection/per-policy/per-snapshot callbacks).
Previously these counters were gated behind EnableMetrics alongside the
expensive detailed path, so callers who wanted basic request/response
counts had to opt into work they did not need, and internal consumers
could not rely on the counters being populated.
- The responses-by-status counter moves from a mutex-guarded
map[int]int to a lock-free atomic.Int64 array indexed by status code,
plus a single overflow bucket for out-of-range codes. (requests and
failures were already atomic as of #776.)
- Metrics() returns the per-request counters unconditionally and only
builds the detailed, callback-augmented fields when detailed is
enabled. It no longer returns an error when metrics are disabled.
- Detailed-callback registration at all 7 policy sites is guarded by a
nil-safe metrics.detailedEnabled() helper, so the counters-only path
does zero per-snapshot work.
- A custom ConnectionPoolFunc returning a single-URL pool no longer
errors during construction: metrics wiring is a no-op on a pool type
mismatch, mirroring the multi-node branch.
- The public Metrics.Responses map keys out-of-range status codes under
-1 (statusOverflow); documented on the field.
Closes#891
Signed-off-by: Ryan Yuan <ryan.yuan@crowdstrike.com>
Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Co-authored-by: Ryan Yuan <ryan.yuan@crowdstrike.com>
Co-authored-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -165,6 +165,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
165
165
166
166
### Changed
167
167
168
+
-**BREAKING**: Per-request transport metrics (`requests`, `failures`, responses-by-status) are now always collected via lock-free atomics, independent of `EnableMetrics`. `EnableMetrics` now gates only the detailed-metrics snapshot (per-connection, per-policy, and router state returned by `Metrics()`). The responses-by-status counter moved from a mutex-guarded map to a lock-free atomic array. `Metrics()` no longer returns an error when metrics are disabled -- it always returns the per-request counters (callers that branched on `if err != nil` for the disabled case should drop that check). See [`UPGRADING_V5.md`](UPGRADING_V5.md#metrics-error-on-disabled-removed) for migration. ([#891](https://github.com/opensearch-project/opensearch-go/issues/891))
168
169
- Reorganize the documentation. Split `UPGRADING.md` into a version-history index plus per-major-version guides (`UPGRADING_V5.md` through `UPGRADING_V2.md`) and rename `opensearchapi/MIGRATING.md` to `opensearchapi/UPGRADING_V4_TO_V5.md`. Group the `guides/` and `_samples/` files by subsystem (`transport-`, `indexing-`, `usage-`, `config-`) and add a `guides/README.md` index. Make `guides/usage-error_handling.md` the single source for partial-error handling and `guides/transport-retry_backoff.md` the single source for resurrection-timeout config, replacing the duplicated copies in `opensearchapi/README.md` and `guides/transport-routing.md` with links. Add package documentation (`doc.go`) for `opensearchapi`, `plugins`, `signer`, and `signer/awsv2`.
169
170
- Trim the CI compatibility matrix to the currently-patched OpenSearch set (2.19.x and 3.x) per the 12-month support policy; older lines (1.3.x - 2.18.x) are no longer part of the tested matrix and the 4.x client remains their supported path. No client code change ([#856](https://github.com/opensearch-project/opensearch-go/issues/856))
170
171
-**BREAKING**: Module path is now `github.com/opensearch-project/opensearch-go/v5`. Update import paths from `/v4` to `/v5`; the in-source `opensearchapi.X` package qualifier is unchanged
The signature already changed earlier in this version to take `context.Context` (see CHANGELOG); this is a behavioral change on top of the signature change.
98
98
99
+
## Metrics error on disabled removed
100
+
101
+
The per-request transport counters (`Requests`, `Failures`, and responses-by-status) are now always collected via lock-free atomics, independent of `EnableMetrics`. As a result, `opensearch.Client.Metrics()` (and `opensearchtransport.Transport.Metrics()`) no longer returns the `"transport metrics not enabled"` error when `EnableMetrics` is false -- it always returns the per-request counters. `EnableMetrics` now gates only the detailed-metrics snapshot (per-connection enumeration, per-policy breakdowns, and router cache state); those fields stay zero/nil when it is unset.
102
+
103
+
Callers that branched on the error to detect the disabled state should drop that check. The returned error is now non-nil only when a detailed-metrics snapshot callback fails.
104
+
105
+
```go
106
+
// v4: Metrics() errored when EnableMetrics was false, so callers used the
107
+
// error to detect the disabled state.
108
+
m, err:= client.Metrics()
109
+
if err != nil {
110
+
// treated as "metrics disabled" -- no counters available
111
+
return
112
+
}
113
+
use(m.Requests, m.Failures)
114
+
115
+
// v5: per-request counters are always populated. A non-nil error now means a
116
+
// detailed-snapshot callback failed, not that metrics are disabled.
// m.Requests / m.Failures / m.Responses are still valid here
121
+
}
122
+
use(m.Requests, m.Failures)
123
+
```
124
+
125
+
Detailed fields such as `Policies` and `Router` remain populated only when `EnableMetrics` is set; reading them without it yields nil, unchanged from v4.
126
+
99
127
## `opensearchtransport.Client` renamed to `opensearchtransport.Transport`
100
128
101
129
The concrete `opensearchtransport.Client` type was renamed to `opensearchtransport.Transport`. The type owns HTTP round-trip concerns -- connection pooling, retries, node selection, and discovery -- so `Transport` reflects its role and avoids colliding conceptually with the API clients above it (`opensearch.Client` and `opensearchapi.Client`).
Copy file name to clipboardExpand all lines: guides/transport-metrics.md
+4-2Lines changed: 4 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,11 @@
2
2
3
3
The opensearch-go transport exposes a pull-based metrics API that returns a point-in-time snapshot of request counters, connection pool state, per-connection health, policy-level breakdowns, and router cache state. All fields are JSON-tagged for easy serialization.
4
4
5
+
Metrics come in two tiers. The **per-request counters** (`requests`, `failures`, and responses-by-status) are always collected via lock-free atomics and returned by `Metrics()` regardless of configuration. The **detailed-metrics snapshot** (connection-pool state, per-connection health, per-policy breakdowns, and router cache state) is opt-in behind `EnableMetrics`.
6
+
5
7
## Quick Start
6
8
7
-
Metrics collection is opt-in. Set `EnableMetrics: true` on `opensearch.Config`; without it, `client.Metrics()` returns `"transport metrics not enabled"`. When constructing through `opensearchapi.NewClient`, set the flag on the embedded `opensearch.Config` and reach the method via `apiClient.Client.Metrics()`.
9
+
The per-request counters require no configuration. To also populate the detailed-metrics snapshot, set `EnableMetrics: true` on `opensearch.Config`. When constructing through `opensearchapi.NewClient`, set the flag on the embedded `opensearch.Config` and reach the method via `apiClient.Client.Metrics()`.
The `Metrics()` method lives on `opensearch.Client`. It returns an `opensearchtransport.Metrics` struct and an error -- non-nil when metrics are disabled or a snapshot callback fails.
29
+
The `Metrics()` method lives on `opensearch.Client`. It returns an `opensearchtransport.Metrics` struct and an error -- non-nil when a detailed-metrics snapshot callback fails. A `New()`-constructed transport always returns the per-request counters, so `Metrics()` does not error merely because `EnableMetrics` is unset.
0 commit comments