Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metrics_tracer_spec.rb « tracers « graphql « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 168f5aa529e8eb82355ce9146b64b18fe4503f93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

require '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 apdex 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

    it "does not record apdex for failing queries" do
      query_string = "query fooOperation { breakingField }"

      expect(Gitlab::Metrics::RailsSlis.graphql_query_apdex).not_to receive(:increment)

      expect { fake_schema.execute(query_string) }.to raise_error(/This field is supposed to break/)
    end
  end
end