Commit c89c6395 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add external HTTP request within transactions tracking

parent d5dfee6a
...@@ -43,6 +43,11 @@ module Gitlab ...@@ -43,6 +43,11 @@ module Gitlab
(@context[:backtraces] ||= []).push(cleaned_backtrace) (@context[:backtraces] ||= []).push(cleaned_backtrace)
end end
def initialize_external_http_tracking
@context[:external_http_count_start] = external_http_requests_count_total
@context[:external_http_duration_start] = external_http_requests_duration_total
end
def duration def duration
return unless @context[:start_time].present? return unless @context[:start_time].present?
...@@ -61,6 +66,11 @@ module Gitlab ...@@ -61,6 +66,11 @@ module Gitlab
return false if logged_already? return false if logged_already?
savepoints_threshold_exceeded? || duration_threshold_exceeded? savepoints_threshold_exceeded? || duration_threshold_exceeded?
## TODO
#
# External requests duration / count exceeded
#
end end
def commit def commit
...@@ -108,6 +118,8 @@ module Gitlab ...@@ -108,6 +118,8 @@ module Gitlab
savepoints_count: @context[:savepoints].to_i, savepoints_count: @context[:savepoints].to_i,
rollbacks_count: @context[:rollbacks].to_i, rollbacks_count: @context[:rollbacks].to_i,
releases_count: @context[:releases].to_i, releases_count: @context[:releases].to_i,
transaction_external_http_requests_count: external_http_requests_count,
transaction_external_http_requests_duration: external_http_requests_duration,
sql: queries, sql: queries,
savepoint_backtraces: backtraces savepoint_backtraces: backtraces
} }
...@@ -118,6 +130,22 @@ module Gitlab ...@@ -118,6 +130,22 @@ module Gitlab
def application_info(attributes) def application_info(attributes)
Gitlab::AppJsonLogger.info(attributes) Gitlab::AppJsonLogger.info(attributes)
end end
def external_http_requests_count
@requests_count ||= external_http_requests_count_total - @context[:external_http_count_start].to_i
end
def external_http_requests_duration
@requests_duration ||= external_http_requests_duration_total - @context[:external_http_duration_start].to_f
end
def external_http_requests_count_total
::Gitlab::Metrics::Subscribers::ExternalHttp.request_count.to_i
end
def external_http_requests_duration_total
::Gitlab::Metrics::Subscribers::ExternalHttp.duration.to_f
end
end end
end end
end end
......
...@@ -21,6 +21,7 @@ module Gitlab ...@@ -21,6 +21,7 @@ module Gitlab
context.set_start_time context.set_start_time
context.set_depth(0) context.set_depth(0)
context.track_sql(event.payload[:sql]) context.track_sql(event.payload[:sql])
context.initialize_external_http_tracking
elsif cmd.start_with?('SAVEPOINT', 'EXCEPTION') elsif cmd.start_with?('SAVEPOINT', 'EXCEPTION')
context.set_depth(manager.open_transactions) context.set_depth(manager.open_transactions)
context.increment_savepoints context.increment_savepoints
......
...@@ -25,7 +25,7 @@ RSpec.describe Gitlab::Database::Transaction::Observer do ...@@ -25,7 +25,7 @@ RSpec.describe Gitlab::Database::Transaction::Observer do
User.first User.first
expect(transaction_context).to be_a(::Gitlab::Database::Transaction::Context) expect(transaction_context).to be_a(::Gitlab::Database::Transaction::Context)
expect(context.keys).to match_array(%i(start_time depth savepoints queries backtraces)) expect(context.keys).to match_array(%i(start_time depth savepoints queries backtraces external_http_count_start external_http_duration_start))
expect(context[:depth]).to eq(2) expect(context[:depth]).to eq(2)
expect(context[:savepoints]).to eq(1) expect(context[:savepoints]).to eq(1)
expect(context[:queries].length).to eq(1) expect(context[:queries].length).to eq(1)
...@@ -38,6 +38,18 @@ RSpec.describe Gitlab::Database::Transaction::Observer do ...@@ -38,6 +38,18 @@ RSpec.describe Gitlab::Database::Transaction::Observer do
expect(context[:backtraces].length).to eq(1) expect(context[:backtraces].length).to eq(1)
end end
it 'tracks external requests duration', :request_store do
# TODO add tests!
ActiveRecord::Base.transaction do
ActiveRecord::Base.transaction(requires_new: true) do
User.first
expect(context[:external_http_count_start]).to eq(0)
expect(context[:external_http_duration_start]).to eq(0)
end
end
end
describe '.extract_sql_command' do describe '.extract_sql_command' do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
......
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