Commit d7b3b66
authored
fix: merge request context attributes with custom metrics (#646)
* feat: add OpenTelemetry context-based base attributes to DiagnosticsMetrics
Implement runWithBaseAttributes() method that uses OpenTelemetry Context API to automatically merge request-scoped base attributes with custom attributes in all metric calls (recordLatency, incrementCounter, setGauge).
Key features:
- Base attributes are set once per request scope and automatically merged
- Custom attributes limited to 5 (MAX_CUSTOM_ATTRIBUTES)
- Base attributes take precedence on key conflicts
- Conflicting custom attributes silently dropped (transparent to apps)
- Base attributes are not limited in count
This enables VTEX IO apps to record custom metrics without manually passing request context (account, route_id, status_code, etc.).
* feat: use base attributes in timings middleware
Update timings middleware to set base attributes (account, component, route_id, route_type) using runWithBaseAttributes() before calling next().
This ensures all metrics recorded during request handling (including by VTEX IO apps) automatically include request context without manual attribute passing.
Response-specific attributes (status_code, status) are provided as custom attributes after request completion.
* test: add comprehensive tests for base attributes merging
Add test coverage for:
- Base attributes merging with custom attributes
- Base attribute precedence on key conflicts
- Custom attribute limiting (5 max)
- Silent dropping of conflicting custom attributes
- Async context propagation
- Concurrent request isolation
- Nested context handling
Includes setup of AsyncHooksContextManager for OpenTelemetry context propagation in tests.
* test: update timings middleware tests for base attributes
Update test mocks to include runWithBaseAttributes() method and adjust expectations to reflect that only response-specific attributes (status_code, status) are passed directly, while base attributes (account, route_id, etc.) are set via context.
* chore: bump version to 7.3.1-beta.0
* fix: increase maximum custom attributes limit from 5 to 7 in DiagnosticsMetrics
* test: update DiagnosticsMetrics tests to reflect increased custom attributes limit from 5 to 7
* fix: add warning for unavailable DiagnosticsMetrics in timings middleware
* chore: update dependencies in yarn.lock
- Added @hapi/bourne version 3.0.0 as a dependency for co-body.
- Updated co-body from version 6.0.0 to 6.2.0.
* chore: release version 7.3.1
- Added OpenTelemetry context-based base attributes to DiagnosticsMetrics.
- Fixed merging of request context attributes with custom metrics to automatically include request-scoped base attributes.1 parent 09bc6a0 commit d7b3b66
File tree
7 files changed
+581
-79
lines changed- src
- metrics
- service/worker/runtime/http/middlewares
7 files changed
+581
-79
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
10 | 26 | | |
11 | 27 | | |
12 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
0 commit comments