Skip to content

Commit 0668d64

Browse files
authored
fix: noexcept for Observer (#4039)
1 parent d2be704 commit 0668d64

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

sdk/include/opentelemetry/sdk/metrics/observer_result.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "opentelemetry/common/attribute_value.h"
99
#include "opentelemetry/common/key_value_iterable.h"
10+
#include "opentelemetry/common/macros.h"
1011
#include "opentelemetry/metrics/observer_result.h"
1112
#include "opentelemetry/nostd/string_view.h"
1213
#include "opentelemetry/sdk/metrics/state/attributes_hashmap.h"
@@ -34,15 +35,37 @@ class ObserverResultT final : public opentelemetry::metrics::ObserverResultT<T>
3435
~ObserverResultT() override = default;
3536

3637
void Observe(T value) noexcept override
38+
#if OPENTELEMETRY_HAVE_EXCEPTIONS
39+
try
40+
#endif
3741
{
3842
data_[MetricAttributes{{}, attributes_processor_}] = value;
3943
}
44+
#if OPENTELEMETRY_HAVE_EXCEPTIONS
45+
catch (...)
46+
{
47+
// Silently drop the measurement; per opentelemetry-cpp guidance (PR #3964),
48+
// exceptions in noexcept API/SDK code must not log or abort.
49+
return;
50+
}
51+
#endif
4052

4153
void Observe(T value, const opentelemetry::common::KeyValueIterable &attributes) noexcept override
54+
#if OPENTELEMETRY_HAVE_EXCEPTIONS
55+
try
56+
#endif
4257
{
4358
data_[MetricAttributes{attributes, attributes_processor_}] =
4459
value; // overwrites the previous value if present
4560
}
61+
#if OPENTELEMETRY_HAVE_EXCEPTIONS
62+
catch (...)
63+
{
64+
// Silently drop the measurement; per opentelemetry-cpp guidance (PR #3964),
65+
// exceptions in noexcept API/SDK code must not log or abort.
66+
return;
67+
}
68+
#endif
4669

4770
const std::unordered_map<MetricAttributes, T, AttributeHashGenerator> &GetMeasurements()
4871
{

0 commit comments

Comments
 (0)