Commit 14077bf2 authored by Andrew Newdigate's avatar Andrew Newdigate
Browse files

Merge branch 'add-graphql' into 'master'

Expose in_tracing_span as a public module method

See merge request gitlab-org/labkit-ruby!7
parents fd6f0b16 cd9c4131
Loading
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -5,13 +5,13 @@ require "active_support/all"
module Labkit
  # Tracing provides distributed tracing functionality
  module Tracing
    autoload :Common, "labkit/tracing/common"
    autoload :Factory, "labkit/tracing/factory"
    autoload :GRPCInterceptor, "labkit/tracing/grpc_interceptor"
    autoload :JaegerFactory, "labkit/tracing/jaeger_factory"
    autoload :RackMiddleware, "labkit/tracing/rack_middleware"
    autoload :Rails, "labkit/tracing/rails"
    autoload :Sidekiq, "labkit/tracing/sidekiq"
    autoload :TracingUtils, "labkit/tracing/tracing_utils"

    # Tracing is only enabled when the `GITLAB_TRACING` env var is configured.
    def self.enabled?
@@ -43,5 +43,15 @@ module Labkit
                          .gsub("{{ correlation_id }}", correlation_id)
                          .gsub("{{ service }}", service_name)
    end

    # This will run a block with a span
    # @param operation_name [String] The operation name for the span
    # @param tags [Hash] Tags to assign to the span
    # @param child_of [SpanContext, Span] SpanContext that acts as a parent to
    #  the newly-started span. If a span instance is provided, its
    #  context is automatically substituted.
    def self.with_tracing(**kwargs, &block)
      TracingUtils.with_tracing(**kwargs, &block)
    end
  end
end
+1 −2
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ module Labkit
    # GRPCInterceptor is a client-side GRPC interceptor
    # for instrumenting GRPC calls with distributed tracing
    class GRPCInterceptor < GRPC::ClientInterceptor
      include Common
      include Singleton

      def request_response(request:, call:, method:, metadata:)
@@ -33,7 +32,7 @@ module Labkit
      def wrap_with_tracing(method, grpc_type, metadata)
        tags = { "component" => "grpc", "span.kind" => "client", "grpc.method" => method, "grpc.type" => grpc_type }

        in_tracing_span(operation_name: "grpc:#{method}", tags: tags) do |span|
        TracingUtils.with_tracing(operation_name: "grpc:#{method}", tags: tags) do |span|
          OpenTracing.inject(span.context, OpenTracing::FORMAT_TEXT_MAP, metadata)

          yield
+2 −4
Original line number Diff line number Diff line
@@ -10,8 +10,6 @@ module Labkit
    # instrumenting incoming http requests into a Rails/Rack
    # server
    class RackMiddleware
      include Common

      REQUEST_METHOD = "REQUEST_METHOD"

      def initialize(app)
@@ -21,10 +19,10 @@ module Labkit
      def call(env)
        method = env[REQUEST_METHOD]

        context = tracer.extract(OpenTracing::FORMAT_RACK, env)
        context = TracingUtils.tracer.extract(OpenTracing::FORMAT_RACK, env)
        tags = { "component" => "rack", "span.kind" => "server", "http.method" => method, "http.url" => self.class.build_sanitized_url_from_env(env) }

        in_tracing_span(operation_name: "http:#{method}", child_of: context, tags: tags) do |span|
        TracingUtils.with_tracing(operation_name: "http:#{method}", child_of: context, tags: tags) do |span|
          @app.call(env).tap { |status_code, _headers, _body| span.set_tag("http.status_code", status_code) }
        end
      end
+1 −2
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ module Labkit
      # functionality for the rails instrumentation classes
      module RailsCommon
        extend ActiveSupport::Concern
        include Labkit::Tracing::Common

        class_methods do
          def create_unsubscriber(subscriptions)
@@ -20,7 +19,7 @@ module Labkit
        def generate_span_for_notification(operation_name, start, finish, payload, tags)
          exception = payload[:exception]

          postnotify_span(operation_name, start, finish, tags: tags, exception: exception)
          TracingUtils.postnotify_span(operation_name, start, finish, tags: tags, exception: exception)
        end
      end
    end
+2 −3
Original line number Diff line number Diff line
@@ -14,10 +14,9 @@ module Labkit
        SPAN_KIND = "client"

        def call(_worker_class, job, _queue, _redis_pool)
          in_tracing_span(operation_name: "sidekiq:#{job["class"]}", tags: tags_from_job(job, SPAN_KIND)) do |span|
          TracingUtils.with_tracing(operation_name: "sidekiq:#{job["class"]}", tags: tags_from_job(job, SPAN_KIND)) do |span|
            # Inject the details directly into the job
            tracer
              .inject(span.context, OpenTracing::FORMAT_TEXT_MAP, job)
            TracingUtils.tracer.inject(span.context, OpenTracing::FORMAT_TEXT_MAP, job)

            yield
          end
Loading