Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rubinius/rubinius
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: d1c444361578
Choose a base ref
...
head repository: rubinius/rubinius
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1008e18b4137
Choose a head ref
  • 4 commits
  • 5 files changed
  • 1 contributor

Commits on Jun 23, 2015

  1. Switched StatsD to counter.

    brixen committed Jun 23, 2015
    Copy the full SHA
    590ebfa View commit details

Commits on Jun 24, 2015

  1. Copy the full SHA
    9bdc284 View commit details
  2. Added Metrics FileEmitter.

    brixen committed Jun 24, 2015
    Copy the full SHA
    944e974 View commit details
  3. Fixed parsing StatsD config .

    brixen committed Jun 24, 2015
    Copy the full SHA
    1008e18 View commit details
Showing with 93 additions and 6 deletions.
  1. +1 −1 library/rubinius/configuration.rb
  2. +3 −3 vm/config_parser.cpp
  3. +66 −2 vm/metrics.cpp
  4. +15 −0 vm/metrics.hpp
  5. +8 −0 vm/test/test_config.hpp
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;