Skip to content

Commit 42918c2

Browse files
committed
stats: rename statshostname to more appropriate statssuffix
`statshostname` implies that alongside specifying the host, the user needs to specify a separate "hostname" field in order to connect when in fact, it is an entirely optional suffix field applied to stats keys. Rename it to `statssuffix` and deprecate `statshostname`, the latter will be removed in a subsequent major release.
1 parent f3a4844 commit 42918c2

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,11 @@ void SetupServerArgs(ArgsManager& argsman)
775775
argsman.AddArg("-statsbatchsize=<bytes>", strprintf("Specify the size of each batch of stats messages (default: %d)", DEFAULT_STATSD_BATCH_SIZE), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
776776
argsman.AddArg("-statsduration=<ms>", strprintf("Specify the number of milliseconds between stats messages (default: %d)", DEFAULT_STATSD_DURATION), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
777777
argsman.AddArg("-statshost=<ip>", strprintf("Specify statsd host (default: %s)", DEFAULT_STATSD_HOST), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
778-
argsman.AddArg("-statshostname=<ip>", strprintf("Specify statsd host name (default: %s)", DEFAULT_STATSD_HOSTNAME), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
778+
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_NAMESPACE), 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);
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("-statssuffix=<string>", strprintf("Specify an optional string appended to every stats key (default: %s)", DEFAULT_STATSD_SUFFIX), ArgsManager::ALLOW_ANY, OptionsCategory::STATSD);
782783
#if HAVE_DECL_FORK
783784
argsman.AddArg("-daemon", strprintf("Run in the background as a daemon and accept commands (default: %d)", DEFAULT_DAEMON), ArgsManager::ALLOW_BOOL, OptionsCategory::OPTIONS);
784785
argsman.AddArg("-daemonwait", strprintf("Wait for initialization to be finished before exiting. This implies -daemon (default: %d)", DEFAULT_DAEMONWAIT), ArgsManager::ALLOW_BOOL, OptionsCategory::OPTIONS);

src/stats/client.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ static constexpr float EPSILON{0.0001f};
4949

5050
/** Delimiter segmenting two fully formed Statsd messages */
5151
static constexpr char STATSD_MSG_DELIMITER{'\n'};
52+
/** Delimiter segmenting namespaces in a Statsd key */
53+
static constexpr char STATSD_NS_DELIMITER{'.'};
5254
/** Character used to denote Statsd message type as count */
5355
static constexpr char STATSD_METRIC_COUNT[]{"c"};
5456
/** Character used to denote Statsd message type as gauge */
@@ -76,21 +78,41 @@ std::unique_ptr<StatsdClient> InitStatsClient(const ArgsManager& args)
7678
is_enabled = true;
7779
}
7880

81+
auto sanitize_string = [](std::string& string) {
82+
// Remove key delimiters from the front and back as they're added back
83+
// in formatting
84+
if (!string.empty()) {
85+
if (string.front() == STATSD_NS_DELIMITER) string.erase(string.begin());
86+
if (string.back() == STATSD_NS_DELIMITER) string.pop_back();
87+
}
88+
};
89+
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.
92+
auto suffix = args.GetArg("-statssuffix", DEFAULT_STATSD_SUFFIX);
93+
if (suffix.empty()) {
94+
suffix = args.GetArg("-statshostname", DEFAULT_STATSD_SUFFIX);
95+
} else {
96+
// We restrict sanitization logic to our newly added arguments to
97+
// prevent breaking changes.
98+
sanitize_string(suffix);
99+
}
100+
79101
return std::make_unique<StatsdClient>(
80102
host,
81103
args.GetArg("-statsport", DEFAULT_STATSD_PORT),
82104
args.GetArg("-statsbatchsize", DEFAULT_STATSD_BATCH_SIZE),
83105
args.GetArg("-statsduration", DEFAULT_STATSD_DURATION),
84-
args.GetArg("-statshostname", DEFAULT_STATSD_HOSTNAME),
85106
args.GetArg("-statsns", DEFAULT_STATSD_NAMESPACE),
107+
suffix,
86108
is_enabled
87109
);
88110
}
89111

90112
StatsdClient::StatsdClient(const std::string& host, uint16_t port, uint64_t batch_size, uint64_t interval_ms,
91-
const std::string& nodename, const std::string& ns, bool enabled) :
92-
m_nodename{nodename},
93-
m_ns{ns}
113+
const std::string& ns, const std::string& suffix, bool enabled) :
114+
m_ns{ns},
115+
m_suffix{[suffix]() { return !suffix.empty() ? STATSD_NS_DELIMITER + suffix : suffix; }()}
94116
{
95117
if (!enabled) {
96118
LogPrintf("Transmitting stats are disabled, will not init StatsdClient\n");
@@ -154,11 +176,8 @@ bool StatsdClient::send(const std::string& key, T1 value, const std::string& typ
154176
return true;
155177
}
156178

157-
// partition stats by node name if set
158-
if (!m_nodename.empty()) key = key + "." + m_nodename;
159-
160179
// Construct the message and if our message isn't always-send, report the sample rate
161-
RawMessage msg{strprintf("%s%s:%f|%s", m_ns, key, value, type)};
180+
RawMessage msg{strprintf("%s%s%s:%f|%s", m_ns, key, m_suffix, value, type)};
162181
if (!always_send) {
163182
msg += strprintf("|@%.2f", sample_rate);
164183
}

src/stats/client.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ class RawSender;
2020
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"};
23-
/** Default suffix appended to Statsd message keys */
24-
static const std::string DEFAULT_STATSD_HOSTNAME{""};
2523
/** Default prefix prepended to Statsd message keys */
2624
static const std::string DEFAULT_STATSD_NAMESPACE{""};
25+
/** Default suffix appended to Statsd message keys */
26+
static const std::string DEFAULT_STATSD_SUFFIX{""};
2727

2828
/** Default number of milliseconds between flushing a queue of messages */
2929
static constexpr int DEFAULT_STATSD_DURATION{1000};
@@ -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& nodename, const std::string& ns, bool enabled);
43+
const std::string& ns, const std::string& suffix, bool enabled);
4444
~StatsdClient();
4545

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

7171
/* Phrase prepended to keys */
72-
const std::string m_nodename;
73-
/* Phrase appended to keys */
7472
const std::string m_ns;
73+
/* Phrase appended to keys */
74+
const std::string m_suffix{""};
7575
};
7676

7777
/** Parses arguments and constructs a StatsdClient instance */

0 commit comments

Comments
 (0)