diff --git a/changelogs/unreleased/sh-improve-api-marginalia-comments.yml b/changelogs/unreleased/sh-improve-api-marginalia-comments.yml new file mode 100644 index 0000000000000000000000000000000000000000..ad4823edadf5a51cccf110a81d2de7073e396fd3 --- /dev/null +++ b/changelogs/unreleased/sh-improve-api-marginalia-comments.yml @@ -0,0 +1,5 @@ +--- +title: Improve Marginalia comments for API +merge_request: 55564 +author: +type: changed diff --git a/config/initializers/0_marginalia.rb b/config/initializers/0_marginalia.rb index 33d677fdf7f01733c99654f2fa306be523fc4b0a..ab21f936cd81db9a9700d5c366562d9fc491a79f 100644 --- a/config/initializers/0_marginalia.rb +++ b/config/initializers/0_marginalia.rb @@ -13,7 +13,7 @@ # matching against the raw SQL, and prepending the comment prevents color # coding from working in the development log. Marginalia::Comment.prepend_comment = true if Rails.env.production? -Marginalia::Comment.components = [:application, :controller, :action, :correlation_id, :jid, :job_class] +Marginalia::Comment.components = [:application, :controller, :action, :correlation_id, :jid, :job_class, :endpoint_id] # As mentioned in https://github.com/basecamp/marginalia/pull/93/files, # adding :line has some overhead because a regexp on the backtrace has diff --git a/lib/gitlab/marginalia/comment.rb b/lib/gitlab/marginalia/comment.rb index 7b4e4b06f00d9e5af4a444bea8eaa5ecdd7b6da1..ed641b38945d15a1a88a143c733cecf561447e35 100644 --- a/lib/gitlab/marginalia/comment.rb +++ b/lib/gitlab/marginalia/comment.rb @@ -37,6 +37,10 @@ def bg_job job end end + + def endpoint_id + Labkit::Context.current.to_h['meta.caller_id'] + end end end end diff --git a/spec/requests/api/api_spec.rb b/spec/requests/api/api_spec.rb index 8bd6049e6fabbcab98a0dd558ca27c1d3525437d..67b3a32230f00fd0d35434d2ce2a2817f67a3fbe 100644 --- a/spec/requests/api/api_spec.rb +++ b/spec/requests/api/api_spec.rb @@ -133,6 +133,28 @@ end end + describe 'Marginalia comments' do + context 'GET /user/:id' do + let_it_be(:user) { create(:user) } + let(:component_map) do + { + "application" => "test", + "endpoint_id" => "/api/:version/users/:id" + } + end + + subject { ActiveRecord::QueryRecorder.new { get api("/users/#{user.id}", user) } } + + it 'generates a query that includes the expected annotations' do + expect(subject.log.last).to match(/correlation_id:.*/) + + component_map.each do |component, value| + expect(subject.log.last).to include("#{component}:#{value}") + end + end + end + end + describe 'supported content-types' do context 'GET /user/:id.txt' do let_it_be(:user) { create(:user) }