Class: GraphQL::Tracing

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/tracing.rb

Instance Method Summary collapse

Instance Method Details

#after_query(query) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/graphql/tracing.rb', line 20

def after_query(query)
  result = query.result
  end_time = Time.now.utc
  duration_nanos = duration_nanos(start_time: query.context['graphql-tracing']['start_time'], end_time: end_time)

  result["extensions"] ||= {}
  result["extensions"]["tracing"] = {
    "version" => 1,
    "startTime" => query.context['graphql-tracing']['start_time'].strftime('%FT%T.%3NZ'),
    "endTime" => end_time.strftime('%FT%T.%3NZ'),
    "duration" => duration_nanos,
    "execution" => {
      "resolvers" => query.context['graphql-tracing']['resolvers']
    }
  }
end

#before_query(query) ⇒ Object



13
14
15
16
17
18
# File 'lib/graphql/tracing.rb', line 13

def before_query(query)
  query.context['graphql-tracing'] = {
    'start_time' => Time.now.utc,
    'resolvers' => []
  }
end

#instrument(type, field) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/graphql/tracing.rb', line 37

def instrument(type, field)
  old_resolve_proc = field.resolve_proc

  new_resolve_proc = ->(obj, args, ctx) do
    resolve_start_time = Time.now.utc
    result = old_resolve_proc.call(obj, args, ctx)
    resolve_end_time = Time.now.utc

    ctx['graphql-tracing']['resolvers'] << {
      'path' => ctx.path,
      'parentType' => type.name,
      'fieldName' => field.name,
      'returnType' => field.type.to_s,
      'startOffset' => duration_nanos(start_time: ctx['graphql-tracing']['start_time'], end_time: resolve_start_time),
      'duration' => duration_nanos(start_time: resolve_start_time, end_time: resolve_end_time)
    }

    result
  end

  field.redefine { resolve(new_resolve_proc) }
end

#use(schema_definition) ⇒ Object



8
9
10
11
# File 'lib/graphql/tracing.rb', line 8

def use(schema_definition)
  schema_definition.instrument(:query, self)
  schema_definition.instrument(:field, self)
end