Module: Datadog::OpenTelemetry::Trace::Span

Defined in:
lib/datadog/opentelemetry/api/trace/span.rb,
lib/datadog/opentelemetry/sdk/trace/span.rb

Overview

Stores associated Datadog entities to the OpenTelemetry Span.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#datadog_spanObject

Returns the value of attribute datadog_span.



8
9
10
# File 'lib/datadog/opentelemetry/api/trace/span.rb', line 8

def datadog_span
  @datadog_span
end

#datadog_traceObject

Returns the value of attribute datadog_trace.



8
9
10
# File 'lib/datadog/opentelemetry/api/trace/span.rb', line 8

def datadog_trace
  @datadog_trace
end

Class Method Details

.enrich_name(kind, attrs) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/datadog/opentelemetry/sdk/trace/span.rb', line 69

def self.enrich_name(kind, attrs)
  if attrs.key?('http.request.method')
    return 'http.server.request' if kind == :server
    return 'http.client.request' if kind == :client
  end

  return "#{attrs['db.system']}.query" if attrs.key?('db.system') && kind == :client

  if (attrs.key?('messaging.system') || attrs.key?('messaging.operation')) &&
      [:consumer, :producer, :server, :client].include?(kind)

    return "#{attrs['messaging.system']}.#{attrs['messaging.operation']}"
  end

  if attrs.key?('rpc.system')
    if attrs['rpc.system'] == 'aws-api' && kind == :client
      service = attrs['rpc.service']
      return "aws.#{service || 'client'}.request"
    end

    if kind == :client
      return "#{attrs['rpc.system']}.client.request"
    elsif kind == :server
      return "#{attrs['rpc.system']}.server.request"
    end
  end

  if attrs.key?('faas.invoked_provider') && attrs.key?('faas.invoked_name') && kind == :client
    provider = attrs['faas.invoked_provider']
    name = attrs['faas.invoked_name']
    return "#{provider}.#{name}.invoke"
  end

  return "#{attrs['faas.trigger']}.invoke" if attrs.key?('faas.trigger') && kind == :server

  return 'graphql.server.request' if attrs.key?('graphql.operation.type')

  if kind == :server
    protocol = attrs['network.protocol.name']
    return protocol ? "#{protocol}.server.request" : 'server.request'
  end

  if kind == :client
    protocol = attrs['network.protocol.name']
    return protocol ? "#{protocol}.client.request" : 'client.request'
  end

  kind.to_s
end

Instance Method Details

#add_attributes(attributes) ⇒ Object

Attributes are equivalent to span tags and metrics.



45
46
47
48
49
50
# File 'lib/datadog/opentelemetry/sdk/trace/span.rb', line 45

def add_attributes(attributes)
  res = super
  # Attributes can get dropped or their values truncated by `super`
  attributes.each { |key, _| datadog_set_attribute(key) }
  res
end

#record_exception(exception, attributes: nil) ⇒ void

This method returns an undefined value.

Record an exception during the execution of this span. Multiple exceptions can be recorded on a span.

Parameters:

  • exception (Exception)

    The exception to recorded

  • attributes (optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}) (defaults to: nil)

    One or more key:value pairs, where the keys must be strings and the values may be (array of) string, boolean or numeric type.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/datadog/opentelemetry/sdk/trace/span.rb', line 28

def record_exception(exception, attributes: nil)
  res = super
  if (span = datadog_span)
    # Sets the exception attributes as span error tags. The values in the attribute hash MUST
    # take precedence over the type, message and stacktrace inferred from the exception object
    type = attributes&.[]('exception.type') || exception.class.to_s
    message = attributes&.[]('exception.message') || exception.message
    stacktrace = attributes&.[]('exception.stacktrace') || exception.full_message(highlight: false, order: :top)
    span.set_error_tags([type, message, stacktrace])
  end
  res
end

#set_attribute(key, value) ⇒ Object Also known as: []=

Attributes are equivalent to span tags and metrics.



11
12
13
14
15
16
# File 'lib/datadog/opentelemetry/sdk/trace/span.rb', line 11

def set_attribute(key, value)
  res = super
  # Attributes can get dropped or their values truncated by `super`
  datadog_set_attribute(key)
  res
end

#status=(s) ⇒ Object

Captures changes to span error state.



53
54
55
56
57
58
59
60
61
62
# File 'lib/datadog/opentelemetry/sdk/trace/span.rb', line 53

def status=(s)
  super

  return unless status # Return if status are currently disabled by OpenTelemetry.
  return unless (span = datadog_span)

  # Status code can only change into an error state.
  # Other change operations should be ignored.
  span.set_error(status.description) if status && status.code == ::OpenTelemetry::Trace::Status::ERROR
end