Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Sep 21, 2023
1 parent 7d4e882 commit a36cee1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
32 changes: 21 additions & 11 deletions exporters/prometheus/src/exporter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace exporter
{
namespace metrics
{

static constexpr const char *kScopeNameKey = "otel_scope_name";
static constexpr const char *kScopeVersionKey = "otel_scope_version";

/**
* Helper function to convert OpenTelemetry metrics data collection
* to Prometheus metrics data collection
Expand Down Expand Up @@ -273,30 +277,36 @@ void PrometheusExporterUtils::SetMetricBasic(
{
metric.timestamp_ms = time.count() / 1000000;

size_t i = 0;
if (!labels.empty())
{
metric.label.resize(labels.size());
metric.label.reserve(labels.size() + 2);
for (auto const &label : labels)
{
auto sanitized = SanitizeNames(label.first);
metric.label[i].name = sanitized;
metric.label[i++].value = AttributeValueToString(label.second);
prometheus_client::ClientMetric::Label sanitizedLabel;
sanitizedLabel.name = SanitizeNames(label.first);
sanitizedLabel.value = AttributeValueToString(label.second);
metric.label.push_back(std::move(sanitizedLabel));
}
}
if (!scope)
{
return;
}
auto scope_name = scope->GetName();
if (!scope_name.empty())
{
metric.label.resize(i + 1);
metric.label[i].name = "otel_scope_name";
metric.label[i++].value = scope_name;
prometheus_client::ClientMetric::Label label;
label.name = std::move(kScopeNameKey);
label.value = std::move(scope_name);
metric.label.push_back(std::move(label));
}
auto scope_version = scope->GetVersion();
if (!scope_version.empty())
{
metric.label.resize(i + 1);
metric.label[i].name = "otel_scope_version";
metric.label[i++].value = scope_version;
prometheus_client::ClientMetric::Label label;
label.name = std::move(kScopeNameKey);
label.value = std::move(scope_version);
metric.label.push_back(std::move(label));
}
}

Expand Down
15 changes: 14 additions & 1 deletion sdk/include/opentelemetry/sdk/metrics/export/metric_producer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,21 @@ namespace metrics
*/
struct ScopeMetrics
{
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope_;
const opentelemetry::sdk::instrumentationscope::InstrumentationScope *scope_ = nullptr;
std::vector<MetricData> metric_data_;

template <class ScopePtr, class MetricDataType>
inline ScopeMetrics(ScopePtr &&scope, MetricDataType &&metric)
: scope_{std::move(scope)}, metric_data_{std::move(metric)}
{}

inline ScopeMetrics() {}
inline ScopeMetrics(const ScopeMetrics &) = default;
inline ScopeMetrics(ScopeMetrics &&) = default;

inline ScopeMetrics &operator=(const ScopeMetrics &) = default;

inline ScopeMetrics &operator=(ScopeMetrics &&) = default;
};

struct ResourceMetrics
Expand Down

0 comments on commit a36cee1

Please sign in to comment.