Skip to content

Commit 3161d5e

Browse files
committed
[SDK] Fix not reporting failures from MultiSpanProcessor/MultiLogRecordProcessor flush/shutdown
1 parent 1c2b007 commit 3161d5e

9 files changed

Lines changed: 330 additions & 49 deletions

File tree

‎sdk/include/opentelemetry/sdk/trace/multi_span_processor.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ class MultiSpanProcessor : public SpanProcessor
119119
while (node != nullptr)
120120
{
121121
auto processor = node->value_.get();
122-
result |= processor->ForceFlush(timeout);
122+
if (!processor->ForceFlush(timeout))
123+
{
124+
result = false;
125+
}
123126
node = node->next_;
124127
}
125128
return result;
@@ -146,7 +149,10 @@ class MultiSpanProcessor : public SpanProcessor
146149
while (node != nullptr)
147150
{
148151
auto processor = node->value_.get();
149-
result |= processor->Shutdown(timeout);
152+
if (!processor->Shutdown(timeout))
153+
{
154+
result = false;
155+
}
150156
node = node->next_;
151157
}
152158
return result;

‎sdk/src/logs/multi_log_record_processor.cc‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ bool MultiLogRecordProcessor::InternalShutdown(std::chrono::microseconds timeout
185185
}
186186
for (auto &processor : processors_)
187187
{
188-
result |= processor->Shutdown(timeout);
188+
if (!processor->Shutdown(timeout))
189+
{
190+
result = false;
191+
}
189192
start_time = std::chrono::system_clock::now();
190193
if (expire_time > start_time)
191194
{

‎sdk/test/logs/BUILD‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,21 @@ cc_test(
6666
],
6767
)
6868

69+
cc_test(
70+
name = "multi_log_record_processor_test",
71+
srcs = [
72+
"multi_log_record_processor_test.cc",
73+
],
74+
tags = [
75+
"logs",
76+
"test",
77+
],
78+
deps = [
79+
"//sdk/src/logs",
80+
"@com_google_googletest//:gtest_main",
81+
],
82+
)
83+
6984
cc_test(
7085
name = "log_record_test",
7186
srcs = [

‎sdk/test/logs/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ foreach(
99
log_record_test
1010
log_record_limits_test
1111
simple_log_record_processor_test
12+
multi_log_record_processor_test
1213
batch_log_record_processor_test
1314
logger_config_test)
1415
add_executable(${testname} "${testname}.cc")
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include <gtest/gtest.h>
5+
#include <chrono>
6+
#include <cstddef>
7+
#include <cstdint>
8+
#include <memory>
9+
#include <string>
10+
#include <utility>
11+
#include <vector>
12+
13+
#include "opentelemetry/common/attribute_value.h"
14+
#include "opentelemetry/common/timestamp.h"
15+
#include "opentelemetry/context/context.h"
16+
#include "opentelemetry/logs/severity.h"
17+
#include "opentelemetry/nostd/string_view.h"
18+
#include "opentelemetry/nostd/variant.h"
19+
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
20+
#include "opentelemetry/sdk/logs/multi_log_record_processor.h"
21+
#include "opentelemetry/sdk/logs/processor.h"
22+
#include "opentelemetry/sdk/logs/recordable.h"
23+
#include "opentelemetry/trace/span_context.h"
24+
25+
using namespace opentelemetry::sdk::logs;
26+
namespace context = opentelemetry::context;
27+
namespace logs_api = opentelemetry::logs;
28+
namespace instrumentation_scope = opentelemetry::sdk::instrumentationscope;
29+
namespace nostd = opentelemetry::nostd;
30+
31+
namespace
32+
{
33+
34+
class TestLogRecordable final : public Recordable
35+
{
36+
public:
37+
void SetTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {}
38+
39+
void SetObservedTimestamp(opentelemetry::common::SystemTimestamp) noexcept override {}
40+
41+
void SetSeverity(logs_api::Severity) noexcept override {}
42+
43+
void SetBody(const opentelemetry::common::AttributeValue &) noexcept override {}
44+
45+
void SetEventId(int64_t, nostd::string_view) noexcept override {}
46+
47+
void SetTraceId(const opentelemetry::trace::TraceId &) noexcept override {}
48+
49+
void SetSpanId(const opentelemetry::trace::SpanId &) noexcept override {}
50+
51+
void SetTraceFlags(const opentelemetry::trace::TraceFlags &) noexcept override {}
52+
53+
void SetAttribute(nostd::string_view,
54+
const opentelemetry::common::AttributeValue &) noexcept override
55+
{}
56+
57+
void SetResource(const opentelemetry::sdk::resource::Resource &) noexcept override {}
58+
59+
void SetInstrumentationScope(
60+
const instrumentation_scope::InstrumentationScope &) noexcept override
61+
{}
62+
};
63+
64+
struct EnabledCallState
65+
{
66+
context::Context context;
67+
std::string scope_name;
68+
logs_api::Severity severity = logs_api::Severity::kInvalid;
69+
std::string event_name;
70+
size_t call_count = 0;
71+
};
72+
73+
class EnabledProcessor final : public LogRecordProcessor
74+
{
75+
public:
76+
explicit EnabledProcessor(bool enabled,
77+
std::shared_ptr<EnabledCallState> call_state = nullptr) noexcept
78+
: enabled_(enabled), call_state_(std::move(call_state))
79+
{}
80+
81+
std::unique_ptr<Recordable> MakeRecordable() noexcept override
82+
{
83+
return std::unique_ptr<Recordable>(new TestLogRecordable());
84+
}
85+
86+
void OnEmit(std::unique_ptr<Recordable> &&record) noexcept override
87+
{
88+
auto ignored = std::move(record);
89+
static_cast<void>(ignored);
90+
}
91+
92+
bool ForceFlush(std::chrono::microseconds /* timeout */) noexcept override { return true; }
93+
94+
bool Shutdown(std::chrono::microseconds /* timeout */) noexcept override { return true; }
95+
96+
protected:
97+
bool EnabledImplementation(
98+
const nostd::variant<opentelemetry::trace::SpanContext, context::Context> &context_or_span,
99+
const instrumentation_scope::InstrumentationScope &scope,
100+
logs_api::Severity severity,
101+
nostd::string_view event_name) const noexcept override
102+
{
103+
if (call_state_ != nullptr)
104+
{
105+
if (const context::Context *ctx = nostd::get_if<context::Context>(&context_or_span))
106+
{
107+
call_state_->context = *ctx;
108+
}
109+
else
110+
{
111+
call_state_->context = context::Context{};
112+
}
113+
call_state_->scope_name = scope.GetName();
114+
call_state_->severity = severity;
115+
call_state_->event_name = std::string(event_name);
116+
++call_state_->call_count;
117+
}
118+
return enabled_;
119+
}
120+
121+
private:
122+
bool enabled_;
123+
std::shared_ptr<EnabledCallState> call_state_;
124+
};
125+
126+
TEST(MultiLogRecordProcessorTest, EnabledWhenAnyChildEnabled)
127+
{
128+
auto first_state = std::make_shared<EnabledCallState>();
129+
auto second_state = std::make_shared<EnabledCallState>();
130+
131+
std::vector<std::unique_ptr<LogRecordProcessor>> processors;
132+
processors.emplace_back(new EnabledProcessor(false, first_state));
133+
processors.emplace_back(new EnabledProcessor(true, second_state));
134+
MultiLogRecordProcessor processor(std::move(processors));
135+
136+
context::Context test_context{"test-key", true};
137+
auto scope = instrumentation_scope::InstrumentationScope::Create("test-scope");
138+
139+
EXPECT_TRUE(
140+
processor.Enabled(test_context, *scope, logs_api::Severity::kError, "test-event-name"));
141+
EXPECT_EQ(first_state->call_count, 1U);
142+
EXPECT_EQ(second_state->call_count, 1U);
143+
EXPECT_EQ(second_state->event_name, "test-event-name");
144+
}
145+
146+
TEST(MultiLogRecordProcessorTest, DisabledWhenAllChildrenDisabled)
147+
{
148+
std::vector<std::unique_ptr<LogRecordProcessor>> processors;
149+
processors.emplace_back(new EnabledProcessor(false));
150+
processors.emplace_back(new EnabledProcessor(false));
151+
MultiLogRecordProcessor processor(std::move(processors));
152+
153+
context::Context test_context{"test-key", true};
154+
auto scope = instrumentation_scope::InstrumentationScope::Create("test-scope");
155+
156+
EXPECT_FALSE(
157+
processor.Enabled(test_context, *scope, logs_api::Severity::kError, "test-event-name"));
158+
}
159+
160+
TEST(MultiLogRecordProcessorTest, EmptyProcessorIsDisabled)
161+
{
162+
MultiLogRecordProcessor processor(std::vector<std::unique_ptr<LogRecordProcessor>>{});
163+
164+
context::Context test_context{"test-key", true};
165+
auto scope = instrumentation_scope::InstrumentationScope::Create("test-scope");
166+
167+
EXPECT_FALSE(
168+
processor.Enabled(test_context, *scope, logs_api::Severity::kDebug, "test-event-name"));
169+
}
170+
171+
// A processor whose ForceFlush/Shutdown outcome is fixed at construction.
172+
class FixedResultProcessor final : public LogRecordProcessor
173+
{
174+
public:
175+
explicit FixedResultProcessor(bool result) : result_(result) {}
176+
177+
std::unique_ptr<Recordable> MakeRecordable() noexcept override
178+
{
179+
return std::unique_ptr<Recordable>(new TestLogRecordable());
180+
}
181+
182+
void OnEmit(std::unique_ptr<Recordable> && /* record */) noexcept override {}
183+
184+
bool ForceFlush(std::chrono::microseconds /* timeout */) noexcept override { return result_; }
185+
186+
bool Shutdown(std::chrono::microseconds /* timeout */) noexcept override { return result_; }
187+
188+
private:
189+
bool result_;
190+
};
191+
192+
std::unique_ptr<LogRecordProcessor> MakeMultiProcessor(bool first, bool second)
193+
{
194+
std::vector<std::unique_ptr<LogRecordProcessor>> processors;
195+
processors.emplace_back(new FixedResultProcessor(first));
196+
processors.emplace_back(new FixedResultProcessor(second));
197+
return std::unique_ptr<LogRecordProcessor>(new MultiLogRecordProcessor(std::move(processors)));
198+
}
199+
200+
TEST(MultiLogRecordProcessorTest, ForceFlushFailsWhenAnyChildFails)
201+
{
202+
EXPECT_TRUE(MakeMultiProcessor(true, true)->ForceFlush());
203+
204+
EXPECT_FALSE(MakeMultiProcessor(true, false)->ForceFlush());
205+
EXPECT_FALSE(MakeMultiProcessor(false, true)->ForceFlush());
206+
}
207+
208+
TEST(MultiLogRecordProcessorTest, ShutdownFailsWhenAnyChildFails)
209+
{
210+
EXPECT_TRUE(MakeMultiProcessor(true, true)->Shutdown());
211+
212+
EXPECT_FALSE(MakeMultiProcessor(true, false)->Shutdown());
213+
EXPECT_FALSE(MakeMultiProcessor(false, true)->Shutdown());
214+
}
215+
216+
} // namespace

‎sdk/test/logs/simple_log_record_processor_test.cc‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "opentelemetry/sdk/common/exporter_utils.h"
2121
#include "opentelemetry/sdk/instrumentationscope/instrumentation_scope.h"
2222
#include "opentelemetry/sdk/logs/exporter.h"
23-
#include "opentelemetry/sdk/logs/multi_log_record_processor.h"
2423
#include "opentelemetry/sdk/logs/processor.h"
2524
#include "opentelemetry/sdk/logs/recordable.h"
2625
#include "opentelemetry/sdk/logs/simple_log_record_processor.h"
@@ -354,49 +353,4 @@ TEST(SimpleLogRecordProcessorTest, EnabledForwardsArgumentsToImplementation)
354353
EXPECT_EQ(call_state->call_count, 1U);
355354
}
356355

357-
TEST(SimpleLogRecordProcessorTest, MultiLogRecordProcessorEnabledWhenAnyChildEnabled)
358-
{
359-
auto first_state = std::make_shared<EnabledCallState>();
360-
auto second_state = std::make_shared<EnabledCallState>();
361-
362-
std::vector<std::unique_ptr<LogRecordProcessor>> processors;
363-
processors.emplace_back(new EnabledProcessor(false, first_state));
364-
processors.emplace_back(new EnabledProcessor(true, second_state));
365-
MultiLogRecordProcessor processor(std::move(processors));
366-
367-
context::Context test_context{"test-key", true};
368-
auto scope = instrumentation_scope::InstrumentationScope::Create("test-scope");
369-
370-
EXPECT_TRUE(
371-
processor.Enabled(test_context, *scope, logs_api::Severity::kError, "test-event-name"));
372-
EXPECT_EQ(first_state->call_count, 1U);
373-
EXPECT_EQ(second_state->call_count, 1U);
374-
EXPECT_EQ(second_state->event_name, "test-event-name");
375-
}
376-
377-
TEST(SimpleLogRecordProcessorTest, MultiLogRecordProcessorDisabledWhenAllChildrenDisabled)
378-
{
379-
std::vector<std::unique_ptr<LogRecordProcessor>> processors;
380-
processors.emplace_back(new EnabledProcessor(false));
381-
processors.emplace_back(new EnabledProcessor(false));
382-
MultiLogRecordProcessor processor(std::move(processors));
383-
384-
context::Context test_context{"test-key", true};
385-
auto scope = instrumentation_scope::InstrumentationScope::Create("test-scope");
386-
387-
EXPECT_FALSE(
388-
processor.Enabled(test_context, *scope, logs_api::Severity::kError, "test-event-name"));
389-
}
390-
391-
TEST(SimpleLogRecordProcessorTest, EmptyMultiLogRecordProcessorIsDisabled)
392-
{
393-
MultiLogRecordProcessor processor(std::vector<std::unique_ptr<LogRecordProcessor>>{});
394-
395-
context::Context test_context{"test-key", true};
396-
auto scope = instrumentation_scope::InstrumentationScope::Create("test-scope");
397-
398-
EXPECT_FALSE(
399-
processor.Enabled(test_context, *scope, logs_api::Severity::kDebug, "test-event-name"));
400-
}
401-
402356
} // namespace

‎sdk/test/trace/BUILD‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ cc_test(
6868
],
6969
)
7070

71+
cc_test(
72+
name = "multi_span_processor_test",
73+
srcs = [
74+
"multi_span_processor_test.cc",
75+
],
76+
tags = [
77+
"test",
78+
"trace",
79+
],
80+
deps = [
81+
"//sdk/src/trace",
82+
"@com_google_googletest//:gtest_main",
83+
],
84+
)
85+
7186
cc_test(
7287
name = "batch_span_processor_test",
7388
srcs = [

‎sdk/test/trace/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ foreach(
77
tracer_provider_test
88
span_data_test
99
simple_processor_test
10+
multi_span_processor_test
1011
tracer_test
1112
always_off_sampler_test
1213
always_on_sampler_test

0 commit comments

Comments
 (0)