Skip to content

Commit 18a2e48

Browse files
committed
stats: rename statsns to clearer statsprefix
`statsns`, aside from being an unclear name, in its default behaviour, doesn't use the namespace delimiter contaminates the root namespace of the key. Let's take care of that by deprecating `statsns` with its replacement, `statsprefix`, that behaves properly.
1 parent 42918c2 commit 18a2e48

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,8 +777,9 @@ void SetupServerArgs(ArgsManager& argsman)
777777
argsman.AddArg("-statshost=<ip>", strprintf("Specify statsd host (default: %s)", DEFAULT_STATSD_HOST), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
778778
hidden_args.emplace_back("-statshostname");
779779
argsman.AddArg("-statsport=<port>", strprintf("Specify statsd port (default: %u)", DEFAULT_STATSD_PORT), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
780-
argsman.AddArg("-statsns=<ns>", strprintf("Specify additional namespace prefix (default: %s)", DEFAULT_STATSD_SUFFIX), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
780+
hidden_args.emplace_back("-statsns");
781781
argsman.AddArg("-statsperiod=<seconds>", strprintf("Specify the number of seconds between periodic measurements (default: %d)", DEFAULT_STATSD_PERIOD), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
782+
argsman.AddArg("-statsprefix=<string>", strprintf("Specify an optional string prepended to every stats key (default: %s)", DEFAULT_STATSD_PREFIX), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
782783
argsman.AddArg("-statssuffix=<string>", strprintf("Specify an optional string appended to every stats key (default: %s)", DEFAULT_STATSD_SUFFIX), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
783784
#if HAVE_DECL_FORK
784785
argsman.AddArg("-daemon", strprintf("Run in the background as a daemon and accept commands (default: %d)", DEFAULT_DAEMON), ArgsManager::ALLOW_BOOL, OptionsCategory::OPTIONS);

src/stats/client.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,29 @@ std::unique_ptr<StatsdClient> InitStatsClient(const ArgsManager& args)
8080

8181
auto sanitize_string = [](std::string& string) {
8282
// Remove key delimiters from the front and back as they're added back
83-
// in formatting
8483
if (!string.empty()) {
8584
if (string.front() == STATSD_NS_DELIMITER) string.erase(string.begin());
8685
if (string.back() == STATSD_NS_DELIMITER) string.pop_back();
8786
}
8887
};
8988

90-
// Get our suffix and if we get nothing, try again with the deprecated
91-
// argument. If we still get nothing, that's fine, suffixes are optional.
89+
// Get our prefix and suffix and if we get nothing, try again with the
90+
// deprecated argument. If we still get nothing, that's fine, they're optional.
91+
auto prefix = args.GetArg("-statsprefix", DEFAULT_STATSD_PREFIX);
92+
if (prefix.empty()) {
93+
prefix = args.GetArg("-statsns", DEFAULT_STATSD_PREFIX);
94+
} else {
95+
// We restrict sanitization logic to our newly added arguments to
96+
// prevent breaking changes.
97+
sanitize_string(prefix);
98+
// We need to add the delimiter here for backwards compatibility with
99+
// the deprecated argument.
100+
//
101+
// TODO: Move this step into the constructor when removing deprecated
102+
// args support
103+
prefix += STATSD_NS_DELIMITER;
104+
}
105+
92106
auto suffix = args.GetArg("-statssuffix", DEFAULT_STATSD_SUFFIX);
93107
if (suffix.empty()) {
94108
suffix = args.GetArg("-statshostname", DEFAULT_STATSD_SUFFIX);
@@ -103,15 +117,15 @@ std::unique_ptr<StatsdClient> InitStatsClient(const ArgsManager& args)
103117
args.GetArg("-statsport", DEFAULT_STATSD_PORT),
104118
args.GetArg("-statsbatchsize", DEFAULT_STATSD_BATCH_SIZE),
105119
args.GetArg("-statsduration", DEFAULT_STATSD_DURATION),
106-
args.GetArg("-statsns", DEFAULT_STATSD_NAMESPACE),
120+
prefix,
107121
suffix,
108122
is_enabled
109123
);
110124
}
111125

112126
StatsdClient::StatsdClient(const std::string& host, uint16_t port, uint64_t batch_size, uint64_t interval_ms,
113-
const std::string& ns, const std::string& suffix, bool enabled) :
114-
m_ns{ns},
127+
const std::string& prefix, const std::string& suffix, bool enabled) :
128+
m_prefix{prefix},
115129
m_suffix{[suffix]() { return !suffix.empty() ? STATSD_NS_DELIMITER + suffix : suffix; }()}
116130
{
117131
if (!enabled) {
@@ -177,7 +191,7 @@ bool StatsdClient::send(const std::string& key, T1 value, const std::string& typ
177191
}
178192

179193
// Construct the message and if our message isn't always-send, report the sample rate
180-
RawMessage msg{strprintf("%s%s%s:%f|%s", m_ns, key, m_suffix, value, type)};
194+
RawMessage msg{strprintf("%s%s%s:%f|%s", m_prefix, key, m_suffix, value, type)};
181195
if (!always_send) {
182196
msg += strprintf("|@%.2f", sample_rate);
183197
}

src/stats/client.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static constexpr uint16_t DEFAULT_STATSD_PORT{8125};
2121
/** Default host assumed to be running a Statsd server */
2222
static const std::string DEFAULT_STATSD_HOST{"127.0.0.1"};
2323
/** Default prefix prepended to Statsd message keys */
24-
static const std::string DEFAULT_STATSD_NAMESPACE{""};
24+
static const std::string DEFAULT_STATSD_PREFIX{""};
2525
/** Default suffix appended to Statsd message keys */
2626
static const std::string DEFAULT_STATSD_SUFFIX{""};
2727

@@ -40,7 +40,7 @@ class StatsdClient
4040
{
4141
public:
4242
explicit StatsdClient(const std::string& host, uint16_t port, uint64_t batch_size, uint64_t interval_ms,
43-
const std::string& ns, const std::string& suffix, bool enabled);
43+
const std::string& prefix, const std::string& suffix, bool enabled);
4444
~StatsdClient();
4545

4646
public:
@@ -69,7 +69,7 @@ class StatsdClient
6969
std::unique_ptr<RawSender> m_sender{nullptr};
7070

7171
/* Phrase prepended to keys */
72-
const std::string m_ns;
72+
const std::string m_prefix{""};
7373
/* Phrase appended to keys */
7474
const std::string m_suffix{""};
7575
};

0 commit comments

Comments
 (0)