Class: ElasticGraph::GraphQL::QueryDetailsTracker

Inherits:
Struct
  • Object
show all
Defined in:
lib/elastic_graph/graphql/query_details_tracker.rb

Overview

Class used to track details of what happens during a single GraphQL query for the purposes of logging. Here we use ‘Struct` instead of `Data` specifically because it is designed to be mutable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#datastore_query_client_duration_msObject

Returns the value of attribute datastore_query_client_duration_ms

Returns:

  • (Object)

    the current value of datastore_query_client_duration_ms



13
14
15
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 13

def datastore_query_client_duration_ms
  @datastore_query_client_duration_ms
end

#datastore_query_server_duration_msObject

Returns the value of attribute datastore_query_server_duration_ms

Returns:

  • (Object)

    the current value of datastore_query_server_duration_ms



13
14
15
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 13

def datastore_query_server_duration_ms
  @datastore_query_server_duration_ms
end

#extension_dataObject

Returns the value of attribute extension_data

Returns:

  • (Object)

    the current value of extension_data



13
14
15
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 13

def extension_data
  @extension_data
end

#mutexObject

Returns the value of attribute mutex

Returns:

  • (Object)

    the current value of mutex



13
14
15
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 13

def mutex
  @mutex
end

#queried_shard_countObject

Returns the value of attribute queried_shard_count

Returns:

  • (Object)

    the current value of queried_shard_count



13
14
15
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 13

def queried_shard_count
  @queried_shard_count
end

#query_counts_per_datastore_requestObject

Returns the value of attribute query_counts_per_datastore_request

Returns:

  • (Object)

    the current value of query_counts_per_datastore_request



13
14
15
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 13

def query_counts_per_datastore_request
  @query_counts_per_datastore_request
end

#search_index_expressionsObject

Returns the value of attribute search_index_expressions

Returns:

  • (Object)

    the current value of search_index_expressions



13
14
15
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 13

def search_index_expressions
  @search_index_expressions
end

#shard_routing_valuesObject

Returns the value of attribute shard_routing_values

Returns:

  • (Object)

    the current value of shard_routing_values



13
14
15
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 13

def shard_routing_values
  @shard_routing_values
end

Class Method Details

.emptyObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 23

def self.empty
  new(
    shard_routing_values: ::Set.new,
    search_index_expressions: ::Set.new,
    query_counts_per_datastore_request: [],
    datastore_query_server_duration_ms: 0,
    datastore_query_client_duration_ms: 0,
    queried_shard_count: 0,
    extension_data: {},
    mutex: ::Thread::Mutex.new
  )
end

Instance Method Details

#[]=(key, value) ⇒ Object

Allows extensions to set custom data that will be included in the query duration log.



59
60
61
62
63
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 59

def []=(key, value)
  mutex.synchronize do
    extension_data[key] = value
  end
end

#datastore_request_transport_duration_msObject

Indicates how long was spent on transport between the client and the datastore server, including network time, JSON serialization time, etc.



54
55
56
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 54

def datastore_request_transport_duration_ms
  datastore_query_client_duration_ms - datastore_query_server_duration_ms
end

#record_datastore_queries_for_single_request(queries) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 36

def record_datastore_queries_for_single_request(queries)
  mutex.synchronize do
    shard_routing_values.merge(queries.flat_map { |q| q.shard_routing_values || [] })
    search_index_expressions.merge(queries.map(&:search_index_expression))
    query_counts_per_datastore_request << queries.size
  end
end

#record_datastore_query_metrics(client_duration_ms:, server_duration_ms:, queried_shard_count:) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/elastic_graph/graphql/query_details_tracker.rb', line 44

def record_datastore_query_metrics(client_duration_ms:, server_duration_ms:, queried_shard_count:)
  mutex.synchronize do
    self.datastore_query_client_duration_ms += client_duration_ms
    self.datastore_query_server_duration_ms += server_duration_ms if server_duration_ms
    self.queried_shard_count += queried_shard_count
  end
end