Skip to content

Commit

Permalink
Remove labels conversations that should only be in target info.
Browse files Browse the repository at this point in the history
Signed-off-by: WenTao Ou <admin@owent.net>
  • Loading branch information
owent committed Sep 29, 2023
1 parent 3ebe160 commit 422bca2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 112 deletions.
53 changes: 0 additions & 53 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,6 @@ void PrometheusExporterUtils::SetTarget(const sdk::metrics::ResourceMetrics &dat
{
return;
}
if (data.scope_metric_data_.empty())
{
return;
}
if ((*data.scope_metric_data_.begin()).metric_data_.empty())
{
return;
}

prometheus_client::MetricFamily metric_family;
metric_family.name = "target";
Expand Down Expand Up @@ -401,51 +393,6 @@ void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &me
<< "'. Ignoring this label.");
}
}

// Convert resource to job and instance labels
if (nullptr != resource)
{
do
{
opentelemetry::sdk::resource::ResourceAttributes::const_iterator service_name_it =
resource->GetAttributes().find(
opentelemetry::sdk::resource::SemanticConventions::kServiceName);
opentelemetry::sdk::resource::ResourceAttributes::const_iterator service_namespace_it =
resource->GetAttributes().find(
opentelemetry::sdk::resource::SemanticConventions::kServiceNamespace);

if (service_namespace_it != resource->GetAttributes().end() &&
service_name_it != resource->GetAttributes().end())
{
AddPrometheusLabel(kPrometheusJob,
AttributeValueToString(service_namespace_it->second) + "/" +
AttributeValueToString(service_name_it->second),
&metric.label);
break;
}
else if (service_name_it != resource->GetAttributes().end())
{
AddPrometheusLabel(kPrometheusJob, AttributeValueToString(service_name_it->second),
&metric.label);
break;
}
} while (false);

opentelemetry::sdk::resource::ResourceAttributes::const_iterator service_instance_id_it =
resource->GetAttributes().find(
opentelemetry::sdk::resource::SemanticConventions::kServiceInstanceId);

if (service_instance_id_it != resource->GetAttributes().end())
{
AddPrometheusLabel(kPrometheusInstance,
AttributeValueToString(service_instance_id_it->second), &metric.label);
}
else
{
// Add a empty instance label if it's not exist
AddPrometheusLabel(kPrometheusInstance, "", &metric.label);
}
}
}

std::string PrometheusExporterUtils::AttributeValueToString(
Expand Down
74 changes: 15 additions & 59 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,34 +113,23 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter)
auto metric1 = translated[1];
std::vector<int> vals = {10};

assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Counter, 3,
assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Counter, 1,
vals);

int checked_label_num = 0;
for (auto &label : metric1.metric[0].label)
for (auto &label : translated[0].metric[0].label)
{
if (label.name == "job")
if (label.name == "service_namespace")
{
ASSERT_EQ(label.value, "test_namespace/test_service");
checked_label_num++;
}
else if (label.name == "instance")
{
ASSERT_EQ(label.value, "localhost:8000");
ASSERT_EQ(label.value, "test_namespace");
checked_label_num++;
}
}
ASSERT_EQ(checked_label_num, 2);

checked_label_num = 0;
for (auto &label : translated[0].metric[0].label)
{
if (label.name == "job")
else if (label.name == "service_name")
{
ASSERT_EQ(label.value, "test_namespace/test_service");
ASSERT_EQ(label.value, "test_service");
checked_label_num++;
}
else if (label.name == "instance")
else if (label.name == "service_instance_id")
{
ASSERT_EQ(label.value, "localhost:8000");
checked_label_num++;
Expand All @@ -151,7 +140,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerCounter)
checked_label_num++;
}
}
ASSERT_EQ(checked_label_num, 3);
ASSERT_EQ(checked_label_num, 4);
}

TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue)
Expand All @@ -169,34 +158,18 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue)

auto metric1 = translated[1];
std::vector<int> vals = {10};
assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Gauge, 3,
assert_basic(metric1, "library_name", "description", prometheus_client::MetricType::Gauge, 1,
vals);

int checked_label_num = 0;
for (auto &label : metric1.metric[0].label)
{
if (label.name == "job")
{
ASSERT_EQ(label.value, "test_service");
checked_label_num++;
}
else if (label.name == "instance")
{
ASSERT_EQ(label.value, "localhost:8000");
checked_label_num++;
}
}
ASSERT_EQ(checked_label_num, 2);

checked_label_num = 0;
for (auto &label : translated[0].metric[0].label)
{
if (label.name == "job")
if (label.name == "service_name")
{
ASSERT_EQ(label.value, "test_service");
checked_label_num++;
}
else if (label.name == "instance")
else if (label.name == "service_instance_id")
{
ASSERT_EQ(label.value, "localhost:8000");
checked_label_num++;
Expand Down Expand Up @@ -224,37 +197,20 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal)

auto metric = translated[1];
std::vector<double> vals = {3, 900.5, 4};
assert_basic(metric, "library_name", "description", prometheus_client::MetricType::Histogram, 3,
assert_basic(metric, "library_name", "description", prometheus_client::MetricType::Histogram, 1,
vals);
assert_histogram(metric, std::list<double>{10.1, 20.2, 30.2}, {200, 300, 400, 500});

int checked_label_num = 0;
for (auto &label : metric.metric[0].label)
{
if (label.name == "job")
{
// job label should be ignored when service.name exist
ASSERT_EQ(label.value, "unknown_service");
checked_label_num++;
}
else if (label.name == "instance")
{
ASSERT_EQ(label.value, "localhost:8001");
checked_label_num++;
}
}
ASSERT_EQ(checked_label_num, 2);

checked_label_num = 0;
for (auto &label : translated[0].metric[0].label)
{
if (label.name == "job")
if (label.name == "service_name")
{
// job label should be ignored when service.name exist
// default service name is "unknown_service"
ASSERT_EQ(label.value, "unknown_service");
checked_label_num++;
}
else if (label.name == "instance")
else if (label.name == "service_instance_id")
{
ASSERT_EQ(label.value, "localhost:8001");
checked_label_num++;
Expand Down

0 comments on commit 422bca2

Please sign in to comment.