Skip to content

Commit 5937ac4

Browse files
committed
Use and extend existing statsd sink class
Signed-off-by: Taiki Ono <taiks.4559@gmail.com>
1 parent 6781c05 commit 5937ac4

11 files changed

Lines changed: 113 additions & 242 deletions

File tree

source/common/stats/BUILD

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,6 @@ envoy_cc_library(
4343
],
4444
)
4545

46-
envoy_cc_library(
47-
name = "dog_statsd_lib",
48-
srcs = ["dog_statsd.cc"],
49-
hdrs = ["dog_statsd.h"],
50-
deps = [
51-
"statsd_lib",
52-
],
53-
)
54-
5546
envoy_cc_library(
5647
name = "thread_local_store_lib",
5748
srcs = ["thread_local_store.cc"],

source/common/stats/dog_statsd.cc

Lines changed: 0 additions & 75 deletions
This file was deleted.

source/common/stats/dog_statsd.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

source/common/stats/statsd.cc

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ Writer::~Writer() {
3333
}
3434
}
3535

36-
void Writer::writeCounter(const std::string& name, uint64_t increment) {
37-
std::string message(fmt::format("envoy.{}:{}|c", name, increment));
36+
void Writer::writeCounter(const std::string& name, const std::string& tag_str, uint64_t increment) {
37+
std::string message(fmt::format("envoy.{}:{}|c{}\n", name, increment, tag_str));
3838
send(message);
3939
}
4040

41-
void Writer::writeGauge(const std::string& name, uint64_t value) {
42-
std::string message(fmt::format("envoy.{}:{}|g", name, value));
41+
void Writer::writeGauge(const std::string& name, const std::string& tag_str, uint64_t value) {
42+
std::string message(fmt::format("envoy.{}:{}|g{}\n", name, value, tag_str));
4343
send(message);
4444
}
4545

46-
void Writer::writeTimer(const std::string& name, const std::chrono::milliseconds& ms) {
47-
std::string message(fmt::format("envoy.{}:{}|ms", name, ms.count()));
46+
void Writer::writeTimer(const std::string& name, const std::string& tag_str,
47+
const std::chrono::milliseconds& ms) {
48+
std::string message(fmt::format("envoy.{}:{}|ms{}\n", name, ms.count(), tag_str));
4849
send(message);
4950
}
5051

@@ -53,22 +54,51 @@ void Writer::send(const std::string& message) {
5354
}
5455

5556
UdpStatsdSink::UdpStatsdSink(ThreadLocal::SlotAllocator& tls,
56-
Network::Address::InstanceConstSharedPtr address)
57-
: tls_(tls.allocateSlot()), server_address_(address) {
58-
setWriter<Writer>();
57+
Network::Address::InstanceConstSharedPtr address, bool useTag)
58+
: tls_(tls.allocateSlot()), server_address_(address), useTag_(useTag) {
59+
tls_->set([this](Event::Dispatcher&) -> ThreadLocal::ThreadLocalObjectSharedPtr {
60+
return std::make_shared<Writer>(this->server_address_);
61+
});
5962
}
6063

6164
void UdpStatsdSink::flushCounter(const Counter& counter, uint64_t delta) {
62-
tls_->getTyped<Writer>().writeCounter(counter.name(), delta);
65+
tls_->getTyped<Writer>().writeCounter(getName(counter), buildTagStr(counter.tags()), delta);
6366
}
6467

6568
void UdpStatsdSink::flushGauge(const Gauge& gauge, uint64_t value) {
66-
tls_->getTyped<Writer>().writeGauge(gauge.name(), value);
69+
tls_->getTyped<Writer>().writeGauge(getName(gauge), buildTagStr(gauge.tags()), value);
6770
}
6871

6972
void UdpStatsdSink::onHistogramComplete(const Histogram& histogram, uint64_t value) {
7073
// For statsd histograms are all timers.
71-
tls_->getTyped<Writer>().writeTimer(histogram.name(), std::chrono::milliseconds(value));
74+
tls_->getTyped<Writer>().writeTimer(getName(histogram), buildTagStr(histogram.tags()),
75+
std::chrono::milliseconds(value));
76+
}
77+
78+
std::string UdpStatsdSink::getName(const Metric& metric) {
79+
if (useTag_) {
80+
return metric.tagExtractedName();
81+
} else {
82+
return metric.name();
83+
}
84+
}
85+
86+
const std::string buildTagStr(const std::vector<Tag>& tags) {
87+
if (tags.empty()) {
88+
return {};
89+
}
90+
91+
std::string tagStr = "|#";
92+
int i = 0;
93+
for (const Tag& tag : tags) {
94+
if (i == 0) {
95+
tagStr.append(fmt::format("{}:{}", tag.name_, tag.value_));
96+
} else {
97+
tagStr.append(fmt::format(",{}:{}", tag.name_, tag.value_));
98+
}
99+
i++;
100+
}
101+
return tagStr;
72102
}
73103

74104
char TcpStatsdSink::STAT_PREFIX[] = "envoy.";

source/common/stats/statsd.h

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ class Writer : public ThreadLocal::ThreadLocalObject {
2424
Writer(Network::Address::InstanceConstSharedPtr address);
2525
~Writer();
2626

27-
void writeCounter(const std::string& name, uint64_t increment);
28-
void writeGauge(const std::string& name, uint64_t value);
29-
void writeTimer(const std::string& name, const std::chrono::milliseconds& ms);
27+
void writeCounter(const std::string& name, const std::string& tag_str, uint64_t increment);
28+
void writeGauge(const std::string& name, const std::string& tag_str, uint64_t value);
29+
void writeTimer(const std::string& name, const std::string& tag_str,
30+
const std::chrono::milliseconds& ms);
3031

3132
// Called in unit test to validate address.
3233
int getFdForTests() const { return fd_; };
3334

34-
protected:
35+
private:
3536
void send(const std::string& message);
3637

37-
private:
3838
int fd_;
3939
};
4040

@@ -43,7 +43,8 @@ class Writer : public ThreadLocal::ThreadLocalObject {
4343
*/
4444
class UdpStatsdSink : public Sink {
4545
public:
46-
UdpStatsdSink(ThreadLocal::SlotAllocator& tls, Network::Address::InstanceConstSharedPtr address);
46+
UdpStatsdSink(ThreadLocal::SlotAllocator& tls, Network::Address::InstanceConstSharedPtr address,
47+
bool useTag);
4748

4849
// Stats::Sink
4950
void beginFlush() override {}
@@ -55,19 +56,16 @@ class UdpStatsdSink : public Sink {
5556
// Called in unit test to validate writer construction and address.
5657
int getFdForTests() { return tls_->getTyped<Writer>().getFdForTests(); }
5758

58-
protected:
59-
ThreadLocal::SlotPtr tls_;
60-
61-
template <class T> void setWriter() {
62-
tls_->set([this](Event::Dispatcher&) -> ThreadLocal::ThreadLocalObjectSharedPtr {
63-
return std::make_shared<T>(this->server_address_);
64-
});
65-
}
66-
6759
private:
60+
std::string getName(const Metric& metric);
61+
62+
ThreadLocal::SlotPtr tls_;
6863
Network::Address::InstanceConstSharedPtr server_address_;
64+
bool useTag_;
6965
};
7066

67+
const std::string buildTagStr(const std::vector<Tag>& tags);
68+
7169
/**
7270
* Per thread implementation of a TCP stats flusher for statsd.
7371
*/

source/server/config/stats/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ envoy_cc_library(
3737
"//source/common/config:well_known_names",
3838
"//source/common/network:address_lib",
3939
"//source/common/network:resolver_lib",
40-
"//source/common/stats:dog_statsd_lib",
40+
"//source/common/stats:statsd_lib",
4141
"//source/server:configuration_lib",
4242
],
4343
)

source/server/config/stats/dog_statsd.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include "common/config/well_known_names.h"
88
#include "common/network/resolver_impl.h"
9-
#include "common/stats/dog_statsd.h"
9+
#include "common/stats/statsd.h"
1010

1111
#include "api/bootstrap.pb.h"
1212

@@ -21,7 +21,7 @@ Stats::SinkPtr DogStatsdSinkFactory::createStatsSink(const Protobuf::Message& co
2121
Network::Address::resolveProtoAddress(sink_config.address());
2222
ENVOY_LOG(debug, "dog_statsd UDP ip address: {}", address->asString());
2323
return Stats::SinkPtr(
24-
new Stats::DogStatsd::UdpStatsdSink(server.threadLocal(), std::move(address)));
24+
new Stats::Statsd::UdpStatsdSink(server.threadLocal(), std::move(address), true));
2525
}
2626

2727
ProtobufTypes::MessagePtr DogStatsdSinkFactory::createEmptyConfigProto() {

source/server/config/stats/statsd.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Stats::SinkPtr StatsdSinkFactory::createStatsSink(const Protobuf::Message& confi
2626
Network::Address::resolveProtoAddress(statsd_sink.address());
2727
ENVOY_LOG(debug, "statsd UDP ip address: {}", address->asString());
2828
return Stats::SinkPtr(
29-
new Stats::Statsd::UdpStatsdSink(server.threadLocal(), std::move(address)));
29+
new Stats::Statsd::UdpStatsdSink(server.threadLocal(), std::move(address), false));
3030
break;
3131
}
3232
case envoy::api::v2::StatsdSink::kTcpClusterName:

test/common/stats/BUILD

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,6 @@ envoy_cc_test(
5151
],
5252
)
5353

54-
envoy_cc_test(
55-
name = "udp_dog_statsd_test",
56-
srcs = ["udp_dog_statsd_test.cc"],
57-
deps = [
58-
"//source/common/network:address_lib",
59-
"//source/common/network:utility_lib",
60-
"//source/common/stats:dog_statsd_lib",
61-
"//test/mocks/stats:stats_mocks",
62-
"//test/mocks/thread_local:thread_local_mocks",
63-
"//test/test_common:environment_lib",
64-
"//test/test_common:network_utility_lib",
65-
],
66-
)
67-
6854
envoy_cc_test(
6955
name = "thread_local_store_test",
7056
srcs = ["thread_local_store_test.cc"],

0 commit comments

Comments
 (0)