Skip to content

fix: Pass metric_prefix to Aggregator finalizer#416

Merged
kelhusseiny merged 3 commits intomainfrom
karim/fix-finalizer-missing-prefix
Feb 9, 2026
Merged

fix: Pass metric_prefix to Aggregator finalizer#416
kelhusseiny merged 3 commits intomainfrom
karim/fix-finalizer-missing-prefix

Conversation

@kelhusseiny
Copy link
Member

Summary

Fixes a bug where metric_prefix was not passed to the Finalizer struct, causing metrics to lose their prefix when flushed during garbage collection.

Root Cause

The Finalizer struct expects 6 arguments:

Finalizer = Struct.new(:aggregation_state, :sink, :datagram_builders, :datagram_builder_class, :default_tags, :metric_prefix)

But only 5 were passed:

@finalizer = Finalizer.new(
  @aggregation_state,
  @sink,
  @datagram_builders,
  @datagram_builder_class,
  @default_tags,
  # metric_prefix was missing!
)

This left metric_prefix as nil, so when the finalizer flushed metrics during GC, they would be emitted without the configured prefix.

When the bug manifests

The bug only occurs when all these conditions are met:

  1. Aggregator is created with a prefix (e.g., "MyApp")
  2. Metrics are added without tags (tags trigger datagram_builder creation via tags_sorted)
  3. GC runs before normal flush, triggering the finalizer

When tags are present, tags_sorted creates the datagram_builder with the correct prefix, masking the bug.

Test plan

  • Added test_finalizer_with_prefix that verifies the finalizer preserves metric prefix
  • Test uses empty tags to ensure datagram_builder is not pre-created
  • Test fails without fix, passes with fix
  • All existing tests pass (370 runs, 932 assertions)

The Finalizer struct expects 6 arguments but only 5 were passed,
leaving metric_prefix as nil. This caused metrics to lose their
prefix when flushed during garbage collection.

The bug only manifests when:
1. Aggregator is created with a prefix
2. Metrics are added without tags (tags trigger datagram_builder creation)
3. GC runs before normal flush, triggering the finalizer
@kelhusseiny kelhusseiny marked this pull request as ready for review February 9, 2026 14:41
@kelhusseiny kelhusseiny merged commit 7258526 into main Feb 9, 2026
11 checks passed
@kelhusseiny kelhusseiny deleted the karim/fix-finalizer-missing-prefix branch February 9, 2026 15:11
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.

2 participants