Class: Datadog::OpenTracer::Span

Inherits:
OpenTracing::Span
  • Object
show all
Defined in:
lib/ddtrace/opentracer/span.rb

Overview

OpenTracing adapter for Datadog::Span

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datadog_span:, span_context:) ⇒ Span

Returns a new instance of Span.



8
9
10
11
# File 'lib/ddtrace/opentracer/span.rb', line 8

def initialize(datadog_span:, span_context:)
  @datadog_span = datadog_span
  @span_context = span_context
end

Instance Attribute Details

#datadog_spanObject (readonly)

Returns the value of attribute datadog_span.



5
6
7
# File 'lib/ddtrace/opentracer/span.rb', line 5

def datadog_span
  @datadog_span
end

Instance Method Details

#contextSpanContext

Span Context

Returns:



23
24
25
# File 'lib/ddtrace/opentracer/span.rb', line 23

def context
  @span_context
end

#finish(end_time: Time.now) ⇒ Object

Parameters:

  • end_time (Time) (defaults to: Time.now)

    custom end time, if not now



93
94
95
# File 'lib/ddtrace/opentracer/span.rb', line 93

def finish(end_time: Time.now)
  datadog_span.finish(end_time)
end

#get_baggage_item(key) ⇒ String

Get a baggage item

Parameters:

  • key (String)

    the key of the baggage item

Returns:

  • (String)

    value of the baggage item



60
61
62
# File 'lib/ddtrace/opentracer/span.rb', line 60

def get_baggage_item(key)
  context.baggage[key]
end

#log(event: nil, timestamp: Time.now, **fields) ⇒ Object

Deprecated.

Use #log_kv instead.

Reason: event is an optional standard log field defined in spec and not required. Also, method name #log_kv is more consistent with other language implementations such as Python and Go.

Add a log entry to this span

Parameters:

  • event (String) (defaults to: nil)

    event name for the log

  • timestamp (Time) (defaults to: Time.now)

    time of the log

  • fields (Hash)

    Additional information to log



72
73
74
75
76
77
78
79
# File 'lib/ddtrace/opentracer/span.rb', line 72

def log(event: nil, timestamp: Time.now, **fields)
  super # Log deprecation warning

  # If the fields specify an error
  if fields.key?(:'error.object')
    datadog_span.set_error(fields[:'error.object'])
  end
end

#log_kv(timestamp: Time.now, **fields) ⇒ Object

Add a log entry to this span

Parameters:

  • timestamp (Time) (defaults to: Time.now)

    time of the log

  • fields (Hash)

    Additional information to log



84
85
86
87
88
89
# File 'lib/ddtrace/opentracer/span.rb', line 84

def log_kv(timestamp: Time.now, **fields)
  # If the fields specify an error
  if fields.key?(:'error.object')
    datadog_span.set_error(fields[:'error.object'])
  end
end

#operation_name=(name) ⇒ Object

Set the name of the operation

Parameters:

  • name (String)


16
17
18
# File 'lib/ddtrace/opentracer/span.rb', line 16

def operation_name=(name)
  datadog_span.name = name
end

#set_baggage_item(key, value) ⇒ Object

Set a baggage item on the span

Parameters:

  • key (String)

    the key of the baggage item

  • value (String)

    the value of the baggage item



46
47
48
49
50
51
52
53
54
55
# File 'lib/ddtrace/opentracer/span.rb', line 46

def set_baggage_item(key, value)
  tap do
    # SpanContext is immutable, so to make changes
    # build a new span context.
    @span_context = SpanContextFactory.clone(
      span_context: context,
      baggage: { key => value }
    )
  end
end

#set_tag(key, value) ⇒ Object

Set a tag value on this span a String, Numeric, or Boolean it will be encoded with to_s

Parameters:

  • key (String)

    the key of the tag

  • value (String, Numeric, Boolean)

    the value of the tag. If it’s not



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ddtrace/opentracer/span.rb', line 31

def set_tag(key, value)
  # Special cases to convert opentracing tags to datadog tags
  case key
  when 'error'
    # Opentracing supports and `error: <bool>` tag, we need to convert to span status
    # DEV: Do not return, we want to still set the `error` tag as they requested
    datadog_span.status = value ? Datadog::Ext::Errors::STATUS : 0
  end

  tap { datadog_span.set_tag(key, value) }
end