Module: OpenTelemetry::Instrumentation::GraphQL::Tracers::GraphQLTrace

Defined in:
lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb

Overview

GraphQLTrace contains the OpenTelemetry tracer implementation compatible with the new GraphQL tracing API (>= 2.0.18)

Instance Method Summary collapse

Instance Method Details

#analyze_multiplex(multiplex:, &block) ⇒ Object



53
54
55
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 53

def analyze_multiplex(multiplex:, &block)
  tracer.in_span('graphql.analyze_query', &block)
end

#analyze_query(query:, &block) ⇒ Object



57
58
59
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 57

def analyze_query(query:, &block)
  tracer.in_span('graphql.analyze_multiplex', &block)
end

#authorized(query:, type:, object:, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 100

def authorized(query:, type:, object:, &block)
  platform_key = @_otel_authorized_key_cache[type]
  return super unless platform_key

  attributes = {
    'graphql.type.name' => type.graphql_name,
    'graphql.lazy' => false
  }

  tracer.in_span(platform_key, attributes: attributes, &block)
end

#authorized_lazy(query:, type:, object:, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 112

def authorized_lazy(query:, type:, object:, &block)
  platform_key = @_otel_authorized_key_cache[type]
  return super unless platform_key

  attributes = {
    'graphql.type.name' => type.graphql_name,
    'graphql.lazy' => true
  }

  tracer.in_span(platform_key, attributes: attributes, &block)
end

#execute_field(field:, query:, ast_node:, arguments:, object:, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 74

def execute_field(field:, query:, ast_node:, arguments:, object:, &block)
  platform_key = _otel_execute_field_key(field: field)
  return super unless platform_key

  attributes = {
    'graphql.field.parent' => field.owner&.graphql_name,
    'graphql.field.name' => field.graphql_name,
    'graphql.lazy' => false
  }

  tracer.in_span(platform_key, attributes: attributes, &block)
end

#execute_field_lazy(field:, query:, ast_node:, arguments:, object:, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 87

def execute_field_lazy(field:, query:, ast_node:, arguments:, object:, &block)
  platform_key = _otel_execute_field_key(field: field)
  return super unless platform_key

  attributes = {
    'graphql.field.parent' => field.owner&.graphql_name,
    'graphql.field.name' => field.graphql_name,
    'graphql.lazy' => true
  }

  tracer.in_span(platform_key, attributes: attributes, &block)
end

#execute_multiplex(multiplex:, &block) ⇒ Object



24
25
26
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 24

def execute_multiplex(multiplex:, &block)
  tracer.in_span('graphql.execute_multiplex', &block)
end

#execute_query(query:, &block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 61

def execute_query(query:, &block)
  attributes = {}
  attributes['graphql.operation.name'] = query.selected_operation_name if query.selected_operation_name
  attributes['graphql.operation.type'] = query.selected_operation.operation_type
  attributes['graphql.document'] = query.query_string

  tracer.in_span('graphql.execute_query', attributes: attributes, &block)
end

#execute_query_lazy(query:, multiplex:, &block) ⇒ Object



70
71
72
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 70

def execute_query_lazy(query:, multiplex:, &block)
  tracer.in_span('graphql.execute_query_lazy', &block)
end

#initialize(trace_scalars: false, **_options) ⇒ Object

rubocop:disable Metrics/ModuleLength



16
17
18
19
20
21
22
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 16

def initialize(trace_scalars: false, **_options)
  @trace_scalars = trace_scalars
  @_otel_field_key_cache = Hash.new { |h, k| h[k] = _otel_field_key(k) }
  @_otel_authorized_key_cache = Hash.new { |h, k| h[k] = _otel_authorized_key(k) }
  @_otel_resolve_type_key_cache = Hash.new { |h, k| h[k] = _otel_resolve_type_key(k) }
  super
end

#lex(query_string:, &block) ⇒ Object



28
29
30
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 28

def lex(query_string:, &block)
  tracer.in_span('graphql.lex', &block)
end

#parse(query_string:, &block) ⇒ Object



32
33
34
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 32

def parse(query_string:, &block)
  tracer.in_span('graphql.parse', &block)
end

#resolve_type(query:, type:, object:, &block) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 124

def resolve_type(query:, type:, object:, &block)
  platform_key = @_otel_resolve_type_key_cache[type]

  attributes = {
    'graphql.type.name' => type.graphql_name,
    'graphql.lazy' => false
  }

  tracer.in_span(platform_key, attributes: attributes, &block)
end

#resolve_type_lazy(query:, type:, object:, &block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 135

def resolve_type_lazy(query:, type:, object:, &block)
  platform_key = @_otel_resolve_type_key_cache[type]

  attributes = {
    'graphql.type.name' => type.graphql_name,
    'graphql.lazy' => true
  }

  tracer.in_span(platform_key, attributes: attributes, &block)
end

#validate(query:, validate:, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opentelemetry/instrumentation/graphql/tracers/graphql_trace.rb', line 36

def validate(query:, validate:, &block)
  tracer.in_span('graphql.validate') do |span|
    super.tap do |response|
      errors = response[:errors]&.compact&.map(&:to_h) || []

      unless errors.empty?
        span.add_event(
          'graphql.validation.error',
          attributes: {
            'exception.message' => errors.to_json
          }
        )
      end
    end
  end
end