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

generic_tracing_spec.rb « graphql « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae92dcc40af5bbde2e1fc0e3a4a28c95731c68c3 (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Graphql::GenericTracing do
  let(:graphql_duration_seconds_histogram) { double('Gitlab::Metrics::NullMetric') }

  it 'updates graphql histogram with expected labels' do
    query = 'query { users { id } }'
    tracer = described_class.new

    allow(tracer)
      .to receive(:graphql_duration_seconds)
      .and_return(graphql_duration_seconds_histogram)

    expect_metric('graphql.lex', 'lex')
    expect_metric('graphql.parse', 'parse')
    expect_metric('graphql.validate', 'validate')
    expect_metric('graphql.analyze', 'analyze_multiplex')
    expect_metric('graphql.execute', 'execute_query_lazy')
    expect_metric('graphql.execute', 'execute_multiplex')

    GitlabSchema.execute(query, context: { tracers: [tracer] })
  end

  context "when labkit tracing is enabled" do
    before do
      expect(Labkit::Tracing).to receive(:enabled?).and_return(true)
    end

    it 'yields with labkit tracing' do
      expected_tags = {
        'component' => 'web',
        'span.kind' => 'server',
        'platform_key' => 'pkey',
        'key' => 'key'
      }

      expect(Labkit::Tracing)
        .to receive(:with_tracing)
        .with(operation_name: "pkey.key", tags: expected_tags)
        .and_yield

      expect { |b| described_class.new.platform_trace('pkey', 'key', nil, &b) }.to yield_control
    end
  end

  context "when labkit tracing is disabled" do
    before do
      expect(Labkit::Tracing).to receive(:enabled?).and_return(false)
    end

    it 'yields without measurement' do
      expect(Labkit::Tracing).not_to receive(:with_tracing)

      expect { |b| described_class.new.platform_trace('pkey', 'key', nil, &b) }.to yield_control
    end
  end

  private

  def expect_metric(platform_key, key)
    expect(graphql_duration_seconds_histogram)
      .to receive(:observe)
      .with({ platform_key: platform_key, key: key }, be > 0.0)
  end
end