Skip to content

Conversation

@jpnurmi
Copy link
Collaborator

@jpnurmi jpnurmi commented Jan 29, 2026

Summary

Implements the Sentry Metrics feature for the native SDK, allowing applications to record counter, gauge, and distribution metrics that are batched and sent to Sentry: https://develop.sentry.dev/sdk/telemetry/metrics/.

Closes #1453

Features

  • Three metric types: counter, gauge, and distribution
  • Batching: Metrics are buffered and sent in batches (same logic as logs - every 100 items or 5 seconds)
  • Attributes: Automatic default attributes (environment, release, SDK info) plus custom user attributes
  • Trace context: Metrics are automatically associated with the current trace/span
  • Hook: before_send_metric callback for filtering or modifying metrics before sending
  • Unit constants: Pre-defined SENTRY_UNIT_* constants for common telemetry units (byte, kilobyte, millisecond, second, etc.)
  • Parallel flush/close: Logs and metrics flush and shutdown operations run in parallel for faster sentry_flush() and sentry_close()

Public API

// Enable metrics
sentry_options_set_enable_metrics(options, true);

// Record metrics
sentry_metrics_count("api.requests", 1, sentry_value_new_null());
sentry_metrics_gauge("memory.usage", 1024, SENTRY_UNIT_BYTE, sentry_value_new_null());
sentry_metrics_distribution("response.time", 150.5, SENTRY_UNIT_MILLISECOND, attributes);

Considerations

The current API requires passing sentry_value_new_null() when no custom attributes are needed. An alternative would be to provide simplified variants without the attributes parameter (e.g., sentry_metrics_count()) alongside explicit variants (e.g., sentry_metrics_count_with_attributes()). Feedback welcome on which approach is preferred.

Dependencies

Test plan

  • Unit tests for all metric types (tests/unit/test_metrics.c)
  • Integration tests for HTTP transport (tests/test_integration_http.py)
  • Tests for custom attributes, before_send_metric hook, and disabled state
  • Crash-safe flush behavior tested

🤖 Generated with Claude Code

jpnurmi and others added 3 commits January 29, 2026 16:01
@jpnurmi jpnurmi force-pushed the jpnurmi/feat/metrics branch from 422c6ad to 875c3d9 Compare January 29, 2026 15:01
@jpnurmi jpnurmi changed the title feat: metrics WIP: feat: metrics Jan 29, 2026
jpnurmi and others added 10 commits January 29, 2026 17:18
Per the Sentry metrics specification, counter metrics should use 64-bit
signed integers. This is consistent with sentry_value_new_int64 /
sentry_value_as_int64 in sentry.h.

Also refactored internal record_metric to pass sentry_value_t instead
of double, allowing proper type preservation.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
The value parameter was not being decremented when metrics were disabled,
causing a memory leak.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
SDKs "should offer constants or similar that help customers send in
units we support" as specified in the developer docs.

See:
- https://develop.sentry.dev/sdk/telemetry/metrics/
- https://develop.sentry.dev/sdk/telemetry/attributes/#units

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Per the developer docs, units are only used for distribution and gauge
metrics, not counters.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Split blocking shutdown and force_flush operations into begin (non-blocking
trigger) and wait (blocking completion) phases. This allows logs and metrics
operations to run in parallel in sentry_flush() and sentry_close().

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add metrics integration tests to match the existing logs test coverage:
- test_metrics_threaded: concurrent metrics from 50 threads
- test_metrics_global_and_local_attributes_merge: global + local attributes
- test_metrics_discarded_on_crash_no_backend: metrics discarded without backend
- test_metrics_on_crash: parameterized test for inproc/breakpad backends

Also refactor thread creation in example.c into a shared run_threads() helper
function used by both logs-threads and metrics-threads commands.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Allow reusing attribute objects across multiple log calls, as documented.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Global attributes set via sentry_set_attribute() were not being merged
into metrics. Now scope attributes are merged with user attributes
having priority on conflicts.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@jpnurmi jpnurmi force-pushed the jpnurmi/feat/metrics branch from 48664ff to 725b062 Compare January 30, 2026 15:14
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.

Sentry Metrics for Native

2 participants