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 145 146 147 148 149 150 151 152
|
#include <memory>
#include <set>
#include <gtest/gtest.h>
#include "opentelemetry/sdk/common/attribute_utils.h"
#include "Common.h"
#include "TestGeckoTraceEvents.h"
#include "mozilla/GeckoTrace.h"
namespace mozilla::gecko_trace::tests {
class GeckoTraceEventDefinitionsTest : public TestWithInMemorySpanExporter {};
using AttributeMap =
std::unordered_map<std::string, otel::sdk::common::OwnedAttributeValue>;
[[maybe_unused]] static AttributeMap GetEventAttributes(
const std::shared_ptr<otel::exporter::memory::InMemorySpanData>& spanData) {
auto spans = spanData->GetSpans();
EXPECT_EQ(spans.size(), 1u) << "Expected exactly 1 span";
const auto& span = spans[0];
auto events = span->GetEvents();
EXPECT_EQ(events.size(), 1u);
return events[0].GetAttributes();
}
[[maybe_unused]] static void AssertOnlyKeys(
const AttributeMap& attrs, const std::set<std::string>& expected) {
std::set<std::string> actual;
for (const auto& [k, _] : attrs) actual.insert(k);
EXPECT_EQ(actual, expected);
}
TEST_F(GeckoTraceEventDefinitionsTest, SimpleEventAttributes) {
// Test with all attributes set
{
GECKO_TRACE_SCOPE("gtests", "Test simple event")
event::TestSimple()
.WithTestString("test_value")
.WithTestInteger(42)
.WithTestBoolean(true)
.Emit();
}
#ifdef GECKO_TRACE_ENABLE
auto attrs = GetEventAttributes(mSpanData);
AssertOnlyKeys(attrs, {"test_string", "test_integer", "test_boolean"});
EXPECT_EQ(std::get<std::string>(attrs["test_string"]), "test_value");
EXPECT_EQ(std::get<int64_t>(attrs["test_integer"]), 42);
EXPECT_EQ(std::get<bool>(attrs["test_boolean"]), true);
#else
EXPECT_EQ(mSpanData->GetSpans().size(), 0u) << "Expected exactly 0 spans";
#endif
}
TEST_F(GeckoTraceEventDefinitionsTest, SimpleEventMissingAttributes) {
// Test with only one attribute set
{
GECKO_TRACE_SCOPE("gtests", "Test simple event with missing attributes")
event::TestSimple().WithTestInteger(-1).Emit();
}
#ifdef GECKO_TRACE_ENABLE
auto attrs = GetEventAttributes(mSpanData);
AssertOnlyKeys(attrs, {"test_integer"});
EXPECT_EQ(std::get<int64_t>(attrs["test_integer"]), -1);
#else
EXPECT_EQ(mSpanData->GetSpans().size(), 0u) << "Expected exactly 0 spans";
#endif
}
TEST_F(GeckoTraceEventDefinitionsTest, ComplexEventAllAttributes) {
// Test multi-level inheritance with all attributes set
{
GECKO_TRACE_SCOPE("gtests", "Test complex event")
event::TestComplex()
.WithTestString("test_value")
.WithTestInteger(42)
.WithTestBoolean(true)
.WithTestData("additional_data")
.WithRetryCount(3)
.Emit();
}
#ifdef GECKO_TRACE_ENABLE
auto attrs = GetEventAttributes(mSpanData);
AssertOnlyKeys(attrs, {"test_string", "test_integer", "test_boolean",
"test_data", "retry.count"});
EXPECT_EQ(std::get<std::string>(attrs["test_string"]), "test_value");
EXPECT_EQ(std::get<int64_t>(attrs["test_integer"]), 42);
EXPECT_EQ(std::get<bool>(attrs["test_boolean"]), true);
EXPECT_EQ(std::get<std::string>(attrs["test_data"]), "additional_data");
EXPECT_EQ(std::get<int64_t>(attrs["retry.count"]), 3);
#else
EXPECT_EQ(mSpanData->GetSpans().size(), 0u) << "Expected exactly 0 spans";
#endif
}
TEST_F(GeckoTraceEventDefinitionsTest, ComplexEventPartialAttributes) {
// Test multi-level inheritance with only some attributes set
{
GECKO_TRACE_SCOPE("gtests", "Test complex event with partial attributes")
event::TestComplex().WithTestString("x").WithRetryCount(-5).Emit();
}
#ifdef GECKO_TRACE_ENABLE
auto attrs = GetEventAttributes(mSpanData);
AssertOnlyKeys(attrs, {"test_string", "retry.count"});
EXPECT_EQ(std::get<std::string>(attrs["test_string"]), "x");
EXPECT_EQ(std::get<int64_t>(attrs["retry.count"]), -5);
#else
EXPECT_EQ(mSpanData->GetSpans().size(), 0u) << "Expected exactly 0 spans";
#endif
}
TEST_F(GeckoTraceEventDefinitionsTest, DeepInheritanceEvent) {
{
GECKO_TRACE_SCOPE("gtests", "Test deep inheritance event")
event::TestDeepInheritance()
.WithTestString("test_value")
.WithTestInteger(42)
.WithTestBoolean(true)
.WithTestData("additional_data")
.WithRetryCount(3)
.WithExtraInfo("deep_inheritance_info")
.Emit();
}
#ifdef GECKO_TRACE_ENABLE
auto attrs = GetEventAttributes(mSpanData);
AssertOnlyKeys(attrs, {"test_string", "test_integer", "test_boolean",
"test_data", "retry.count", "extra.info"});
EXPECT_EQ(std::get<std::string>(attrs["test_string"]), "test_value");
EXPECT_EQ(std::get<int64_t>(attrs["test_integer"]), 42);
EXPECT_EQ(std::get<bool>(attrs["test_boolean"]), true);
EXPECT_EQ(std::get<std::string>(attrs["test_data"]), "additional_data");
EXPECT_EQ(std::get<int64_t>(attrs["retry.count"]), 3);
EXPECT_EQ(std::get<std::string>(attrs["extra.info"]),
"deep_inheritance_info");
#else
EXPECT_EQ(mSpanData->GetSpans().size(), 0u) << "Expected exactly 0 spans";
#endif
}
} // namespace mozilla::gecko_trace::tests
|