From 2cb394878753287403ad4f8ad97f1624feb3453b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Hal=C3=A1sz?= Date: Tue, 25 Oct 2022 10:24:48 +0200 Subject: [PATCH] Measure overall query execution times by operation name --- lib/yabeda/graphql.rb | 5 +++++ lib/yabeda/graphql/instrumentation.rb | 4 ++++ spec/yabeda/graphql_spec.rb | 11 ++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/yabeda/graphql.rb b/lib/yabeda/graphql.rb index 1a1f334..65d2937 100644 --- a/lib/yabeda/graphql.rb +++ b/lib/yabeda/graphql.rb @@ -19,6 +19,11 @@ class Error < StandardError; end tags: %i[type field deprecated], buckets: REQUEST_BUCKETS + histogram :operation_resolve_runtime, comment: "A histogram of operation resolving time", + unit: :seconds, per: :operation, + tags: %i[operation deprecated], + buckets: REQUEST_BUCKETS + counter :fields_request_count, comment: "A counter for specific fields requests", tags: %i[type field deprecated] diff --git a/lib/yabeda/graphql/instrumentation.rb b/lib/yabeda/graphql/instrumentation.rb index af0ab7d..90a33fa 100644 --- a/lib/yabeda/graphql/instrumentation.rb +++ b/lib/yabeda/graphql/instrumentation.rb @@ -8,6 +8,10 @@ def before_query(query) def after_query(query) cache(query).each do |_path, options| Yabeda.graphql.field_resolve_runtime.measure(options[:tags], options[:duration]) + Yabeda.graphql.operation_resolve_runtime.measure({ + operation: query.operation_name, + deprecated: options[:tags][:deprecated] + }, options[:duration]) Yabeda.graphql.fields_request_count.increment(options[:tags]) end end diff --git a/spec/yabeda/graphql_spec.rb b/spec/yabeda/graphql_spec.rb index e611a8c..57b4ab9 100644 --- a/spec/yabeda/graphql_spec.rb +++ b/spec/yabeda/graphql_spec.rb @@ -14,7 +14,7 @@ describe "queries" do let(:query) do <<~GRAPHQL - query { + query getProductsAndUsers { products { id title shmitle price { amount currency } } @@ -40,6 +40,15 @@ ) end + it "measures operation executions" do + Yabeda.graphql.operation_resolve_runtime.sums.clear # This is a hack + subject + expect(Yabeda.graphql.operation_resolve_runtime.sums).to match( + { operation: "getProductsAndUsers", deprecated: false } => kind_of(Numeric), + { operation: "getProductsAndUsers", deprecated: true } => kind_of(Numeric), + ) + end + it "increment counters" do Yabeda.graphql.fields_request_count.values.clear # This is a hack subject