Class: OpticsAgent::Instrumenters::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/optics-agent/instrumenters/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



4
5
6
# File 'lib/optics-agent/instrumenters/field.rb', line 4

def agent
  @agent
end

Instance Method Details

#instrument(type, field) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/optics-agent/instrumenters/field.rb', line 6

def instrument(type, field)
  old_resolve_proc = field.resolve_proc
  new_resolve_proc = ->(obj, args, ctx) {
    if @agent
      middleware(@agent, type, obj, field, args, ctx, ->() { old_resolve_proc.call(obj, args, ctx) })
    else
      old_resolve_proc.call(obj, args, ctx)
    end
  }

  new_field = field.redefine do
    resolve(new_resolve_proc)
  end

  if old_resolve_proc.instance_of? GraphQL::Relay::ConnectionResolve
    new_field.arguments = field.arguments
  end

  new_field
end

#middleware(agent, parent_type, parent_object, field_definition, field_args, query_context, next_middleware) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/optics-agent/instrumenters/field.rb', line 27

def middleware(agent, parent_type, parent_object, field_definition, field_args, query_context, next_middleware)
  agent_context = query_context[:optics_agent]

  # This happens when an introspection query occurs (reporting schema)
  # Also, people could potentially use it to skip reporting
  return next_middleware.call if agent_context == :skip

  query = agent_context.query

  start_offset = query.duration_so_far
  result = next_middleware.call
  duration = query.duration_so_far - start_offset

  query.report_field(parent_type.to_s, field_definition.name, start_offset, duration)

  result
end