Skip to content

Conversation

@harshit078
Copy link
Contributor

@harshit078 harshit078 commented Jan 29, 2026

Before submitting a pull request, please take a look at our
Contributing guidelines and verify:

  • If you've added code that should be tested, please add tests.
  • Ensure your code lints and the test suite passes (yarn lint) & (yarn test).
  • Link an issue if there is one related to your pull request. If no issue is linked, one will be auto-generated and linked.

Closes #18913

What changed

  • implemented attribute-based routing for metrics
  • added MetricRoutingInfo with dsn and release as its field
  • added routing to MetricOptions and enabled captureMetric to inject routing info into attributes
  • similarly added routing to InternalCaptureMetricOptions
  • created _stripRoutingAttributes function which removes routing info before sending to sentry

@harshit078 harshit078 marked this pull request as ready for review February 2, 2026 12:05
attributes: {
...serializeAttributes(scopeAttributes),
...serializeAttributes(metric.attributes, 'skip-undefined'),
...serializeAttributes(_stripRoutingAttributes(metric.attributes), 'skip-undefined'),
Copy link

Choose a reason for hiding this comment

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

Bug: The _buildSerializedMetric function strips routing attributes from metrics, preventing the multiplexed transport from correctly routing them. The public captureMetric API for routing is non-functional.
Severity: HIGH

Suggested Fix

The _buildSerializedMetric function should not strip the routing attributes. The routing information needs to be preserved in the metric attributes until it reaches the transport layer so that the multiplexed transport can correctly route the metric based on the provided options. The call to _stripRoutingAttributes() within _buildSerializedMetric() should be removed or modified.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/core/src/metrics/internal.ts#L168

Potential issue: When a user calls `captureMetric()` with routing options, the routing
information is added to the metric's attributes. However, during serialization in
`_buildSerializedMetric()`, the `_stripRoutingAttributes()` function is called, which
removes this routing information. Consequently, when the multiplexed transport later
attempts to access the routing key, it is no longer present. This results in all metrics
being sent to the default DSN, rendering the metric routing feature non-functional when
used via the public API. Existing tests pass because they bypass the public API and
serialization flow.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 3 potential issues.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

attributes: {
...serializeAttributes(scopeAttributes),
...serializeAttributes(metric.attributes, 'skip-undefined'),
...serializeAttributes(_stripRoutingAttributes(metric.attributes), 'skip-undefined'),
Copy link

Choose a reason for hiding this comment

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

Metric routing stripped before transport can read it

High Severity

The routing information added via MULTIPLEXED_METRIC_ROUTING_KEY in public-api.ts is stripped by _stripRoutingAttributes() in _buildSerializedMetric() before the metric is added to the buffer. When the multiplexed transport later reads the metric from the envelope, the routing key has already been removed, so metric.attributes[MULTIPLEXED_METRIC_ROUTING_KEY] is undefined. This makes the entire metric routing feature non-functional when using the public API (metrics.count(), metrics.gauge(), metrics.distribution() with routing option).

Additional Locations (1)

Fix in Cursor Fix in Web

Copy link
Contributor Author

Choose a reason for hiding this comment

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

looking into it. Thanks !

const envelope = createMetricEnvelope([metricWithRouting], undefined, undefined, undefined);
const transport = makeTransport({ url: DSN1_URL, ...transportOptions });
await transport.send(envelope);
});
Copy link

Choose a reason for hiding this comment

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

Missing integration test for metric routing feature

Low Severity

Per the review rules for feat PRs: "check if the PR includes at least one integration or E2E test." This PR adds only unit tests that test components in isolation. The multiplexed transport tests manually create SerializedMetric objects with routing already present in attributes, and the internal tests verify routing is stripped from the buffer. No test exercises the full flow from metrics.count() with routing option through to the multiplexed transport, which would have caught the logic bug where routing is stripped before the transport can read it.

Fix in Cursor Fix in Web

metric.attributes = {};
}
metric.attributes['sentry.release'] = { type: 'string', value: release };
}
Copy link

Choose a reason for hiding this comment

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

Release override only applies to first metric in batch

Medium Severity

The makeOverrideReleaseTransport function is documented to override "the release on all events and metrics" but metricFromEnvelope only returns containerItems[0] - the first metric. When the metrics buffer is flushed, multiple metrics are batched into a single envelope. Only the first metric receives the sentry.release attribute; the remaining metrics in the batch are skipped. This causes inconsistent release tracking data where some metrics have the attribute and others don't.

Additional Locations (1)

Fix in Cursor Fix in Web

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.

Support routing metrics to different DSNs in MFE setup

1 participant