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

[API] Add InstrumentationScope attributes in MeterProvider::GetMeter() #2224

Merged
merged 21 commits into from
Sep 30, 2023
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
15 changes: 15 additions & 0 deletions api/include/opentelemetry/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace common
{
class KeyValueIterable;
}

namespace metrics
{

Expand All @@ -26,9 +31,19 @@ class MeterProvider
* Optionally a version can be passed to create a named and versioned Meter
* instance.
*/

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
virtual nostd::shared_ptr<Meter> GetMeter(
nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "",
const common::KeyValueIterable *attributes = nullptr) noexcept = 0;
#else
virtual nostd::shared_ptr<Meter> GetMeter(nostd::string_view library_name,
nostd::string_view library_version = "",
nostd::string_view schema_url = "") noexcept = 0;
#endif

#ifdef ENABLE_REMOVE_METER_PREVIEW
marcalff marked this conversation as resolved.
Show resolved Hide resolved
virtual void RemoveMeter(nostd::string_view library_name,
nostd::string_view library_version = "",
Expand Down
11 changes: 11 additions & 0 deletions api/include/opentelemetry/metrics/noop.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,23 @@ class NoopMeterProvider final : public MeterProvider
public:
NoopMeterProvider() : meter_{nostd::shared_ptr<Meter>(new NoopMeter)} {}

#if OPENTELEMETRY_ABI_VERSION_NO >= 2
nostd::shared_ptr<Meter> GetMeter(
nostd::string_view /* library_name */,
nostd::string_view /* library_version */,
nostd::string_view /* schema_url */,
const common::KeyValueIterable * /* attributes */) noexcept override
{
return meter_;
}
#else
nostd::shared_ptr<Meter> GetMeter(nostd::string_view /* library_name */,
nostd::string_view /* library_version */,
nostd::string_view /* schema_url */) noexcept override
{
return meter_;
}
#endif

#ifdef ENABLE_REMOVE_METER_PREVIEW
void RemoveMeter(nostd::string_view /* name */,
Expand Down
3 changes: 2 additions & 1 deletion api/include/opentelemetry/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include "opentelemetry/common/macros.h"
#include "opentelemetry/detail/preprocessor.h"

#define OPENTELEMETRY_ABI_VERSION_NO 1
// For proof of concept only, testing version 2 build.
#define OPENTELEMETRY_ABI_VERSION_NO 2
#define OPENTELEMETRY_VERSION "1.10.0"
#define OPENTELEMETRY_VERSION_MAJOR 1
#define OPENTELEMETRY_VERSION_MINOR 10
Expand Down
17 changes: 15 additions & 2 deletions sdk/include/opentelemetry/sdk/common/attribute_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ struct AttributeConverter
class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
{
public:
// Contruct empty attribute map
// Construct empty attribute map
AttributeMap() : std::unordered_map<std::string, OwnedAttributeValue>() {}

// Contruct attribute map and populate with attributes
// Construct attribute map and populate with attributes
AttributeMap(const opentelemetry::common::KeyValueIterable &attributes) : AttributeMap()
{
attributes.ForEachKeyValue(
Expand All @@ -118,6 +118,19 @@ class AttributeMap : public std::unordered_map<std::string, OwnedAttributeValue>
});
}

// Construct attribute map and populate with optional attributes
AttributeMap(const opentelemetry::common::KeyValueIterable *attributes) : AttributeMap()
{
if (attributes != nullptr)
{
attributes->ForEachKeyValue(
[&](nostd::string_view key, opentelemetry::common::AttributeValue value) noexcept {
SetAttribute(key, value);
return true;
Copy link
Member

@lalitb lalitb Sep 26, 2023

Choose a reason for hiding this comment

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

nit - some code duplication, which can be moved to a separate utils method. But don't see it really required to fix

});
}
}

// Construct map from initializer list by applying `SetAttribute` transform for every attribute
AttributeMap(
std::initializer_list<std::pair<nostd::string_view, opentelemetry::common::AttributeValue>>
Expand Down
8 changes: 8 additions & 0 deletions sdk/include/opentelemetry/sdk/metrics/meter_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,18 @@ 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::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "",
const opentelemetry::common::KeyValueIterable *attributes = nullptr) noexcept override;
#else
nostd::shared_ptr<opentelemetry::metrics::Meter> GetMeter(
nostd::string_view name,
nostd::string_view version = "",
nostd::string_view schema_url = "") noexcept override;
#endif

#ifdef ENABLE_REMOVE_METER_PREVIEW
void RemoveMeter(nostd::string_view name,
Expand Down
19 changes: 16 additions & 3 deletions sdk/src/metrics/meter_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ MeterProvider::MeterProvider(std::unique_ptr<ViewRegistry> views,
nostd::shared_ptr<metrics_api::Meter> MeterProvider::GetMeter(
nostd::string_view name,
nostd::string_view version,
nostd::string_view schema_url) noexcept
nostd::string_view schema_url
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
,
const opentelemetry::common::KeyValueIterable *attributes
#endif
) noexcept
{
#if OPENTELEMETRY_ABI_VERSION_NO < 2
const opentelemetry::common::KeyValueIterable *attributes = nullptr;
#endif

if (name.data() == nullptr || name == "")
{
OTEL_INTERNAL_LOG_WARN("[MeterProvider::GetMeter] Library name is empty.");
Expand All @@ -53,8 +62,12 @@ nostd::shared_ptr<metrics_api::Meter> MeterProvider::GetMeter(
return nostd::shared_ptr<metrics_api::Meter>{meter};
}
}
auto lib = instrumentationscope::InstrumentationScope::Create(name, version, schema_url);
auto meter = std::shared_ptr<Meter>(new Meter(context_, std::move(lib)));

instrumentationscope::InstrumentationScopeAttributes attrs_map(attributes);
auto scope =
instrumentationscope::InstrumentationScope::Create(name, version, schema_url, attrs_map);

auto meter = std::shared_ptr<Meter>(new Meter(context_, std::move(scope)));
context_->AddMeter(meter);
return nostd::shared_ptr<metrics_api::Meter>{meter};
}
Expand Down