Skip to content

Conversation

@daniyelnnr
Copy link
Contributor

@daniyelnnr daniyelnnr commented Dec 23, 2025

What is the purpose of this pull request?

Fixes an issue where custom metrics recorded by IO apps were missing request context attributes (account, route_id, status_code, etc.), resulting in incomplete metric data.

Implements automatic merging of request-scoped base attributes with custom attributes using OpenTelemetry Context API.

What problem is this solving?

Before: Custom metrics from apps only contained app-specific attributes:

{ "operation": "vm-global-css", "status": "success" }

After: Custom metrics automatically include request context:

{
  "vtex.account.name": "paypalstore",
  "component": "http-handler", 
  "route_id": "apiRouteHandler",
  "route_type": "public",
  "status_code": "200",
  "operation": "vm-global-css",
  "status": "success"
}

Key features:

  • Base attributes set once per request via runWithBaseAttributes()
  • Automatically merged with custom attributes in all metric calls
  • Base attributes take precedence on key conflicts (transparent to apps)
  • Custom attributes limited to 7, base attributes unlimited
  • Zero breaking changes - apps continue using existing API

How should this be manually tested?

  1. Deploy an app that records custom metrics using global.diagnosticsMetrics.recordLatency()
  2. Verify metrics in data source include both custom attributes AND request context (account, route_id, status_code)
  3. Confirm no duplicate or missing attributes

Screenshots or example usage

// From render-to-string app - no changes needed
global.diagnosticsMetrics?.recordLatency(elapsed, { 
  operation: 'my-operation',
  status: 'success' 
})

// Result automatically includes request context from middleware

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Requires change to documentation, which has been updated accordingly.

…etrics

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.).
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.
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.
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.
@daniyelnnr daniyelnnr changed the title fix: merge request context attributes with custom metrics in DiagnosticsMetrics fix: merge request context attributes with custom metrics Dec 23, 2025
@daniyelnnr daniyelnnr requested review from a team, silvadenisaraujo and vsseixaso December 23, 2025 19:18
- 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.
Copy link
Contributor

@silvadenisaraujo silvadenisaraujo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

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.

3 participants