diff options
author | Jan Provaznik <jprovaznik@gitlab.com> | 2019-04-30 21:31:08 +0200 |
---|---|---|
committer | Jan Provaznik <jprovaznik@gitlab.com> | 2019-04-30 21:32:29 +0200 |
commit | 068a6ad9bc1bd90d6be6c0e4301861d56e541a37 (patch) | |
tree | f72378ffe5b2a0e14d2f8c8eb0826fc8cc9602bf | |
parent | ca2856891f7ca7f8ce6a870b48e9c052931eb152 (diff) | |
download | gitlab-ce-jprovazn-graphql-opentracing.tar.gz |
Switched to Labkitjprovazn-graphql-opentracing
Labkit now supports a generic `.with_tracing` method which we can use
for tracing graphql calls.
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | Gemfile.lock | 4 | ||||
-rw-r--r-- | lib/gitlab/graphql/generic_tracing.rb | 16 | ||||
-rw-r--r-- | lib/gitlab/tracing/graphql.rb | 29 | ||||
-rw-r--r-- | spec/lib/gitlab/graphql/generic_tracing_spec.rb | 34 | ||||
-rw-r--r-- | spec/lib/gitlab/tracing/graphql_spec.rb | 38 |
6 files changed, 48 insertions, 75 deletions
@@ -276,7 +276,7 @@ gem 'sentry-raven', '~> 2.7' gem 'premailer-rails', '~> 1.9.7' # LabKit: Tracing and Correlation -gem 'gitlab-labkit', '~> 0.1.2' +gem 'gitlab-labkit', '~> 0.2.0' # I18n gem 'ruby_parser', '~> 3.8', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 3b03a8ef691..4db00ba4e18 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -288,7 +288,7 @@ GEM github-markup (1.7.0) gitlab-default_value_for (3.1.1) activerecord (>= 3.2.0, < 6.0) - gitlab-labkit (0.1.2) + gitlab-labkit (0.2.0) actionpack (~> 5) activesupport (~> 5) grpc (~> 1.15) @@ -1059,7 +1059,7 @@ DEPENDENCIES gitaly-proto (~> 1.26.0) github-markup (~> 1.7.0) gitlab-default_value_for (~> 3.1.1) - gitlab-labkit (~> 0.1.2) + gitlab-labkit (~> 0.2.0) gitlab-markup (~> 1.7.0) gitlab-sidekiq-fetcher (~> 0.4.0) gitlab-styles (~> 2.5) diff --git a/lib/gitlab/graphql/generic_tracing.rb b/lib/gitlab/graphql/generic_tracing.rb index 72b7512d5c9..936b22d5afa 100644 --- a/lib/gitlab/graphql/generic_tracing.rb +++ b/lib/gitlab/graphql/generic_tracing.rb @@ -27,9 +27,7 @@ module Gitlab tags = { platform_key: platform_key, key: key } start = Gitlab::Metrics::System.monotonic_time - opentracing.measure(tags: tags) do - yield - end + with_labkit_tracing(tags, &block) ensure duration = Gitlab::Metrics::System.monotonic_time - start @@ -38,8 +36,16 @@ module Gitlab private - def opentracing - @opentracing ||= Gitlab::Tracing::Graphql.new(enabled: Gitlab::Tracing.enabled?) + def with_labkit_tracing(tags, &block) + return yield unless Labkit::Tracing.enabled? + + name = "#{tags[:platform_key]}.#{tags[:key]}" + span_tags = { + 'component' => 'web', + 'span.kind' => 'server' + }.merge(tags.stringify_keys) + + Labkit::Tracing.with_tracing(operation_name: name, tags: span_tags, &block) end def graphql_duration_seconds diff --git a/lib/gitlab/tracing/graphql.rb b/lib/gitlab/tracing/graphql.rb deleted file mode 100644 index 7f3192c8c42..00000000000 --- a/lib/gitlab/tracing/graphql.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -require 'opentracing' - -module Gitlab - module Tracing - class Graphql - include Common - - attr_reader :enabled - - def initialize(enabled: true) - @enabled = enabled - end - - def measure(tags: {}, &block) - return yield unless enabled - - name = "#{tags[:platform_key]}.#{tags[:key]}" - span_tags = { - 'component' => 'web', - 'span.kind' => 'server' - }.merge(tags.stringify_keys) - - in_tracing_span(operation_name: name, tags: span_tags, &block) - end - end - end -end diff --git a/spec/lib/gitlab/graphql/generic_tracing_spec.rb b/spec/lib/gitlab/graphql/generic_tracing_spec.rb index 3e0aae589e9..ae92dcc40af 100644 --- a/spec/lib/gitlab/graphql/generic_tracing_spec.rb +++ b/spec/lib/gitlab/graphql/generic_tracing_spec.rb @@ -23,6 +23,40 @@ describe Gitlab::Graphql::GenericTracing do 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) diff --git a/spec/lib/gitlab/tracing/graphql_spec.rb b/spec/lib/gitlab/tracing/graphql_spec.rb deleted file mode 100644 index a20dfb0b4a0..00000000000 --- a/spec/lib/gitlab/tracing/graphql_spec.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -require 'fast_spec_helper' - -describe Gitlab::Tracing::Graphql do - describe "#measure" do - context "when enabled" do - subject { described_class.new } - - it 'yields with measurement' do - input_tags = { platform_key: 'pkey', key: 'key' } - expected_tags = { - 'component' => 'web', - 'span.kind' => 'server', - 'platform_key' => 'pkey', - 'key' => 'key' - } - - expect(subject) - .to receive(:in_tracing_span) - .with(operation_name: "pkey.key", tags: expected_tags) - .and_yield - - expect { |b| subject.measure(tags: input_tags, &b) }.to yield_control - end - end - - context "when disabled" do - subject { described_class.new(enabled: false) } - - it 'yields without measurement' do - expect(subject).not_to receive(:in_tracing_span) - - expect { |b| subject.measure(tags: {}, &b) }.to yield_control - end - end - end -end |