Module: GraphQL::Tracing::NewRelicTrace

Defined in:
lib/graphql/tracing/new_relic_trace.rb

Overview

A tracer for reporting GraphQL-Ruby time to New Relic

Examples:

Installing the tracer

class MySchema < GraphQL::Schema
  trace_with GraphQL::Tracing::NewRelicTrace

  # Optional, use the operation name to set the new relic transaction name:
  # trace_with GraphQL::Tracing::NewRelicTrace, set_transaction_name: true
end

Installing without trace events for authorized? or resolve_type calls

trace_with GraphQL::Tracing::NewRelicTrace, trace_authorized: false, trace_resolve_type: false

Instance Method Summary collapse

Instance Method Details

#begin_analyze_multiplex(multiplex, analyzers) ⇒ Object



71
72
73
74
# File 'lib/graphql/tracing/new_relic_trace.rb', line 71

def begin_analyze_multiplex(multiplex, analyzers)
  @nr_analyze = NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: "GraphQL/analyze", category: :web)
  super
end

#begin_authorized(type, obj, ctx) ⇒ Object



114
115
116
117
118
119
# File 'lib/graphql/tracing/new_relic_trace.rb', line 114

def begin_authorized(type, obj, ctx)
  if @trace_authorized
    start_segment(partial_name: @nr_authorized_names[type], category: :web)
  end
  super
end

#begin_dataloader_source(source) ⇒ Object



142
143
144
145
# File 'lib/graphql/tracing/new_relic_trace.rb', line 142

def begin_dataloader_source(source)
  start_segment(partial_name: @nr_source_names[source], category: :web)
  super
end

#begin_execute_field(field, object, arguments, query) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/graphql/tracing/new_relic_trace.rb', line 96

def begin_execute_field(field, object, arguments, query)
  return_type = field.type.unwrap
  trace_field = if return_type.kind.scalar? || return_type.kind.enum?
    (field.trace.nil? && @trace_scalars) || field.trace
  else
    true
  end
  if trace_field
    start_segment(partial_name: @nr_field_names[field], category: :web)
  end
  super
end

#begin_execute_multiplex(multiplex) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/graphql/tracing/new_relic_trace.rb', line 81

def begin_execute_multiplex(multiplex)
  query = multiplex.queries.first
  set_this_txn_name = query.context[:set_new_relic_transaction_name]
  if set_this_txn_name || (set_this_txn_name.nil? && @set_transaction_name)
    NewRelic::Agent.set_transaction_name(transaction_name(query))
  end
  @nr_execute = NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: "GraphQL/execute", category: :web)
  super
end

#begin_parse(query_str) ⇒ Object



51
52
53
54
# File 'lib/graphql/tracing/new_relic_trace.rb', line 51

def begin_parse(query_str)
  @nr_parse = NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: "GraphQL/parse", category: :web)
  super
end

#begin_resolve_type(type, value, context) ⇒ Object



128
129
130
131
132
133
# File 'lib/graphql/tracing/new_relic_trace.rb', line 128

def begin_resolve_type(type, value, context)
  if @trace_resolve_type
    start_segment(partial_name: @nr_resolve_type_names[type], category: :web)
  end
  super
end

#begin_validate(query, validate) ⇒ Object



61
62
63
64
# File 'lib/graphql/tracing/new_relic_trace.rb', line 61

def begin_validate(query, validate)
  @nr_validate = NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: "GraphQL/validate", category: :web)
  super
end

#dataloader_fiber_resume(source) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/graphql/tracing/new_relic_trace.rb', line 158

def dataloader_fiber_resume(source)
  prev_segment = Fiber[:graphql_nr_previous_segment]
  Fiber[:graphql_nr_previous_segment] = nil
  if prev_segment
    seg_partial_name = prev_segment.name.sub(/^.*(GraphQL.*)$/, '\1')
    start_segment(partial_name: seg_partial_name, category: :web)
  end
  super
end

#dataloader_fiber_yield(source) ⇒ Object



152
153
154
155
156
# File 'lib/graphql/tracing/new_relic_trace.rb', line 152

def dataloader_fiber_yield(source)
  prev_segment = finish_segment
  Fiber[:graphql_nr_previous_segment] = prev_segment
  super
end

#end_analyze_multiplex(multiplex, analyzers) ⇒ Object



76
77
78
79
# File 'lib/graphql/tracing/new_relic_trace.rb', line 76

def end_analyze_multiplex(multiplex, analyzers)
  @nr_analyze.finish
  super
end

#end_authorized(type, obj, ctx, is_authed) ⇒ Object



121
122
123
124
125
126
# File 'lib/graphql/tracing/new_relic_trace.rb', line 121

def end_authorized(type, obj, ctx, is_authed)
  if @trace_authorized
    finish_segment
  end
  super
end

#end_dataloader_source(source) ⇒ Object



147
148
149
150
# File 'lib/graphql/tracing/new_relic_trace.rb', line 147

def end_dataloader_source(source)
  finish_segment
  super
end

#end_execute_field(field, objects, arguments, query, result) ⇒ Object



109
110
111
112
# File 'lib/graphql/tracing/new_relic_trace.rb', line 109

def end_execute_field(field, objects, arguments, query, result)
  finish_segment
  super
end

#end_execute_multiplex(multiplex) ⇒ Object



91
92
93
94
# File 'lib/graphql/tracing/new_relic_trace.rb', line 91

def end_execute_multiplex(multiplex)
  @nr_execute.finish
  super
end

#end_parse(query_str) ⇒ Object



56
57
58
59
# File 'lib/graphql/tracing/new_relic_trace.rb', line 56

def end_parse(query_str)
  @nr_parse.finish
  super
end

#end_resolve_type(type, value, context, resolved_type) ⇒ Object



135
136
137
138
139
140
# File 'lib/graphql/tracing/new_relic_trace.rb', line 135

def end_resolve_type(type, value, context, resolved_type)
  if @trace_resolve_type
    finish_segment
  end
  super
end

#end_validate(query, validate, validation_errors) ⇒ Object



66
67
68
69
# File 'lib/graphql/tracing/new_relic_trace.rb', line 66

def end_validate(query, validate, validation_errors)
  @nr_validate.finish
  super
end

#initialize(set_transaction_name: false, trace_authorized: true, trace_resolve_type: true, trace_scalars: false, **_rest) ⇒ Object

Parameters:

  • set_transaction_name (Boolean) (defaults to: false)

    If true, the GraphQL operation name will be used as the transaction name. This is not advised if you run more than one query per HTTP request, for example, with graphql-client or multiplexing. It can also be specified per-query with context[:set_new_relic_transaction_name].

  • trace_authorized (Boolean) (defaults to: true)

    If false, skip tracing authorized? calls

  • trace_resolve_type (Boolean) (defaults to: true)

    If false, skip tracing resolve_type? calls

  • trace_scalars (Boolean) (defaults to: false)

    If true, Enum and Scalar fields will be traced by default



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/graphql/tracing/new_relic_trace.rb', line 26

def initialize(set_transaction_name: false, trace_authorized: true, trace_resolve_type: true, trace_scalars: false, **_rest)
  @set_transaction_name = set_transaction_name
  @trace_authorized = trace_authorized
  @trace_resolve_type = trace_resolve_type
  @trace_scalars = trace_scalars
  @nr_field_names = Hash.new do |h, field|
    h[field] = "GraphQL/#{field.owner.graphql_name}/#{field.graphql_name}"
  end.compare_by_identity

  @nr_authorized_names = Hash.new do |h, type|
    h[type] = "GraphQL/Authorized/#{type.graphql_name}"
  end.compare_by_identity

  @nr_resolve_type_names = Hash.new do |h, type|
    h[type] = "GraphQL/ResolveType/#{type.graphql_name}"
  end.compare_by_identity

  @nr_source_names = Hash.new do |h, source_inst|
    h[source_inst] = "GraphQL/Source/#{source_inst.class.name}"
  end.compare_by_identity

  @nr_parse = @nr_validate = @nr_analyze = @nr_execute = nil
  super
end