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

Instance Method Summary collapse

Instance Method Details

#begin_analyze_multiplex(multiplex, analyzers) ⇒ Object



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

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



101
102
103
104
105
106
# File 'lib/graphql/tracing/new_relic_trace.rb', line 101

def begin_authorized(type, obj, ctx)
  if @trace_authorized
    nr_segment_stack << NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: @nr_authorized_names[type], category: :web)
  end
  super
end

#begin_dataloader(dl) ⇒ Object



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

def begin_dataloader(dl)
  super
end

#begin_dataloader_source(source) ⇒ Object



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

def begin_dataloader_source(source)
  nr_segment_stack << NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: @nr_source_names[source], category: :web)
  super
end

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



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

def begin_execute_field(field, object, arguments, query)
  nr_segment_stack << NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: @nr_field_names[field], category: :web)
  super
end

#begin_execute_multiplex(multiplex) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/graphql/tracing/new_relic_trace.rb', line 76

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



46
47
48
49
# File 'lib/graphql/tracing/new_relic_trace.rb', line 46

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



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

def begin_resolve_type(type, value, context)
  if @trace_resolve_type
    nr_segment_stack << NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: @nr_resolve_type_names[type], category: :web)
  end
  super
end

#begin_validate(query, validate) ⇒ Object



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

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



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

def dataloader_fiber_resume(source)
  prev_segment = nr_segment_stack.pop
  seg_partial_name = prev_segment.name.sub(/^.*(GraphQL.*)$/, '\1')
  nr_segment_stack << NewRelic::Agent::Tracer.start_transaction_or_segment(partial_name: seg_partial_name, category: :web)
  super
end

#dataloader_fiber_yield(source) ⇒ Object



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

def dataloader_fiber_yield(source)
  current_segment = nr_segment_stack.last
  current_segment.finish
  super
end

#end_analyze_multiplex(multiplex, analyzers) ⇒ Object



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

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

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



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

def end_authorized(type, obj, ctx, is_authed)
  if @trace_authorized
    nr_segment_stack.pop.finish
  end
  super
end

#end_dataloader(dl) ⇒ Object



133
134
135
# File 'lib/graphql/tracing/new_relic_trace.rb', line 133

def end_dataloader(dl)
  super
end

#end_dataloader_source(source) ⇒ Object



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

def end_dataloader_source(source)
  nr_segment_stack.pop.finish
  super
end

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



96
97
98
99
# File 'lib/graphql/tracing/new_relic_trace.rb', line 96

def end_execute_field(field, objects, arguments, query, result)
  nr_segment_stack.pop.finish
  super
end

#end_execute_multiplex(multiplex) ⇒ Object



86
87
88
89
# File 'lib/graphql/tracing/new_relic_trace.rb', line 86

def end_execute_multiplex(multiplex)
  @nr_execute.finish
  super
end

#end_parse(query_str) ⇒ Object



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

def end_parse(query_str)
  @nr_parse.finish
  super
end

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



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

def end_resolve_type(type, value, context, resolved_type)
  if @trace_resolve_type
    nr_segment_stack.pop.finish
  end
  super
end

#end_validate(query, validate, validation_errors) ⇒ Object



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

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

#initialize(set_transaction_name: false, trace_authorized: true, trace_resolve_type: true, **_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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/graphql/tracing/new_relic_trace.rb', line 22

def initialize(set_transaction_name: false, trace_authorized: true, trace_resolve_type: true, **_rest)
  @set_transaction_name = set_transaction_name
  @trace_authorized = trace_authorized
  @trace_resolve_type = trace_resolve_type
  @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