Commit 54fdbba9 authored by Alper Akgun's avatar Alper Akgun

Merge branch 'dz-et-improve-gzip-support-2' into 'master'

Refactor error tracking gzip decompression

See merge request gitlab-org/gitlab!73151
parents 074ebc96 6c1b4d38
......@@ -4,15 +4,7 @@ module ErrorTracking
module Collector
class SentryRequestParser
def self.parse(request)
# Request body can be "" or "gzip".
# If later then body was compressed with Zlib.gzip
encoding = request.headers['Content-Encoding']
body = if encoding == 'gzip'
Zlib.gunzip(request.body.read)
else
request.body.read
end
body = request.body.read
# Request body contains 3 json objects merged together in one StringIO.
# We need to separate and parse them into array of hash objects.
......
......@@ -27,10 +27,10 @@ module Gitlab
end
def compressed_et_request?(env)
env['REQUEST_METHOD'] == 'POST' &&
env['HTTP_CONTENT_ENCODING'] == 'gzip' &&
env['CONTENT_TYPE'] == 'application/json' &&
env['PATH_INFO'].start_with?((File.join(relative_url, COLLECTOR_PATH)))
post_request?(env) &&
gzip_encoding?(env) &&
match_content_type?(env) &&
match_path?(env)
end
def too_large
......@@ -44,6 +44,23 @@ module Gitlab
def extract(input)
Zlib::GzipReader.new(input).read(MAXIMUM_BODY_SIZE + 1)
end
def post_request?(env)
env['REQUEST_METHOD'] == 'POST'
end
def gzip_encoding?(env)
env['HTTP_CONTENT_ENCODING'] == 'gzip'
end
def match_content_type?(env)
env['CONTENT_TYPE'] == 'application/json' ||
env['CONTENT_TYPE'] == 'application/x-sentry-envelope'
end
def match_path?(env)
env['PATH_INFO'].start_with?((File.join(relative_url, COLLECTOR_PATH)))
end
end
end
end
......@@ -33,12 +33,5 @@ RSpec.describe ErrorTracking::Collector::SentryRequestParser do
context 'plain text sentry request' do
it_behaves_like 'valid parser'
end
context 'gzip encoded sentry request' do
let(:headers) { { 'Content-Encoding' => 'gzip' } }
let(:body) { Zlib.gzip(raw_event) }
it_behaves_like 'valid parser'
end
end
end
......@@ -96,6 +96,20 @@ RSpec.describe API::ErrorTracking::Collector do
end
end
context 'gzip body' do
let(:headers) do
{
'X-Sentry-Auth' => "Sentry sentry_key=#{client_key.public_key}",
'HTTP_CONTENT_ENCODING' => 'gzip',
'CONTENT_TYPE' => 'application/x-sentry-envelope'
}
end
let(:params) { ActiveSupport::Gzip.compress(raw_event) }
it_behaves_like 'successful request'
end
it_behaves_like 'successful request'
end
......@@ -131,7 +145,7 @@ RSpec.describe API::ErrorTracking::Collector do
}
end
let(:raw_event) { ActiveSupport::Gzip.compress(fixture_file('error_tracking/parsed_event.json')) }
let(:params) { ActiveSupport::Gzip.compress(raw_event) }
it_behaves_like 'successful request'
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