Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d64677

Browse files
author
Ivan Baidakou
committed
Add tags
1 parent 2fe0482 commit 8d64677

File tree

3 files changed

+37
-23
lines changed

3 files changed

+37
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.2)
22
project (StatsdClient)
33

44
set (StatsdClient_VERSION_MAJOR 1)
5-
set (StatsdClient_VERSION_MINOR 0)
5+
set (StatsdClient_VERSION_MINOR 1)
66

77
include_directories ("src")
88
add_subdirectory ("src")

src/statsd_client.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,49 +139,61 @@ void StatsdClient::cleanup(string& key)
139139
}
140140
}
141141

142-
int StatsdClient::dec(const string& key, float sample_rate)
142+
int StatsdClient::dec(const string& key, float sample_rate, tags_t tags)
143143
{
144-
return count(key, -1, sample_rate);
144+
return count(key, -1, sample_rate, tags);
145145
}
146146

147-
int StatsdClient::inc(const string& key, float sample_rate)
147+
int StatsdClient::inc(const string& key, float sample_rate, tags_t tags)
148148
{
149-
return count(key, 1, sample_rate);
149+
return count(key, 1, sample_rate, tags);
150150
}
151151

152-
int StatsdClient::count(const string& key, size_t value, float sample_rate)
152+
int StatsdClient::count(const string& key, size_t value, float sample_rate, tags_t tags)
153153
{
154-
return send(key, value, "c", sample_rate);
154+
return send(key, value, "c", sample_rate, tags);
155155
}
156156

157-
int StatsdClient::gauge(const string& key, size_t value, float sample_rate)
157+
int StatsdClient::gauge(const string& key, size_t value, float sample_rate, tags_t tags)
158158
{
159-
return send(key, value, "g", sample_rate);
159+
return send(key, value, "g", sample_rate, tags);
160160
}
161161

162-
int StatsdClient::timing(const string& key, size_t ms, float sample_rate)
162+
int StatsdClient::timing(const string& key, size_t ms, float sample_rate, tags_t tags)
163163
{
164-
return send(key, ms, "ms", sample_rate);
164+
return send(key, ms, "ms", sample_rate, tags);
165165
}
166166

167-
int StatsdClient::send(string key, size_t value, const string &type, float sample_rate)
167+
int StatsdClient::send(string key, size_t value, const string &type, float sample_rate, tags_t tags)
168168
{
169169
if (!should_send(this->d, sample_rate)) {
170170
return 0;
171171
}
172172

173173
cleanup(key);
174174

175+
std::string tags_str;
176+
if (!tags.empty()) {
177+
tags_str.reserve(256);
178+
tags_str += "|#";
179+
for(std::size_t i = 0, size = tags.size(); i < size; ++i) {
180+
tags_str += tags[i];
181+
if (i < size -1 ) {
182+
tags_str += ",";
183+
}
184+
}
185+
}
186+
175187
char buf[256];
176188
if ( fequal( sample_rate, 1.0 ) )
177189
{
178-
snprintf(buf, sizeof(buf), "%s%s:%zd|%s",
179-
d->ns.c_str(), key.c_str(), value, type.c_str());
190+
snprintf(buf, sizeof(buf), "%s%s:%zd|%s%s",
191+
d->ns.c_str(), key.c_str(), value, type.c_str(), tags_str.c_str());
180192
}
181193
else
182194
{
183-
snprintf(buf, sizeof(buf), "%s%s:%zd|%s|@%.2f",
184-
d->ns.c_str(), key.c_str(), value, type.c_str(), sample_rate);
195+
snprintf(buf, sizeof(buf), "%s%s:%zd|%s|@%.2f%s",
196+
d->ns.c_str(), key.c_str(), value, type.c_str(), sample_rate, tags_str.c_str());
185197
}
186198

187199
return send(buf);

src/statsd_client.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#define STATSD_CLIENT_H
44

55
#include <StatsdClientConfig.h>
6-
6+
#include <vector>
77
#include <string>
88

99

@@ -16,17 +16,19 @@ class StatsdClient {
1616
StatsdClient(const std::string& host="127.0.0.1", int port=8125, const std::string& ns = "");
1717
~StatsdClient();
1818

19+
using tags_t = std::vector<std::string>;
20+
1921
public:
2022
// you can config at anytime; client will use new address (useful for Singleton)
2123
void config(const std::string& host, int port, const std::string& ns = "");
2224
const char* errmsg();
2325

2426
public:
25-
int inc(const std::string& key, float sample_rate = 1.0);
26-
int dec(const std::string& key, float sample_rate = 1.0);
27-
int count(const std::string& key, size_t value, float sample_rate = 1.0);
28-
int gauge(const std::string& key, size_t value, float sample_rate = 1.0);
29-
int timing(const std::string& key, size_t ms, float sample_rate = 1.0);
27+
int inc(const std::string& key, float sample_rate = 1.0, tags_t = tags_t(0));
28+
int dec(const std::string& key, float sample_rate = 1.0, tags_t = tags_t(0));
29+
int count(const std::string& key, size_t value, float sample_rate = 1.0, tags_t = tags_t(0));
30+
int gauge(const std::string& key, size_t value, float sample_rate = 1.0, tags_t = tags_t(0));
31+
int timing(const std::string& key, size_t ms, float sample_rate = 1.0, tags_t = tags_t(0));
3032

3133
public:
3234
/**
@@ -39,7 +41,7 @@ class StatsdClient {
3941
* type = "c", "g" or "ms"
4042
*/
4143
int send(std::string key, size_t value,
42-
const std::string& type, float sample_rate);
44+
const std::string& type, float sample_rate, tags_t tags);
4345

4446
protected:
4547
int init();

0 commit comments

Comments
 (0)