Commit 1d68fefe authored by Yorick Peterse's avatar Yorick Peterse

Re-use the default logger for performance metrics

This removes the need for configuring anything extra. To make scraping
easier each line is prefixed by "metrics:".

Fixes #64
parent 3cba0e6e
v3.6.6
- Re-use the default logger when logging metrics data
v3.6.5 v3.6.5
- Test against ruby 2.3 - Test against ruby 2.3
- Instrument GitLab Shell and log metrics data to a file - Instrument GitLab Shell and log metrics data to a file
......
require 'logger' require 'logger'
require_relative 'gitlab_config' require_relative 'gitlab_config'
require_relative 'gitlab_logger'
module GitlabMetrics module GitlabMetrics
module System module System
...@@ -25,7 +26,7 @@ module GitlabMetrics ...@@ -25,7 +26,7 @@ module GitlabMetrics
end end
def self.logger def self.logger
@logger ||= Logger.new(GitlabConfig.new.metrics_log_file) $logger
end end
# Measures the execution time of a block. # Measures the execution time of a block.
...@@ -48,7 +49,7 @@ module GitlabMetrics ...@@ -48,7 +49,7 @@ module GitlabMetrics
real_time = System.monotonic_time - start_real real_time = System.monotonic_time - start_real
cpu_time = System.cpu_time - start_cpu cpu_time = System.cpu_time - start_cpu
logger.debug { "name=#{name.inspect} wall_time=#{real_time.inspect} cpu_time=#{cpu_time.inspect}" } logger.debug("metrics: name=#{name.inspect} wall_time=#{real_time} cpu_time=#{cpu_time}")
retval retval
end end
......
...@@ -2,22 +2,18 @@ require_relative 'spec_helper' ...@@ -2,22 +2,18 @@ require_relative 'spec_helper'
require_relative '../lib/gitlab_metrics' require_relative '../lib/gitlab_metrics'
describe GitlabMetrics do describe GitlabMetrics do
describe '::measure' do describe '.measure' do
it 'returns the return value of the block' do it 'returns the return value of the block' do
val = described_class.measure('foo') { 10 } val = described_class.measure('foo') { 10 }
expect(val).to eq(10) expect(val).to eq(10)
end end
it 'write in a file metrics data' do it 'writes the metrics data to a log file' do
result = nil expect(described_class.logger).to receive(:debug).
expect(described_class.logger).to receive(:debug) do |&b| with(/metrics: name=\"foo\" wall_time=\d+ cpu_time=\d+/)
result = b.call
end
described_class.measure('foo') { 10 } described_class.measure('foo') { 10 }
expect(result).to match(/name=\"foo\" wall_time=\d+ cpu_time=\d+/)
end end
it 'calls proper measure methods' do it 'calls proper measure methods' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment