googleapis / ruby-spanner-activerecord

Spanner<->ActiveRecord adapter for Ruby
MIT License
87 stars 30 forks source link

feat: support Query Logs #291

Closed olavloite closed 9 months ago

olavloite commented 10 months ago

Rails 7.0 introduced support for Query Logs. These automatically append comments to all SQL statements that are executed, so it is easier to find queries that perform badly. Cloud Spanner does not include comments from the SQL string in the query statistics, meaning that in order for these comments to be useful, they need to be translated to request tags.

This change modifies the query execution path so the query transformer that is used by Query Logs is called by the Cloud Spanner provider. The comments that are added can then be translated to request tags. The latter requires that Query Logs is configured with two specific options:

  1. ActiveRecord::QueryLogs.prepend_comment = true
  2. The first tag must be { request_tag: "true" }

See the included sample application for a full working sample.

Fixes #285