Skip to content

Commit

Permalink
Merge branch 'main' into fix_semconv_2459
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalff authored Dec 23, 2023
2 parents 9525b62 + ddfafff commit 2f86e87
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class OtlpMetricUtils
{
public:
static opentelemetry::sdk::metrics::AggregationType GetAggregationType(
const opentelemetry::sdk::metrics::InstrumentType &instrument_type) noexcept;
const opentelemetry::sdk::metrics::MetricData &metric_data) noexcept;

static proto::metrics::v1::AggregationTemporality GetProtoAggregationTemporality(
const opentelemetry::sdk::metrics::AggregationTemporality &aggregation_temporality) noexcept;
Expand Down
21 changes: 12 additions & 9 deletions exporters/otlp/src/otlp_metric_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,24 @@ proto::metrics::v1::AggregationTemporality OtlpMetricUtils::GetProtoAggregationT
}

metric_sdk::AggregationType OtlpMetricUtils::GetAggregationType(
const opentelemetry::sdk::metrics::InstrumentType &instrument_type) noexcept
const opentelemetry::sdk::metrics::MetricData &metric_data) noexcept
{

if (instrument_type == metric_sdk::InstrumentType::kCounter ||
instrument_type == metric_sdk::InstrumentType::kUpDownCounter ||
instrument_type == metric_sdk::InstrumentType::kObservableCounter ||
instrument_type == metric_sdk::InstrumentType::kObservableUpDownCounter)
if (metric_data.point_data_attr_.size() == 0)
{
return metric_sdk::AggregationType::kDrop;
}
auto point_data_with_attributes = metric_data.point_data_attr_[0];
if (nostd::holds_alternative<sdk::metrics::SumPointData>(point_data_with_attributes.point_data))
{
return metric_sdk::AggregationType::kSum;
}
else if (instrument_type == metric_sdk::InstrumentType::kHistogram)
else if (nostd::holds_alternative<sdk::metrics::HistogramPointData>(
point_data_with_attributes.point_data))
{
return metric_sdk::AggregationType::kHistogram;
}
else if (instrument_type == metric_sdk::InstrumentType::kObservableGauge)
else if (nostd::holds_alternative<sdk::metrics::LastValuePointData>(
point_data_with_attributes.point_data))
{
return metric_sdk::AggregationType::kLastValue;
}
Expand Down Expand Up @@ -188,7 +191,7 @@ void OtlpMetricUtils::PopulateInstrumentInfoMetrics(
metric->set_name(metric_data.instrument_descriptor.name_);
metric->set_description(metric_data.instrument_descriptor.description_);
metric->set_unit(metric_data.instrument_descriptor.unit_);
auto kind = GetAggregationType(metric_data.instrument_descriptor.type_);
auto kind = GetAggregationType(metric_data);
switch (kind)
{
case metric_sdk::AggregationType::kSum: {
Expand Down
91 changes: 91 additions & 0 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include <algorithm>
#include <limits>
#include <regex>
#include <sstream>
Expand All @@ -13,6 +14,7 @@
#include "prometheus/metric_family.h"
#include "prometheus/metric_type.h"

#include "opentelemetry/common/macros.h"
#include "opentelemetry/exporters/prometheus/exporter_utils.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/resource/resource.h"
Expand Down Expand Up @@ -280,11 +282,13 @@ std::string PrometheusExporterUtils::SanitizeNames(std::string name)
return name;
}

#if OPENTELEMETRY_HAVE_WORKING_REGEX
std::regex INVALID_CHARACTERS_PATTERN("[^a-zA-Z0-9]");
std::regex CHARACTERS_BETWEEN_BRACES_PATTERN("\\{(.*?)\\}");
std::regex SANITIZE_LEADING_UNDERSCORES("^_+");
std::regex SANITIZE_TRAILING_UNDERSCORES("_+$");
std::regex SANITIZE_CONSECUTIVE_UNDERSCORES("[_]{2,}");
#endif

std::string PrometheusExporterUtils::GetEquivalentPrometheusUnit(
const std::string &raw_metric_unit_name)
Expand Down Expand Up @@ -360,7 +364,32 @@ std::string PrometheusExporterUtils::GetPrometheusPerUnit(const std::string &per

std::string PrometheusExporterUtils::RemoveUnitPortionInBraces(const std::string &unit)
{
#if OPENTELEMETRY_HAVE_WORKING_REGEX
return std::regex_replace(unit, CHARACTERS_BETWEEN_BRACES_PATTERN, "");
#else
bool in_braces = false;
std::string cleaned_unit;
cleaned_unit.reserve(unit.size());
for (auto c : unit)
{
if (in_braces)
{
if (c == '}')
{
in_braces = false;
}
}
else if (c == '{')
{
in_braces = true;
}
else
{
cleaned_unit += c;
}
}
return cleaned_unit;
#endif
}

std::string PrometheusExporterUtils::ConvertRateExpressedToPrometheusUnit(
Expand Down Expand Up @@ -389,12 +418,74 @@ std::string PrometheusExporterUtils::ConvertRateExpressedToPrometheusUnit(

std::string PrometheusExporterUtils::CleanUpString(const std::string &str)
{
#if OPENTELEMETRY_HAVE_WORKING_REGEX
std::string cleaned_string = std::regex_replace(str, INVALID_CHARACTERS_PATTERN, "_");
cleaned_string = std::regex_replace(cleaned_string, SANITIZE_CONSECUTIVE_UNDERSCORES, "_");
cleaned_string = std::regex_replace(cleaned_string, SANITIZE_TRAILING_UNDERSCORES, "");
cleaned_string = std::regex_replace(cleaned_string, SANITIZE_LEADING_UNDERSCORES, "");
return cleaned_string;
#else
std::string cleaned_string = str;
if (cleaned_string.empty())
{
return cleaned_string;
}
std::transform(cleaned_string.begin(), cleaned_string.end(), cleaned_string.begin(),
[](const char c) {
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
return c;
}
return '_';
});

std::string::size_type trim_start = 0;
std::string::size_type trim_end = 0;
bool previous_underscore = false;
for (std::string::size_type i = 0; i < cleaned_string.size(); ++i)
{
if (cleaned_string[i] == '_')
{
if (previous_underscore)
{
continue;
}

previous_underscore = true;
}
else
{
previous_underscore = false;
}

if (trim_end != i)
{
cleaned_string[trim_end] = cleaned_string[i];
}
++trim_end;
}

while (trim_end > 0 && cleaned_string[trim_end - 1] == '_')
{
--trim_end;
}
while (trim_start < trim_end && cleaned_string[trim_start] == '_')
{
++trim_start;
}

// All characters are underscore
if (trim_start >= trim_end)
{
return "_";
}
if (0 != trim_start || cleaned_string.size() != trim_end)
{
return cleaned_string.substr(trim_start, trim_end - trim_start);
}

return cleaned_string;
#endif
}

std::string PrometheusExporterUtils::MapToPrometheusName(
Expand Down

0 comments on commit 2f86e87

Please sign in to comment.