class Yabeda::GraphQL::Tracing

Public Instance Methods

cache(query) click to toggle source
# File lib/yabeda/graphql/tracing.rb, line 81
def cache(query)
  query.context.namespace(Yabeda::GraphQL)[:field_call_cache]
end
extract_field_tags(field) click to toggle source
# File lib/yabeda/graphql/tracing.rb, line 57
def extract_field_tags(field)
  owner = field.respond_to?(:owner) ? field.owner : field.metadata[:type_class].owner
  {
    type: owner.graphql_name,
    field: field.graphql_name,
    deprecated: !field.deprecation_reason.nil?,
  }
end
extract_field_trace_data(data) click to toggle source

See graphql-ruby.org/api-doc/1.10.5/GraphQL/Tracing

# File lib/yabeda/graphql/tracing.rb, line 49
def extract_field_trace_data(data)
  if data[:context] # Legacy non-interpreter mode
    [data[:context].field, data[:context].path, data[:context].query]
  else # Interpreter mode
    data.values_at(:field, :path, :query)
  end
end
instrument_field_execution(query, path, tags, duration) click to toggle source
# File lib/yabeda/graphql/tracing.rb, line 66
def instrument_field_execution(query, path, tags, duration)
  cache(query)[path][:tags] = tags
  cache(query)[path][:duration] += duration
end
instrument_mutation_execution(tags) click to toggle source
# File lib/yabeda/graphql/tracing.rb, line 71
def instrument_mutation_execution(tags)
  tags = { name: tags[:field], deprecated: tags[:deprecated] }
  Yabeda.graphql.mutation_fields_count.increment(tags)
end
instrument_query_execution(tags) click to toggle source
# File lib/yabeda/graphql/tracing.rb, line 76
def instrument_query_execution(tags)
  tags = { name: tags[:field], deprecated: tags[:deprecated] }
  Yabeda.graphql.query_fields_count.increment(tags)
end
platform_authorized_key(type) click to toggle source

We don't use these yet, but graphql-ruby require us to declare them

# File lib/yabeda/graphql/tracing.rb, line 91
def platform_authorized_key(type)
  "#{type.graphql_name}.authorized"
end
platform_field_key(type, field) click to toggle source
# File lib/yabeda/graphql/tracing.rb, line 85
def platform_field_key(type, field)
  "#{type.graphql_name}.#{field.graphql_name}"
end
platform_resolve_type_key(type) click to toggle source
# File lib/yabeda/graphql/tracing.rb, line 95
def platform_resolve_type_key(type)
  "#{type.graphql_name}.resolve_type"
end
platform_trace(platform_key, key, data, &block) click to toggle source
# File lib/yabeda/graphql/tracing.rb, line 20
def platform_trace(platform_key, key, data, &block)
  start = ::Process.clock_gettime ::Process::CLOCK_MONOTONIC
  result = block.call
  duration = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) - start

  case key
  when "execute_field", "execute_field_lazy"
    field, path, query = extract_field_trace_data(data)

    tags = extract_field_tags(field)
    if path.length == 1
      return result if key == "execute_field" && query.schema.lazy?(result)

      if query.query?
        instrument_query_execution(tags)
      elsif query.mutation?
        instrument_mutation_execution(tags)
      elsif query.subscription?
        # Not implemented yet
      end
    else
      instrument_field_execution(query, path, tags, duration)
    end
  end

  result
end