Class: Logtide::Tracing::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/logtide/tracing/span.rb

Overview

A span in the SDK tracing API, exported as OTLP/JSON (spec 005 section 3).

Constant Summary collapse

KINDS =
{ internal: 1, server: 2, client: 3, producer: 4, consumer: 5 }.freeze
STATUS =
{ unset: 0, ok: 1, error: 2 }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, trace_id:, span_id:, parent_span_id: nil, kind: :internal, sampled: true, start_time: nil, reporter: nil) ⇒ Span

Returns a new instance of Span.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/logtide/tracing/span.rb', line 12

def initialize(name:, trace_id:, span_id:, parent_span_id: nil,
               kind: :internal, sampled: true, start_time: nil, reporter: nil)
  @name = name
  @trace_id = trace_id
  @span_id = span_id
  @parent_span_id = parent_span_id
  @kind = kind
  @sampled = sampled
  @start_time = start_time || now_nano
  @reporter = reporter
  @attributes = {}
  @events = []
  @status = :unset
  @end_time = nil
  @finished = false
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



10
11
12
# File 'lib/logtide/tracing/span.rb', line 10

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/logtide/tracing/span.rb', line 10

def name
  @name
end

#parent_span_idObject (readonly)

Returns the value of attribute parent_span_id.



10
11
12
# File 'lib/logtide/tracing/span.rb', line 10

def parent_span_id
  @parent_span_id
end

#span_idObject (readonly)

Returns the value of attribute span_id.



10
11
12
# File 'lib/logtide/tracing/span.rb', line 10

def span_id
  @span_id
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



10
11
12
# File 'lib/logtide/tracing/span.rb', line 10

def trace_id
  @trace_id
end

Instance Method Details

#add_event(name) ⇒ Object



37
38
39
40
# File 'lib/logtide/tracing/span.rb', line 37

def add_event(name)
  @events << { name: name, time: now_nano }
  self
end

#finish(status = :ok) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/logtide/tracing/span.rb', line 42

def finish(status = :ok)
  return self if @finished

  @finished = true
  @end_time = now_nano
  @status = status
  # Unsampled spans are no-ops: cheap, never exported (spec 005 section 5).
  @reporter.call(self) if @sampled && @reporter
  self
end

#recording?Boolean

Returns:

  • (Boolean)


30
# File 'lib/logtide/tracing/span.rb', line 30

def recording? = @sampled

#sampled?Boolean

Returns:

  • (Boolean)


29
# File 'lib/logtide/tracing/span.rb', line 29

def sampled? = @sampled

#set_attribute(key, value) ⇒ Object



32
33
34
35
# File 'lib/logtide/tracing/span.rb', line 32

def set_attribute(key, value)
  @attributes[key.to_s] = value
  self
end

#to_otlpObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/logtide/tracing/span.rb', line 53

def to_otlp
  otlp = {
    "traceId" => @trace_id,
    "spanId" => @span_id,
    "name" => @name,
    "kind" => KINDS.fetch(@kind, 1),
    "startTimeUnixNano" => @start_time.to_s,
    "endTimeUnixNano" => (@end_time || @start_time).to_s,
    "attributes" => @attributes.map { |key, value| otlp_attribute(key, value) },
    "events" => @events.map { |event| { "name" => event[:name], "timeUnixNano" => event[:time].to_s } },
    "status" => { "code" => STATUS.fetch(@status, 0) }
  }
  otlp["parentSpanId"] = @parent_span_id if @parent_span_id
  otlp
end