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 Epic Games, Inc. All Rights Reserved.
#include "statsreporter.h"
#include <zencore/logging.h>
#include <zencore/profiling/counterstrace.h>
#include <zencore/trace.h>
#include <zennet/statsdclient.h>
ZEN_THIRD_PARTY_INCLUDES_START
#include <EASTL/functional.h>
#include <EASTL/hash_map.h>
ZEN_THIRD_PARTY_INCLUDES_END
#include <memory>
#include <string>
namespace zen {
// Decorator that forwards every metric call to an inner StatsMetrics (statsd
// in production) and additionally emits a counter trace point so the metric
// shows up in the .utrace stream / `zen trace serve` viewer.
//
// Counter ids are allocated lazily on first sighting of a metric name. The
// name string is owned by the lookup map so the TraceCounter keeps a stable
// pointer to it for late-init re-emission.
class TracingStatsMetrics : public StatsMetrics
{
public:
void Rebind(StatsMetrics* Inner) { m_Inner = Inner; }
void Increment(std::string_view Metric) override
{
if (m_Inner)
m_Inner->Increment(Metric);
Counter(Metric).Increment();
}
void Decrement(std::string_view Metric) override
{
if (m_Inner)
m_Inner->Decrement(Metric);
Counter(Metric).Decrement();
}
void Count(std::string_view Metric, int64_t CountDelta) override
{
if (m_Inner)
m_Inner->Count(Metric, CountDelta);
Counter(Metric).Add(CountDelta);
}
void Gauge(std::string_view Metric, uint64_t CurrentValue) override
{
if (m_Inner)
m_Inner->Gauge(Metric, CurrentValue);
Counter(Metric).SetAlways(int64_t(CurrentValue));
}
void Meter(std::string_view Metric, uint64_t IncrementValue) override
{
if (m_Inner)
m_Inner->Meter(Metric, IncrementValue);
Counter(Metric).Add(int64_t(IncrementValue));
}
private:
TraceCounterInt& Counter(std::string_view Metric)
{
if (auto It = m_Counters.find_as(Metric, std::hash<std::string_view>(), eastl::equal_to_2<std::string, std::string_view>());
It != m_Counters.end())
{
return *It->second;
}
auto [Inserted, _] = m_Counters.try_emplace(std::string(Metric), nullptr);
Inserted->second = std::make_unique<TraceCounterInt>(Inserted->first.c_str(), TraceCounterDisplayHint::None);
return *Inserted->second;
}
StatsMetrics* m_Inner = nullptr;
eastl::hash_map<std::string, std::unique_ptr<TraceCounterInt>, std::hash<std::string>, std::equal_to<std::string>> m_Counters;
};
StatsReporter::StatsReporter() : m_TracingMetrics(std::make_unique<TracingStatsMetrics>())
{
}
StatsReporter::~StatsReporter()
{
}
void
StatsReporter::Initialize(const ZenStatsConfig& Config)
{
ZEN_TRACE_CPU("StatsReporter::Initialize");
RwLock::ExclusiveLockScope _(m_Lock);
if (Config.Enabled)
{
ZEN_INFO("initializing stats reporter: {}:{}", Config.StatsdHost, Config.StatsdPort)
m_Statsd = CreateStatsDaemonClient(Config.StatsdHost, gsl::narrow<uint16_t>(Config.StatsdPort));
m_Statsd->SetMessageSize(1500, false);
}
}
void
StatsReporter::Shutdown()
{
RwLock::ExclusiveLockScope _(m_Lock);
m_Statsd.reset();
}
void
StatsReporter::AddProvider(StatsProvider* Provider)
{
if (!Provider)
{
return;
}
RwLock::ExclusiveLockScope _(m_Lock);
m_Providers.push_back(Provider);
}
void
StatsReporter::ReportStats()
{
RwLock::ExclusiveLockScope _(m_Lock);
// Always run providers through the tracing decorator so counter trace
// points fire even when statsd is disabled. The decorator no-ops the
// inner forward when m_Statsd is null.
m_TracingMetrics->Rebind(m_Statsd.get());
for (StatsProvider* Provider : m_Providers)
{
Provider->ReportMetrics(*m_TracingMetrics);
}
if (m_Statsd)
{
m_Statsd->Flush();
}
}
} // namespace zen
|