debian-mirror-gitlab/doc/development/database_query_comments.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

63 lines
2.3 KiB
Markdown
Raw Normal View History

2021-01-03 14:25:43 +05:30
---
2022-07-23 23:45:48 +05:30
stage: Data Stores
2021-01-03 14:25:43 +05:30
group: Database
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2021-01-03 14:25:43 +05:30
---
2020-01-01 13:55:28 +05:30
# Database query comments with Marginalia
The [Marginalia gem](https://github.com/basecamp/marginalia) is used to add
query comments containing application related context information to PostgreSQL
queries generated by ActiveRecord.
It is very useful for tracing problematic queries back to the application source.
2022-07-23 23:45:48 +05:30
An engineer during an on-call incident has the full context of a query
2020-01-01 13:55:28 +05:30
and its application source from the comments.
## Metadata information in comments
Queries generated from **Rails** include the following metadata in comments:
- `application`
- `correlation_id`
2021-04-29 21:17:54 +05:30
- `endpoint_id`
2020-01-01 13:55:28 +05:30
- `line`
2022-07-23 23:45:48 +05:30
Queries generated from **Sidekiq** workers include the following metadata
2020-01-01 13:55:28 +05:30
in comments:
- `application`
- `jid`
- `correlation_id`
2021-04-29 21:17:54 +05:30
- `endpoint_id`
2020-01-01 13:55:28 +05:30
- `line`
2021-04-29 21:17:54 +05:30
`endpoint_id` is a single field that can represent any endpoint in the application:
2020-01-01 13:55:28 +05:30
2021-04-29 21:17:54 +05:30
- For Rails controllers, it's the controller and action. For example, `Projects::BlobController#show`.
- For Grape API endpoints, it's the route. For example, `/api/:version/users/:id`.
- For Sidekiq workers, it's the worker class name. For example, `UserStatusCleanup::BatchWorker`.
`line` is not present in production logs due to the additional overhead required.
Examples of queries with comments:
- Rails:
```sql
/*application:web,controller:blob,action:show,correlation_id:01EZVMR923313VV44ZJDJ7PMEZ,endpoint_id:Projects::BlobController#show*/ SELECT "routes".* FROM "routes" WHERE "routes"."source_id" = 75 AND "routes"."source_type" = 'Namespace' LIMIT 1
```
- Grape:
2020-01-01 13:55:28 +05:30
```sql
2021-04-29 21:17:54 +05:30
/*application:web,correlation_id:01EZVN0DAYGJF5XHG9N4VX8FAH,endpoint_id:/api/:version/users/:id*/ SELECT COUNT(*) FROM "users" INNER JOIN "user_follow_users" ON "users"."id" = "user_follow_users"."followee_id" WHERE "user_follow_users"."follower_id" = 1
2020-01-01 13:55:28 +05:30
```
2021-04-29 21:17:54 +05:30
- Sidekiq:
2020-01-01 13:55:28 +05:30
```sql
2021-04-29 21:17:54 +05:30
/*application:sidekiq,correlation_id:df643992563683313bc0a0288fb55e23,jid:15fbc506590c625d7664b074,endpoint_id:UserStatusCleanup::BatchWorker,line:/app/workers/user_status_cleanup/batch_worker.rb:19:in `perform'*/ SELECT $1 AS one FROM "user_statuses" WHERE "user_statuses"."clear_status_at" <= $2 LIMIT $3
2020-01-01 13:55:28 +05:30
```