Skip to content

identical instruments are not aggregated by the SDK #3356

@dbarker

Description

@dbarker

Describe your environment
main at 4d52ab2

Steps to reproduce

  • Create a meter
  • Create a counter "my_counter"
  • Create a second instance of "my_counter"
  • Add 1 to both with different attributes

Example test (added to meter_test.cc):

TEST(MeterTest, IdenticalInstrumentTests)
{
  MetricReader *metric_reader_ptr = nullptr;
  auto meter                      = InitMeter(&metric_reader_ptr);
  auto counter1         = meter->CreateUInt64Counter("my_counter");
  auto counter2         = meter->CreateUInt64Counter("my_counter");

  counter1->Add(1, {{"key", "value1"}});
  counter2->Add(1, {{"key", "value2"}});
   
  size_t count = 0;
  metric_reader_ptr->Collect([&count](ResourceMetrics &metric_data) {
    EXPECT_EQ(metric_data.scope_metric_data_.size(), 1);
    EXPECT_EQ(metric_data.scope_metric_data_[0].metric_data_.size(), 1);
    EXPECT_EQ(metric_data.scope_metric_data_[0].metric_data_[0].point_data_attr_.size(), 2);
    return true;
  });
}

What is the expected behavior?
The test passes. Metric streams are created and exported for each attribute set of the counter.

What is the actual behavior?
The first counter instance's count is not exported and the test fails with no warnings or errors from the internal logger.

320: Note: Google Test filter = MeterTest.IdenticalInstrumentTests
320: [==========] Running 1 test from 1 test suite.
320: [----------] Global test environment set-up.
320: [----------] 1 test from MeterTest
320: [ RUN      ] MeterTest.IdenticalInstrumentTests
320: /workspaces/opentelemetry-cpp/sdk/test/metrics/meter_test.cc:391: Failure
320: Expected equality of these values:
320:   metric_data.scope_metric_data_[0].metric_data_[0].point_data_attr_.size()
320:     Which is: 1
320:   2
320: 
320: [  FAILED  ] MeterTest.IdenticalInstrumentTests (1 ms)
320: [----------] 1 test from MeterTest (1 ms total)
320: 
320: [----------] Global test environment tear-down
320: [==========] 1 test from 1 test suite ran. (1 ms total)
320: [  PASSED  ] 0 tests.
320: [  FAILED  ] 1 test, listed below:
320: [  FAILED  ] MeterTest.IdenticalInstrumentTests
320: 
320:  1 FAILED TEST
1/1 Test #320: metrics.MeterTest.IdenticalInstrumentTests ...***Failed    0.00 sec

Additional context
The spec defines identical instruments as:

The term _identical_ applied to
Instruments describes instances where all [identifying
fields](./api.md#instrument) are equal.  The term _distinct_ applied
to Instruments describes instances where at least one field value is
different.

The spec requires identical instruments to be aggregated and exported by the sdk.
See: sdk.md#L905

The meter has a map storage_registry_ that uses the instrument name only as a key. When a new sync instrument is created the storage registry entry for the key is set even if the key exists. See meter.cc#L499

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage/acceptedIndicates an issue or PR is ready to be actively worked on.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions