Skip to content

Commit

Permalink
Implemented support for key value iterable.
Browse files Browse the repository at this point in the history
Added unit tests.
  • Loading branch information
marcalff committed Sep 25, 2023
1 parent acad9e7 commit d4ec4af
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 26 deletions.
87 changes: 71 additions & 16 deletions api/include/opentelemetry/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ class MeterProvider
virtual ~MeterProvider() = default;

#if OPENTELEMETRY_ABI_VERSION_NO >= 2

/**
* Gets or creates a named Meter instance.
* Gets or creates a named Meter instance (ABI).
*
* @since ABI_VERSION 2
*
Expand All @@ -35,42 +36,96 @@ class MeterProvider
* @param[in] schema_url Instrumentation scope schema URL, optional
* @param[in] attributes Instrumentation scope attributes, optional
*/
virtual nostd::shared_ptr<Meter> GetMeter(
virtual nostd::shared_ptr<Meter> DoGetMeter(
nostd::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "",
const common::KeyValueIterable *attributes = nullptr) noexcept = 0;
#else
nostd::string_view version,
nostd::string_view schema_url,
const common::KeyValueIterable *attributes) noexcept = 0;

/**
* Gets or creates a named Meter instance.
* Gets or creates a named Meter instance (API helper).
*
* @since ABI_VERSION 1
* @since ABI_VERSION 2
*
* @param[in] name Meter instrumentation scope
* @param[in] version Instrumentation scope version, optional
* @param[in] schema_url Instrumentation scope schema URL, optional
* @param[in] version Instrumentation scope version
* @param[in] schema_url Instrumentation scope schema URL
* @param[in] attributes Instrumentation scope attributes
*/
virtual nostd::shared_ptr<Meter> GetMeter(nostd::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "") noexcept = 0;
#endif
nostd::shared_ptr<Meter> GetMeter(nostd::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "",
const common::KeyValueIterable *attributes = nullptr)
{
return DoGetMeter(name, version, schema_url, attributes);
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
/**
* Gets or creates a named Meter instance (API helper).
*
* @since ABI_VERSION 2
*
* @param[in] name Meter instrumentation scope
* @param[in] version Instrumentation scope version
* @param[in] schema_url Instrumentation scope schema URL
* @param[in] attributes Instrumentation scope attributes
*/
nostd::shared_ptr<Meter> GetMeter(
nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url,
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes)
{
/* Build a container from std::initializer_list. */
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> attributes_span{
attributes.begin(), attributes.end()};

/* Build a view on the container. */
common::KeyValueIterableView<
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>>
iterable_attributes{attributes_span};

return GetMeter(name, version, schema_url, &iterable_attributes);
/* Add attributes using the view. */
return DoGetMeter(name, version, schema_url, &iterable_attributes);
}

/**
* Gets or creates a named Meter instance (API helper).
*
* @since ABI_VERSION 2
*
* @param[in] name Meter instrumentation scope
* @param[in] version Instrumentation scope version
* @param[in] schema_url Instrumentation scope schema URL
* @param[in] attributes Instrumentation scope attributes
*/
template <class T,
nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
nostd::shared_ptr<Meter> GetMeter(nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url,
const T &attributes)
{
/* Build a view on the container. */
common::KeyValueIterableView<T> iterable_attributes(attributes);

/* Add attributes using the view. */
return DoGetMeter(name, version, schema_url, &iterable_attributes);
}

#else
/**
* Gets or creates a named Meter instance (ABI)
*
* @since ABI_VERSION 1
*
* @param[in] name Meter instrumentation scope
* @param[in] version Instrumentation scope version, optional
* @param[in] schema_url Instrumentation scope schema URL, optional
*/
virtual nostd::shared_ptr<Meter> GetMeter(nostd::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "") noexcept = 0;
#endif

#ifdef ENABLE_REMOVE_METER_PREVIEW
Expand Down
2 changes: 1 addition & 1 deletion api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class NoopMeterProvider final : public MeterProvider
NoopMeterProvider() : meter_{nostd::shared_ptr<Meter>(new NoopMeter)} {}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<Meter> GetMeter(
nostd::shared_ptr<Meter> DoGetMeter(
nostd::string_view /* name */,
nostd::string_view /* version */,
nostd::string_view /* schema_url */,
Expand Down
8 changes: 4 additions & 4 deletions sdk/include/opentelemetry/sdk/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class MeterProvider final : public opentelemetry::metrics::MeterProvider
explicit MeterProvider(std::unique_ptr<MeterContext> context) noexcept;

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<opentelemetry::metrics::Meter> GetMeter(
nostd::shared_ptr<opentelemetry::metrics::Meter> DoGetMeter(
nostd::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "",
const opentelemetry::common::KeyValueIterable *attributes = nullptr) noexcept override;
nostd::string_view version,
nostd::string_view schema_url,
const opentelemetry::common::KeyValueIterable *attributes) noexcept override;
#else
nostd::shared_ptr<opentelemetry::metrics::Meter> GetMeter(
nostd::string_view name,
Expand Down
13 changes: 8 additions & 5 deletions sdk/src/metrics/meter_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ MeterProvider::MeterProvider(std::unique_ptr<ViewRegistry> views,
OTEL_INTERNAL_LOG_DEBUG("[MeterProvider] MeterProvider created.");
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<metrics_api::Meter> MeterProvider::DoGetMeter(
nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url,
const opentelemetry::common::KeyValueIterable *attributes) noexcept
#else
nostd::shared_ptr<metrics_api::Meter> MeterProvider::GetMeter(
nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
,
const opentelemetry::common::KeyValueIterable *attributes
nostd::string_view schema_url) noexcept
#endif
) noexcept
{
#if OPENTELEMETRY_ABI_VERSION_NO < 2
const opentelemetry::common::KeyValueIterable *attributes = nullptr;
Expand Down
64 changes: 64 additions & 0 deletions sdk/test/metrics/meter_provider_sdk_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,70 @@ TEST(MeterProvider, GetMeter)
mp1.Shutdown();
}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
TEST(MeterProvider, GetMeterAbiv2)
{
MeterProvider mp;

auto m1 = mp.GetMeter("name1", "version1", "url1");

auto m2 = mp.GetMeter("name2", "version2", "url2", nullptr);

auto m3 = mp.GetMeter("name3", "version3", "url3", {{"accept_single_attr", true}});

std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> attr4 = {
"accept_single_attr", true};

auto m4 = mp.GetMeter("name4", "version4", "url4", {attr4});

auto m5 = mp.GetMeter("name5", "version5", "url5", {{"foo", "1"}, {"bar", "2"}});

std::initializer_list<
std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue>>
attrs6 = {{"foo", "1"}, {"bar", "2"}};

auto m6 = mp.GetMeter("name6", "version6", "url6", attrs6);

typedef std::pair<opentelemetry::nostd::string_view, opentelemetry::common::AttributeValue> KV;

std::initializer_list<KV> attrs7 = {{"foo", "1"}, {"bar", "2"}};
auto m7 = mp.GetMeter("name7", "version7", "url7", attrs6);

auto m8 = mp.GetMeter("name8", "version8", "url8",
{{"a", "string"},
{"b", false},
{"c", 314159},
{"d", (unsigned int)314159},
{"e", (int32_t)-20},
{"f", (uint32_t)20},
{"g", (int64_t)-20},
{"h", (uint64_t)20},
{"i", 3.1},
{"j", "string"}});

std::map<std::string, opentelemetry::common::AttributeValue> attr9{
{"a", "string"}, {"b", false}, {"c", 314159}, {"d", (unsigned int)314159},
{"e", (int32_t)-20}, {"f", (uint32_t)20}, {"g", (int64_t)-20}, {"h", (uint64_t)20},
{"i", 3.1}, {"j", "string"}};

auto m9 = mp.GetMeter("name9", "version9", "url9", attr9);

ASSERT_NE(nullptr, m1);
ASSERT_NE(nullptr, m2);
ASSERT_NE(nullptr, m3);
ASSERT_NE(nullptr, m4);
ASSERT_NE(nullptr, m5);
ASSERT_NE(nullptr, m6);
ASSERT_NE(nullptr, m7);
ASSERT_NE(nullptr, m8);
ASSERT_NE(nullptr, m9);

// cleanup properly without crash
mp.ForceFlush();
mp.Shutdown();
}
#endif /* OPENTELEMETRY_ABI_VERSION_NO >= 2 */

#ifdef ENABLE_REMOVE_METER_PREVIEW
TEST(MeterProvider, RemoveMeter)
{
Expand Down

0 comments on commit d4ec4af

Please sign in to comment.