Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin' into 1.8.7
Browse files Browse the repository at this point in the history
brixen committed Jun 24, 2015
2 parents b823b26 + 1008e18 commit 587100d
Showing 5 changed files with 93 additions and 6 deletions.
2 changes: 1 addition & 1 deletion library/rubinius/configuration.rb
Original file line number Diff line number Diff line change
@@ -192,7 +192,7 @@
"Number of milliseconds between aggregation of VM metrics"

s.vm_variable "metrics.target", "none",
"Location to send metrics every interval: 'statsd', 'disk'"
"Location to send metrics every interval: 'statsd', path"

s.vm_variable "metrics.statsd.server", "localhost:8125",
"The [host:]port of the StatsD server"
6 changes: 3 additions & 3 deletions vm/config_parser.cpp
Original file line number Diff line number Diff line change
@@ -21,16 +21,16 @@ namespace rubinius {
}

static bool valid_char(char c) {
return isalnum(c) || c == '-';
return isalnum(c) || c == '-' || c == '.' || c == '/';
}


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

for(i = strlen(str); i-- && !valid_char(str[i]) && str[i] != '/';) {
for(i = strlen(str); i-- && !valid_char(str[i]);) {
str[i] = 0;
}

68 changes: 66 additions & 2 deletions vm/metrics.cpp
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
#include <fcntl.h>
#include <stdint.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/utsname.h>
@@ -64,6 +65,66 @@ namespace rubinius {
add(data.system_metrics);
}

FileEmitter::FileEmitter(MetricsMap& map, std::string path)
: MetricsEmitter()
, metrics_map_(map)
, path_(path)
, fd_(-1)
{
initialize();
}

FileEmitter::~FileEmitter() {
cleanup();
}

#define RBX_METRICS_FILE_BUFLEN 22

void FileEmitter::send_metrics() {
char buf[RBX_METRICS_FILE_BUFLEN];

for(MetricsMap::iterator i = metrics_map_.begin();
i != metrics_map_.end();
++i)
{
snprintf(buf, RBX_METRICS_FILE_BUFLEN, "%s%lld",
i == metrics_map_.begin() ? "" : " ", (long long unsigned int)(*i)->second);
if(write(fd_, buf, strlen(buf)) < 0) {
logger::error("%s: unable to write file metrics", strerror(errno));
}
}
write(fd_, "\n", 1);
}

void FileEmitter::initialize() {
if(!(fd_ = ::open(path_.c_str(), O_CREAT | O_WRONLY | O_CLOEXEC, 0660))) {
logger::error("%s: unable to open metrics file", strerror(errno));
}

if(lseek(fd_, 0, SEEK_END) == 0) {
for(MetricsMap::iterator i = metrics_map_.begin();
i != metrics_map_.end();
++i)
{
if(i != metrics_map_.begin()) write(fd_, ", ", 2);
write(fd_, (*i)->first.c_str(), (*i)->first.size());
}
write(fd_, "\n", 1);
}
}

void FileEmitter::cleanup() {
if(fd_ > 0) {
close(fd_);
fd_ = -1;
}
}

void FileEmitter::reinit() {
// Don't turn on FileEmitter in children by default.
cleanup();
}

StatsDEmitter::StatsDEmitter(MetricsMap& map, std::string server, std::string prefix)
: MetricsEmitter()
, metrics_map_(map)
@@ -94,7 +155,7 @@ namespace rubinius {

for(size_t p = n.size(), i = p; i != 0; p = i - 1) {
if((i = n.rfind('.', p)) == std::string::npos) {
parts << n.substr(0, p);
parts << n.substr(0, p + 1);
break;
} else {
parts << n.substr(i + 1, p - i) << ".";
@@ -135,7 +196,7 @@ namespace rubinius {
i != metrics_map_.end();
++i)
{
snprintf(buf, RBX_METRICS_STATSD_BUFLEN, "%s%s:%lld|g",
snprintf(buf, RBX_METRICS_STATSD_BUFLEN, "%s%s:%lld|c",
prefix_.c_str(), (*i)->first.c_str(), (long long unsigned int)(*i)->second);
if(send(socket_fd_, buf, strlen(buf), 0) < 0) {
logger::error("%s: unable to send StatsD metrics", strerror(errno));
@@ -203,6 +264,9 @@ namespace rubinius {
emitter_ = new StatsDEmitter(metrics_map_,
state->shared().config.system_metrics_statsd_server.value,
state->shared().config.system_metrics_statsd_prefix.value);
} else if(state->shared().config.system_metrics_target.value.compare("none")) {
emitter_ = new FileEmitter(metrics_map_,
state->shared().config.system_metrics_target.value);
}
}

15 changes: 15 additions & 0 deletions vm/metrics.hpp
Original file line number Diff line number Diff line change
@@ -319,6 +319,21 @@ namespace rubinius {
virtual void reinit() = 0;
};

class FileEmitter : public MetricsEmitter {
MetricsMap& metrics_map_;
std::string path_;
int fd_;

public:
FileEmitter(MetricsMap& map, std::string path);
virtual ~FileEmitter();

void send_metrics();
void initialize();
void cleanup();
void reinit();
};

class StatsDEmitter : public MetricsEmitter {
MetricsMap& metrics_map_;
std::string host_;
8 changes: 8 additions & 0 deletions vm/test/test_config.hpp
Original file line number Diff line number Diff line change
@@ -35,6 +35,14 @@ class TestConfig : public CxxTest::TestSuite {
TS_ASSERT_EQUALS(std::string("-3"), e->value);
}

void test_parse_line_with_dot() {
ConfigParser cfg;

ConfigParser::Entry* e = cfg.parse_line("rbx.blah = ./foo.bar");
TS_ASSERT_EQUALS(std::string("rbx.blah"), e->variable);
TS_ASSERT_EQUALS(std::string("./foo.bar"), e->value);
}

void test_parse_stream() {
std::istringstream stream;

0 comments on commit 587100d

Please sign in to comment.