|
26 | 26 | #include <fcntl.h>
|
27 | 27 | #include <stdint.h>
|
28 | 28 | #include <netdb.h>
|
| 29 | +#include <unistd.h> |
29 | 30 | #include <sys/socket.h>
|
30 | 31 | #include <sys/types.h>
|
31 | 32 | #include <sys/utsname.h>
|
@@ -64,6 +65,66 @@ namespace rubinius {
|
64 | 65 | add(data.system_metrics);
|
65 | 66 | }
|
66 | 67 |
|
| 68 | + FileEmitter::FileEmitter(MetricsMap& map, std::string path) |
| 69 | + : MetricsEmitter() |
| 70 | + , metrics_map_(map) |
| 71 | + , path_(path) |
| 72 | + , fd_(-1) |
| 73 | + { |
| 74 | + initialize(); |
| 75 | + } |
| 76 | + |
| 77 | + FileEmitter::~FileEmitter() { |
| 78 | + cleanup(); |
| 79 | + } |
| 80 | + |
| 81 | +#define RBX_METRICS_FILE_BUFLEN 22 |
| 82 | + |
| 83 | + void FileEmitter::send_metrics() { |
| 84 | + char buf[RBX_METRICS_FILE_BUFLEN]; |
| 85 | + |
| 86 | + for(MetricsMap::iterator i = metrics_map_.begin(); |
| 87 | + i != metrics_map_.end(); |
| 88 | + ++i) |
| 89 | + { |
| 90 | + snprintf(buf, RBX_METRICS_FILE_BUFLEN, "%s%lld", |
| 91 | + i == metrics_map_.begin() ? "" : " ", (long long unsigned int)(*i)->second); |
| 92 | + if(write(fd_, buf, strlen(buf)) < 0) { |
| 93 | + logger::error("%s: unable to write file metrics", strerror(errno)); |
| 94 | + } |
| 95 | + } |
| 96 | + write(fd_, "\n", 1); |
| 97 | + } |
| 98 | + |
| 99 | + void FileEmitter::initialize() { |
| 100 | + if(!(fd_ = ::open(path_.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0660))) { |
| 101 | + logger::error("%s: unable to open metrics file", strerror(errno)); |
| 102 | + } |
| 103 | + |
| 104 | + if(lseek(fd_, 0, SEEK_END) == 0) { |
| 105 | + for(MetricsMap::iterator i = metrics_map_.begin(); |
| 106 | + i != metrics_map_.end(); |
| 107 | + ++i) |
| 108 | + { |
| 109 | + if(i != metrics_map_.begin()) write(fd_, ", ", 2); |
| 110 | + write(fd_, (*i)->first.c_str(), (*i)->first.size()); |
| 111 | + } |
| 112 | + write(fd_, "\n", 1); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + void FileEmitter::cleanup() { |
| 117 | + if(fd_ > 0) { |
| 118 | + close(fd_); |
| 119 | + fd_ = -1; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + void FileEmitter::reinit() { |
| 124 | + // Don't turn on FileEmitter in children by default. |
| 125 | + cleanup(); |
| 126 | + } |
| 127 | + |
67 | 128 | StatsDEmitter::StatsDEmitter(MetricsMap& map, std::string server, std::string prefix)
|
68 | 129 | : MetricsEmitter()
|
69 | 130 | , metrics_map_(map)
|
@@ -94,7 +155,7 @@ namespace rubinius {
|
94 | 155 |
|
95 | 156 | for(size_t p = n.size(), i = p; i != 0; p = i - 1) {
|
96 | 157 | if((i = n.rfind('.', p)) == std::string::npos) {
|
97 |
| - parts << n.substr(0, p); |
| 158 | + parts << n.substr(0, p + 1); |
98 | 159 | break;
|
99 | 160 | } else {
|
100 | 161 | parts << n.substr(i + 1, p - i) << ".";
|
@@ -135,7 +196,7 @@ namespace rubinius {
|
135 | 196 | i != metrics_map_.end();
|
136 | 197 | ++i)
|
137 | 198 | {
|
138 |
| - snprintf(buf, RBX_METRICS_STATSD_BUFLEN, "%s%s:%lld|g", |
| 199 | + snprintf(buf, RBX_METRICS_STATSD_BUFLEN, "%s%s:%lld|c", |
139 | 200 | prefix_.c_str(), (*i)->first.c_str(), (long long unsigned int)(*i)->second);
|
140 | 201 | if(send(socket_fd_, buf, strlen(buf), 0) < 0) {
|
141 | 202 | logger::error("%s: unable to send StatsD metrics", strerror(errno));
|
@@ -203,6 +264,9 @@ namespace rubinius {
|
203 | 264 | emitter_ = new StatsDEmitter(metrics_map_,
|
204 | 265 | state->shared().config.system_metrics_statsd_server.value,
|
205 | 266 | state->shared().config.system_metrics_statsd_prefix.value);
|
| 267 | + } else if(state->shared().config.system_metrics_target.value.compare("none")) { |
| 268 | + emitter_ = new FileEmitter(metrics_map_, |
| 269 | + state->shared().config.system_metrics_target.value); |
206 | 270 | }
|
207 | 271 | }
|
208 | 272 |
|
|
0 commit comments