diff options
Diffstat (limited to 'spec/lib/gitlab/graphql')
7 files changed, 290 insertions, 10 deletions
diff --git a/spec/lib/gitlab/graphql/known_operations_spec.rb b/spec/lib/gitlab/graphql/known_operations_spec.rb new file mode 100644 index 00000000000..411c0876f82 --- /dev/null +++ b/spec/lib/gitlab/graphql/known_operations_spec.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'rspec-parameterized' +require "support/graphql/fake_query_type" + +RSpec.describe Gitlab::Graphql::KnownOperations do + using RSpec::Parameterized::TableSyntax + + # Include duplicated operation names to test that we are unique-ifying them + let(:fake_operations) { %w(foo foo bar bar) } + let(:fake_schema) do + Class.new(GraphQL::Schema) do + query Graphql::FakeQueryType + end + end + + subject { described_class.new(fake_operations) } + + describe "#from_query" do + where(:query_string, :expected) do + "query { helloWorld }" | described_class::ANONYMOUS + "query fuzzyyy { helloWorld }" | described_class::UNKNOWN + "query foo { helloWorld }" | described_class::Operation.new("foo") + end + + with_them do + it "returns known operation name from GraphQL Query" do + query = ::GraphQL::Query.new(fake_schema, query_string) + + expect(subject.from_query(query)).to eq(expected) + end + end + end + + describe "#operations" do + it "returns array of known operations" do + expect(subject.operations.map(&:name)).to match_array(%w(anonymous unknown foo bar)) + end + end + + describe "Operation#to_caller_id" do + where(:query_string, :expected) do + "query { helloWorld }" | "graphql:#{described_class::ANONYMOUS.name}" + "query foo { helloWorld }" | "graphql:foo" + end + + with_them do + it "formats operation name for caller_id metric property" do + query = ::GraphQL::Query.new(fake_schema, query_string) + + expect(subject.from_query(query).to_caller_id).to eq(expected) + end + end + end + + describe "Opeartion#query_urgency" do + it "returns the associated query urgency" do + query = ::GraphQL::Query.new(fake_schema, "query foo { helloWorld }") + + expect(subject.from_query(query).query_urgency).to equal(::Gitlab::EndpointAttributes::DEFAULT_URGENCY) + end + end + + describe ".default" do + it "returns a memoization of values from webpack", :aggregate_failures do + # .default could have been referenced in another spec, so we need to clean it up here + described_class.instance_variable_set(:@default, nil) + + expect(Gitlab::Webpack::GraphqlKnownOperations).to receive(:load).once.and_return(fake_operations) + + 2.times { described_class.default } + + # Uses reference equality to verify memoization + expect(described_class.default).to equal(described_class.default) + expect(described_class.default).to be_a(described_class) + expect(described_class.default.operations.map(&:name)).to include(*fake_operations) + end + end +end diff --git a/spec/lib/gitlab/graphql/pagination/connections_spec.rb b/spec/lib/gitlab/graphql/pagination/connections_spec.rb index f3f59113c81..97389b6250e 100644 --- a/spec/lib/gitlab/graphql/pagination/connections_spec.rb +++ b/spec/lib/gitlab/graphql/pagination/connections_spec.rb @@ -8,7 +8,7 @@ RSpec.describe ::Gitlab::Graphql::Pagination::Connections do before(:all) do ActiveRecord::Schema.define do - create_table :testing_pagination_nodes, force: true do |t| + create_table :_test_testing_pagination_nodes, force: true do |t| t.integer :value, null: false end end @@ -16,13 +16,13 @@ RSpec.describe ::Gitlab::Graphql::Pagination::Connections do after(:all) do ActiveRecord::Schema.define do - drop_table :testing_pagination_nodes, force: true + drop_table :_test_testing_pagination_nodes, force: true end end let_it_be(:node_model) do Class.new(ActiveRecord::Base) do - self.table_name = 'testing_pagination_nodes' + self.table_name = '_test_testing_pagination_nodes' end end diff --git a/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb b/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb index fc723138d88..dee8f9e3c64 100644 --- a/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb +++ b/spec/lib/gitlab/graphql/query_analyzers/logger_analyzer_spec.rb @@ -18,12 +18,6 @@ RSpec.describe Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer do GRAPHQL end - describe 'variables' do - subject { initial_value.fetch(:variables) } - - it { is_expected.to eq('{:body=>"[FILTERED]"}') } - end - describe '#final_value' do let(:monotonic_time_before) { 42 } let(:monotonic_time_after) { 500 } @@ -42,7 +36,14 @@ RSpec.describe Gitlab::Graphql::QueryAnalyzers::LoggerAnalyzer do it 'inserts duration in seconds to memo and sets request store' do expect { final_value }.to change { memo[:duration_s] }.to(monotonic_time_duration) - .and change { RequestStore.store[:graphql_logs] }.to([memo]) + .and change { RequestStore.store[:graphql_logs] }.to([{ + complexity: 4, + depth: 2, + operation_name: query.operation_name, + used_deprecated_fields: [], + used_fields: [], + variables: { body: "[FILTERED]" }.to_s + }]) end end end 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 |