Skip to content

Commit

Permalink
fix CI
Browse files Browse the repository at this point in the history
  • Loading branch information
esigo committed Nov 19, 2023
1 parent 71638d4 commit 53c1fa3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ class PrometheusExporterUtils

private:
/**
* Append key-value pair to prometheus labels.
* Sanitize the given metric name or label according to Prometheus rule.
*
* @param name label name
* @param value label value
* @param labels target labels
* This function is needed because names in OpenTelemetry can contain
* alphanumeric characters, '_', '.', and '-', whereas in Prometheus the
* name should only contain alphanumeric characters and '_'.
* @param name name
*/
static void AddPrometheusLabel(std::string name,
std::string value,
std::vector<::prometheus::ClientMetric::Label> *labels);
static std::string SanitizeNames(std::string name);

/**
* Sanitize the given metric name or label according to Prometheus rule.
Expand Down Expand Up @@ -211,6 +210,9 @@ class PrometheusExporterUtils
const std::vector<double> &boundaries,
const std::vector<uint64_t> &counts,
::prometheus::ClientMetric *metric);

// For testing
friend class SanitizeNameTester;
};
} // namespace metrics
} // namespace exporter
Expand Down
2 changes: 1 addition & 1 deletion exporters/prometheus/src/collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ std::vector<prometheus_client::MetricFamily> PrometheusCollector::Collect() cons
auto prometheus_metric_data =
PrometheusExporterUtils::TranslateToPrometheus(metric_data, this->populate_target_info_);
for (auto &data : prometheus_metric_data)
result.emplace_back(data.second);
result.emplace_back(data);
return true;
});
collection_lock_.unlock();
Expand Down
39 changes: 35 additions & 4 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,41 @@ void PrometheusExporterUtils::AddPrometheusLabel(
*/
std::string PrometheusExporterUtils::SanitizeNames(std::string name)
{
prometheus_client::ClientMetric::Label prometheus_label;
prometheus_label.name = std::move(name);
prometheus_label.value = std::move(value);
labels->emplace_back(std::move(prometheus_label));
constexpr const auto replacement = '_';
constexpr const auto replacement_dup = '=';

auto valid = [](int i, char c) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == ':' ||
(c >= '0' && c <= '9' && i > 0))
{
return true;
}
return false;
};

bool has_dup = false;
for (int i = 0; i < (int)name.size(); ++i)
{
if (valid(i, name[i]))
{
continue;
}
if (i > 0 && (name[i - 1] == replacement || name[i - 1] == replacement_dup))
{
has_dup = true;
name[i] = replacement_dup;
}
else
{
name[i] = replacement;
}
}
if (has_dup)
{
auto end = std::remove(name.begin(), name.end(), replacement_dup);
return std::string{name.begin(), end};
}
return name;
}

std::regex INVALID_CHARACTERS_PATTERN("[^a-zA-Z0-9]");
Expand Down
108 changes: 1 addition & 107 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,53 +299,7 @@ TEST_F(SanitizeTest, Label)
CheckSanitizeLabel("name?__name:", "name_name_");
}

class SanitizeTest : public ::testing::Test
{
Resource resource_ = Resource::Create({});
nostd::unique_ptr<InstrumentationScope> instrumentation_scope_ =
InstrumentationScope::Create("library_name", "1.2.0");

protected:
void CheckSanitizeName(const std::string &original, const std::string &sanitized)
{
metric_sdk::InstrumentDescriptor instrument_descriptor{
original, "description", "unit", metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble};
std::vector<prometheus::MetricFamily> result = PrometheusExporterUtils::TranslateToPrometheus(
{&resource_,
std::vector<metric_sdk::ScopeMetrics>{
{instrumentation_scope_.get(),
std::vector<metric_sdk::MetricData>{
{{instrument_descriptor, {}, {}, {}, {{{}, {}}}}}}}}},
false);
EXPECT_EQ(result.begin()->name, sanitized + "_unit");
}

void CheckSanitizeLabel(const std::string &original, const std::string &sanitized)
{
metric_sdk::InstrumentDescriptor instrument_descriptor{
"name", "description", "unit", metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble};
std::vector<prometheus::MetricFamily> result = PrometheusExporterUtils::TranslateToPrometheus(
{&resource_,
std::vector<metric_sdk::ScopeMetrics>{
{instrumentation_scope_.get(),
std::vector<metric_sdk::MetricData>{
{instrument_descriptor, {}, {}, {}, {{{{original, "value"}}, {}}}}}}}},
false);
EXPECT_EQ(result.begin()->metric.begin()->label.begin()->name, sanitized);
}
};

TEST_F(SanitizeTest, Name)
{
CheckSanitizeName("name", "name");
CheckSanitizeName("name?", "name_");
CheckSanitizeName("name???", "name_");
CheckSanitizeName("name?__", "name_");
CheckSanitizeName("name?__name", "name_name");
CheckSanitizeName("name?__name:", "name_name:");
}
TEST_F(SanitizeTest, Name) {}

class AttributeCollisionTest : public ::testing::Test
{
Expand Down Expand Up @@ -391,22 +345,6 @@ TEST_F(AttributeCollisionTest, SeparatesDistinctKeys)
{"otel_scope_version", "1.2.0"}});
}

TEST_F(AttributeCollisionTest, JoinsCollidingKeys)
{
CheckTranslation({{"foo.a", "value1"}, {"foo_a", "value2"}}, {{"foo_a", "value1;value2"},
{"otel_scope_name", "library_name"},
{"otel_scope_version", "1.2.0"}});
}

TEST_F(AttributeCollisionTest, DropsInvertedKeys)
{
CheckTranslation({{"foo.a", "value1"}, {"foo.b", "value2"}, {"foo__a", "value3"}},
{{"foo_a", "value1"},
{"foo_b", "value2"},
{"otel_scope_name", "library_name"},
{"otel_scope_version", "1.2.0"}});
}

TEST(PrometheusExporterUtils, PrometheusUnit)
{
ASSERT_EQ(exporter::metrics::SanitizeNameTester::getPrometheusUnit("d"), "days");
Expand Down Expand Up @@ -480,50 +418,6 @@ TEST(PrometheusExporterUtils, ConvertRateExpressedToPrometheusUnit)
"_per_minute");
}

class AttributeCollisionTest : public ::testing::Test
{
Resource resource_ = Resource::Create(ResourceAttributes{});
nostd::unique_ptr<InstrumentationScope> instrumentation_scope_ =
InstrumentationScope::Create("library_name", "1.2.0");
metric_sdk::InstrumentDescriptor instrument_descriptor_{"library_name", "description", "unit",
metric_sdk::InstrumentType::kCounter,
metric_sdk::InstrumentValueType::kDouble};

protected:
void CheckTranslation(const metric_sdk::PointAttributes &attrs,
const std::vector<prometheus::ClientMetric::Label> &expected)
{
auto result = PrometheusExporterUtils::TranslateToPrometheus(
{&resource_,
std::vector<metric_sdk::ScopeMetrics>{
{instrumentation_scope_.get(),
std::vector<metric_sdk::MetricData>{
{instrument_descriptor_, {}, {}, {}, {{attrs, {}}}}}}}},
false);
for (auto &expected_kv : expected)
{
bool found = false;
for (auto &found_kv : result.begin()->metric.begin()->label)
{
if (found_kv.name == expected_kv.name)
{
EXPECT_EQ(found_kv.value, expected_kv.value);
found = true;
}
}
EXPECT_TRUE(found);
}
}
};

TEST_F(AttributeCollisionTest, SeparatesDistinctKeys)
{
CheckTranslation({{"foo.a", "value1"}, {"foo.b", "value2"}}, {{"foo_a", "value1"},
{"foo_b", "value2"},
{"otel_scope_name", "library_name"},
{"otel_scope_version", "1.2.0"}});
}

TEST_F(AttributeCollisionTest, JoinsCollidingKeys)
{
CheckTranslation({{"foo.a", "value1"}, {"foo_a", "value2"}}, {{"foo_a", "value1;value2"},
Expand Down

0 comments on commit 53c1fa3

Please sign in to comment.