diff --git a/system/jlib/jmetrics.cpp b/system/jlib/jmetrics.cpp index c32d86049dd..14df3250e6a 100644 --- a/system/jlib/jmetrics.cpp +++ b/system/jlib/jmetrics.cpp @@ -28,6 +28,8 @@ MODULE_EXIT() metricsManager.destroy(); } +// MetricBase static member +std::atomic MetricBase::metricId = 0; HistogramMetric::HistogramMetric(const char *_name, const char *_desc, StatisticMeasure _units, const std::vector<__uint64> &_bucketLimits, const MetricMetaData &_metaData) : MetricBase(_name, _desc, MetricType::METRICS_HISTOGRAM, _units, _metaData) diff --git a/system/jlib/jmetrics.hpp b/system/jlib/jmetrics.hpp index 75ead51652d..236b64176ee 100644 --- a/system/jlib/jmetrics.hpp +++ b/system/jlib/jmetrics.hpp @@ -126,6 +126,7 @@ class jlib_decl MetricBase : public IMetric StatisticMeasure queryUnits() const override { return units; } virtual std::vector<__uint64> queryHistogramValues() const override { return {}; } virtual std::vector<__uint64> queryHistogramBucketLimits() const override { return {}; } + unsigned int queryId() const { return myId; } protected: @@ -135,7 +136,7 @@ class jlib_decl MetricBase : public IMetric description{_desc}, metricType{_metricType}, units{_units}, - metaData{_metaData} { } + metaData{_metaData} { myId = ++metricId; } protected: std::string name; @@ -143,6 +144,10 @@ class jlib_decl MetricBase : public IMetric MetricType metricType; StatisticMeasure units; MetricMetaData metaData; + +private: + static std::atomic metricId; + unsigned int myId; }; diff --git a/testing/unittests/metrics/MetricFrameworkTests.cpp b/testing/unittests/metrics/MetricFrameworkTests.cpp index daa1e1bdf65..1b4cadd42a9 100644 --- a/testing/unittests/metrics/MetricFrameworkTests.cpp +++ b/testing/unittests/metrics/MetricFrameworkTests.cpp @@ -76,6 +76,7 @@ class MetricFrameworkTests : public CppUnit::TestFixture CPPUNIT_TEST(Test_metric_meta_data); CPPUNIT_TEST(Test_gauge_by_counters_metric); CPPUNIT_TEST(Test_histogram_metric); + CPPUNIT_TEST(Test_validate_unique_metric_ids); CPPUNIT_TEST_SUITE_END(); protected: @@ -469,6 +470,31 @@ class MetricFrameworkTests : public CppUnit::TestFixture CPPUNIT_ASSERT(result); } + void Test_validate_unique_metric_ids() + { + std::shared_ptr pCounter1 = std::make_shared("testcounter1", "description", SMeasureCount); + auto initialMetricId1 = pCounter1->queryId(); + std::shared_ptr pCounter2 = std::make_shared("testcounter2", "description", SMeasureCount); + auto initialMetricId2 = pCounter2->queryId(); + std::shared_ptr pCounter3 = std::make_shared("testcounter3", "description", SMeasureCount); + auto initialMetricId3 = pCounter3->queryId(); + + // make sure initial id query values all different + bool different = initialMetricId1 != initialMetricId2 && initialMetricId1 != initialMetricId3 && initialMetricId2 != initialMetricId3; + CPPUNIT_ASSERT(different); + + // requery values since some were queried before additional metrics allocated + auto afterMetricId1 = pCounter1->queryId(); + auto afterMetricId2 = pCounter2->queryId(); + auto afterMetricId3 = pCounter3->queryId(); + + bool different2 = afterMetricId1 != afterMetricId2 && afterMetricId1 != afterMetricId3 && afterMetricId2 != afterMetricId3; + CPPUNIT_ASSERT(different2); + + bool unchanged = initialMetricId1 == afterMetricId1 && initialMetricId2 == afterMetricId2 && initialMetricId3 == afterMetricId3; + CPPUNIT_ASSERT(unchanged); + } + protected: MetricsManager frameworkTestManager; MetricFrameworkTestSink *pTestSink;