Commit 6c1b4d38 authored by Dmytro Zaporozhets's avatar Dmytro Zaporozhets

Refactor error tracking gzip decompress

Use middleware for both store and envelope endpoints.

Changelog: changed
Signed-off-by: default avatarDmytro Zaporozhets <dzaporozhets@gitlab.com>
parent 993ebdf8
...@@ -4,15 +4,7 @@ module ErrorTracking ...@@ -4,15 +4,7 @@ module ErrorTracking
module Collector module Collector
class SentryRequestParser class SentryRequestParser
def self.parse(request) def self.parse(request)
# Request body can be "" or "gzip". body = request.body.read
# 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
# Request body contains 3 json objects merged together in one StringIO. # Request body contains 3 json objects merged together in one StringIO.
# We need to separate and parse them into array of hash objects. # We need to separate and parse them into array of hash objects.
......
...@@ -27,10 +27,10 @@ module Gitlab ...@@ -27,10 +27,10 @@ module Gitlab
end end
def compressed_et_request?(env) def compressed_et_request?(env)
env['REQUEST_METHOD'] == 'POST' && post_request?(env) &&
env['HTTP_CONTENT_ENCODING'] == 'gzip' && gzip_encoding?(env) &&
env['CONTENT_TYPE'] == 'application/json' && match_content_type?(env) &&
env['PATH_INFO'].start_with?((File.join(relative_url, COLLECTOR_PATH))) match_path?(env)
end end
def too_large def too_large
...@@ -44,6 +44,23 @@ module Gitlab ...@@ -44,6 +44,23 @@ module Gitlab
def extract(input) def extract(input)
Zlib::GzipReader.new(input).read(MAXIMUM_BODY_SIZE + 1) Zlib::GzipReader.new(input).read(MAXIMUM_BODY_SIZE + 1)
end 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 end
end end
...@@ -33,12 +33,5 @@ RSpec.describe ErrorTracking::Collector::SentryRequestParser do ...@@ -33,12 +33,5 @@ RSpec.describe ErrorTracking::Collector::SentryRequestParser do
context 'plain text sentry request' do context 'plain text sentry request' do
it_behaves_like 'valid parser' it_behaves_like 'valid parser'
end 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
end end
...@@ -96,6 +96,20 @@ RSpec.describe API::ErrorTracking::Collector do ...@@ -96,6 +96,20 @@ RSpec.describe API::ErrorTracking::Collector do
end end
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' it_behaves_like 'successful request'
end end
...@@ -131,7 +145,7 @@ RSpec.describe API::ErrorTracking::Collector do ...@@ -131,7 +145,7 @@ RSpec.describe API::ErrorTracking::Collector do
} }
end 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' it_behaves_like 'successful request'
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