Skip to content

Commit dc15309

Browse files
authored
[CODE HEALTH] Fix bugprone-exception-escape in Tracer::StartSpan() (#4672)
1 parent 4340811 commit dc15309

2 files changed

Lines changed: 62 additions & 111 deletions

File tree

‎.github/workflows/clang-tidy.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
matrix:
1818
include:
1919
- cmake_options: all-options-abiv1-preview
20-
warning_limit: 205
20+
warning_limit: 204
2121
- cmake_options: all-options-abiv2-preview
22-
warning_limit: 219
22+
warning_limit: 218
2323
env:
2424
CC: /usr/bin/clang-22
2525
CXX: /usr/bin/clang++-22

‎sdk/src/trace/tracer.cc‎

Lines changed: 60 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
#include <chrono>
55
#include <cstdint>
66
#include <mutex>
7-
#include <new>
87
#include <utility>
98

10-
#include "opentelemetry/common/key_value_iterable.h"
119
#include "opentelemetry/context/context.h"
1210
#include "opentelemetry/context/runtime_context.h"
1311
#include "opentelemetry/nostd/shared_ptr.h"
@@ -22,9 +20,7 @@
2220
#include "opentelemetry/sdk/trace/tracer_context.h"
2321
#include "opentelemetry/trace/context.h"
2422
#include "opentelemetry/trace/default_span.h"
25-
#include "opentelemetry/trace/span.h"
2623
#include "opentelemetry/trace/span_context.h"
27-
#include "opentelemetry/trace/span_context_kv_iterable.h"
2824
#include "opentelemetry/trace/span_id.h"
2925
#include "opentelemetry/trace/span_startoptions.h"
3026
#include "opentelemetry/trace/trace_flags.h"
@@ -34,82 +30,33 @@
3430

3531
#include "src/trace/span.h"
3632

37-
OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk
33+
OPENTELEMETRY_BEGIN_NAMESPACE
34+
namespace sdk
35+
{
36+
namespace trace
3837
{
39-
namespace trace
40-
{
41-
42-
namespace
43-
{
4438

45-
nostd::shared_ptr<opentelemetry::trace::Span> MakeNonRecordingSpan(
46-
opentelemetry::trace::SpanContext &&span_context) noexcept
47-
{
48-
#if OPENTELEMETRY_HAVE_EXCEPTIONS
49-
try
50-
{
51-
#endif
52-
return {std::make_shared<opentelemetry::trace::DefaultSpan>(std::move(span_context))};
39+
Tracer::Tracer(std::shared_ptr<TracerContext> context,
40+
std::unique_ptr<InstrumentationScope> instrumentation_scope)
41+
: instrumentation_scope_{std::move(instrumentation_scope)},
42+
context_{std::move(context)},
43+
tracer_config_(context_->GetTracerConfigurator().ComputeConfig(*instrumentation_scope_)),
44+
noop_span_{std::make_shared<opentelemetry::trace::DefaultSpan>(
45+
opentelemetry::trace::SpanContext::GetInvalid())}
46+
{
47+
UpdateEnabled(tracer_config_.IsEnabled());
48+
}
49+
50+
nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
51+
nostd::string_view name,
52+
const opentelemetry::common::KeyValueIterable &attributes,
53+
const opentelemetry::trace::SpanContextKeyValueIterable &links,
54+
const opentelemetry::trace::StartSpanOptions &options) noexcept
55+
{
5356
#if OPENTELEMETRY_HAVE_EXCEPTIONS
54-
}
55-
catch (const std::bad_alloc &)
56-
{
57-
return {};
58-
}
59-
#else
60-
return nostd::shared_ptr<opentelemetry::trace::Span>{
61-
new (std::nothrow) opentelemetry::trace::DefaultSpan(std::move(span_context))};
62-
#endif
63-
}
64-
65-
nostd::shared_ptr<opentelemetry::trace::Span> MakeSpan(
66-
std::shared_ptr<Tracer> &&tracer,
67-
nostd::string_view name,
68-
const opentelemetry::common::KeyValueIterable &attributes,
69-
const opentelemetry::trace::SpanContextKeyValueIterable &links,
70-
const opentelemetry::trace::StartSpanOptions &options,
71-
const opentelemetry::sdk::trace::SamplingResult &sampling_result,
72-
const opentelemetry::trace::SpanContext &parent_context,
73-
opentelemetry::trace::SpanContext &&span_context) noexcept
57+
try
7458
{
75-
#if OPENTELEMETRY_HAVE_EXCEPTIONS
76-
try
77-
{
78-
#endif
79-
return {std::make_shared<Span>(std::move(tracer), name, attributes, links, options,
80-
sampling_result, parent_context, std::move(span_context))};
81-
#if OPENTELEMETRY_HAVE_EXCEPTIONS
82-
}
83-
catch (const std::bad_alloc &)
84-
{
85-
return {};
86-
}
87-
#else
88-
return nostd::shared_ptr<opentelemetry::trace::Span>{
89-
new (std::nothrow) Span{std::move(tracer), name, attributes, links, options,
90-
sampling_result, parent_context, std::move(span_context)}};
9159
#endif
92-
}
93-
94-
} // namespace
95-
96-
Tracer::Tracer(std::shared_ptr<TracerContext> context,
97-
std::unique_ptr<InstrumentationScope> instrumentation_scope)
98-
: instrumentation_scope_{std::move(instrumentation_scope)},
99-
context_{std::move(context)},
100-
tracer_config_(context_->GetTracerConfigurator().ComputeConfig(*instrumentation_scope_)),
101-
noop_span_{std::make_shared<opentelemetry::trace::DefaultSpan>(
102-
opentelemetry::trace::SpanContext::GetInvalid())}
103-
{
104-
UpdateEnabled(tracer_config_.IsEnabled());
105-
}
106-
107-
nostd::shared_ptr<opentelemetry::trace::Span> Tracer::StartSpan(
108-
nostd::string_view name,
109-
const opentelemetry::common::KeyValueIterable &attributes,
110-
const opentelemetry::trace::SpanContextKeyValueIterable &links,
111-
const opentelemetry::trace::StartSpanOptions &options) noexcept
112-
{
11360
// Check if the tracer is enabled using the API Tracer::Enabled() accessor if available.
11461
if (!Enabled())
11562
{
@@ -200,56 +147,60 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace sdk
200147
opentelemetry::trace::SpanContext span_context(trace_id, span_id, trace_flags, false,
201148
get_trace_state());
202149

150+
nostd::shared_ptr<opentelemetry::trace::Span> span;
151+
203152
if (!sampling_result.IsRecording())
204153
{
205-
auto non_recording_span = MakeNonRecordingSpan(std::move(span_context));
206-
207-
if (!non_recording_span)
208-
{
209-
return noop_span_;
210-
}
211-
return non_recording_span;
154+
span = nostd::shared_ptr<opentelemetry::trace::Span>{
155+
std::make_shared<opentelemetry::trace::DefaultSpan>(std::move(span_context))};
212156
}
213-
214-
auto span = MakeSpan(shared_from_this(), name, attributes, links, options, sampling_result,
215-
parent_context, std::move(span_context));
216-
if (!span)
157+
else
217158
{
218-
return noop_span_;
159+
span = nostd::shared_ptr<opentelemetry::trace::Span>{
160+
std::make_shared<Span>(shared_from_this(), name, attributes, links, options,
161+
sampling_result, parent_context, std::move(span_context))};
219162
}
163+
220164
return span;
165+
#if OPENTELEMETRY_HAVE_EXCEPTIONS
221166
}
167+
catch (...)
168+
{
169+
return noop_span_;
170+
}
171+
#endif
172+
}
222173

223-
void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept
174+
void Tracer::ForceFlushWithMicroseconds(uint64_t timeout) noexcept
175+
{
176+
if (context_)
224177
{
225-
if (context_)
226-
{
227-
context_->ForceFlush(
228-
std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(timeout)});
229-
}
178+
context_->ForceFlush(
179+
std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(timeout)});
230180
}
181+
}
231182

232-
void Tracer::CloseWithMicroseconds(uint64_t timeout) noexcept
183+
void Tracer::CloseWithMicroseconds(uint64_t timeout) noexcept
184+
{
185+
// Trace context is shared by many tracers.So we just call ForceFlush to flush all pending spans
186+
// and do not shutdown it.
187+
if (context_)
233188
{
234-
// Trace context is shared by many tracers.So we just call ForceFlush to flush all pending spans
235-
// and do not shutdown it.
236-
if (context_)
237-
{
238-
context_->ForceFlush(
239-
std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(timeout)});
240-
}
189+
context_->ForceFlush(
190+
std::chrono::microseconds{static_cast<std::chrono::microseconds::rep>(timeout)});
241191
}
192+
}
242193

243-
void Tracer::UpdateTracerConfig(TracerConfig config) noexcept
194+
void Tracer::UpdateTracerConfig(TracerConfig config) noexcept
195+
{
196+
const bool enabled = config.IsEnabled();
244197
{
245-
const bool enabled = config.IsEnabled();
246-
{
247-
std::lock_guard<std::mutex> lock(tracer_config_mutex_);
248-
tracer_config_ = config;
249-
}
250-
UpdateEnabled(enabled);
198+
std::lock_guard<std::mutex> lock(tracer_config_mutex_);
199+
tracer_config_ = config;
251200
}
201+
UpdateEnabled(enabled);
202+
}
252203

253-
} // namespace trace
204+
} // namespace trace
254205
} // namespace sdk
255206
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)