1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#include <gtest/gtest.h>
#include <chrono>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "opentelemetry/exporters/memory/in_memory_span_data.h"
#include "opentelemetry/exporters/memory/in_memory_span_exporter.h"
#include "opentelemetry/nostd/span.h"
#include "opentelemetry/sdk/common/exporter_utils.h"
#include "opentelemetry/sdk/trace/exporter.h"
#include "opentelemetry/sdk/trace/recordable.h"
#include "opentelemetry/sdk/trace/simple_processor.h"
#include "opentelemetry/sdk/trace/span_data.h"
#include "opentelemetry/trace/span_context.h"
using namespace opentelemetry::sdk::trace;
using namespace opentelemetry::sdk::common;
using opentelemetry::exporter::memory::InMemorySpanData;
using opentelemetry::exporter::memory::InMemorySpanExporter;
using opentelemetry::trace::SpanContext;
TEST(SimpleProcessor, ToInMemorySpanExporter)
{
InMemorySpanExporter *exporter = new InMemorySpanExporter();
std::shared_ptr<InMemorySpanData> span_data = exporter->GetData();
SimpleSpanProcessor processor(std::unique_ptr<SpanExporter>{exporter});
auto recordable = processor.MakeRecordable();
processor.OnStart(*recordable, SpanContext::GetInvalid());
ASSERT_EQ(0, span_data->GetSpans().size());
processor.OnEnd(std::move(recordable));
ASSERT_EQ(1, span_data->GetSpans().size());
EXPECT_TRUE(processor.Shutdown());
}
// An exporter that does nothing but record (and give back ) the # of times Shutdown was called.
class RecordShutdownExporter final : public SpanExporter
{
public:
RecordShutdownExporter(int *force_flush_counter, int *shutdown_counter)
: force_flush_counter_(force_flush_counter), shutdown_counter_(shutdown_counter)
{}
std::unique_ptr<Recordable> MakeRecordable() noexcept override
{
return std::unique_ptr<Recordable>(new SpanData());
}
ExportResult Export(const opentelemetry::nostd::span<std::unique_ptr<Recordable>>
& /* recordables */) noexcept override
{
return ExportResult::kSuccess;
}
bool ForceFlush(std::chrono::microseconds /* timeout */) noexcept override
{
*force_flush_counter_ += 1;
return true;
}
bool Shutdown(std::chrono::microseconds /* timeout */) noexcept override
{
*shutdown_counter_ += 1;
return true;
}
private:
int *force_flush_counter_;
int *shutdown_counter_;
};
TEST(SimpleSpanProcessor, ShutdownCalledOnce)
{
int force_flush = 0;
int shutdowns = 0;
RecordShutdownExporter *exporter = new RecordShutdownExporter(&force_flush, &shutdowns);
SimpleSpanProcessor processor(std::unique_ptr<SpanExporter>{exporter});
EXPECT_EQ(0, shutdowns);
processor.Shutdown();
EXPECT_EQ(1, shutdowns);
processor.Shutdown();
EXPECT_EQ(1, shutdowns);
EXPECT_EQ(0, force_flush);
}
TEST(SimpleSpanProcessor, ForceFlush)
{
int force_flush = 0;
int shutdowns = 0;
RecordShutdownExporter *exporter = new RecordShutdownExporter(&force_flush, &shutdowns);
SimpleSpanProcessor processor(std::unique_ptr<SpanExporter>{exporter});
processor.ForceFlush();
EXPECT_EQ(0, shutdowns);
EXPECT_EQ(1, force_flush);
processor.ForceFlush();
EXPECT_EQ(2, force_flush);
}
// An exporter that does nothing but record (and give back ) the # of times Shutdown was called.
class FailShutDownForceFlushExporter final : public SpanExporter
{
public:
FailShutDownForceFlushExporter() {}
std::unique_ptr<Recordable> MakeRecordable() noexcept override
{
return std::unique_ptr<Recordable>(new SpanData());
}
ExportResult Export(const opentelemetry::nostd::span<std::unique_ptr<Recordable>>
& /* recordables */) noexcept override
{
return ExportResult::kSuccess;
}
bool ForceFlush(std::chrono::microseconds /* timeout */) noexcept override { return false; }
bool Shutdown(std::chrono::microseconds /* timeout */) noexcept override { return false; }
};
TEST(SimpleSpanProcessor, ShutdownFail)
{
SimpleSpanProcessor processor(
std::unique_ptr<SpanExporter>{new FailShutDownForceFlushExporter()});
EXPECT_EQ(false, processor.Shutdown());
}
TEST(SimpleSpanProcessor, ForceFlushFail)
{
SimpleSpanProcessor processor(
std::unique_ptr<SpanExporter>{new FailShutDownForceFlushExporter()});
EXPECT_EQ(false, processor.ForceFlush());
}
|