gitlab-org--gitlab-foss/spec/initializers/lograge_spec.rb

71 lines
2 KiB
Ruby

# frozen_string_literal: true
require 'spec_helper'
describe 'lograge', type: :request do
let(:headers) { { 'X-Request-ID' => 'new-correlation-id' } }
context 'for API requests' do
subject { get("/api/v4/endpoint", params: {}, headers: headers) }
it 'logs to api_json log' do
# we assert receiving parameters by grape logger
expect_any_instance_of(Gitlab::GrapeLogging::Formatters::LogrageWithTimestamp).to receive(:call)
.with(anything, anything, anything, a_hash_including("correlation_id" => "new-correlation-id"))
.and_call_original
subject
end
end
context 'for Controller requests' do
subject { get("/", params: {}, headers: headers) }
it 'logs to production_json log' do
# formatter receives a hash with correlation id
expect(Lograge.formatter).to receive(:call)
.with(a_hash_including("correlation_id" => "new-correlation-id"))
.and_call_original
# a log file receives a line with correlation id
expect(Lograge.logger).to receive(:send)
.with(anything, include('"correlation_id":"new-correlation-id"'))
.and_call_original
subject
end
it 'logs cpu_s on supported platform' do
allow(Gitlab::Metrics::System).to receive(:thread_cpu_time)
.and_return(
0.111222333,
0.222333833
)
expect(Lograge.formatter).to receive(:call)
.with(a_hash_including(cpu_s: 0.1111115))
.and_call_original
expect(Lograge.logger).to receive(:send)
.with(anything, include('"cpu_s":0.1111115'))
.and_call_original
subject
end
it 'does not log cpu_s on unsupported platform' do
allow(Gitlab::Metrics::System).to receive(:thread_cpu_time)
.and_return(nil)
expect(Lograge.formatter).to receive(:call)
.with(hash_not_including(:cpu_s))
.and_call_original
expect(Lograge.logger).not_to receive(:send)
.with(anything, include('"cpu_s":'))
.and_call_original
subject
end
end
end