metriks log to file not working

45 views Asked by At

I wrote a basic program to test the ruby metriks gem

require 'metriks'
require 'metriks/reporter/logger'

@registry = Metriks::Registry.new

@logger = Logger.new('/tmp/metrics.log')
@reporter = Metriks::Reporter::Logger.new(:logger => @logger)
@reporter.start

@registry.meter('tasks').mark
print "Hello"
@registry.meter('tasks').mark

@reporter.stop

After i execute the program, there is nothing in the log other than it got created.

$ cat /tmp/metrics.log
# Logfile created on 2015-06-15 14:23:40 -0700 by logger.rb/44203
1

There are 1 answers

0
limekin On BEST ANSWER

You should either pass in your own registry while instantiating Metriks::Reporter::Logger or use the deafult registry (Metrics::Resgitry.default) if you are using a logger to log metrics.

Also the default log write interval is 60 seconds, your code completes before that so even if everything is setup okay it won't get recorded. So, since you want to use your own registry, this should work for you (I'm adding a little sleep since I'm gonna use an interval of 1 second) :

require 'metriks'
require 'metriks/reporter/logger'

@registry = Metriks::Registry.new

@logger = Logger.new('/tmp/metrics.log')
@reporter = Metriks::Reporter::Logger.new(:logger => @logger, 
                                          :registry => @registry
                                          :interval => 1)
@reporter.start

@registry.meter('tasks').mark
print "Hello"
@registry.meter('tasks').mark

# Just giving it a little time so the metrics will be recorded.
sleep 2

@reporter.stop

But I don't really think short intervals are good.

UPDATE : Also I think @reporter.write will help you write down the logs instantly regardless of the time interval. So you don't have to use sleep (better).