Class: GraphQL::Tracing::DataDogTrace::DatadogMonitor

Inherits:
MonitorTrace::Monitor show all
Includes:
MonitorTrace::Monitor::GraphQLSuffixNames
Defined in:
lib/graphql/tracing/data_dog_trace.rb

Defined Under Namespace

Classes: Event

Constant Summary

Constants included from MonitorTrace::Monitor::GraphQLSuffixNames

MonitorTrace::Monitor::GraphQLSuffixNames::ANALYZE_NAME, MonitorTrace::Monitor::GraphQLSuffixNames::EXECUTE_NAME, MonitorTrace::Monitor::GraphQLSuffixNames::LEX_NAME, MonitorTrace::Monitor::GraphQLSuffixNames::PARSE_NAME, MonitorTrace::Monitor::GraphQLSuffixNames::VALIDATE_NAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MonitorTrace::Monitor

#fallback_transaction_name, #name_for, #start_event, #transaction_name

Constructor Details

#initialize(set_transaction_name:, service: nil, tracer: nil, **_rest) ⇒ DatadogMonitor

Returns a new instance of DatadogMonitor.



16
17
18
19
20
21
22
23
24
# File 'lib/graphql/tracing/data_dog_trace.rb', line 16

def initialize(set_transaction_name:, service: nil, tracer: nil, **_rest)
  super
  if tracer.nil?
    tracer = defined?(Datadog::Tracing) ? Datadog::Tracing : Datadog.tracer
  end
  @tracer = tracer
  @service_name = service
  @has_prepare_span = @trace.respond_to?(:prepare_span)
end

Instance Attribute Details

#service_nameObject (readonly)

Returns the value of attribute service_name.



26
27
28
# File 'lib/graphql/tracing/data_dog_trace.rb', line 26

def service_name
  @service_name
end

#tracerObject (readonly)

Returns the value of attribute tracer.



26
27
28
# File 'lib/graphql/tracing/data_dog_trace.rb', line 26

def tracer
  @tracer
end

Instance Method Details

#instrument(keyword, object) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/graphql/tracing/data_dog_trace.rb', line 28

def instrument(keyword, object)
  trace_key = name_for(keyword, object)
  @tracer.trace(trace_key, service: @service_name, type: 'custom') do |span|
    span.set_tag('component', 'graphql')
    op_name = keyword.respond_to?(:name) ? keyword.name : keyword.to_s
    span.set_tag('operation', op_name)

    if keyword == :execute
      operations = object.queries.map(&:selected_operation_name).join(', ')
      first_query = object.queries.first
      resource = if operations.empty?
        fallback_transaction_name(first_query && first_query.context)
      else
        operations
      end
      span.resource = resource if resource

      span.set_tag("selected_operation_name", first_query.selected_operation_name)
      span.set_tag("selected_operation_type", first_query.selected_operation&.operation_type)
      span.set_tag("query_string", first_query.query_string)
    end

    if @has_prepare_span
      @trace.prepare_span(keyword, object, span)
    end
    yield
  end
end