Class: Datadog::OpenTracer::Span

Inherits:
OpenTracing::Span
  • Object
show all
Defined in:
lib/datadog/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.



13
14
15
16
# File 'lib/datadog/opentracer/span.rb', line 13

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.



10
11
12
# File 'lib/datadog/opentracer/span.rb', line 10

def datadog_span
  @datadog_span
end

Instance Method Details

#contextSpanContext

Span Context

Returns:



28
29
30
# File 'lib/datadog/opentracer/span.rb', line 28

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



94
95
96
# File 'lib/datadog/opentracer/span.rb', line 94

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



65
66
67
# File 'lib/datadog/opentracer/span.rb', line 65

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



77
78
79
80
81
82
# File 'lib/datadog/opentracer/span.rb', line 77

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

  # If the fields specify an error
  datadog_span.set_error(fields[:'error.object']) if fields.key?(:'error.object')
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



87
88
89
90
# File 'lib/datadog/opentracer/span.rb', line 87

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

#operation_name=(name) ⇒ Object

Set the name of the operation

Parameters:

  • name (String)


21
22
23
# File 'lib/datadog/opentracer/span.rb', line 21

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



51
52
53
54
55
56
57
58
59
60
# File 'lib/datadog/opentracer/span.rb', line 51

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



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/datadog/opentracer/span.rb', line 36

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::Tracing::Metadata::Ext::Errors::STATUS : 0
  end

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