Skip to content

Commit

Permalink
Remove ignores in resource attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed Sep 28, 2023
1 parent 765eb99 commit c476939
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ class PrometheusExporterUtils
std::string value,
std::vector<::prometheus::ClientMetric::Label> *labels);

/**
* Some attributes should be ignored when converting resource attributes to
* prometheus labels.
*
* @param name resource attribute name
* @return true if the attribute should be ignored, false otherwise.
*/
static bool ShouldIgnoreResourceAttribute(const std::string &name);

static opentelemetry::sdk::metrics::AggregationType getAggregationType(
const opentelemetry::sdk::metrics::PointType &point_type);

Expand Down
71 changes: 12 additions & 59 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,6 @@ void PrometheusExporterUtils::AddPrometheusLabel(
labels->emplace_back(std::move(prometheus_label));
}

bool PrometheusExporterUtils::ShouldIgnoreResourceAttribute(const std::string &name)
{
static std::unordered_set<std::string> ignores{
opentelemetry::sdk::resource::SemanticConventions::kServiceName,
opentelemetry::sdk::resource::SemanticConventions::kServiceNamespace,
opentelemetry::sdk::resource::SemanticConventions::kServiceInstanceId, kPrometheusJob,
kPrometheusInstance};
return ignores.end() != ignores.find(name);
}

metric_sdk::AggregationType PrometheusExporterUtils::getAggregationType(
const metric_sdk::PointType &point_type)
{
Expand Down Expand Up @@ -329,11 +319,6 @@ void PrometheusExporterUtils::SetTarget(const sdk::metrics::ResourceMetrics &dat

for (auto &label : data.resource_->GetAttributes())
{
if (ShouldIgnoreResourceAttribute(label.first))
{
continue;
}

AddPrometheusLabel(SanitizeName(label.first), AttributeValueToString(label.second),
&metric.label);
}
Expand Down Expand Up @@ -394,20 +379,9 @@ void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &me
// in this hot code path. Instead, we ignore out-of-order keys and emit a warning.
metric.label.reserve(labels.size() + 2);
std::string previous_key;
bool has_instance_label = false;
bool has_job_label = false;
for (auto const &label : labels)
{
auto sanitized = SanitizeLabel(label.first);
if (!has_instance_label && sanitized == kPrometheusInstance)
{
has_instance_label = true;
}
else if (!has_job_label && sanitized == kPrometheusJob)
{
has_job_label = true;
}

int comparison = previous_key.compare(sanitized);
if (metric.label.empty() || comparison < 0) // new key
{
Expand All @@ -433,11 +407,6 @@ void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &me
{
do
{
if (has_job_label)
{
break;
}

opentelemetry::sdk::resource::ResourceAttributes::const_iterator service_name_it =
resource->GetAttributes().find(
opentelemetry::sdk::resource::SemanticConventions::kServiceName);
Expand All @@ -460,38 +429,22 @@ void PrometheusExporterUtils::SetMetricBasic(prometheus_client::ClientMetric &me
&metric.label);
break;
}

opentelemetry::sdk::resource::ResourceAttributes::const_iterator prometheus_job_it =
resource->GetAttributes().find(kPrometheusJob);
if (prometheus_job_it != resource->GetAttributes().end())
{
AddPrometheusLabel(kPrometheusJob, AttributeValueToString(prometheus_job_it->second),
&metric.label);
}
} while (false);

if (!has_instance_label)
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())
{
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())
{
service_instance_id_it = resource->GetAttributes().find(kPrometheusInstance);
}
if (service_instance_id_it != resource->GetAttributes().end())
{
has_instance_label = true;
AddPrometheusLabel(kPrometheusInstance,
AttributeValueToString(service_instance_id_it->second), &metric.label);
}
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);
}
}

// Add a empty instance label if it's not exist
if (!has_instance_label)
{
AddPrometheusLabel(kPrometheusInstance, "", &metric.label);
}
}

Expand Down
3 changes: 1 addition & 2 deletions exporters/prometheus/test/exporter_utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ TEST(PrometheusExporterUtils, TranslateToPrometheusIntegerLastValue)
TEST(PrometheusExporterUtils, TranslateToPrometheusHistogramNormal)
{
opentelemetry::sdk::resource::Resource resource = opentelemetry::sdk::resource::Resource::Create(
{{"job", "test_service2"},
{"instance", "localhost:8001"},
{{"service.instance.id", "localhost:8001"},
{"custom_resource_attr", "custom_resource_value"}});
TestDataPoints dp;
metric_sdk::ResourceMetrics metrics_data = dp.CreateHistogramPointData();
Expand Down

0 comments on commit c476939

Please sign in to comment.