diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a41804..93f7cab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## master +- [PR#64](https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries/pull/64) +Add query to cache_hit trace event ([@DmitryTsepelev][]) + ## 1.7.0 (2023-02-02) - [PR#62](https://github.com/DmitryTsepelev/graphql-ruby-persisted_queries/pull/62) diff --git a/docs/tracing.md b/docs/tracing.md index 4dae0ca..25f2e91 100644 --- a/docs/tracing.md +++ b/docs/tracing.md @@ -30,7 +30,7 @@ class MyPersistedQueriesTracer case key when "persisted_queries.fetch_query.cache_hit" # data = { adapter: :redis } - # result = nil + # result = query string that got hit # increment a counter metric to track cache hits when "persisted_queries.fetch_query.cache_miss" # data = { adapter: :redis } diff --git a/lib/graphql/persisted_queries/store_adapters/base_store_adapter.rb b/lib/graphql/persisted_queries/store_adapters/base_store_adapter.rb index 9fce5ba..bbe2283 100644 --- a/lib/graphql/persisted_queries/store_adapters/base_store_adapter.rb +++ b/lib/graphql/persisted_queries/store_adapters/base_store_adapter.rb @@ -17,8 +17,11 @@ def fetch_query(hash, options = {}) key = build_key(hash, compiled_query) fetch(key).tap do |result| - event = result ? "cache_hit" : "cache_miss" - trace("fetch_query.#{event}", adapter: @name) + if result + trace("fetch_query.cache_hit", adapter: @name) { result } + else + trace("fetch_query.cache_miss", adapter: @name) + end end end diff --git a/spec/graphql/persisted_queries/schema_patch_spec.rb b/spec/graphql/persisted_queries/schema_patch_spec.rb index 2f57e3d..20502cb 100644 --- a/spec/graphql/persisted_queries/schema_patch_spec.rb +++ b/spec/graphql/persisted_queries/schema_patch_spec.rb @@ -79,7 +79,7 @@ def perform_request(with_tracer: false, with_query: true) perform_request(with_tracer: true) perform_request(with_tracer: true, with_query: false) events = tracer.events["persisted_queries.fetch_query.cache_hit"] - expect(events).to eq([{ metadata: { adapter: :memory }, result: nil }]) + expect(events).to eq([{ metadata: { adapter: :memory }, result: query }]) end end diff --git a/spec/graphql/persisted_queries/store_adapters/base_store_adapter_spec.rb b/spec/graphql/persisted_queries/store_adapters/base_store_adapter_spec.rb index 075b39f..47190b5 100644 --- a/spec/graphql/persisted_queries/store_adapters/base_store_adapter_spec.rb +++ b/spec/graphql/persisted_queries/store_adapters/base_store_adapter_spec.rb @@ -70,7 +70,7 @@ def save(hash, query) expect(tracer.events).to eq( "persisted_queries.fetch_query.cache_hit" => [{ metadata: { adapter: :testable }, - result: nil + result: "welcome-to-paradise" }] ) end