Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 587100d

Browse files
committedJun 24, 2015
Merge remote-tracking branch 'origin' into 1.8.7
2 parents b823b26 + 1008e18 commit 587100d

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed
 

‎library/rubinius/configuration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@
192192
"Number of milliseconds between aggregation of VM metrics"
193193

194194
s.vm_variable "metrics.target", "none",
195-
"Location to send metrics every interval: 'statsd', 'disk'"
195+
"Location to send metrics every interval: 'statsd', path"
196196

197197
s.vm_variable "metrics.statsd.server", "localhost:8125",
198198
"The [host:]port of the StatsD server"

‎vm/config_parser.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ namespace rubinius {
2121
}
2222

2323
static bool valid_char(char c) {
24-
return isalnum(c) || c == '-';
24+
return isalnum(c) || c == '-' || c == '.' || c == '/';
2525
}
2626

2727

2828
/* utility: strips trailing non-alnum chars from string */
2929
static char *trim_str(char *str) {
3030
int i;
31-
while(*str && !valid_char(*str) && *str != '/') str++;
31+
while(*str && !valid_char(*str)) str++;
3232

33-
for(i = strlen(str); i-- && !valid_char(str[i]) && str[i] != '/';) {
33+
for(i = strlen(str); i-- && !valid_char(str[i]);) {
3434
str[i] = 0;
3535
}
3636

‎vm/metrics.cpp

+66-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <fcntl.h>
2727
#include <stdint.h>
2828
#include <netdb.h>
29+
#include <unistd.h>
2930
#include <sys/socket.h>
3031
#include <sys/types.h>
3132
#include <sys/utsname.h>
@@ -64,6 +65,66 @@ namespace rubinius {
6465
add(data.system_metrics);
6566
}
6667

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+
67128
StatsDEmitter::StatsDEmitter(MetricsMap& map, std::string server, std::string prefix)
68129
: MetricsEmitter()
69130
, metrics_map_(map)
@@ -94,7 +155,7 @@ namespace rubinius {
94155

95156
for(size_t p = n.size(), i = p; i != 0; p = i - 1) {
96157
if((i = n.rfind('.', p)) == std::string::npos) {
97-
parts << n.substr(0, p);
158+
parts << n.substr(0, p + 1);
98159
break;
99160
} else {
100161
parts << n.substr(i + 1, p - i) << ".";
@@ -135,7 +196,7 @@ namespace rubinius {
135196
i != metrics_map_.end();
136197
++i)
137198
{
138-
snprintf(buf, RBX_METRICS_STATSD_BUFLEN, "%s%s:%lld|g",
199+
snprintf(buf, RBX_METRICS_STATSD_BUFLEN, "%s%s:%lld|c",
139200
prefix_.c_str(), (*i)->first.c_str(), (long long unsigned int)(*i)->second);
140201
if(send(socket_fd_, buf, strlen(buf), 0) < 0) {
141202
logger::error("%s: unable to send StatsD metrics", strerror(errno));
@@ -203,6 +264,9 @@ namespace rubinius {
203264
emitter_ = new StatsDEmitter(metrics_map_,
204265
state->shared().config.system_metrics_statsd_server.value,
205266
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);
206270
}
207271
}
208272

‎vm/metrics.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,21 @@ namespace rubinius {
319319
virtual void reinit() = 0;
320320
};
321321

322+
class FileEmitter : public MetricsEmitter {
323+
MetricsMap& metrics_map_;
324+
std::string path_;
325+
int fd_;
326+
327+
public:
328+
FileEmitter(MetricsMap& map, std::string path);
329+
virtual ~FileEmitter();
330+
331+
void send_metrics();
332+
void initialize();
333+
void cleanup();
334+
void reinit();
335+
};
336+
322337
class StatsDEmitter : public MetricsEmitter {
323338
MetricsMap& metrics_map_;
324339
std::string host_;

‎vm/test/test_config.hpp

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ class TestConfig : public CxxTest::TestSuite {
3535
TS_ASSERT_EQUALS(std::string("-3"), e->value);
3636
}
3737

38+
void test_parse_line_with_dot() {
39+
ConfigParser cfg;
40+
41+
ConfigParser::Entry* e = cfg.parse_line("rbx.blah = ./foo.bar");
42+
TS_ASSERT_EQUALS(std::string("rbx.blah"), e->variable);
43+
TS_ASSERT_EQUALS(std::string("./foo.bar"), e->value);
44+
}
45+
3846
void test_parse_stream() {
3947
std::istringstream stream;
4048

0 commit comments

Comments
 (0)
Please sign in to comment.