diff --git a/lib/perfdata/otlpmetricswriter.cpp b/lib/perfdata/otlpmetricswriter.cpp index 749dba930..a47bb661d 100644 --- a/lib/perfdata/otlpmetricswriter.cpp +++ b/lib/perfdata/otlpmetricswriter.cpp @@ -27,8 +27,8 @@ REGISTER_STATSFUNCTION(OTLPMetricsWriter, &OTLPMetricsWriter::StatsFunc); // // [^1]: https://opentelemetry.io/docs/specs/semconv/general/metrics/#general-guidelines // [^2]: https://opentelemetry.io/docs/specs/semconv/general/naming -static const String l_PerfdataMetric = "state_check.perfdata"; -static const String l_ThresholdMetric = "state_check.threshold"; +static constexpr std::string_view l_PerfdataMetric = "state_check.perfdata"; +static constexpr std::string_view l_ThresholdMetric = "state_check.threshold"; void OTLPMetricsWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr& perfdata) { @@ -199,7 +199,7 @@ void OTLPMetricsWriter::CheckResultHandler(const Checkable::Ptr& checkable, cons attrs.emplace("unit", std::move(unit)); } AddBytesAndFlushIfNeeded( - Record(checkable, l_PerfdataMetric, pdv->GetValue(), startTime, endTime, std::move(attrs)) + Record(checkable, Metric::Perfdata, pdv->GetValue(), startTime, endTime, std::move(attrs)) ); if (GetEnableSendThresholds()) { @@ -218,7 +218,7 @@ void OTLPMetricsWriter::CheckResultHandler(const Checkable::Ptr& checkable, cons AddBytesAndFlushIfNeeded( Record( checkable, - l_ThresholdMetric, + Metric::Threshold, Convert::ToDouble(threshold), startTime, endTime, @@ -255,7 +255,7 @@ void OTLPMetricsWriter::Flush(bool fromTimer) continue; } auto metric = sm->add_metrics(); - metric->set_name(metricName); + metric->set_name(std::string(MetricName(metricName))); gauge->Transform(metric); } if (sm->metrics_size() > 0) { @@ -291,7 +291,7 @@ void OTLPMetricsWriter::AddBytesAndFlushIfNeeded(std::size_t newBytes) * @tparam T The type of the data point to record (e.g., int64_t, double). * * @param checkable The configuration object to associate the metric with. - * @param metric The name of the metric to record the data point for. + * @param metric The OTel metric enum value indicating which metric stream to record the data point for. * @param value The data point value to record. * @param startTime The start time of the data point in seconds. * @param endTime The end time of the data point in seconds. @@ -302,7 +302,7 @@ void OTLPMetricsWriter::AddBytesAndFlushIfNeeded(std::size_t newBytes) template std::size_t OTLPMetricsWriter::Record( const Checkable::Ptr& checkable, - const String& metric, + Metric metric, T value, double startTime, double endTime, @@ -353,7 +353,7 @@ std::size_t OTLPMetricsWriter::Record( auto it = metricsForObj.Metrics.find(metric); if (it == metricsForObj.Metrics.end()) { - OTel::ValidateName(metric.GetData()); + OTel::ValidateName(MetricName(metric)); auto pair = metricsForObj.Metrics.emplace(metric, std::make_unique()); it = pair.first; } @@ -362,6 +362,27 @@ std::size_t OTLPMetricsWriter::Record( return bytes; } +/** + * Get the OTel metric name corresponding to the given Metric enum value. + * + * @param metric The Metric enum value to get the name for. + * + * @return The OTel metric name as a string_view. + * + * @throws std::invalid_argument if the metric type is unsupported. + */ +std::string_view OTLPMetricsWriter::MetricName(Metric metric) +{ + switch (metric) { + case Metric::Perfdata: + return l_PerfdataMetric; + case Metric::Threshold: + return l_ThresholdMetric; + default: + BOOST_THROW_EXCEPTION(std::invalid_argument("Unsupported metric type.")); + } +} + void OTLPMetricsWriter::ValidatePort(const Lazy& lvalue, const ValidationUtils& utils) { ObjectImpl::ValidatePort(lvalue, utils); diff --git a/lib/perfdata/otlpmetricswriter.hpp b/lib/perfdata/otlpmetricswriter.hpp index 2357070f1..d09305d1d 100644 --- a/lib/perfdata/otlpmetricswriter.hpp +++ b/lib/perfdata/otlpmetricswriter.hpp @@ -38,10 +38,16 @@ private: void Flush(bool fromTimer = false); void AddBytesAndFlushIfNeeded(std::size_t newBytes = 0); + enum class Metric : unsigned char { + Perfdata, // The main metric stream for checkable perfdata values. + Threshold // An optional metric stream for checkable perfdata thresholds (critical, warning, min, max). + }; + static std::string_view MetricName(Metric metric); + template [[nodiscard]] std::size_t Record( const Checkable::Ptr& checkable, - const String& metric, + Metric metric, T value, double startTime, double endTime, @@ -53,7 +59,7 @@ private: struct CheckableMetrics { - std::map Metrics; + std::map Metrics; /** * The ResourceMetrics Protobuf object for this checkable. *