diff options
Diffstat (limited to 'src/common/Metric/Metric.h')
-rw-r--r-- | src/common/Metric/Metric.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/common/Metric/Metric.h b/src/common/Metric/Metric.h index 5e843139354..5038338421c 100644 --- a/src/common/Metric/Metric.h +++ b/src/common/Metric/Metric.h @@ -90,6 +90,7 @@ private: static std::string FormatInfluxDBValue(char const* value); static std::string FormatInfluxDBValue(double value); static std::string FormatInfluxDBValue(float value); + static std::string FormatInfluxDBValue(std::chrono::nanoseconds value); static std::string FormatInfluxDBTagValue(std::string const& value); @@ -127,6 +128,34 @@ public: #define sMetric Metric::instance() + +template<typename LoggerType> +class MetricStopWatch +{ +public: + MetricStopWatch(LoggerType&& loggerFunc) : + _logger(std::forward<LoggerType>(loggerFunc)), + _startTime(sMetric->IsEnabled() ? std::chrono::steady_clock::now() : TimePoint()) + { + } + + ~MetricStopWatch() + { + if (sMetric->IsEnabled()) + _logger(_startTime); + } + +private: + LoggerType _logger; + TimePoint _startTime; +}; + +template<typename LoggerType> +MetricStopWatch<LoggerType> MakeMetricStopWatch(LoggerType&& loggerFunc) +{ + return { std::forward<LoggerType>(loggerFunc) }; +} + #define TC_METRIC_TAG(name, value) { name, value } #ifdef PERFORMANCE_PROFILING @@ -161,5 +190,10 @@ public: } while (0) \ __pragma(warning(pop)) #endif +#define TC_METRIC_TIMER(category, ...) \ + MetricStopWatch __tc_metric_stop_watch = MakeMetricStopWatch([&](TimePoint start) \ + { \ + sMetric->LogValue(category, std::chrono::steady_clock::now() - start, { __VA_ARGS__ }); \ + }); #endif // METRIC_H__ |