Commit ae8f7666 authored by Pawel Chojnacki's avatar Pawel Chojnacki

Add prometheus text formatter

 + rename controler method to #index from #metrics
 + remove assertion from nullMetric
parent c134a72c
...@@ -24,11 +24,11 @@ class MetricsService ...@@ -24,11 +24,11 @@ class MetricsService
private private
def formatter def formatter
@formatter ||= PrometheusText.new @formatter ||= Gitlab::HealthChecks::PrometheusTextFormat.new
end end
def multiprocess_metrics_path def multiprocess_metrics_path
@multiprocess_metrics_path ||= Rails.root.join(ENV['prometheus_multiproc_dir']) @multiprocess_metrics_path ||= Rails.root.join(ENV['prometheus_multiproc_dir']).freeze
end end
def metric_to_prom_line(metric) def metric_to_prom_line(metric)
......
...@@ -41,7 +41,7 @@ Rails.application.routes.draw do ...@@ -41,7 +41,7 @@ Rails.application.routes.draw do
scope path: '-' do scope path: '-' do
get 'liveness' => 'health#liveness' get 'liveness' => 'health#liveness'
get 'readiness' => 'health#readiness' get 'readiness' => 'health#readiness'
get 'metrics' => 'metrics#metrics' resources :metrics, only: [:index]
end end
# Koding route # Koding route
......
module Gitlab::HealthChecks module Gitlab::HealthChecks
class PrometheusText class PrometheusTextFormat
def marshal(metrics) def marshal(metrics)
metrics_with_type_declarations(metrics).join("\n") metrics_with_type_declarations(metrics).join("\n")
end end
......
...@@ -5,15 +5,6 @@ module Gitlab ...@@ -5,15 +5,6 @@ module Gitlab
def method_missing(name, *args, &block) def method_missing(name, *args, &block)
nil nil
end end
# these methods shouldn't be called when metrics are disabled
def get(*args)
raise NotImplementedError
end
def values(*args)
raise NotImplementedError
end
end end
end end
end end
...@@ -19,14 +19,14 @@ describe MetricsController do ...@@ -19,14 +19,14 @@ describe MetricsController do
allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(true) allow(Gitlab::Metrics).to receive(:prometheus_metrics_enabled?).and_return(true)
end end
describe '#metrics' do describe '#index' do
context 'authorization token provided' do context 'authorization token provided' do
before do before do
request.headers['TOKEN'] = token request.headers['TOKEN'] = token
end end
it 'returns DB ping metrics' do it 'returns DB ping metrics' do
get :metrics get :index
expect(response.body).to match(/^db_ping_timeout 0$/) expect(response.body).to match(/^db_ping_timeout 0$/)
expect(response.body).to match(/^db_ping_success 1$/) expect(response.body).to match(/^db_ping_success 1$/)
...@@ -34,7 +34,7 @@ describe MetricsController do ...@@ -34,7 +34,7 @@ describe MetricsController do
end end
it 'returns Redis ping metrics' do it 'returns Redis ping metrics' do
get :metrics get :index
expect(response.body).to match(/^redis_ping_timeout 0$/) expect(response.body).to match(/^redis_ping_timeout 0$/)
expect(response.body).to match(/^redis_ping_success 1$/) expect(response.body).to match(/^redis_ping_success 1$/)
...@@ -42,7 +42,7 @@ describe MetricsController do ...@@ -42,7 +42,7 @@ describe MetricsController do
end end
it 'returns file system check metrics' do it 'returns file system check metrics' do
get :metrics get :index
expect(response.body).to match(/^filesystem_access_latency{shard="default"} [0-9\.]+$/) expect(response.body).to match(/^filesystem_access_latency{shard="default"} [0-9\.]+$/)
expect(response.body).to match(/^filesystem_accessible{shard="default"} 1$/) expect(response.body).to match(/^filesystem_accessible{shard="default"} 1$/)
...@@ -58,7 +58,7 @@ describe MetricsController do ...@@ -58,7 +58,7 @@ describe MetricsController do
end end
it 'returns proper response' do it 'returns proper response' do
get :metrics get :index
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
...@@ -67,7 +67,7 @@ describe MetricsController do ...@@ -67,7 +67,7 @@ describe MetricsController do
context 'without authorization token' do context 'without authorization token' do
it 'returns proper response' do it 'returns proper response' do
get :metrics get :index
expect(response.status).to eq(404) expect(response.status).to eq(404)
end end
......
describe Gitlab::HealthChecks::PrometheusTextFormat do
let(:metric_class) { Gitlab::HealthChecks::Metric }
subject { described_class.new }
describe '#marshal' do
let(:sample_metrics) do
[
metric_class.new('metric1', 1),
metric_class.new('metric2', 2)
]
end
it 'marshal to text with non repeating type definition' do
expected = <<-EXPECTED
# TYPE metric1 gauge
metric1 1
# TYPE metric2 gauge
metric2 2
EXPECTED
expect(subject.marshal(sample_metrics)).to eq(expected.chomp)
end
context 'metrics where name repeats' do
let(:sample_metrics) do
[
metric_class.new('metric1', 1),
metric_class.new('metric1', 2),
metric_class.new('metric2', 3)
]
end
it 'marshal to text with non repeating type definition' do
expected = <<-EXPECTED
# TYPE metric1 gauge
metric1 1
metric1 2
# TYPE metric2 gauge
metric2 3
EXPECTED
expect(subject.marshal(sample_metrics)).to eq(expected.chomp)
end
end
end
end
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