Commit 751ff2b1 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'extract-latest-event-from-sentry-client' into 'master'

Extract latest event from Sentry::Client

See merge request gitlab-org/gitlab!22521
parents 2db7afa3 505f0009
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
module Sentry module Sentry
class Client class Client
include Sentry::Client::Event
include Sentry::Client::Projects include Sentry::Client::Projects
include Sentry::Client::Issue include Sentry::Client::Issue
...@@ -24,12 +25,6 @@ module Sentry ...@@ -24,12 +25,6 @@ module Sentry
@token = token @token = token
end end
def issue_latest_event(issue_id:)
latest_event = get_issue_latest_event(issue_id: issue_id)
map_to_event(latest_event)
end
def list_issues(**keyword_args) def list_issues(**keyword_args)
response = get_issues(keyword_args) response = get_issues(keyword_args)
...@@ -115,10 +110,6 @@ module Sentry ...@@ -115,10 +110,6 @@ module Sentry
}.compact }.compact
end end
def get_issue_latest_event(issue_id:)
http_get(issue_latest_event_api_url(issue_id))[:body]
end
def handle_request_exceptions def handle_request_exceptions
yield yield
rescue Gitlab::HTTP::Error => e rescue Gitlab::HTTP::Error => e
...@@ -149,13 +140,6 @@ module Sentry ...@@ -149,13 +140,6 @@ module Sentry
raise Client::Error, message raise Client::Error, message
end end
def issue_latest_event_api_url(issue_id)
latest_event_url = URI(@url)
latest_event_url.path = "/api/0/issues/#{issue_id}/events/latest/"
latest_event_url
end
def issues_api_url def issues_api_url
issues_url = URI(@url + '/issues/') issues_url = URI(@url + '/issues/')
issues_url.path.squeeze!('/') issues_url.path.squeeze!('/')
...@@ -188,27 +172,6 @@ module Sentry ...@@ -188,27 +172,6 @@ module Sentry
uri uri
end end
def map_to_event(event)
stack_trace = parse_stack_trace(event)
Gitlab::ErrorTracking::ErrorEvent.new(
issue_id: event.dig('groupID'),
date_received: event.dig('dateReceived'),
stack_trace_entries: stack_trace
)
end
def parse_stack_trace(event)
exception_entry = event.dig('entries')&.detect { |h| h['type'] == 'exception' }
return [] unless exception_entry
exception_values = exception_entry.dig('data', 'values')
stack_trace_entry = exception_values&.detect { |h| h['stacktrace'].present? }
return [] unless stack_trace_entry
stack_trace_entry.dig('stacktrace', 'frames') || []
end
def map_to_error(issue) def map_to_error(issue)
Gitlab::ErrorTracking::Error.new( Gitlab::ErrorTracking::Error.new(
id: issue.fetch('id'), id: issue.fetch('id'),
......
# frozen_string_literal: true
module Sentry
class Client
module Event
def issue_latest_event(issue_id:)
latest_event = http_get(issue_latest_event_api_url(issue_id))[:body]
map_to_event(latest_event)
end
private
def issue_latest_event_api_url(issue_id)
latest_event_url = URI(url)
latest_event_url.path = "/api/0/issues/#{issue_id}/events/latest/"
latest_event_url
end
def map_to_event(event)
stack_trace = parse_stack_trace(event)
Gitlab::ErrorTracking::ErrorEvent.new(
issue_id: event.dig('groupID'),
date_received: event.dig('dateReceived'),
stack_trace_entries: stack_trace
)
end
def parse_stack_trace(event)
exception_entry = event.dig('entries')&.detect { |h| h['type'] == 'exception' }
return [] unless exception_entry
exception_values = exception_entry.dig('data', 'values')
stack_trace_entry = exception_values&.detect { |h| h['stacktrace'].present? }
return [] unless stack_trace_entry
stack_trace_entry.dig('stacktrace', 'frames') || []
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Sentry::Client do
include SentryClientHelpers
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:token) { 'test-token' }
let(:default_httparty_options) do
{
follow_redirects: false,
headers: { "Authorization" => "Bearer test-token" }
}
end
let(:client) { described_class.new(sentry_url, token) }
describe '#issue_latest_event' do
let(:sample_response) do
Gitlab::Utils.deep_indifferent_access(
JSON.parse(fixture_file('sentry/issue_latest_event_sample_response.json'))
)
end
let(:issue_id) { '1234' }
let(:sentry_api_response) { sample_response }
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0' }
let(:sentry_request_url) { "#{sentry_url}/issues/#{issue_id}/events/latest/" }
let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response) }
subject { client.issue_latest_event(issue_id: issue_id) }
it_behaves_like 'calls sentry api'
it 'has correct return type' do
expect(subject).to be_a(Gitlab::ErrorTracking::ErrorEvent)
end
shared_examples 'assigns error tracking event correctly' do
using RSpec::Parameterized::TableSyntax
where(:event_object, :sentry_response) do
:issue_id | :groupID
:date_received | :dateReceived
end
with_them do
it { expect(subject.public_send(event_object)).to eq(sentry_api_response.dig(*sentry_response)) }
end
end
context 'error object created from sentry response' do
it_behaves_like 'assigns error tracking event correctly'
it 'parses the stack trace' do
expect(subject.stack_trace_entries).to be_a Array
expect(subject.stack_trace_entries).not_to be_empty
end
context 'error without stack trace' do
before do
sample_response['entries'] = []
stub_sentry_request(sentry_request_url, body: sample_response)
end
it_behaves_like 'assigns error tracking event correctly'
it 'returns an empty array for stack_trace_entries' do
expect(subject.stack_trace_entries).to eq []
end
end
end
end
end
...@@ -218,62 +218,4 @@ describe Sentry::Client do ...@@ -218,62 +218,4 @@ describe Sentry::Client do
it_behaves_like 'issues has correct length', 1 it_behaves_like 'issues has correct length', 1
end end
end end
describe '#issue_latest_event' do
let(:sample_response) do
Gitlab::Utils.deep_indifferent_access(
JSON.parse(fixture_file('sentry/issue_latest_event_sample_response.json'))
)
end
let(:issue_id) { '1234' }
let(:sentry_api_response) { sample_response }
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0' }
let(:sentry_request_url) { sentry_url + "/issues/#{issue_id}/events/latest/" }
let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response) }
subject { client.issue_latest_event(issue_id: issue_id) }
it_behaves_like 'calls sentry api'
it 'has correct return type' do
expect(subject).to be_a(Gitlab::ErrorTracking::ErrorEvent)
end
shared_examples 'assigns error tracking event correctly' do
using RSpec::Parameterized::TableSyntax
where(:event_object, :sentry_response) do
:issue_id | :groupID
:date_received | :dateReceived
end
with_them do
it { expect(subject.public_send(event_object)).to eq(sentry_api_response.dig(*sentry_response)) }
end
end
context 'error object created from sentry response' do
it_behaves_like 'assigns error tracking event correctly'
it 'parses the stack trace' do
expect(subject.stack_trace_entries).to be_a Array
expect(subject.stack_trace_entries).not_to be_empty
end
context 'error without stack trace' do
before do
sample_response['entries'] = []
stub_sentry_request(sentry_request_url, body: sample_response)
end
it_behaves_like 'assigns error tracking event correctly'
it 'returns an empty array for stack_trace_entries' do
expect(subject.stack_trace_entries).to eq []
end
end
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