|
| 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 |
0 commit comments