2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe 'lograge', type: :request do
|
|
|
|
let(:headers) { { 'X-Request-ID' => 'new-correlation-id' } }
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:large_params) do
|
|
|
|
half_limit = Gitlab::Utils::LogLimitedArray::MAXIMUM_ARRAY_LENGTH / 2
|
|
|
|
|
|
|
|
{
|
|
|
|
a: 'a',
|
|
|
|
b: 'b' * half_limit,
|
|
|
|
c: 'c' * half_limit,
|
|
|
|
d: 'd'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:limited_params) do
|
2020-04-08 14:13:33 +05:30
|
|
|
large_params.slice(:a, :b).map { |k, v| { key: k.to_s, value: v } } + [{ key: 'truncated', value: '...' }]
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'for API requests' do
|
2019-02-15 15:39:39 +05:30
|
|
|
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
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
get("/api/v4/endpoint", params: {}, headers: headers)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'limits param size' do
|
|
|
|
expect(Lograge.formatter).to receive(:call)
|
|
|
|
.with(a_hash_including(params: limited_params))
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
get("/api/v4/endpoint", params: large_params, headers: headers)
|
2019-02-15 15:39:39 +05:30
|
|
|
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
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
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)
|
2020-04-22 19:07:51 +05:30
|
|
|
.with(a_hash_including(cpu_s: 0.11))
|
2019-12-21 20:55:43 +05:30
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
expect(Lograge.logger).to receive(:send)
|
2020-04-22 19:07:51 +05:30
|
|
|
.with(anything, include('"cpu_s":0.11'))
|
2019-12-21 20:55:43 +05:30
|
|
|
.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
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
it 'limits param size' do
|
|
|
|
expect(Lograge.formatter).to receive(:call)
|
|
|
|
.with(a_hash_including(params: limited_params))
|
|
|
|
.and_call_original
|
|
|
|
|
|
|
|
get("/", params: large_params, headers: headers)
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
context 'with a log subscriber' do
|
2020-07-01 16:08:20 +05:30
|
|
|
include_context 'parsed logs'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:subscriber) { Lograge::LogSubscribers::ActionController.new }
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
let(:event) do
|
|
|
|
ActiveSupport::Notifications::Event.new(
|
|
|
|
'process_action.action_controller',
|
|
|
|
Time.now,
|
|
|
|
Time.now,
|
|
|
|
2,
|
|
|
|
status: 200,
|
|
|
|
controller: 'HomeController',
|
|
|
|
action: 'index',
|
|
|
|
format: 'application/json',
|
|
|
|
method: 'GET',
|
|
|
|
path: '/home?foo=bar',
|
|
|
|
params: {},
|
|
|
|
db_runtime: 0.02,
|
|
|
|
view_runtime: 0.01
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with an exception' do
|
|
|
|
let(:exception) { RuntimeError.new('bad request') }
|
|
|
|
let(:backtrace) { caller }
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(exception).to receive(:backtrace).and_return(backtrace)
|
|
|
|
event.payload[:exception_object] = exception
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds exception data to log' do
|
|
|
|
subscriber.process_action(event)
|
|
|
|
|
|
|
|
expect(log_data['exception.class']).to eq('RuntimeError')
|
|
|
|
expect(log_data['exception.message']).to eq('bad request')
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(log_data['exception.backtrace']).to eq(Gitlab::BacktraceCleaner.clean_backtrace(backtrace))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'with etag_route' do
|
|
|
|
let(:etag_route) { 'etag route' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
event.payload[:etag_route] = etag_route
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds etag_route to log' do
|
|
|
|
subscriber.process_action(event)
|
|
|
|
|
|
|
|
expect(log_data['etag_route']).to eq(etag_route)
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|