Skip to content

Commit 2c7599a

Browse files
authored
impl(otel): add a function to create a span with links (#12857)
1 parent 4f69589 commit 2c7599a

2 files changed

Lines changed: 100 additions & 18 deletions

File tree

google/cloud/internal/opentelemetry.cc

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,52 @@ opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(
3333
return provider->GetTracer("gcloud-cpp", version_string());
3434
}
3535

36-
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
37-
opentelemetry::nostd::string_view name) {
36+
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpanImpl(
37+
opentelemetry::nostd::string_view name,
38+
opentelemetry::common::KeyValueIterable const& attributes,
39+
opentelemetry::trace::SpanContextKeyValueIterable const& links) {
3840
opentelemetry::trace::StartSpanOptions options;
3941
options.kind = opentelemetry::trace::SpanKind::kClient;
40-
return GetTracer(CurrentOptions())->StartSpan(name, options);
42+
return GetTracer(CurrentOptions())
43+
->StartSpan(name, attributes, links, options);
44+
}
45+
46+
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
47+
opentelemetry::nostd::string_view name) {
48+
return MakeSpanImpl(name);
4149
}
4250

4351
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
4452
opentelemetry::nostd::string_view name,
4553
std::initializer_list<std::pair<opentelemetry::nostd::string_view,
4654
opentelemetry::common::AttributeValue>>
4755
attributes) {
48-
opentelemetry::trace::StartSpanOptions options;
49-
options.kind = opentelemetry::trace::SpanKind::kClient;
50-
return GetTracer(CurrentOptions())->StartSpan(name, attributes, options);
56+
return MakeSpan(name, attributes, {});
57+
}
58+
59+
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
60+
opentelemetry::nostd::string_view name,
61+
std::initializer_list<std::pair<opentelemetry::nostd::string_view,
62+
opentelemetry::common::AttributeValue>>
63+
attributes,
64+
std::initializer_list<
65+
std::pair<opentelemetry::trace::SpanContext,
66+
std::initializer_list<
67+
std::pair<opentelemetry::nostd::string_view,
68+
opentelemetry::common::AttributeValue>>>>
69+
links) {
70+
return MakeSpan(
71+
name,
72+
opentelemetry::nostd::span<
73+
std::pair<opentelemetry::nostd::string_view,
74+
opentelemetry::common::AttributeValue> const>{
75+
attributes.begin(), attributes.end()},
76+
opentelemetry::nostd::span<
77+
std::pair<opentelemetry::trace::SpanContext,
78+
std::initializer_list<std::pair<
79+
opentelemetry::nostd::string_view,
80+
opentelemetry::common::AttributeValue>>> const>{
81+
links.begin(), links.end()});
5182
}
5283

5384
void EndSpanImpl(opentelemetry::trace::Span& span, Status const& status) {

google/cloud/internal/opentelemetry.h

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,30 @@ opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> GetTracer(
5555
/**
5656
* Start a [span] using the current [tracer].
5757
*
58-
* The current tracer is determined by the prevailing `CurrentOptions()`.
58+
* The current tracer is determined by the prevailing `CurrentOptions()`. Each
59+
* span is set as a client span.
5960
*
6061
* @see https://opentelemetry.io/docs/instrumentation/cpp/manual/#start-a-span
6162
*
6263
* [span]:
6364
* https://opentelemetry.io/docs/concepts/signals/traces/#spans-in-opentelemetry
6465
* [tracer]: https://opentelemetry.io/docs/concepts/signals/traces/#tracer
6566
*/
67+
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpanImpl(
68+
opentelemetry::nostd::string_view name,
69+
opentelemetry::common::KeyValueIterable const& attributes =
70+
opentelemetry::common::NoopKeyValueIterable(),
71+
opentelemetry::trace::SpanContextKeyValueIterable const& links =
72+
opentelemetry::trace::NullSpanContext());
73+
74+
/**
75+
* Start a span with a @p name.
76+
*/
6677
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
6778
opentelemetry::nostd::string_view name);
6879

6980
/**
70-
* Start a [span] using the current [tracer] with the specified attributes.
71-
*
72-
* The current tracer is determined by the prevailing `CurrentOptions()`.
73-
*
74-
* @see https://opentelemetry.io/docs/instrumentation/cpp/manual/#start-a-span
75-
*
76-
* [span]:
77-
* https://opentelemetry.io/docs/concepts/signals/traces/#spans-in-opentelemetry
78-
* [tracer]: https://opentelemetry.io/docs/concepts/signals/traces/#tracer
81+
* Start a span with a @p name and @p attributes using an initializer list.
7982
*/
8083
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
8184
opentelemetry::nostd::string_view name,
@@ -84,9 +87,57 @@ opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
8487
attributes);
8588

8689
/**
87-
* Extracts information from a `Status` and adds it to a span.
90+
* Start a span with a @p name, @p attributes using an initializer list, and @p
91+
* links using an initializer lists.
92+
*/
93+
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
94+
opentelemetry::nostd::string_view name,
95+
std::initializer_list<std::pair<opentelemetry::nostd::string_view,
96+
opentelemetry::common::AttributeValue>>
97+
attributes,
98+
std::initializer_list<
99+
std::pair<opentelemetry::trace::SpanContext,
100+
std::initializer_list<
101+
std::pair<opentelemetry::nostd::string_view,
102+
opentelemetry::common::AttributeValue>>>>
103+
links);
104+
105+
/**
106+
* Start a span with a @p name, @p attributes, and @p links.
107+
*/
108+
template <class T, class U>
109+
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
110+
opentelemetry::nostd::string_view name, T const& attributes,
111+
U const& links) {
112+
return MakeSpanImpl(
113+
name, opentelemetry::common::KeyValueIterableView<T>(attributes),
114+
opentelemetry::trace::SpanContextKeyValueIterableView<U>(links));
115+
}
116+
117+
/**
118+
* Start a span with a @p name, @p attributes, and @p links where attributes
119+
* uses an initializer list.
120+
*/
121+
template <class T>
122+
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
123+
opentelemetry::nostd::string_view name,
124+
std::initializer_list<std::pair<opentelemetry::nostd::string_view,
125+
opentelemetry::common::AttributeValue>>
126+
attributes,
127+
T const& links) {
128+
return MakeSpan(
129+
name,
130+
opentelemetry::nostd::span<
131+
std::pair<opentelemetry::nostd::string_view,
132+
opentelemetry::common::AttributeValue> const>{
133+
attributes.begin(), attributes.end()},
134+
opentelemetry::trace::SpanContextKeyValueIterableView<T>(links));
135+
}
136+
137+
/**
138+
* Extracts information from a `status` and adds it to a span.
88139
*
89-
* This method will end the span, and set its [span status], accordingly. Other
140+
* This method will end the span, and set its [span status], accordingly. other
90141
* details, such as error information, will be set as [attributes] on the span.
91142
*
92143
* @see https://opentelemetry.io/docs/concepts/signals/traces/#spans-in-opentelemetry

0 commit comments

Comments
 (0)