diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-03-01 08:10:29 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2023-03-01 17:03:02 +0000 |
commit | feba6c21c8ebe54c649789969b738fade4dbc939 (patch) | |
tree | fc0ca7ce54f375d7ac551cc99ce27f6b460699aa /metrics | |
parent | a763fc387a1444a425b630498fa9ab051ed8e400 (diff) |
Use a typed period.
Diffstat (limited to 'metrics')
-rw-r--r-- | metrics/src/tests/metricmanagertest.cpp | 61 | ||||
-rw-r--r-- | metrics/src/tests/snapshottest.cpp | 6 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/jsonwriter.cpp | 2 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/metricmanager.cpp | 46 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/metricmanager.h | 10 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/metricsnapshot.cpp | 11 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/metricsnapshot.h | 12 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/state_api_adapter.cpp | 16 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/textwriter.cpp | 6 | ||||
-rw-r--r-- | metrics/src/vespa/metrics/textwriter.h | 5 |
10 files changed, 87 insertions, 88 deletions
diff --git a/metrics/src/tests/metricmanagertest.cpp b/metrics/src/tests/metricmanagertest.cpp index 8417a68d780..a2f3e9cd853 100644 --- a/metrics/src/tests/metricmanagertest.cpp +++ b/metrics/src/tests/metricmanagertest.cpp @@ -411,14 +411,11 @@ std::string dumpAllSnapshots(const MetricManager& mm, const std::string& consume mm.visit(metricLock, mm.getTotalMetricSnapshot(metricLock), briefValuePrinter, consumer); ost << "Total: " << briefValuePrinter.ost.str() << "\n"; } - std::vector<uint32_t> periods; - { - MetricLockGuard metricLock(mm.getMetricLock()); - periods = mm.getSnapshotPeriods(metricLock); - } - for (uint32_t i=0; i<periods.size(); ++i) { - MetricLockGuard metricLock(mm.getMetricLock()); - const MetricSnapshotSet& set(mm.getMetricSnapshotSet(metricLock, periods[i])); + + MetricLockGuard metricLock(mm.getMetricLock()); + auto periods = mm.getSnapshotPeriods(metricLock); + for (vespalib::duration period : periods) { + const MetricSnapshotSet& set(mm.getMetricSnapshotSet(metricLock, period)); ost << set.getName() << "\n"; for (uint32_t count=0,j=0; j<2; ++j) { if (set.getCount() == 1 && j == 1) continue; @@ -438,9 +435,9 @@ std::string dumpAllSnapshots(const MetricManager& mm, const std::string& consume { \ MetricLockGuard lockGuard(mm.getMetricLock()); \ BriefValuePrinter briefValuePrinter; \ - if (period == -1) { \ + if (period < vespalib::duration::zero()) { \ mm.visit(lockGuard, mm.getActiveMetrics(lockGuard), briefValuePrinter, "snapper"); \ - } else if (period == 0) { \ + } else if (period == vespalib::duration::zero()) { \ mm.visit(lockGuard, mm.getTotalMetricSnapshot(lockGuard), briefValuePrinter, "snapper"); \ } else { \ mm.visit(lockGuard, mm.getMetricSnapshot(lockGuard, period), briefValuePrinter, "snapper"); \ @@ -495,7 +492,7 @@ TEST_F(MetricManagerTest, test_snapshots) } // Initially, there should be no metrics logged ASSERT_PROCESS_TIME(mm, 1000); - ASSERT_VALUES(mm, 5 * 60, ""); + ASSERT_VALUES(mm, 5 * 60s, ""); // Adding metrics done in first five minutes. mySet.val6.addValue(2); @@ -506,9 +503,9 @@ TEST_F(MetricManagerTest, test_snapshots) mySet.val10.b.val1.addValue(1); timer.add_time(5 * 60); ASSERT_PROCESS_TIME(mm, 1000 + 5 * 60); - ASSERT_VALUES(mm, 5 * 60, "2,4,4,1,7,9,1,1,8,2,10"); - ASSERT_VALUES(mm, 60 * 60, ""); - ASSERT_VALUES(mm, 0 * 60, "2,4,4,1,7,9,1,1,8,2,10"); + ASSERT_VALUES(mm, 5 * 60s, "2,4,4,1,7,9,1,1,8,2,10"); + ASSERT_VALUES(mm, 60 * 60s, ""); + ASSERT_VALUES(mm, 0 * 60s, "2,4,4,1,7,9,1,1,8,2,10"); // Adding metrics done in second five minute period. Total should // be updated to account for both @@ -520,17 +517,17 @@ TEST_F(MetricManagerTest, test_snapshots) mySet.val10.b.val1.addValue(2); timer.add_time(5 * 60); ASSERT_PROCESS_TIME(mm, 1000 + 5 * 60 * 2); - ASSERT_VALUES(mm, 5 * 60, "4,5,5,1,8,11,2,2,10,3,13"); - ASSERT_VALUES(mm, 60 * 60, ""); - ASSERT_VALUES(mm, 0 * 60, "4,5,5,2,8,11,2,2,10,3,13"); + ASSERT_VALUES(mm, 5 * 60s, "4,5,5,1,8,11,2,2,10,3,13"); + ASSERT_VALUES(mm, 60 * 60s, ""); + ASSERT_VALUES(mm, 0 * 60s, "4,5,5,2,8,11,2,2,10,3,13"); // Adding another five minute period where nothing have happened. // Metric for last 5 minutes should be 0. timer.add_time(5 * 60); ASSERT_PROCESS_TIME(mm, 1000 + 5 * 60 * 3); - ASSERT_VALUES(mm, 5 * 60, "0,0,0,0,0,0,0,0,0,0,0"); - ASSERT_VALUES(mm, 60 * 60, ""); - ASSERT_VALUES(mm, 0 * 60, "4,5,5,2,8,11,2,2,10,3,13"); + ASSERT_VALUES(mm, 5 * 60s, "0,0,0,0,0,0,0,0,0,0,0"); + ASSERT_VALUES(mm, 60 * 60s, ""); + ASSERT_VALUES(mm, 0 * 60s, "4,5,5,2,8,11,2,2,10,3,13"); // Advancing time to 60 minute period, we should create a proper // 60 minute period timer. @@ -540,16 +537,16 @@ TEST_F(MetricManagerTest, test_snapshots) timer.add_time(5 * 60); ASSERT_PROCESS_TIME(mm, 1000 + 5 * 60 * (4 + i)); } - ASSERT_VALUES(mm, 5 * 60, "0,0,0,0,0,0,0,0,0,0,0"); - ASSERT_VALUES(mm, 60 * 60, "6,5,5,2,8,11,2,2,10,3,13"); - ASSERT_VALUES(mm, 0 * 60, "6,5,5,2,8,11,2,2,10,3,13"); + ASSERT_VALUES(mm, 5 * 60s, "0,0,0,0,0,0,0,0,0,0,0"); + ASSERT_VALUES(mm, 60 * 60s, "6,5,5,2,8,11,2,2,10,3,13"); + ASSERT_VALUES(mm, 0 * 60s, "6,5,5,2,8,11,2,2,10,3,13"); // Test that reset works mm.reset(system_time(1000s)); - ASSERT_VALUES(mm, -1, "0,0,0,0,0,0,0,0,0,0,0"); - ASSERT_VALUES(mm, 5 * 60, "0,0,0,0,0,0,0,0,0,0,0"); - ASSERT_VALUES(mm, 60 * 60, "0,0,0,0,0,0,0,0,0,0,0"); - ASSERT_VALUES(mm, 0 * 60, "0,0,0,0,0,0,0,0,0,0,0"); + ASSERT_VALUES(mm, -1s, "0,0,0,0,0,0,0,0,0,0,0"); + ASSERT_VALUES(mm, 5 * 60s, "0,0,0,0,0,0,0,0,0,0,0"); + ASSERT_VALUES(mm, 60 * 60s, "0,0,0,0,0,0,0,0,0,0,0"); + ASSERT_VALUES(mm, 0 * 60s, "0,0,0,0,0,0,0,0,0,0,0"); } TEST_F(MetricManagerTest, test_json_output) @@ -589,7 +586,7 @@ TEST_F(MetricManagerTest, test_json_output) JsonWriter writer(jsonStream); { MetricLockGuard lockGuard(mm.getMetricLock()); - mm.visit(lockGuard, mm.getMetricSnapshot(lockGuard, 300, false), writer, "snapper"); + mm.visit(lockGuard, mm.getMetricSnapshot(lockGuard, 300s, false), writer, "snapper"); } jsonStream.finalize(); std::string jsonData = as.str(); @@ -678,7 +675,7 @@ struct MetricSnapshotTestFixture JsonWriter writer(jsonStream); { MetricLockGuard lockGuard(manager.getMetricLock()); - manager.visit(lockGuard, manager.getMetricSnapshot(lockGuard, 300, false), writer, "snapper"); + manager.visit(lockGuard, manager.getMetricSnapshot(lockGuard, 300s, false), writer, "snapper"); } jsonStream.finalize(); return as.str(); @@ -687,10 +684,10 @@ struct MetricSnapshotTestFixture std::string renderLastSnapshotAsText(const std::string& matchPattern = ".*") const { std::ostringstream ss; - TextWriter writer(ss, 300, matchPattern, true); + TextWriter writer(ss, 300s, matchPattern, true); { MetricLockGuard lockGuard(manager.getMetricLock()); - manager.visit(lockGuard, manager.getMetricSnapshot(lockGuard, 300, false), writer, "snapper"); + manager.visit(lockGuard, manager.getMetricSnapshot(lockGuard, 300s, false), writer, "snapper"); } return ss.str(); } @@ -913,7 +910,7 @@ TEST_F(MetricManagerTest, test_text_output) "temp.multisub.sum.val2 average=2 last=2 min=2 max=2 count=1 total=2\n" "temp.multisub.sum.valsum average=10 last=10"); std::ostringstream ost; - TextWriter writer(ost, 300, ".*", true); + TextWriter writer(ost, 300s, ".*", true); { MetricLockGuard lockGuard(mm.getMetricLock()); mm.visit(lockGuard, mm.getActiveMetrics(lockGuard), writer, "snapper"); diff --git a/metrics/src/tests/snapshottest.cpp b/metrics/src/tests/snapshottest.cpp index 52bcaf600ca..5561825e2ba 100644 --- a/metrics/src/tests/snapshottest.cpp +++ b/metrics/src/tests/snapshottest.cpp @@ -220,7 +220,7 @@ TEST_F(SnapshotTest, test_snapshot_two_days) ASSERT_VALUE(0, *snap, "test.set1.set1.countSum"); // 5 minute snapshot - snap = &mm.getMetricSnapshot(lockGuard, 5 * 60); + snap = &mm.getMetricSnapshot(lockGuard, 5 * 60s); ASSERT_VALUE(1, *snap, "test.set1.set1.count1"); ASSERT_VALUE(2, *snap, "test.set1.set1.countSum"); @@ -228,7 +228,7 @@ TEST_F(SnapshotTest, test_snapshot_two_days) ASSERT_VALUE(1, *snap, "test.set1.set1.averageSum"); // 1 hour snapshot - snap = &mm.getMetricSnapshot(lockGuard, 60 * 60); + snap = &mm.getMetricSnapshot(lockGuard, 60 * 60s); ASSERT_VALUE(12, *snap, "test.set1.set1.count1"); ASSERT_VALUE(24, *snap, "test.set1.set1.countSum"); @@ -236,7 +236,7 @@ TEST_F(SnapshotTest, test_snapshot_two_days) ASSERT_VALUE(1, *snap, "test.set1.set1.averageSum"); // 1 day snapshot - snap = &mm.getMetricSnapshot(lockGuard, 24 * 60 * 60); + snap = &mm.getMetricSnapshot(lockGuard, 24 * 60 * 60s); ASSERT_VALUE(288, *snap, "test.set1.set1.count1"); ASSERT_VALUE(576, *snap, "test.set1.set1.countSum"); diff --git a/metrics/src/vespa/metrics/jsonwriter.cpp b/metrics/src/vespa/metrics/jsonwriter.cpp index 0c9ed306ec9..1b9df3988e1 100644 --- a/metrics/src/vespa/metrics/jsonwriter.cpp +++ b/metrics/src/vespa/metrics/jsonwriter.cpp @@ -28,7 +28,7 @@ JsonWriter::visitSnapshot(const MetricSnapshot& snapshot) << End() << "values" << Array(); _flag = SNAPSHOT_STARTED; - _period = snapshot.getPeriod(); + _period = vespalib::count_s(snapshot.getPeriod()); // Only prints second resolution return true; } diff --git a/metrics/src/vespa/metrics/metricmanager.cpp b/metrics/src/vespa/metrics/metricmanager.cpp index 3ba3837bf19..13bdf18a194 100644 --- a/metrics/src/vespa/metrics/metricmanager.cpp +++ b/metrics/src/vespa/metrics/metricmanager.cpp @@ -27,6 +27,7 @@ using vespalib::make_string_short::fmt; using vespalib::count_ms; using vespalib::count_s; using vespalib::from_s; +using vespalib::to_s; using vespalib::to_string; MetricManager::ConsumerSpec::ConsumerSpec() = default; @@ -80,7 +81,7 @@ MetricManager::MetricManager(std::unique_ptr<Timer> timer) _config(), _consumerConfig(), _snapshots(), - _totalMetrics(std::make_shared<MetricSnapshot>("Empty metrics before init", 0, _activeMetrics.getMetrics(), false)), + _totalMetrics(std::make_shared<MetricSnapshot>("Empty metrics before init", 0s, _activeMetrics.getMetrics(), false)), _timer(std::move(timer)), _lastProcessedTime(0), _snapshotUnsetMetrics(false), @@ -425,10 +426,10 @@ MetricManager::createSnapshotPeriods(const Config& config) } else { name << length << " seconds"; } - result.push_back(SnapSpec(length, name.str())); + result.emplace_back(vespalib::from_s(length), name.str()); } for (uint32_t i=1; i<result.size(); ++i) { - if (result[i].first % result[i-1].first != 0) { + if (result[i].first % result[i-1].first != vespalib::duration::zero()) { std::ostringstream ost; ost << "Period " << result[i].first << " is not a multiplum of period " << result[i-1].first << " which is needs to be."; @@ -440,10 +441,10 @@ MetricManager::createSnapshotPeriods(const Config& config) result.clear(); } if (result.empty()) { - result.push_back(SnapSpec(60 * 5, "5 minute")); - result.push_back(SnapSpec(60 * 60, "1 hour")); - result.push_back(SnapSpec(60 * 60 * 24, "1 day")); - result.push_back(SnapSpec(60 * 60 * 24 * 7, "1 week")); + result.emplace_back(60s * 5, "5 minute"); + result.emplace_back(60s * 60, "1 hour"); + result.emplace_back(60s * 60 * 24, "1 day"); + result.emplace_back(60s * 60 * 24 * 7, "1 week"); } return result; } @@ -471,7 +472,7 @@ MetricManager::configure(const MetricLockGuard & , std::unique_ptr<Config> confi uint32_t nextCount = 1; if (i + 1 < snapshotPeriods.size()) { nextCount = snapshotPeriods[i + 1].first / snapshotPeriods[i].first; - if ((snapshotPeriods[i + 1].first % snapshotPeriods[i].first) != 0) { + if ((snapshotPeriods[i + 1].first % snapshotPeriods[i].first) != vespalib::duration::zero()) { throw IllegalStateException("Snapshot periods must be multiplum of each other",VESPA_STRLOC); } } @@ -481,7 +482,7 @@ MetricManager::configure(const MetricLockGuard & , std::unique_ptr<Config> confi count = nextCount; } // Add all time snapshot. - _totalMetrics = std::make_shared<MetricSnapshot>("All time snapshot", 0, _activeMetrics.getMetrics(), _snapshotUnsetMetrics); + _totalMetrics = std::make_shared<MetricSnapshot>("All time snapshot", 0s, _activeMetrics.getMetrics(), _snapshotUnsetMetrics); _totalMetrics->reset(currentTime); } if (_config.get() == 0 || (_config->consumer.size() != config->consumer.size())) { @@ -596,20 +597,21 @@ MetricManager::visit(const MetricLockGuard & guard, const MetricSnapshot& snapsh visitor.doneVisiting(); } -std::vector<uint32_t> +std::vector<vespalib::duration> MetricManager::getSnapshotPeriods(const MetricLockGuard& l) const { assertMetricLockLocked(l); - std::vector<uint32_t> result(_snapshots.size()); - for (uint32_t i=0; i<_snapshots.size(); ++i) { - result[i] = _snapshots[i]->getPeriod(); + std::vector<vespalib::duration> result; + result.reserve(_snapshots.size()); + for (const auto & snapshot : _snapshots) { + result.emplace_back(snapshot->getPeriod()); } return result; } // Client should have grabbed metrics lock before doing this const MetricSnapshot& -MetricManager::getMetricSnapshot(const MetricLockGuard& l, uint32_t period, bool getInProgressSet) const +MetricManager::getMetricSnapshot(const MetricLockGuard& l, vespalib::duration period, bool getInProgressSet) const { assertMetricLockLocked(l); for (const auto & snapshot : _snapshots) { @@ -620,12 +622,12 @@ MetricManager::getMetricSnapshot(const MetricLockGuard& l, uint32_t period, bool return snapshot->getSnapshot(getInProgressSet); } } - throw IllegalArgumentException(fmt("No snapshot for period of length %u exist.", period), VESPA_STRLOC); + throw IllegalArgumentException(fmt("No snapshot for period of length %f exist.", vespalib::to_s(period)), VESPA_STRLOC); } // Client should have grabbed metrics lock before doing this const MetricSnapshotSet& -MetricManager::getMetricSnapshotSet(const MetricLockGuard& l, uint32_t period) const +MetricManager::getMetricSnapshotSet(const MetricLockGuard& l, vespalib::duration period) const { assertMetricLockLocked(l); for (const auto & snapshot : _snapshots) { @@ -633,7 +635,7 @@ MetricManager::getMetricSnapshotSet(const MetricLockGuard& l, uint32_t period) c return *snapshot; } } - throw IllegalArgumentException(fmt("No snapshot set for period of length %u exist.", period), VESPA_STRLOC); + throw IllegalArgumentException(fmt("No snapshot set for period of length %f exist.", to_s(period)), VESPA_STRLOC); } void @@ -806,9 +808,9 @@ MetricManager::takeSnapshots(const MetricLockGuard & guard, system_time timeToPr // If not time to do dump data from active snapshot yet, nothing to do if (!_snapshots[0]->timeForAnotherSnapshot(timeToProcess)) { LOG(spam, "Not time to process snapshot %s at time %s. Current " - "first period (%u) snapshot goes from %s to %s", + "first period (%f) snapshot goes from %s to %s", _snapshots[0]->getName().c_str(), to_string(timeToProcess).c_str(), - _snapshots[0]->getPeriod(), to_string(_snapshots[0]->getFromTime()).c_str(), + to_s(_snapshots[0]->getPeriod()), to_string(_snapshots[0]->getFromTime()).c_str(), to_string(_snapshots[0]->getToTime()).c_str()); return; } @@ -832,11 +834,11 @@ MetricManager::takeSnapshots(const MetricLockGuard & guard, system_time timeToPr _snapshots[i-1]->getSnapshot().addToSnapshot(target, false, timeToProcess); target.setToTime(timeToProcess); if (!_snapshots[i]->haveCompletedNewPeriod(timeToProcess)) { - LOG(debug, "Not time to roll snapshot %s yet. %u of %u snapshot taken at time %s, and period of %u " + LOG(debug, "Not time to roll snapshot %s yet. %u of %u snapshot taken at time %s, and period of %f " "is not up yet as we're currently processing for time %s.", _snapshots[i]->getName().c_str(), _snapshots[i]->getBuilderCount(), _snapshots[i]->getCount(), - to_string(_snapshots[i]->getBuilderCount() * from_s(_snapshots[i]->getPeriod()) + _snapshots[i]->getFromTime()).c_str(), - _snapshots[i]->getPeriod(), to_string(timeToProcess).c_str()); + to_string(_snapshots[i]->getBuilderCount() * _snapshots[i]->getPeriod() + _snapshots[i]->getFromTime()).c_str(), + to_s(_snapshots[i]->getPeriod()), to_string(timeToProcess).c_str()); break; } else { LOG(debug, "Rolled snapshot %s at time %s.", diff --git a/metrics/src/vespa/metrics/metricmanager.h b/metrics/src/vespa/metrics/metricmanager.h index 82a6523b18b..357c58043f4 100644 --- a/metrics/src/vespa/metrics/metricmanager.h +++ b/metrics/src/vespa/metrics/metricmanager.h @@ -237,13 +237,13 @@ public: return *_totalMetrics; } /** While accessing snapshots you should have the metric lock. */ - const MetricSnapshot& getMetricSnapshot( const MetricLockGuard& guard, uint32_t period) const { + const MetricSnapshot& getMetricSnapshot( const MetricLockGuard& guard, vespalib::duration period) const { return getMetricSnapshot(guard, period, false); } - const MetricSnapshot& getMetricSnapshot( const MetricLockGuard&, uint32_t period, bool getInProgressSet) const; - const MetricSnapshotSet& getMetricSnapshotSet(const MetricLockGuard&, uint32_t period) const; + const MetricSnapshot& getMetricSnapshot( const MetricLockGuard&, vespalib::duration period, bool getInProgressSet) const; + const MetricSnapshotSet& getMetricSnapshotSet(const MetricLockGuard&, vespalib::duration period) const; - std::vector<uint32_t> getSnapshotPeriods(const MetricLockGuard& l) const; + std::vector<vespalib::duration> getSnapshotPeriods(const MetricLockGuard& l) const; // Public only for testing. The returned pointer is only valid while holding the lock. const ConsumerSpec * getConsumerSpec(const MetricLockGuard & guard, const Metric::String& consumer) const; @@ -290,7 +290,7 @@ private: void handleMetricsAltered(const MetricLockGuard & guard); - using SnapSpec = std::pair<uint32_t, std::string>; + using SnapSpec = std::pair<vespalib::duration, std::string>; static std::vector<SnapSpec> createSnapshotPeriods( const MetricsmanagerConfig& config); void assertMetricLockLocked(const MetricLockGuard& g) const; }; diff --git a/metrics/src/vespa/metrics/metricsnapshot.cpp b/metrics/src/vespa/metrics/metricsnapshot.cpp index 2b2d73b6117..cd06fb731c2 100644 --- a/metrics/src/vespa/metrics/metricsnapshot.cpp +++ b/metrics/src/vespa/metrics/metricsnapshot.cpp @@ -7,6 +7,7 @@ LOG_SETUP(".metrics.snapshot"); using vespalib::to_string; +using vespalib::to_s; namespace metrics { @@ -23,7 +24,7 @@ MetricSnapshot::MetricSnapshot(const Metric::String& name) { } -MetricSnapshot::MetricSnapshot(const Metric::String& name, uint32_t period, const MetricSet& source, bool copyUnset) +MetricSnapshot::MetricSnapshot(const Metric::String& name, vespalib::duration period, const MetricSet& source, bool copyUnset) : _name(name), _period(period), _fromTime(system_time_epoch), @@ -72,7 +73,7 @@ MetricSnapshot::addMemoryUsage(MemoryConsumption& mc) const _snapshot->addMemoryUsage(mc); } -MetricSnapshotSet::MetricSnapshotSet(const Metric::String& name, uint32_t period, uint32_t count, +MetricSnapshotSet::MetricSnapshotSet(const Metric::String& name, vespalib::duration period, uint32_t count, const MetricSet& source, bool snapshotUnsetMetrics) : _count(count), _builderCount(0), @@ -110,13 +111,13 @@ MetricSnapshotSet::haveCompletedNewPeriod(system_time newFromTime) bool MetricSnapshotSet::timeForAnotherSnapshot(system_time currentTime) { system_time lastTime = getToTime(); - vespalib::duration period = vespalib::from_s(getPeriod()); + vespalib::duration period = getPeriod(); if (currentTime >= lastTime + period) { if (currentTime >= lastTime + 2 * period) { LOG(warning, "Metric snapshot set %s was asked if it was time for another snapshot, a whole period beyond " "when it should have been done (Last update was at time %s, current time is %s and period " - "is %u). Clearing data and updating time to current time.", - getName().c_str(), to_string(lastTime).c_str(), to_string(currentTime).c_str(), getPeriod()); + "is %f seconds). Clearing data and updating time to current time.", + getName().c_str(), to_string(lastTime).c_str(), to_string(currentTime).c_str(), to_s(getPeriod())); reset(currentTime); } return true; diff --git a/metrics/src/vespa/metrics/metricsnapshot.h b/metrics/src/vespa/metrics/metricsnapshot.h index 08d22c3c588..945f9dc7326 100644 --- a/metrics/src/vespa/metrics/metricsnapshot.h +++ b/metrics/src/vespa/metrics/metricsnapshot.h @@ -23,7 +23,7 @@ class MetricSnapshot { Metric::String _name; // Period length of this snapshot - uint32_t _period; + vespalib::duration _period; // Time this snapshot was last updated. system_time _fromTime; // If set to 0, use _fromTime + _period. @@ -37,7 +37,7 @@ public: /** Create a fresh empty top level snapshot. */ MetricSnapshot(const Metric::String& name); /** Create a snapshot of another metric source. */ - MetricSnapshot(const Metric::String& name, uint32_t period, + MetricSnapshot(const Metric::String& name, vespalib::duration period, const MetricSet& source, bool copyUnset); ~MetricSnapshot(); @@ -54,7 +54,7 @@ public: void setToTime(system_time toTime) { _toTime = toTime; } const Metric::String& getName() const { return _name; } - uint32_t getPeriod() const { return _period; } + vespalib::duration getPeriod() const { return _period; } system_time getFromTime() const { return _fromTime; } system_time getToTime() const { return _toTime; } const MetricSet& getMetrics() const { return *_snapshot; } @@ -78,14 +78,14 @@ class MetricSnapshotSet { std::unique_ptr<MetricSnapshot> _current; // The last full period std::unique_ptr<MetricSnapshot> _building; // The building period public: - MetricSnapshotSet(const Metric::String& name, uint32_t period, uint32_t count, + MetricSnapshotSet(const Metric::String& name, vespalib::duration period, uint32_t count, const MetricSet& source, bool snapshotUnsetMetrics); const Metric::String& getName() const { return _current->getName(); } - uint32_t getPeriod() const { return _current->getPeriod(); } + vespalib::duration getPeriod() const { return _current->getPeriod(); } system_time getFromTime() const { return _current->getFromTime(); } system_time getToTime() const { return _current->getToTime(); } - system_time getNextWorkTime() const { return getToTime() + vespalib::from_s(getPeriod()); } + system_time getNextWorkTime() const { return getToTime() + getPeriod(); } uint32_t getCount() const { return _count; } uint32_t getBuilderCount() const { return _builderCount; } MetricSnapshot& getSnapshot() { diff --git a/metrics/src/vespa/metrics/state_api_adapter.cpp b/metrics/src/vespa/metrics/state_api_adapter.cpp index 61de02d3b71..136ccf6e06a 100644 --- a/metrics/src/vespa/metrics/state_api_adapter.cpp +++ b/metrics/src/vespa/metrics/state_api_adapter.cpp @@ -9,12 +9,12 @@ namespace metrics { vespalib::string StateApiAdapter::getMetrics(const vespalib::string &consumer) { - metrics::MetricLockGuard guard(_manager.getMetricLock()); - std::vector<uint32_t> periods = _manager.getSnapshotPeriods(guard); + MetricLockGuard guard(_manager.getMetricLock()); + auto periods = _manager.getSnapshotPeriods(guard); if (periods.empty()) { return ""; // no configuration yet } - const metrics::MetricSnapshot &snapshot(_manager.getMetricSnapshot(guard, periods[0])); + const MetricSnapshot &snapshot(_manager.getMetricSnapshot(guard, periods[0])); vespalib::asciistream json; vespalib::JsonStream stream(json); metrics::JsonWriter metricJsonWriter(stream); @@ -27,16 +27,14 @@ vespalib::string StateApiAdapter::getTotalMetrics(const vespalib::string &consumer) { _manager.updateMetrics(); - metrics::MetricLockGuard guard(_manager.getMetricLock()); + MetricLockGuard guard(_manager.getMetricLock()); _manager.checkMetricsAltered(guard); system_time currentTime = vespalib::system_clock::now(); - auto generated = std::make_unique<metrics::MetricSnapshot>( - "Total metrics from start until current time", 0, - _manager.getTotalMetricSnapshot(guard).getMetrics(), - true); + auto generated = std::make_unique<MetricSnapshot>("Total metrics from start until current time", 0s, + _manager.getTotalMetricSnapshot(guard).getMetrics(), true); _manager.getActiveMetrics(guard).addToSnapshot(*generated, false, currentTime); generated->setFromTime(_manager.getTotalMetricSnapshot(guard).getFromTime()); - const metrics::MetricSnapshot &snapshot = *generated; + const MetricSnapshot &snapshot = *generated; vespalib::asciistream json; vespalib::JsonStream stream(json); metrics::JsonWriter metricJsonWriter(stream); diff --git a/metrics/src/vespa/metrics/textwriter.cpp b/metrics/src/vespa/metrics/textwriter.cpp index cc9a68f2b3d..fbb31e7013a 100644 --- a/metrics/src/vespa/metrics/textwriter.cpp +++ b/metrics/src/vespa/metrics/textwriter.cpp @@ -11,7 +11,7 @@ using vespalib::to_string; namespace metrics { -TextWriter::TextWriter(std::ostream& out, uint32_t period, +TextWriter::TextWriter(std::ostream& out, vespalib::duration period, const std::string& regex, bool verbose) : _period(period), _out(out), _regex(), _verbose(verbose) { @@ -28,7 +28,7 @@ TextWriter::visitSnapshot(const MetricSnapshot& snapshot) { _out << "snapshot \"" << snapshot.getName() << "\" from " << to_string(snapshot.getFromTime()) << " to " << to_string(snapshot.getToTime()) - << " period " << snapshot.getPeriod(); + << " period " << vespalib::count_s(snapshot.getPeriod()); return true; } @@ -84,7 +84,7 @@ bool TextWriter::visitValueMetric(const AbstractValueMetric& m, bool) { if (writeCommon(m)) { - m.print(_out, _verbose, " ", _period); + m.print(_out, _verbose, " ", vespalib::count_s(_period)); } return true; } diff --git a/metrics/src/vespa/metrics/textwriter.h b/metrics/src/vespa/metrics/textwriter.h index f060429e931..f23d1cf585c 100644 --- a/metrics/src/vespa/metrics/textwriter.h +++ b/metrics/src/vespa/metrics/textwriter.h @@ -3,20 +3,21 @@ #pragma once #include "metric.h" +#include <vespa/vespalib/util/time.h> #include <regex> #include <optional> namespace metrics { class TextWriter : public MetricVisitor { - uint32_t _period; + vespalib::duration _period; std::ostream& _out; std::vector<std::string> _path; std::optional<std::regex> _regex; bool _verbose; public: - TextWriter(std::ostream& out, uint32_t period, + TextWriter(std::ostream& out, vespalib::duration period, const std::string& regex, bool verbose); ~TextWriter(); |