Skip to content

Commit 91dd15f

Browse files
authored
[API] Add InstrumentationScope attributes in TracerProvider::GetTracer() (#2371)
1 parent d9ad76f commit 91dd15f

File tree

8 files changed

+327
-23
lines changed

8 files changed

+327
-23
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ Increment the:
1919
[#2370](https://github.com/open-telemetry/opentelemetry-cpp/pull/2370)
2020
* [BUILD] Make WITH_OTLP_HTTP_SSL_PREVIEW mainstream
2121
[#2378](https://github.com/open-telemetry/opentelemetry-cpp/pull/2378)
22+
* [API] Add InstrumentationScope attributes in TracerProvider::GetTracer()
23+
[#2371](https://github.com/open-telemetry/opentelemetry-cpp/pull/2371)
2224

2325
Important changes:
2426

27+
* [API] Add InstrumentationScope attributes in TracerProvider::GetTracer()
28+
[#2371](https://github.com/open-telemetry/opentelemetry-cpp/pull/2371)
29+
* TracerProvider::GetTracer() now accepts InstrumentationScope attributes.
30+
* Because this is an `ABI` breaking change, the fix is only available
31+
with the `CMake` option `WITH_ABI_VERSION_2=ON`.
32+
* When building with `CMake` option `WITH_ABI_VERSION_1=ON` (by default)
33+
the `ABI` is unchanged, and the fix is not available.
34+
2535
* [BUILD] Make WITH_OTLP_HTTP_SSL_PREVIEW mainstream
2636
[#2378](https://github.com/open-telemetry/opentelemetry-cpp/pull/2378)
2737
* The experimental `CMake` option `WITH_OTLP_HTTP_SSL_PREVIEW`

api/include/opentelemetry/trace/noop.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,23 @@ class OPENTELEMETRY_EXPORT NoopTracerProvider final : public trace::TracerProvid
108108
: tracer_{nostd::shared_ptr<trace::NoopTracer>(new trace::NoopTracer)}
109109
{}
110110

111-
nostd::shared_ptr<trace::Tracer> GetTracer(nostd::string_view /* library_name */,
112-
nostd::string_view /* library_version */,
111+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
112+
nostd::shared_ptr<trace::Tracer> GetTracer(
113+
nostd::string_view /* name */,
114+
nostd::string_view /* version */,
115+
nostd::string_view /* schema_url */,
116+
const common::KeyValueIterable * /* attributes */) noexcept override
117+
{
118+
return tracer_;
119+
}
120+
#else
121+
nostd::shared_ptr<trace::Tracer> GetTracer(nostd::string_view /* name */,
122+
nostd::string_view /* version */,
113123
nostd::string_view /* schema_url */) noexcept override
114124
{
115125
return tracer_;
116126
}
127+
#endif
117128

118129
private:
119130
nostd::shared_ptr<trace::Tracer> tracer_;

api/include/opentelemetry/trace/tracer_provider.h

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#pragma once
55

6+
#include "opentelemetry/common/key_value_iterable.h"
7+
#include "opentelemetry/common/key_value_iterable_view.h"
68
#include "opentelemetry/nostd/shared_ptr.h"
79
#include "opentelemetry/nostd/string_view.h"
810
#include "opentelemetry/version.h"
@@ -20,15 +22,106 @@ class OPENTELEMETRY_EXPORT TracerProvider
2022
{
2123
public:
2224
virtual ~TracerProvider() = default;
25+
26+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
27+
28+
/**
29+
* Gets or creates a named Tracer instance (ABI).
30+
*
31+
* @since ABI_VERSION 2
32+
*
33+
* @param[in] name Tracer instrumentation scope
34+
* @param[in] version Instrumentation scope version
35+
* @param[in] schema_url Instrumentation scope schema URL
36+
* @param[in] attributes Instrumentation scope attributes (optional, may be nullptr)
37+
*/
38+
virtual nostd::shared_ptr<Tracer> GetTracer(
39+
nostd::string_view name,
40+
nostd::string_view version,
41+
nostd::string_view schema_url,
42+
const common::KeyValueIterable *attributes) noexcept = 0;
43+
44+
/**
45+
* Gets or creates a named Tracer instance (API helper).
46+
*
47+
* @since ABI_VERSION 2
48+
*
49+
* @param[in] name Tracer instrumentation scope
50+
* @param[in] version Instrumentation scope version, optional
51+
* @param[in] schema_url Instrumentation scope schema URL, optional
52+
*/
53+
nostd::shared_ptr<Tracer> GetTracer(nostd::string_view name,
54+
nostd::string_view version = "",
55+
nostd::string_view schema_url = "")
56+
{
57+
return GetTracer(name, version, schema_url, nullptr);
58+
}
59+
60+
/**
61+
* Gets or creates a named Tracer instance (API helper).
62+
*
63+
* @since ABI_VERSION 2
64+
*
65+
* @param[in] name Tracer instrumentation scope
66+
* @param[in] version Instrumentation scope version
67+
* @param[in] schema_url Instrumentation scope schema URL
68+
* @param[in] attributes Instrumentation scope attributes
69+
*/
70+
nostd::shared_ptr<Tracer> GetTracer(
71+
nostd::string_view name,
72+
nostd::string_view version,
73+
nostd::string_view schema_url,
74+
std::initializer_list<std::pair<nostd::string_view, common::AttributeValue>> attributes)
75+
{
76+
/* Build a container from std::initializer_list. */
77+
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>> attributes_span{
78+
attributes.begin(), attributes.end()};
79+
80+
/* Build a view on the container. */
81+
common::KeyValueIterableView<
82+
nostd::span<const std::pair<nostd::string_view, common::AttributeValue>>>
83+
iterable_attributes{attributes_span};
84+
85+
/* Add attributes using the view. */
86+
return GetTracer(name, version, schema_url, &iterable_attributes);
87+
}
88+
89+
/**
90+
* Gets or creates a named Tracer instance (API helper).
91+
*
92+
* @since ABI_VERSION 2
93+
*
94+
* @param[in] name Tracer instrumentation scope
95+
* @param[in] version Instrumentation scope version
96+
* @param[in] schema_url Instrumentation scope schema URL
97+
* @param[in] attributes Instrumentation scope attributes container
98+
*/
99+
template <class T,
100+
nostd::enable_if_t<common::detail::is_key_value_iterable<T>::value> * = nullptr>
101+
nostd::shared_ptr<Tracer> GetTracer(nostd::string_view name,
102+
nostd::string_view version,
103+
nostd::string_view schema_url,
104+
const T &attributes)
105+
{
106+
/* Build a view on the container. */
107+
common::KeyValueIterableView<T> iterable_attributes(attributes);
108+
109+
/* Add attributes using the view. */
110+
return GetTracer(name, version, schema_url, &iterable_attributes);
111+
}
112+
113+
#else
114+
23115
/**
24116
* Gets or creates a named tracer instance.
25117
*
26118
* Optionally a version can be passed to create a named and versioned tracer
27119
* instance.
28120
*/
29-
virtual nostd::shared_ptr<Tracer> GetTracer(nostd::string_view library_name,
30-
nostd::string_view library_version = "",
31-
nostd::string_view schema_url = "") noexcept = 0;
121+
virtual nostd::shared_ptr<Tracer> GetTracer(nostd::string_view name,
122+
nostd::string_view version = "",
123+
nostd::string_view schema_url = "") noexcept = 0;
124+
#endif
32125
};
33126
} // namespace trace
34127
OPENTELEMETRY_END_NAMESPACE

api/test/singleton/singleton_test.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,13 +264,25 @@ class MyTracerProvider : public trace::TracerProvider
264264
return result;
265265
}
266266

267-
nostd::shared_ptr<trace::Tracer> GetTracer(nostd::string_view /* library_name */,
268-
nostd::string_view /* library_version */,
267+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
268+
nostd::shared_ptr<trace::Tracer> GetTracer(
269+
nostd::string_view /* name */,
270+
nostd::string_view /* version */,
271+
nostd::string_view /* schema_url */,
272+
const common::KeyValueIterable * /* attributes */) noexcept override
273+
{
274+
nostd::shared_ptr<trace::Tracer> result(new MyTracer());
275+
return result;
276+
}
277+
#else
278+
nostd::shared_ptr<trace::Tracer> GetTracer(nostd::string_view /* name */,
279+
nostd::string_view /* version */,
269280
nostd::string_view /* schema_url */) noexcept override
270281
{
271282
nostd::shared_ptr<trace::Tracer> result(new MyTracer());
272283
return result;
273284
}
285+
#endif
274286
};
275287

276288
void setup_otel()

api/test/trace/provider_test.cc

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "opentelemetry/trace/provider.h"
55
#include "opentelemetry/nostd/shared_ptr.h"
6+
#include "opentelemetry/trace/tracer_provider.h"
67

78
#include <gtest/gtest.h>
89

@@ -14,12 +15,24 @@ namespace nostd = opentelemetry::nostd;
1415

1516
class TestProvider : public TracerProvider
1617
{
17-
nostd::shared_ptr<Tracer> GetTracer(nostd::string_view /* library_name */,
18-
nostd::string_view /* library_version */,
18+
19+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
20+
nostd::shared_ptr<Tracer> GetTracer(
21+
nostd::string_view /* name */,
22+
nostd::string_view /* version */,
23+
nostd::string_view /* schema_url */,
24+
const opentelemetry::common::KeyValueIterable * /* attributes */) noexcept override
25+
{
26+
return nostd::shared_ptr<Tracer>(nullptr);
27+
}
28+
#else
29+
nostd::shared_ptr<Tracer> GetTracer(nostd::string_view /* name */,
30+
nostd::string_view /* version */,
1931
nostd::string_view /* schema_url */) noexcept override
2032
{
2133
return nostd::shared_ptr<Tracer>(nullptr);
2234
}
35+
#endif
2336
};
2437

2538
TEST(Provider, GetTracerProviderDefault)

sdk/include/opentelemetry/sdk/trace/tracer_provider.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,23 @@ class TracerProvider final : public opentelemetry::trace::TracerProvider
6363

6464
~TracerProvider() override;
6565

66+
/*
67+
Make sure GetTracer() helpers from the API are seen in overload resolution.
68+
*/
69+
using opentelemetry::trace::TracerProvider::GetTracer;
70+
71+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
72+
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(
73+
nostd::string_view name,
74+
nostd::string_view version,
75+
nostd::string_view schema_url,
76+
const opentelemetry::common::KeyValueIterable *attributes) noexcept override;
77+
#else
6678
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(
67-
nostd::string_view library_name,
68-
nostd::string_view library_version = "",
69-
nostd::string_view schema_url = "") noexcept override;
79+
nostd::string_view name,
80+
nostd::string_view version = "",
81+
nostd::string_view schema_url = "") noexcept override;
82+
#endif
7083

7184
/**
7285
* Attaches a span processor to list of configured processors for this tracer provider.

sdk/src/trace/tracer_provider.cc

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,29 @@ TracerProvider::~TracerProvider()
5353
}
5454
}
5555

56+
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
5657
nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracer(
57-
nostd::string_view library_name,
58-
nostd::string_view library_version,
58+
nostd::string_view name,
59+
nostd::string_view version,
60+
nostd::string_view schema_url,
61+
const opentelemetry::common::KeyValueIterable *attributes) noexcept
62+
#else
63+
nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracer(
64+
nostd::string_view name,
65+
nostd::string_view version,
5966
nostd::string_view schema_url) noexcept
67+
#endif
6068
{
61-
if (library_name.data() == nullptr)
69+
#if OPENTELEMETRY_ABI_VERSION_NO < 2
70+
const opentelemetry::common::KeyValueIterable *attributes = nullptr;
71+
#endif
72+
73+
if (name.data() == nullptr)
6274
{
6375
OTEL_INTERNAL_LOG_ERROR("[TracerProvider::GetTracer] Library name is null.");
64-
library_name = "";
76+
name = "";
6577
}
66-
else if (library_name == "")
78+
else if (name == "")
6779
{
6880
OTEL_INTERNAL_LOG_ERROR("[TracerProvider::GetTracer] Library name is empty.");
6981
}
@@ -72,17 +84,20 @@ nostd::shared_ptr<trace_api::Tracer> TracerProvider::GetTracer(
7284

7385
for (auto &tracer : tracers_)
7486
{
75-
auto &tracer_lib = tracer->GetInstrumentationScope();
76-
if (tracer_lib.equal(library_name, library_version, schema_url))
87+
auto &tracer_scope = tracer->GetInstrumentationScope();
88+
if (tracer_scope.equal(name, version, schema_url))
7789
{
7890
return nostd::shared_ptr<trace_api::Tracer>{tracer};
7991
}
8092
}
8193

82-
auto lib = InstrumentationScope::Create(library_name, library_version, schema_url);
83-
tracers_.push_back(std::shared_ptr<opentelemetry::sdk::trace::Tracer>(
84-
new sdk::trace::Tracer(context_, std::move(lib))));
85-
return nostd::shared_ptr<trace_api::Tracer>{tracers_.back()};
94+
instrumentationscope::InstrumentationScopeAttributes attrs_map(attributes);
95+
auto scope =
96+
instrumentationscope::InstrumentationScope::Create(name, version, schema_url, attrs_map);
97+
98+
auto tracer = std::shared_ptr<Tracer>(new Tracer(context_, std::move(scope)));
99+
tracers_.push_back(tracer);
100+
return nostd::shared_ptr<trace_api::Tracer>{tracer};
86101
}
87102

88103
void TracerProvider::AddProcessor(std::unique_ptr<SpanProcessor> processor) noexcept

0 commit comments

Comments
 (0)