Sha256: f278134116049d1dbec3afd577b7b5377f3ceb65dd0a0c5b599ec5cc91422daa

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

require 'graphql/opentracing/version'

module GraphQL
  module Tracer
    class IncompatibleGemVersion < StandardError; end

    class << self
      attr_accessor :ignore_request, :tracer

      IgnoreRequest = ->(_name, _metadata) { false }

      def instrument(schema: nil, tracer: OpenTracing.global_tracer,
                     ignore_request: IgnoreRequest)
        begin
          require 'graphql'
        rescue LoadError
          return
        end
        raise IncompatibleGemVersion unless compatible_version?

        @schema = schema
        @ignore_request = ignore_request
        @tracer = tracer
        install_tracer
      end

      def compatible_version?
        # support for ActiveSupportNotificationsTracing
        Gem::Version.new(GraphQL::VERSION) >= Gem::Version.new('1.7.0')
      end

      def install_tracer
        @schema.tracer self if @schema
      end

      def trace(key, metadata)
        return yield if @ignore_request.call(key, metadata)

        @tracer.start_active_span("graphql.#{key}", tags: {
                                    'component' => 'ruby-graphql',
                                    'span.kind' => 'server'
                                  }) do |scope|
          begin
            yield
          rescue StandardError
            scope.span.set_tag('error', true)
            raise
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-opentracing-0.2.0 lib/graphql/tracer.rb