Commit a3f8a7a1 authored by Stan Hu's avatar Stan Hu Committed by Bob Van Landuyt

Improve Marginalia comments for API requests

This change adds a `endpoint_id` annotation to the Marginalia comments
to help trace SQL queries to their origin. Examples:

* For Grape API requests, this is the route
  (e.g. `/api/:version/users/:id`).
* For GraphQL requests, this is `GraphqlController#execute`.
* For Rails requests, this is in the controller and action
  (e.g. `Projects::BlobController#show`).

Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/323161
parent e3ac6c9d
---
title: Improve Marginalia comments for API
merge_request: 55564
author:
type: changed
...@@ -13,7 +13,7 @@ require 'marginalia' ...@@ -13,7 +13,7 @@ require 'marginalia'
# matching against the raw SQL, and prepending the comment prevents color # matching against the raw SQL, and prepending the comment prevents color
# coding from working in the development log. # coding from working in the development log.
Marginalia::Comment.prepend_comment = true if Rails.env.production? 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, # As mentioned in https://github.com/basecamp/marginalia/pull/93/files,
# adding :line has some overhead because a regexp on the backtrace has # adding :line has some overhead because a regexp on the backtrace has
......
...@@ -37,6 +37,10 @@ module Gitlab ...@@ -37,6 +37,10 @@ module Gitlab
job job
end end
end end
def endpoint_id
Labkit::Context.current.to_h['meta.caller_id']
end
end end
end end
end end
...@@ -133,6 +133,28 @@ RSpec.describe API::API do ...@@ -133,6 +133,28 @@ RSpec.describe API::API do
end end
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 describe 'supported content-types' do
context 'GET /user/:id.txt' do context 'GET /user/:id.txt' do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
......
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