Skip to content

Commit

Permalink
[Metrics API/SDK] Change Meter API/SDK to return nostd::unique_ptr fo…
Browse files Browse the repository at this point in the history
…r Sync Instruments (#1707)
  • Loading branch information
esigo authored Oct 25, 2022
1 parent 7fd76ee commit 9b8a4dc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 41 deletions.
19 changes: 10 additions & 9 deletions api/include/opentelemetry/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# include "opentelemetry/nostd/shared_ptr.h"
# include "opentelemetry/nostd/span.h"
# include "opentelemetry/nostd/string_view.h"
# include "opentelemetry/nostd/unique_ptr.h"
# include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
Expand All @@ -28,20 +29,20 @@ class Meter
virtual ~Meter() = default;

/**
* Creates a Counter with the passed characteristics and returns a shared_ptr to that Counter.
* Creates a Counter with the passed characteristics and returns a unique_ptr to that Counter.
*
* @param name the name of the new Counter.
* @param description a brief description of what the Counter is used for.
* @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html.
* @return a shared pointer to the created Counter.
*/

virtual nostd::shared_ptr<Counter<uint64_t>> CreateUInt64Counter(
virtual nostd::unique_ptr<Counter<uint64_t>> CreateUInt64Counter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<Counter<double>> CreateDoubleCounter(
virtual nostd::unique_ptr<Counter<double>> CreateDoubleCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
Expand All @@ -65,19 +66,19 @@ class Meter
nostd::string_view unit = "") noexcept = 0;

/**
* Creates a Histogram with the passed characteristics and returns a shared_ptr to that Histogram.
* Creates a Histogram with the passed characteristics and returns a unique_ptr to that Histogram.
*
* @param name the name of the new Histogram.
* @param description a brief description of what the Histogram is used for.
* @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html.
* @return a shared pointer to the created Histogram.
*/
virtual nostd::shared_ptr<Histogram<uint64_t>> CreateUInt64Histogram(
virtual nostd::unique_ptr<Histogram<uint64_t>> CreateUInt64Histogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<Histogram<double>> CreateDoubleHistogram(
virtual nostd::unique_ptr<Histogram<double>> CreateDoubleHistogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
Expand All @@ -101,20 +102,20 @@ class Meter
nostd::string_view unit = "") noexcept = 0;

/**
* Creates an UpDownCounter with the passed characteristics and returns a shared_ptr to that
* Creates an UpDownCounter with the passed characteristics and returns a unique_ptr to that
* UpDownCounter.
*
* @param name the name of the new UpDownCounter.
* @param description a brief description of what the UpDownCounter is used for.
* @param unit the unit of metric values following https://unitsofmeasure.org/ucum.html.
* @return a shared pointer to the created UpDownCounter.
*/
virtual nostd::shared_ptr<UpDownCounter<int64_t>> CreateInt64UpDownCounter(
virtual nostd::unique_ptr<UpDownCounter<int64_t>> CreateInt64UpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;

virtual nostd::shared_ptr<UpDownCounter<double>> CreateDoubleUpDownCounter(
virtual nostd::unique_ptr<UpDownCounter<double>> CreateDoubleUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept = 0;
Expand Down
24 changes: 12 additions & 12 deletions api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ class NoopObservableInstrument : public ObservableInstrument
class NoopMeter final : public Meter
{
public:
nostd::shared_ptr<Counter<uint64_t>> CreateUInt64Counter(
nostd::unique_ptr<Counter<uint64_t>> CreateUInt64Counter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<Counter<uint64_t>>{new NoopCounter<uint64_t>(name, description, unit)};
return nostd::unique_ptr<Counter<uint64_t>>{new NoopCounter<uint64_t>(name, description, unit)};
}

nostd::shared_ptr<Counter<double>> CreateDoubleCounter(
nostd::unique_ptr<Counter<double>> CreateDoubleCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<Counter<double>>{new NoopCounter<double>(name, description, unit)};
return nostd::unique_ptr<Counter<double>>{new NoopCounter<double>(name, description, unit)};
}

nostd::shared_ptr<ObservableInstrument> CreateInt64ObservableCounter(
Expand All @@ -121,21 +121,21 @@ class NoopMeter final : public Meter
new NoopObservableInstrument(name, description, unit));
}

nostd::shared_ptr<Histogram<uint64_t>> CreateUInt64Histogram(
nostd::unique_ptr<Histogram<uint64_t>> CreateUInt64Histogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<Histogram<uint64_t>>{
return nostd::unique_ptr<Histogram<uint64_t>>{
new NoopHistogram<uint64_t>(name, description, unit)};
}

nostd::shared_ptr<Histogram<double>> CreateDoubleHistogram(
nostd::unique_ptr<Histogram<double>> CreateDoubleHistogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<Histogram<double>>{new NoopHistogram<double>(name, description, unit)};
return nostd::unique_ptr<Histogram<double>>{new NoopHistogram<double>(name, description, unit)};
}

nostd::shared_ptr<ObservableInstrument> CreateInt64ObservableGauge(
Expand All @@ -156,21 +156,21 @@ class NoopMeter final : public Meter
new NoopObservableInstrument(name, description, unit));
}

nostd::shared_ptr<UpDownCounter<int64_t>> CreateInt64UpDownCounter(
nostd::unique_ptr<UpDownCounter<int64_t>> CreateInt64UpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<UpDownCounter<int64_t>>{
return nostd::unique_ptr<UpDownCounter<int64_t>>{
new NoopUpDownCounter<int64_t>(name, description, unit)};
}

nostd::shared_ptr<UpDownCounter<double>> CreateDoubleUpDownCounter(
nostd::unique_ptr<UpDownCounter<double>> CreateDoubleUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<UpDownCounter<double>>{
return nostd::unique_ptr<UpDownCounter<double>>{
new NoopUpDownCounter<double>(name, description, unit)};
}

Expand Down
12 changes: 6 additions & 6 deletions sdk/include/opentelemetry/sdk/metrics/meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class Meter final : public opentelemetry::metrics::Meter
std::unique_ptr<opentelemetry::sdk::instrumentationscope::InstrumentationScope> scope =
opentelemetry::sdk::instrumentationscope::InstrumentationScope::Create("")) noexcept;

nostd::shared_ptr<opentelemetry::metrics::Counter<uint64_t>> CreateUInt64Counter(
nostd::unique_ptr<opentelemetry::metrics::Counter<uint64_t>> CreateUInt64Counter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::Counter<double>> CreateDoubleCounter(
nostd::unique_ptr<opentelemetry::metrics::Counter<double>> CreateDoubleCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
Expand All @@ -55,12 +55,12 @@ class Meter final : public opentelemetry::metrics::Meter
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::Histogram<uint64_t>> CreateUInt64Histogram(
nostd::unique_ptr<opentelemetry::metrics::Histogram<uint64_t>> CreateUInt64Histogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::Histogram<double>> CreateDoubleHistogram(
nostd::unique_ptr<opentelemetry::metrics::Histogram<double>> CreateDoubleHistogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
Expand All @@ -75,12 +75,12 @@ class Meter final : public opentelemetry::metrics::Meter
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::UpDownCounter<int64_t>> CreateInt64UpDownCounter(
nostd::unique_ptr<opentelemetry::metrics::UpDownCounter<int64_t>> CreateInt64UpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;

nostd::shared_ptr<opentelemetry::metrics::UpDownCounter<double>> CreateDoubleUpDownCounter(
nostd::unique_ptr<opentelemetry::metrics::UpDownCounter<double>> CreateDoubleUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override;
Expand Down
24 changes: 12 additions & 12 deletions sdk/src/metrics/meter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Meter::Meter(
observable_registry_(new ObservableRegistry())
{}

nostd::shared_ptr<metrics::Counter<uint64_t>> Meter::CreateUInt64Counter(
nostd::unique_ptr<metrics::Counter<uint64_t>> Meter::CreateUInt64Counter(
nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
Expand All @@ -43,11 +43,11 @@ nostd::shared_ptr<metrics::Counter<uint64_t>> Meter::CreateUInt64Counter(
std::string{name.data(), name.size()}, std::string{description.data(), description.size()},
std::string{unit.data(), unit.size()}, InstrumentType::kCounter, InstrumentValueType::kLong};
auto storage = RegisterSyncMetricStorage(instrument_descriptor);
return nostd::shared_ptr<metrics::Counter<uint64_t>>(
return nostd::unique_ptr<metrics::Counter<uint64_t>>(
new LongCounter<uint64_t>(instrument_descriptor, std::move(storage)));
}

nostd::shared_ptr<metrics::Counter<double>> Meter::CreateDoubleCounter(
nostd::unique_ptr<metrics::Counter<double>> Meter::CreateDoubleCounter(
nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
Expand All @@ -57,7 +57,7 @@ nostd::shared_ptr<metrics::Counter<double>> Meter::CreateDoubleCounter(
std::string{unit.data(), unit.size()}, InstrumentType::kCounter,
InstrumentValueType::kDouble};
auto storage = RegisterSyncMetricStorage(instrument_descriptor);
return nostd::shared_ptr<metrics::Counter<double>>{
return nostd::unique_ptr<metrics::Counter<double>>{
new DoubleCounter(instrument_descriptor, std::move(storage))};
}

Expand Down Expand Up @@ -89,7 +89,7 @@ Meter::CreateDoubleObservableCounter(nostd::string_view name,
new ObservableInstrument(instrument_descriptor, std::move(storage), observable_registry_)};
}

nostd::shared_ptr<metrics::Histogram<uint64_t>> Meter::CreateUInt64Histogram(
nostd::unique_ptr<metrics::Histogram<uint64_t>> Meter::CreateUInt64Histogram(
nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
Expand All @@ -99,11 +99,11 @@ nostd::shared_ptr<metrics::Histogram<uint64_t>> Meter::CreateUInt64Histogram(
std::string{unit.data(), unit.size()}, InstrumentType::kHistogram,
InstrumentValueType::kLong};
auto storage = RegisterSyncMetricStorage(instrument_descriptor);
return nostd::shared_ptr<metrics::Histogram<uint64_t>>{
return nostd::unique_ptr<metrics::Histogram<uint64_t>>{
new LongHistogram<uint64_t>(instrument_descriptor, std::move(storage))};
}

nostd::shared_ptr<metrics::Histogram<double>> Meter::CreateDoubleHistogram(
nostd::unique_ptr<metrics::Histogram<double>> Meter::CreateDoubleHistogram(
nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
Expand All @@ -113,7 +113,7 @@ nostd::shared_ptr<metrics::Histogram<double>> Meter::CreateDoubleHistogram(
std::string{unit.data(), unit.size()}, InstrumentType::kHistogram,
InstrumentValueType::kDouble};
auto storage = RegisterSyncMetricStorage(instrument_descriptor);
return nostd::shared_ptr<metrics::Histogram<double>>{
return nostd::unique_ptr<metrics::Histogram<double>>{
new DoubleHistogram(instrument_descriptor, std::move(storage))};
}

Expand Down Expand Up @@ -145,7 +145,7 @@ nostd::shared_ptr<opentelemetry::metrics::ObservableInstrument> Meter::CreateDou
new ObservableInstrument(instrument_descriptor, std::move(storage), observable_registry_)};
}

nostd::shared_ptr<metrics::UpDownCounter<int64_t>> Meter::CreateInt64UpDownCounter(
nostd::unique_ptr<metrics::UpDownCounter<int64_t>> Meter::CreateInt64UpDownCounter(
nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
Expand All @@ -155,11 +155,11 @@ nostd::shared_ptr<metrics::UpDownCounter<int64_t>> Meter::CreateInt64UpDownCount
std::string{unit.data(), unit.size()}, InstrumentType::kUpDownCounter,
InstrumentValueType::kLong};
auto storage = RegisterSyncMetricStorage(instrument_descriptor);
return nostd::shared_ptr<metrics::UpDownCounter<int64_t>>{
return nostd::unique_ptr<metrics::UpDownCounter<int64_t>>{
new LongUpDownCounter(instrument_descriptor, std::move(storage))};
}

nostd::shared_ptr<metrics::UpDownCounter<double>> Meter::CreateDoubleUpDownCounter(
nostd::unique_ptr<metrics::UpDownCounter<double>> Meter::CreateDoubleUpDownCounter(
nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
Expand All @@ -169,7 +169,7 @@ nostd::shared_ptr<metrics::UpDownCounter<double>> Meter::CreateDoubleUpDownCount
std::string{unit.data(), unit.size()}, InstrumentType::kUpDownCounter,
InstrumentValueType::kDouble};
auto storage = RegisterSyncMetricStorage(instrument_descriptor);
return nostd::shared_ptr<metrics::UpDownCounter<double>>{
return nostd::unique_ptr<metrics::UpDownCounter<double>>{
new DoubleUpDownCounter(instrument_descriptor, std::move(storage))};
}

Expand Down
30 changes: 30 additions & 0 deletions sdk/test/metrics/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark")

cc_test(
name = "meter_test",
srcs = [
"meter_test.cc",
],
tags = [
"metrics",
"test",
],
deps = [
"//sdk/src/metrics",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "meter_provider_sdk_test",
srcs = [
Expand Down Expand Up @@ -112,6 +127,21 @@ cc_test(
],
)

cc_test(
name = "async_instruments_test",
srcs = [
"async_instruments_test.cc",
],
tags = [
"metrics",
"test",
],
deps = [
"//sdk/src/metrics",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "async_metric_storage_test",
srcs = [
Expand Down
4 changes: 2 additions & 2 deletions sdk/test/metrics/meter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ TEST(MeterTest, StressMultiThread)
MetricReader *metric_reader_ptr = nullptr;
auto meter = InitMeter(&metric_reader_ptr, "stress_test_meter");
std::atomic<unsigned> threadCount(0);
size_t numIterations = MAX_ITERATIONS_MT;
std::atomic<size_t> numIterations(MAX_ITERATIONS_MT);
std::atomic<bool> do_collect{false}, do_sync_create{true}, do_async_create{false};
std::vector<nostd::shared_ptr<opentelemetry::metrics::ObservableInstrument>>
observable_instruments;
std::vector<std::thread> meter_operation_threads;
size_t instrument_id = 0;
std::atomic<size_t> instrument_id(0);
while (numIterations--)
{
for (size_t i = 0; i < MAX_THREADS; i++)
Expand Down

1 comment on commit 9b8a4dc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'OpenTelemetry-cpp sdk Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 9b8a4dc Previous: 7fd76ee Ratio
BM_AttributeMapHash 359.8133982593274 ns/iter 171.07436482633634 ns/iter 2.10
BM_BaselineBuffer/1 12711389.064788818 ns/iter 531473.6366271973 ns/iter 23.92

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.