Class: NewRelic::TelemetrySdk::Span

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/newrelic/telemetry_sdk/span.rb

Overview

This class represents a timed operation that is part of a distributed trace. This operation will be represented as a Span in the New Relic UI.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#clear_already_logged, #log_error, #log_once, logger, #logger, logger=, #logger=

Constructor Details

#initialize(id: Util.generate_guid(16), trace_id: Util.generate_guid(32), start_time: Util.current_time, duration_ms: nil, name: nil, parent_id: nil, service_name: nil, custom_attributes: nil) ⇒ Span

Returns a new instance of Span.

Parameters:

  • id (optional, String) (defaults to: Util.generate_guid(16))

    A random, unique identifier associated with this specific New Relic span.

  • trace_id (optional, String) (defaults to: Util.generate_guid(32))

    A random, unique identifier associated with a collection of spans that will be grouped together as a trace in the New Relic UI.

  • start_time (optional, Time) (defaults to: Util.current_time)

    A Time object corresponding to the start time of the operation represented by this span.

  • duration_ms (optional, Integer) (defaults to: nil)

    The duration of the operation represented by this span, in milliseconds.

  • name (optional, String) (defaults to: nil)

    The name of the span.

  • parent_id (optional, String) (defaults to: nil)

    A random, unique identifier associated with the parent of this span.

  • service_name (optional, String) (defaults to: nil)

    The name of the entity that created this span.

  • custom_attributes (optional, Hash) (defaults to: nil)

    Custom attributes that will appear on this span.

See Also:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/newrelic/telemetry_sdk/span.rb', line 45

def initialize id: Util.generate_guid(16),
               trace_id: Util.generate_guid(32),
               start_time: Util.current_time,
               duration_ms: nil,
               name: nil,
               parent_id: nil,
               service_name: nil,
               custom_attributes: nil

  @id = id
  @trace_id = trace_id
  @start_time = start_time
  @duration_ms = duration_ms
  @name = name
  @parent_id = parent_id
  @service_name = service_name
  @custom_attributes = custom_attributes
end

Instance Attribute Details

#custom_attributesObject



15
16
17
# File 'lib/newrelic/telemetry_sdk/span.rb', line 15

def custom_attributes
  @custom_attributes
end

#duration_msObject



15
16
17
# File 'lib/newrelic/telemetry_sdk/span.rb', line 15

def duration_ms
  @duration_ms
end

#idObject



15
16
17
# File 'lib/newrelic/telemetry_sdk/span.rb', line 15

def id
  @id
end

#nameObject



15
16
17
# File 'lib/newrelic/telemetry_sdk/span.rb', line 15

def name
  @name
end

#parent_idObject



15
16
17
# File 'lib/newrelic/telemetry_sdk/span.rb', line 15

def parent_id
  @parent_id
end

#service_nameObject



15
16
17
# File 'lib/newrelic/telemetry_sdk/span.rb', line 15

def service_name
  @service_name
end

#start_timeObject



15
16
17
# File 'lib/newrelic/telemetry_sdk/span.rb', line 15

def start_time
  @start_time
end

#trace_idObject



15
16
17
# File 'lib/newrelic/telemetry_sdk/span.rb', line 15

def trace_id
  @trace_id
end

Instance Method Details

#finish(end_time: Util.current_time) ⇒ Object

Mark the operation represented by this Span as finished and calculate is duration.

Parameters:

  • end_time (optional, Time) (defaults to: Util.current_time)

    A Time object corresponding to the end time of the operation represented by this span.



70
71
72
73
74
# File 'lib/newrelic/telemetry_sdk/span.rb', line 70

def finish end_time: Util.current_time
  @duration_ms = Util.time_to_ms(end_time - @start_time)
rescue => e
  log_error "Encountered error finishing span", e
end

#to_hObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/newrelic/telemetry_sdk/span.rb', line 76

def to_h
  data = {
    :id => @id,
    :'trace.id' => @trace_id,
    :timestamp => Util.time_to_ms(@start_time),
    :attributes => {
      :'duration.ms' => @duration_ms,
      :name => @name,
      :'parent.id' => @parent_id,
      :'service.name' => @service_name
    }
  }

  data[:attributes].merge! @custom_attributes if @custom_attributes

  data
rescue => e
  log_error "Encountered error converting span to hash", e
end