Skip to content

Commit

Permalink
HPCC-33167 Add unique ID to each metric
Browse files Browse the repository at this point in the history
Added unique ID to each metric when constructed

Signed-Off-By: Kenneth Rowland kenneth.rowland@lexisnexisrisk.com
  • Loading branch information
kenrowland committed Jan 9, 2025
1 parent eb69d88 commit 05acba2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions system/jlib/jmetrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ MODULE_EXIT()
metricsManager.destroy();
}

// MetricBase static member
std::atomic<int> 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)
Expand Down
7 changes: 6 additions & 1 deletion system/jlib/jmetrics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}; }
int queryId() const { return myId; }


protected:
Expand All @@ -135,14 +136,18 @@ class jlib_decl MetricBase : public IMetric
description{_desc},
metricType{_metricType},
units{_units},
metaData{_metaData} { }
metaData{_metaData} { myId = ++metricId; }

protected:
std::string name;
std::string description;
MetricType metricType;
StatisticMeasure units;
MetricMetaData metaData;

private:
static std::atomic<int> metricId;
int myId;
};


Expand Down
26 changes: 26 additions & 0 deletions testing/unittests/metrics/MetricFrameworkTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -469,6 +470,31 @@ class MetricFrameworkTests : public CppUnit::TestFixture
CPPUNIT_ASSERT(result);
}

void Test_validate_unique_metric_ids()
{
std::shared_ptr<CounterMetric> pCounter1 = std::make_shared<CounterMetric>("testcounter1", "description", SMeasureCount);
auto initialMetricId1 = pCounter1->queryId();
std::shared_ptr<CounterMetric> pCounter2 = std::make_shared<CounterMetric>("testcounter2", "description", SMeasureCount);
auto initialMetricId2 = pCounter2->queryId();
std::shared_ptr<CounterMetric> pCounter3 = std::make_shared<CounterMetric>("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;
Expand Down

0 comments on commit 05acba2

Please sign in to comment.