diff options
Diffstat (limited to 'spec/lib/gitlab/graphql/tracers')
4 files changed, 199 insertions, 0 deletions
diff --git a/spec/lib/gitlab/graphql/tracers/application_context_tracer_spec.rb b/spec/lib/gitlab/graphql/tracers/application_context_tracer_spec.rb new file mode 100644 index 00000000000..6eff816b95a --- /dev/null +++ b/spec/lib/gitlab/graphql/tracers/application_context_tracer_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true +require "fast_spec_helper" +require "support/graphql/fake_tracer" +require "support/graphql/fake_query_type" + +RSpec.describe Gitlab::Graphql::Tracers::ApplicationContextTracer do + let(:tracer_spy) { spy('tracer_spy') } + let(:default_known_operations) { ::Gitlab::Graphql::KnownOperations.new(['fooOperation']) } + let(:dummy_schema) do + schema = Class.new(GraphQL::Schema) do + use Gitlab::Graphql::Tracers::ApplicationContextTracer + + query Graphql::FakeQueryType + end + + fake_tracer = Graphql::FakeTracer.new(lambda do |key, *args| + tracer_spy.trace(key, Gitlab::ApplicationContext.current) + end) + + schema.tracer(fake_tracer) + + schema + end + + before do + allow(::Gitlab::Graphql::KnownOperations).to receive(:default).and_return(default_known_operations) + end + + it "sets application context during execute_query and cleans up afterwards", :aggregate_failures do + dummy_schema.execute("query fooOperation { helloWorld }") + + # "parse" is just an arbitrary trace event that isn't setting caller_id + expect(tracer_spy).to have_received(:trace).with("parse", hash_excluding("meta.caller_id")) + expect(tracer_spy).to have_received(:trace).with("execute_query", hash_including("meta.caller_id" => "graphql:fooOperation")).once + expect(Gitlab::ApplicationContext.current).not_to include("meta.caller_id") + end + + it "sets caller_id when operation is not known" do + dummy_schema.execute("query fuzz { helloWorld }") + + expect(tracer_spy).to have_received(:trace).with("execute_query", hash_including("meta.caller_id" => "graphql:unknown")).once + end +end diff --git a/spec/lib/gitlab/graphql/tracers/logger_tracer_spec.rb b/spec/lib/gitlab/graphql/tracers/logger_tracer_spec.rb new file mode 100644 index 00000000000..d83ac4dabc5 --- /dev/null +++ b/spec/lib/gitlab/graphql/tracers/logger_tracer_spec.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true +require "fast_spec_helper" +require "support/graphql/fake_query_type" + +RSpec.describe Gitlab::Graphql::Tracers::LoggerTracer do + let(:dummy_schema) do + Class.new(GraphQL::Schema) do + # LoggerTracer depends on TimerTracer + use Gitlab::Graphql::Tracers::LoggerTracer + use Gitlab::Graphql::Tracers::TimerTracer + + query_analyzer Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer.new + + query Graphql::FakeQueryType + end + end + + around do |example| + Gitlab::ApplicationContext.with_context(caller_id: 'caller_a', feature_category: 'feature_a') do + example.run + end + end + + it "logs every query", :aggregate_failures do + variables = { name: "Ada Lovelace" } + query_string = 'query fooOperation($name: String) { helloWorld(message: $name) }' + + # Build an actual query so we don't have to hardocde the "fingerprint" calculations + query = GraphQL::Query.new(dummy_schema, query_string, variables: variables) + + expect(::Gitlab::GraphqlLogger).to receive(:info).with({ + "correlation_id" => anything, + "meta.caller_id" => "caller_a", + "meta.feature_category" => "feature_a", + "query_analysis.duration_s" => kind_of(Numeric), + "query_analysis.complexity" => 1, + "query_analysis.depth" => 1, + "query_analysis.used_deprecated_fields" => [], + "query_analysis.used_fields" => ["FakeQuery.helloWorld"], + duration_s: be > 0, + is_mutation: false, + operation_fingerprint: query.operation_fingerprint, + operation_name: 'fooOperation', + query_fingerprint: query.fingerprint, + query_string: query_string, + trace_type: "execute_query", + variables: variables.to_s + }) + + dummy_schema.execute(query_string, variables: variables) + end +end diff --git a/spec/lib/gitlab/graphql/tracers/metrics_tracer_spec.rb b/spec/lib/gitlab/graphql/tracers/metrics_tracer_spec.rb new file mode 100644 index 00000000000..ff6a76aa319 --- /dev/null +++ b/spec/lib/gitlab/graphql/tracers/metrics_tracer_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rspec-parameterized' +require "support/graphql/fake_query_type" + +RSpec.describe Gitlab::Graphql::Tracers::MetricsTracer do + using RSpec::Parameterized::TableSyntax + + let(:default_known_operations) { ::Gitlab::Graphql::KnownOperations.new(%w(lorem foo bar)) } + + let(:fake_schema) do + Class.new(GraphQL::Schema) do + use Gitlab::Graphql::Tracers::ApplicationContextTracer + use Gitlab::Graphql::Tracers::MetricsTracer + use Gitlab::Graphql::Tracers::TimerTracer + + query Graphql::FakeQueryType + end + end + + around do |example| + ::Gitlab::ApplicationContext.with_context(feature_category: 'test_feature_category') do + example.run + end + end + + before do + allow(::Gitlab::Graphql::KnownOperations).to receive(:default).and_return(default_known_operations) + end + + describe 'when used as tracer and query is executed' do + where(:duration, :expected_success) do + 0.1 | true + 0.1 + ::Gitlab::EndpointAttributes::DEFAULT_URGENCY.duration | false + end + + with_them do + it 'increments sli' do + # Trigger initialization + fake_schema + + # setup timer + current_time = 0 + allow(Gitlab::Metrics::System).to receive(:monotonic_time) { current_time += duration } + + expect(Gitlab::Metrics::RailsSlis.graphql_query_apdex).to receive(:increment).with( + labels: { + endpoint_id: 'graphql:lorem', + feature_category: 'test_feature_category', + query_urgency: ::Gitlab::EndpointAttributes::DEFAULT_URGENCY.name + }, + success: expected_success + ) + + fake_schema.execute("query lorem { helloWorld }") + end + end + end +end diff --git a/spec/lib/gitlab/graphql/tracers/timer_tracer_spec.rb b/spec/lib/gitlab/graphql/tracers/timer_tracer_spec.rb new file mode 100644 index 00000000000..7f837e28772 --- /dev/null +++ b/spec/lib/gitlab/graphql/tracers/timer_tracer_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true +require "fast_spec_helper" +require "support/graphql/fake_tracer" +require "support/graphql/fake_query_type" + +RSpec.describe Gitlab::Graphql::Tracers::TimerTracer do + let(:expected_duration) { 5 } + let(:tracer_spy) { spy('tracer_spy') } + let(:dummy_schema) do + schema = Class.new(GraphQL::Schema) do + use Gitlab::Graphql::Tracers::TimerTracer + + query Graphql::FakeQueryType + end + + schema.tracer(Graphql::FakeTracer.new(lambda { |*args| tracer_spy.trace(*args) })) + + schema + end + + before do + current_time = 0 + allow(Gitlab::Metrics::System).to receive(:monotonic_time) do + current_time += expected_duration + end + end + + it "adds duration_s to the trace metadata", :aggregate_failures do + query_string = "query fooOperation { helloWorld }" + + dummy_schema.execute(query_string) + + # "parse" and "execute_query" are just arbitrary trace events + expect(tracer_spy).to have_received(:trace).with("parse", { + duration_s: expected_duration, + query_string: query_string + }) + expect(tracer_spy).to have_received(:trace).with("execute_query", { + # greater than expected duration because other calls made to `.monotonic_time` are outside our control + duration_s: be >= expected_duration, + query: instance_of(GraphQL::Query) + }) + end +end |