2021-12-11 22:18:48 +05:30
|
|
|
# frozen_string_literal: true
|
2022-01-26 12:08:38 +05:30
|
|
|
require "spec_helper"
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
query_analyzer Gitlab::Graphql::QueryAnalyzers::AST::LoggerAnalyzer
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
it "logs every query", :aggregate_failures, :unlimited_max_formatted_output_length do
|
2021-12-11 22:18:48 +05:30
|
|
|
variables = { name: "Ada Lovelace" }
|
|
|
|
query_string = 'query fooOperation($name: String) { helloWorld(message: $name) }'
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
# Build an actual query so we don't have to hardcode the "fingerprint" calculations
|
2021-12-11 22:18:48 +05:30
|
|
|
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
|
2022-01-26 12:08:38 +05:30
|
|
|
|
|
|
|
it 'logs exceptions for breaking queries' do
|
|
|
|
query_string = "query fooOperation { breakingField }"
|
|
|
|
|
|
|
|
expect(::Gitlab::GraphqlLogger).to receive(:info).with(a_hash_including({
|
|
|
|
'exception.message' => 'This field is supposed to break',
|
|
|
|
'exception.class' => 'RuntimeError'
|
|
|
|
}))
|
|
|
|
|
|
|
|
expect { dummy_schema.execute(query_string) }.to raise_error(/This field is supposed to break/)
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|