Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Metrics SDK] Add unit to Instrument selection criteria #2236

Merged
merged 8 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions examples/metrics_simple/metrics_ostream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,38 @@ void InitMetrics(const std::string &name)

// counter view
std::string counter_name = name + "_counter";
std::string unit = "counter-unit";

auto instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kCounter, counter_name);
metrics_sdk::InstrumentType::kCounter, counter_name, unit);

auto meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto sum_view =
metrics_sdk::ViewFactory::Create(name, "description", metrics_sdk::AggregationType::kSum);
auto sum_view = metrics_sdk::ViewFactory::Create(name, "description", unit,
metrics_sdk::AggregationType::kSum);

p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));

// observable counter view
std::string observable_counter_name = name + "_observable_counter";

auto observable_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kObservableCounter, observable_counter_name);
metrics_sdk::InstrumentType::kObservableCounter, observable_counter_name, unit);

auto observable_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description",
auto observable_sum_view = metrics_sdk::ViewFactory::Create(name, "test_description", unit,
metrics_sdk::AggregationType::kSum);

p->AddView(std::move(observable_instrument_selector), std::move(observable_meter_selector),
std::move(observable_sum_view));

// histogram view
std::string histogram_name = name + "_histogram";
unit = "histogram-unit";

auto histogram_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kHistogram, histogram_name);
metrics_sdk::InstrumentType::kHistogram, histogram_name, unit);

auto histogram_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

Expand All @@ -96,7 +98,7 @@ void InitMetrics(const std::string &name)
std::move(histogram_aggregation_config));

auto histogram_view = metrics_sdk::ViewFactory::Create(
name, "description", metrics_sdk::AggregationType::kHistogram, aggregation_config);
name, "description", unit, metrics_sdk::AggregationType::kHistogram, aggregation_config);

p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));
Expand Down
12 changes: 7 additions & 5 deletions examples/prometheus/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,29 @@ void InitMetrics(const std::string &name, const std::string &addr)

// counter view
std::string counter_name = name + "_counter";
std::string counter_unit = "unit";

auto instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kCounter, counter_name);
metrics_sdk::InstrumentType::kCounter, counter_name, counter_unit);

auto meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto sum_view = metrics_sdk::ViewFactory::Create(counter_name, "description",
auto sum_view = metrics_sdk::ViewFactory::Create(counter_name, "description", counter_unit,
metrics_sdk::AggregationType::kSum);

p->AddView(std::move(instrument_selector), std::move(meter_selector), std::move(sum_view));

// histogram view
std::string histogram_name = name + "_histogram";
std::string histogram_unit = "unit";

auto histogram_instrument_selector = metrics_sdk::InstrumentSelectorFactory::Create(
metrics_sdk::InstrumentType::kHistogram, histogram_name);
metrics_sdk::InstrumentType::kHistogram, histogram_name, histogram_unit);

auto histogram_meter_selector = metrics_sdk::MeterSelectorFactory::Create(name, version, schema);

auto histogram_view = metrics_sdk::ViewFactory::Create(histogram_name, "description",
metrics_sdk::AggregationType::kHistogram);
auto histogram_view = metrics_sdk::ViewFactory::Create(
histogram_name, "description", histogram_unit, metrics_sdk::AggregationType::kHistogram);

p->AddView(std::move(histogram_instrument_selector), std::move(histogram_meter_selector),
std::move(histogram_view));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,24 @@ class InstrumentSelector
{
public:
InstrumentSelector(opentelemetry::sdk::metrics::InstrumentType instrument_type,
opentelemetry::nostd::string_view name)
opentelemetry::nostd::string_view name,
opentelemetry::nostd::string_view units)
: name_filter_{PredicateFactory::GetPredicate(name, PredicateType::kPattern)},
unit_filter_{PredicateFactory::GetPredicate(units, PredicateType::kExact)},
instrument_type_{instrument_type}

lalitb marked this conversation as resolved.
Show resolved Hide resolved
{}

// Returns name filter predicate. This shouldn't be deleted
const opentelemetry::sdk::metrics::Predicate *GetNameFilter() const { return name_filter_.get(); }

// Returns unit filter predicate. This shouldn't be deleted
const opentelemetry::sdk::metrics::Predicate *GetUnitFilter() const { return unit_filter_.get(); }
// Returns instrument filter.
InstrumentType GetInstrumentType() { return instrument_type_; }

private:
std::unique_ptr<opentelemetry::sdk::metrics::Predicate> name_filter_;
std::unique_ptr<opentelemetry::sdk::metrics::Predicate> unit_filter_;
opentelemetry::sdk::metrics::InstrumentType instrument_type_;
};
} // namespace metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class InstrumentSelectorFactory
public:
static std::unique_ptr<InstrumentSelector> Create(
opentelemetry::sdk::metrics::InstrumentType instrument_type,
opentelemetry::nostd::string_view name);
opentelemetry::nostd::string_view name,
opentelemetry::nostd::string_view unit);
};

} // namespace metrics
Expand Down
3 changes: 3 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/view/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ class View
public:
View(const std::string &name,
const std::string &description = "",
const std::string &unit = "",
AggregationType aggregation_type = AggregationType::kDefault,
std::shared_ptr<AggregationConfig> aggregation_config = nullptr,
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor =
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor>(
new opentelemetry::sdk::metrics::DefaultAttributesProcessor()))
: name_(name),
description_(description),
unit_(unit),
aggregation_type_{aggregation_type},
aggregation_config_{aggregation_config},
attributes_processor_{std::move(attributes_processor)}
Expand Down Expand Up @@ -60,6 +62,7 @@ class View
private:
std::string name_;
std::string description_;
std::string unit_;
AggregationType aggregation_type_;
std::shared_ptr<AggregationConfig> aggregation_config_;
std::unique_ptr<opentelemetry::sdk::metrics::AttributesProcessor> attributes_processor_;
Expand Down
7 changes: 7 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/view/view_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,22 @@ class OPENTELEMETRY_EXPORT ViewFactory

static std::unique_ptr<View> Create(const std::string &name,
const std::string &description,
const std::string &unit);

static std::unique_ptr<View> Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type);

static std::unique_ptr<View> Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type,
std::shared_ptr<AggregationConfig> aggregation_config);

static std::unique_ptr<View> Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type,
std::shared_ptr<AggregationConfig> aggregation_config,
std::unique_ptr<AttributesProcessor> attributes_processor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ViewRegistry
const opentelemetry::sdk::metrics::InstrumentDescriptor &instrument_descriptor)
{
return selector->GetNameFilter()->Match(instrument_descriptor.name_) &&
selector->GetUnitFilter()->Match(instrument_descriptor.unit_) &&
(selector->GetInstrumentType() == instrument_descriptor.type_);
}
};
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/metrics/view/instrument_selector_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ namespace metrics

std::unique_ptr<InstrumentSelector> InstrumentSelectorFactory::Create(
opentelemetry::sdk::metrics::InstrumentType instrument_type,
opentelemetry::nostd::string_view name)
opentelemetry::nostd::string_view name,
opentelemetry::nostd::string_view unit)
{
std::unique_ptr<InstrumentSelector> instrument_selector(
new InstrumentSelector(instrument_type, name));
new InstrumentSelector(instrument_type, name, unit));
return instrument_selector;
}

Expand Down
18 changes: 14 additions & 4 deletions sdk/src/metrics/view/view_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,46 @@ std::unique_ptr<View> ViewFactory::Create(const std::string &name)

std::unique_ptr<View> ViewFactory::Create(const std::string &name, const std::string &description)
{
return Create(name, description, AggregationType::kDefault);
return Create(name, description, "", AggregationType::kDefault);
}

std::unique_ptr<View> ViewFactory::Create(const std::string &name,
const std::string &description,
const std::string &unit)
{
return Create(name, description, unit, AggregationType::kDefault);
}

std::unique_ptr<View> ViewFactory::Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type)
{
std::shared_ptr<AggregationConfig> aggregation_config(nullptr);
return Create(name, description, aggregation_type, aggregation_config);
return Create(name, description, unit, aggregation_type, aggregation_config);
}

std::unique_ptr<View> ViewFactory::Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type,
std::shared_ptr<AggregationConfig> aggregation_config)
{
auto attributes_processor =
std::unique_ptr<AttributesProcessor>(new DefaultAttributesProcessor());

return Create(name, description, aggregation_type, aggregation_config,
return Create(name, description, unit, aggregation_type, aggregation_config,
std::move(attributes_processor));
}

std::unique_ptr<View> ViewFactory::Create(const std::string &name,
const std::string &description,
const std::string &unit,
AggregationType aggregation_type,
std::shared_ptr<AggregationConfig> aggregation_config,
std::unique_ptr<AttributesProcessor> attributes_processor)
{
std::unique_ptr<View> view(new View(name, description, aggregation_type, aggregation_config,
std::unique_ptr<View> view(new View(name, description, unit, aggregation_type, aggregation_config,
std::move(attributes_processor)));
return view;
}
Expand Down
7 changes: 4 additions & 3 deletions sdk/test/metrics/histogram_aggregation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ TEST(CounterToHistogram, Double)
std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
mp.AddMetricReader(reader);

std::unique_ptr<View> view{new View("view1", "view1_description", AggregationType::kHistogram)};
std::unique_ptr<View> view{
new View("view1", "view1_description", "unit", AggregationType::kHistogram)};
std::unique_ptr<InstrumentSelector> instrument_selector{
new InstrumentSelector(InstrumentType::kCounter, "counter1")};
new InstrumentSelector(InstrumentType::kCounter, "counter1", "unit")};
std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("meter1", "version1", "schema1")};
mp.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));

auto h = m->CreateDoubleCounter("counter1", "counter1_description", "counter1_unit");
auto h = m->CreateDoubleCounter("counter1", "counter1_description", "unit");

h->Add(5, {});
h->Add(10, {});
Expand Down
22 changes: 14 additions & 8 deletions sdk/test/metrics/histogram_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ TEST(Histogram, Double)
TEST(Histogram, DoubleCustomBuckets)
{
MeterProvider mp;
auto m = mp.GetMeter("meter1", "version1", "schema1");
auto m = mp.GetMeter("meter1", "version1", "schema1");
std::string instrument_unit = "ms";
std::string instrument_name = "historgram1";
std::string instrument_desc = "histogram metrics";

std::unique_ptr<MockMetricExporter> exporter(new MockMetricExporter());
std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
Expand All @@ -87,13 +90,13 @@ TEST(Histogram, DoubleCustomBuckets)
std::shared_ptr<HistogramAggregationConfig> config(new HistogramAggregationConfig());
config->boundaries_ = {10, 20, 30, 40};
std::unique_ptr<View> view{
new View("view1", "view1_description", AggregationType::kHistogram, config)};
new View("view1", "view1_description", instrument_unit, AggregationType::kHistogram, config)};
std::unique_ptr<InstrumentSelector> instrument_selector{
new InstrumentSelector(InstrumentType::kHistogram, "histogram1")};
new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)};
std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("meter1", "version1", "schema1")};
mp.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));

auto h = m->CreateDoubleHistogram("histogram1", "histogram1_description", "histogram1_unit");
auto h = m->CreateDoubleHistogram(instrument_name, instrument_desc, instrument_unit);

h->Record(5, {});
h->Record(10, {});
Expand Down Expand Up @@ -190,7 +193,10 @@ TEST(Histogram, UInt64)
TEST(Histogram, UInt64CustomBuckets)
{
MeterProvider mp;
auto m = mp.GetMeter("meter1", "version1", "schema1");
auto m = mp.GetMeter("meter1", "version1", "schema1");
std::string instrument_name = "historgram1";
std::string instrument_desc = "histogram metrics";
std::string instrument_unit = "ms";

std::unique_ptr<MockMetricExporter> exporter(new MockMetricExporter());
std::shared_ptr<MetricReader> reader{new MockMetricReader(std::move(exporter))};
Expand All @@ -199,13 +205,13 @@ TEST(Histogram, UInt64CustomBuckets)
std::shared_ptr<HistogramAggregationConfig> config(new HistogramAggregationConfig());
config->boundaries_ = {10, 20, 30, 40};
std::unique_ptr<View> view{
new View("view1", "view1_description", AggregationType::kHistogram, config)};
new View("view1", "view1_description", "ms", AggregationType::kHistogram, config)};
std::unique_ptr<InstrumentSelector> instrument_selector{
new InstrumentSelector(InstrumentType::kHistogram, "histogram1")};
new InstrumentSelector(InstrumentType::kHistogram, instrument_name, instrument_unit)};
std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("meter1", "version1", "schema1")};
mp.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));

auto h = m->CreateUInt64Histogram("histogram1", "histogram1_description", "histogram1_unit");
auto h = m->CreateUInt64Histogram(instrument_name, instrument_desc, instrument_unit);

h->Record(5, {});
h->Record(10, {});
Expand Down
2 changes: 1 addition & 1 deletion sdk/test/metrics/meter_provider_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TEST(MeterProvider, GetMeter)

std::unique_ptr<View> view{std::unique_ptr<View>()};
std::unique_ptr<InstrumentSelector> instrument_selector{
new InstrumentSelector(InstrumentType::kCounter, "instru1")};
new InstrumentSelector(InstrumentType::kCounter, "instru1", "unit1")};
std::unique_ptr<MeterSelector> meter_selector{new MeterSelector("name1", "version1", "schema1")};

mp1.AddView(std::move(instrument_selector), std::move(meter_selector), std::move(view));
Expand Down
Loading