-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: translate annotate to tags (#267)
* feat: translate annotate to tags Adds support for adding request and transaction tags through the annotate method. Fixes #125 * fix: test and format errors * chore: address review comments
- Loading branch information
Showing
14 changed files
with
270 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Sample - Request tags and Transaction tags | ||
|
||
Queries can be annotated with request tags and transaction tags. These can be used to give | ||
you more insights in your queries and transactions. | ||
See https://cloud.google.com/spanner/docs/introspection/troubleshooting-with-tags for more | ||
information about request and transaction tags in Cloud Spanner. | ||
|
||
You can use the [`annotate`](https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-annotate) | ||
method in Ruby ActiveRecord to add a request and/or transaction tag to a __query__. You must | ||
prefix the annotation with either `request_tag:` or `transaction_tag:` to instruct the | ||
Cloud Spanner ActiveRecord provider to recognize the annotation as a tag. | ||
|
||
__NOTE:__ Ruby ActiveRecord does not add comments for `INSERT`, `UPDATE` and `DELETE` statements | ||
when you add annotations to a model. This means that these statements will not be tagged. | ||
|
||
Example: | ||
|
||
```ruby | ||
|
||
``` | ||
|
||
The sample will automatically start a Spanner Emulator in a docker container and execute the sample | ||
against that emulator. The emulator will automatically be stopped when the application finishes. | ||
|
||
Run the application with the command | ||
|
||
```bash | ||
bundle exec rake run | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
|
||
require_relative "../config/environment" | ||
require "sinatra/activerecord/rake" | ||
|
||
desc "Sample showing how to tag queries on Cloud Spanner with ActiveRecord." | ||
task :run do | ||
Dir.chdir("..") { sh "bundle exec rake run[tags]" } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
|
||
require "io/console" | ||
require_relative "../config/environment" | ||
require_relative "models/singer" | ||
require_relative "models/album" | ||
|
||
class Application | ||
def self.run | ||
puts "" | ||
puts "Query all Albums and include a request tag" | ||
albums = Album.annotate("request_tag: query-all-albums").all | ||
puts "Queried #{albums.length} albums using a request tag" | ||
|
||
puts "" | ||
puts "Query all Albums in a transaction and include a request tag and a transaction tag" | ||
Album.transaction do | ||
albums = Album.annotate("request_tag: query-all-albums", "transaction_tag: sample-transaction").all | ||
puts "Queried #{albums.length} albums using a request and a transaction tag" | ||
end | ||
|
||
puts "" | ||
puts "Press any key to end the application" | ||
STDIN.getch | ||
end | ||
end | ||
|
||
Application.run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
development: | ||
adapter: spanner | ||
emulator_host: localhost:9010 | ||
project: test-project | ||
instance: test-instance | ||
database: testdb | ||
pool: 5 | ||
timeout: 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
|
||
class CreateTables < ActiveRecord::Migration[6.0] | ||
def change | ||
connection.ddl_batch do | ||
create_table :singers do |t| | ||
t.string :first_name | ||
t.string :last_name | ||
end | ||
|
||
create_table :albums do |t| | ||
t.string :title | ||
t.references :singer, index: false, foreign_key: true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# This file is the source Rails uses to define your schema when running `bin/rails | ||
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to | ||
# be faster and is potentially less error prone than running all of your | ||
# migrations from scratch. Old migrations may fail to apply correctly if those | ||
# migrations use external dependencies or application code. | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 1) do | ||
connection.start_batch_ddl | ||
|
||
create_table "albums", id: { limit: 8 }, force: :cascade do |t| | ||
t.string "title" | ||
t.integer "singer_id", limit: 8 | ||
end | ||
|
||
create_table "singers", id: { limit: 8 }, force: :cascade do |t| | ||
t.string "first_name" | ||
t.string "last_name" | ||
end | ||
|
||
add_foreign_key "albums", "singers" | ||
connection.run_batch | ||
rescue | ||
abort_batch | ||
raise | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
|
||
require_relative "../../config/environment.rb" | ||
require_relative "../models/singer" | ||
require_relative "../models/album" | ||
|
||
first_names = ["Pete", "Alice", "John", "Ethel", "Trudy", "Naomi", "Wendy", "Ruben", "Thomas", "Elly"] | ||
last_names = ["Wendelson", "Allison", "Peterson", "Johnson", "Henderson", "Ericsson", "Aronson", "Tennet", "Courtou"] | ||
|
||
adjectives = ["daily", "happy", "blue", "generous", "cooked", "bad", "open"] | ||
nouns = ["windows", "potatoes", "bank", "street", "tree", "glass", "bottle"] | ||
|
||
5.times do | ||
Singer.create first_name: first_names.sample, last_name: last_names.sample | ||
end | ||
|
||
20.times do | ||
singer_id = Singer.all.sample.id | ||
Album.create title: "#{adjectives.sample} #{nouns.sample}", singer_id: singer_id | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
|
||
class Album < ActiveRecord::Base | ||
belongs_to :singer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Use of this source code is governed by an MIT-style | ||
# license that can be found in the LICENSE file or at | ||
# https://opensource.org/licenses/MIT. | ||
|
||
class Singer < ActiveRecord::Base | ||
has_many :albums | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters