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



60
61
62
63
64
65
66
67
68
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
# File 'lib/datadog/opentelemetry/sdk/trace/span.rb', line 60

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

.serialize_attribute(key, value) ⇒ Object

Serialize values into Datadog span tags and metrics. Notably, arrays are exploded into many keys, each with a numeric suffix representing the array index, for example: ‘’foo’ => [‘a’,‘b’]‘ becomes `’foo.0’ => ‘a’, ‘foo.1’ => ‘b’‘



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

def self.serialize_attribute(key, value)
  if value.is_a?(Array)
    value.flat_map.with_index do |v, idx|
      serialize_attribute("#{key}.#{idx}", v)
    end
  elsif value.is_a?(TrueClass) || value.is_a?(FalseClass)
    [[key, value.to_s]]
  else
    [[key, value]]
  end
end

Instance Method Details

#add_attributes(attributes) ⇒ Object

Attributes are equivalent to span tags and metrics.



20
21
22
23
24
25
# File 'lib/datadog/opentelemetry/sdk/trace/span.rb', line 20

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

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

Attributes are equivalent to span tags and metrics.



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

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.



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

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