Skip to content

Commit

Permalink
GraphQL: cache attribute hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Nov 21, 2023
1 parent 02799c8 commit fff3db5
Showing 1 changed file with 61 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,61 @@ module GraphQLTrace # rubocop:disable Metrics/ModuleLength
def initialize(trace_scalars: false, **_options)
@trace_scalars = trace_scalars
@_otel_field_key_cache = Hash.new { |h, k| h[k] = _otel_field_key(k) }
@_otel_field_key_cache.compare_by_identity
@_otel_authorized_key_cache = Hash.new { |h, k| h[k] = _otel_authorized_key(k) }
@_otel_authorized_key_cache.compare_by_identity
@_otel_resolve_type_key_cache = Hash.new { |h, k| h[k] = _otel_resolve_type_key(k) }
@_otel_resolve_type_key_cache.compare_by_identity

@_otel_authorized_attrs_cache = Hash.new do |h, type|
h[type] = {
'graphql.type.name' => type.graphql_name,
'graphql.lazy' => false
}.freeze
end
@_otel_authorized_attrs_cache.compare_by_identity

@_otel_lazy_authorized_attrs_cache = Hash.new do |h, type|
h[type] = {
'graphql.type.name' => type.graphql_name,
'graphql.lazy' => true
}.freeze
end
@_otel_lazy_authorized_attrs_cache.compare_by_identity

@_otel_field_attrs_cache = Hash.new do |h, field|
h[field] = {
'graphql.field.parent' => field.owner&.graphql_name,
'graphql.field.name' => field.graphql_name,
'graphql.lazy' => false
}.freeze
end
@_otel_field_attrs_cache.compare_by_identity

@_otel_lazy_field_attrs_cache = Hash.new do |h, field|
h[field] = {
'graphql.field.parent' => field.owner&.graphql_name,
'graphql.field.name' => field.graphql_name,
'graphql.lazy' => true
}.freeze
end
@_otel_lazy_field_attrs_cache.compare_by_identity

@_otel_resolve_type_attrs_cache = Hash.new do |h, type|
h[type] = {
'graphql.type.name' => type.graphql_name,
'graphql.lazy' => false
}.freeze
end
@_otel_resolve_type_attrs_cache.compare_by_identity

@_otel_lazy_resolve_type_attrs_cache = Hash.new do |h, type|
h[type] = {
'graphql.type.name' => type.graphql_name,
'graphql.lazy' => true
}.freeze
end
@_otel_lazy_resolve_type_attrs_cache.compare_by_identity
super
end

Expand Down Expand Up @@ -73,26 +126,18 @@ def execute_query_lazy(query:, multiplex:, &block)

def execute_field(field:, query:, ast_node:, arguments:, object:, &block)
platform_key = _otel_execute_field_key(field: field)
return super unless platform_key
return super(field: field, query: query, ast_node: ast_node, object: object, arguments: arguments, &block) unless platform_key

attributes = {
'graphql.field.parent' => field.owner&.graphql_name,
'graphql.field.name' => field.graphql_name,
'graphql.lazy' => false
}
attributes = @_otel_field_attrs_cache[field]

tracer.in_span(platform_key, attributes: attributes, &block)
end

def execute_field_lazy(field:, query:, ast_node:, arguments:, object:, &block)
platform_key = _otel_execute_field_key(field: field)
return super unless platform_key
return super(field: field, query: query, ast_node: ast_node, object: object, arguments: arguments, &block) unless platform_key

attributes = {
'graphql.field.parent' => field.owner&.graphql_name,
'graphql.field.name' => field.graphql_name,
'graphql.lazy' => true
}
attributes = @_otel_lazy_field_attrs_cache[field]

tracer.in_span(platform_key, attributes: attributes, &block)
end
Expand All @@ -101,10 +146,7 @@ def authorized(query:, type:, object:, &block)
platform_key = @_otel_authorized_key_cache[type]
return super unless platform_key

attributes = {
'graphql.type.name' => type.graphql_name,
'graphql.lazy' => false
}
attributes = @_otel_authorized_attrs_cache[type]

tracer.in_span(platform_key, attributes: attributes, &block)
end
Expand All @@ -113,33 +155,19 @@ def authorized_lazy(query:, type:, object:, &block)
platform_key = @_otel_authorized_key_cache[type]
return super unless platform_key

attributes = {
'graphql.type.name' => type.graphql_name,
'graphql.lazy' => true
}

attributes = @_otel_lazy_authorized_attrs_cache[type]
tracer.in_span(platform_key, attributes: attributes, &block)
end

def resolve_type(query:, type:, object:, &block)
platform_key = @_otel_resolve_type_key_cache[type]

attributes = {
'graphql.type.name' => type.graphql_name,
'graphql.lazy' => false
}

attributes = @_otel_resolve_type_attrs_cache[type]
tracer.in_span(platform_key, attributes: attributes, &block)
end

def resolve_type_lazy(query:, type:, object:, &block)
platform_key = @_otel_resolve_type_key_cache[type]

attributes = {
'graphql.type.name' => type.graphql_name,
'graphql.lazy' => true
}

attributes = @_otel_lazy_resolve_type_attrs_cache[type]
tracer.in_span(platform_key, attributes: attributes, &block)
end

Expand Down

0 comments on commit fff3db5

Please sign in to comment.