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 API/SDK] Change Meter API/SDK to return nostd::unique_ptr for Sync Instruments #1707

Merged
merged 7 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
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<long>> CreateLongCounter(
virtual nostd::unique_ptr<Counter<long>> CreateLongCounter(
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<long>> CreateLongHistogram(
virtual nostd::unique_ptr<Histogram<long>> CreateLongHistogram(
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<long>> CreateLongUpDownCounter(
virtual nostd::unique_ptr<UpDownCounter<long>> CreateLongUpDownCounter(
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,19 +87,19 @@ class NoopObservableInstrument : public ObservableInstrument
class NoopMeter final : public Meter
{
public:
nostd::shared_ptr<Counter<long>> CreateLongCounter(nostd::string_view name,
nostd::unique_ptr<Counter<long>> CreateLongCounter(nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<Counter<long>>{new NoopCounter<long>(name, description, unit)};
return nostd::unique_ptr<Counter<long>>{new NoopCounter<long>(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> CreateLongObservableCounter(
Expand All @@ -120,20 +120,20 @@ class NoopMeter final : public Meter
new NoopObservableInstrument(name, description, unit));
}

nostd::shared_ptr<Histogram<long>> CreateLongHistogram(
nostd::unique_ptr<Histogram<long>> CreateLongHistogram(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<Histogram<long>>{new NoopHistogram<long>(name, description, unit)};
return nostd::unique_ptr<Histogram<long>>{new NoopHistogram<long>(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> CreateLongObservableGauge(
Expand All @@ -154,21 +154,21 @@ class NoopMeter final : public Meter
new NoopObservableInstrument(name, description, unit));
}

nostd::shared_ptr<UpDownCounter<long>> CreateLongUpDownCounter(
nostd::unique_ptr<UpDownCounter<long>> CreateLongUpDownCounter(
nostd::string_view name,
nostd::string_view description = "",
nostd::string_view unit = "") noexcept override
{
return nostd::shared_ptr<UpDownCounter<long>>{
return nostd::unique_ptr<UpDownCounter<long>>{
new NoopUpDownCounter<long>(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<long>> CreateLongCounter(
nostd::unique_ptr<opentelemetry::metrics::Counter<long>> CreateLongCounter(
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<long>> CreateLongHistogram(
nostd::unique_ptr<opentelemetry::metrics::Histogram<long>> CreateLongHistogram(
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<long>> CreateLongUpDownCounter(
nostd::unique_ptr<opentelemetry::metrics::UpDownCounter<long>> CreateLongUpDownCounter(
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 @@ -33,19 +33,19 @@ Meter::Meter(
observable_registry_(new ObservableRegistry())
{}

nostd::shared_ptr<metrics::Counter<long>> Meter::CreateLongCounter(nostd::string_view name,
nostd::unique_ptr<metrics::Counter<long>> Meter::CreateLongCounter(nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
{
InstrumentDescriptor instrument_descriptor = {
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<long>>(
return nostd::unique_ptr<metrics::Counter<long>>(
new LongCounter(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 @@ -55,7 +55,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 @@ -87,7 +87,7 @@ Meter::CreateDoubleObservableCounter(nostd::string_view name,
new ObservableInstrument(instrument_descriptor, std::move(storage), observable_registry_)};
}

nostd::shared_ptr<metrics::Histogram<long>> Meter::CreateLongHistogram(
nostd::unique_ptr<metrics::Histogram<long>> Meter::CreateLongHistogram(
nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
Expand All @@ -97,11 +97,11 @@ nostd::shared_ptr<metrics::Histogram<long>> Meter::CreateLongHistogram(
std::string{unit.data(), unit.size()}, InstrumentType::kHistogram,
InstrumentValueType::kLong};
auto storage = RegisterSyncMetricStorage(instrument_descriptor);
return nostd::shared_ptr<metrics::Histogram<long>>{
return nostd::unique_ptr<metrics::Histogram<long>>{
new LongHistogram(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 @@ -111,7 +111,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 @@ -143,7 +143,7 @@ nostd::shared_ptr<opentelemetry::metrics::ObservableInstrument> Meter::CreateDou
new ObservableInstrument(instrument_descriptor, std::move(storage), observable_registry_)};
}

nostd::shared_ptr<metrics::UpDownCounter<long>> Meter::CreateLongUpDownCounter(
nostd::unique_ptr<metrics::UpDownCounter<long>> Meter::CreateLongUpDownCounter(
nostd::string_view name,
nostd::string_view description,
nostd::string_view unit) noexcept
Expand All @@ -153,11 +153,11 @@ nostd::shared_ptr<metrics::UpDownCounter<long>> Meter::CreateLongUpDownCounter(
std::string{unit.data(), unit.size()}, InstrumentType::kUpDownCounter,
InstrumentValueType::kLong};
auto storage = RegisterSyncMetricStorage(instrument_descriptor);
return nostd::shared_ptr<metrics::UpDownCounter<long>>{
return nostd::unique_ptr<metrics::UpDownCounter<long>>{
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 @@ -167,7 +167,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