Class: Trace::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/zipkin-tracer/trace.rb

Overview

A span may contain many annotations

Defined Under Namespace

Modules: Kind, Tag

Constant Summary collapse

STATUS_ERROR_REGEXP =
/\A(4.*|5.*)\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, span_id, timestamp = Time.now) ⇒ Span

Returns a new instance of Span.



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/zipkin-tracer/trace.rb', line 200

def initialize(name, span_id, timestamp = Time.now)
  @name = name
  @span_id = span_id
  @kind = nil
  @local_endpoint = nil
  @remote_endpoint = nil
  @annotations = []
  @tags = {}
  @debug = span_id.debug?
  @timestamp = to_microseconds(timestamp)
  @duration = UNKNOWN_DURATION
end

Instance Attribute Details

#annotationsObject

Returns the value of attribute annotations.



198
199
200
# File 'lib/zipkin-tracer/trace.rb', line 198

def annotations
  @annotations
end

#debugObject

Returns the value of attribute debug.



198
199
200
# File 'lib/zipkin-tracer/trace.rb', line 198

def debug
  @debug
end

#kindObject

Returns the value of attribute kind.



198
199
200
# File 'lib/zipkin-tracer/trace.rb', line 198

def kind
  @kind
end

#local_endpointObject

Returns the value of attribute local_endpoint.



198
199
200
# File 'lib/zipkin-tracer/trace.rb', line 198

def local_endpoint
  @local_endpoint
end

#nameObject

Returns the value of attribute name.



198
199
200
# File 'lib/zipkin-tracer/trace.rb', line 198

def name
  @name
end

#remote_endpointObject

Returns the value of attribute remote_endpoint.



198
199
200
# File 'lib/zipkin-tracer/trace.rb', line 198

def remote_endpoint
  @remote_endpoint
end

#tagsObject

Returns the value of attribute tags.



198
199
200
# File 'lib/zipkin-tracer/trace.rb', line 198

def tags
  @tags
end

Instance Method Details

#close(timestamp = Time.now) ⇒ Object



213
214
215
# File 'lib/zipkin-tracer/trace.rb', line 213

def close(timestamp = Time.now)
  @duration = to_microseconds(timestamp) - @timestamp
end

#has_parent_span?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/zipkin-tracer/trace.rb', line 249

def has_parent_span?
  !@span_id.parent_id.nil?
end

#record(value) ⇒ Object

We record information into spans, then we send these spans to zipkin



237
238
239
# File 'lib/zipkin-tracer/trace.rb', line 237

def record(value)
  annotations << Trace::Annotation.new(value.to_s)
end

#record_local_component(value) ⇒ Object



245
246
247
# File 'lib/zipkin-tracer/trace.rb', line 245

def record_local_component(value)
  record_tag(Tag::LOCAL_COMPONENT, value)
end

#record_status(status) ⇒ Object



255
256
257
258
259
260
# File 'lib/zipkin-tracer/trace.rb', line 255

def record_status(status)
  return if status.nil?
  status = status.to_s
  record_tag(Tag::STATUS, status)
  record_tag(Tag::ERROR, status) if STATUS_ERROR_REGEXP.match(status)
end

#record_tag(key, value) ⇒ Object



241
242
243
# File 'lib/zipkin-tracer/trace.rb', line 241

def record_tag(key, value)
  @tags[key] = value.to_s
end

#to_hObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/zipkin-tracer/trace.rb', line 217

def to_h
  h = {
    name: @name,
    traceId: @span_id.trace_id.to_s,
    id: @span_id.span_id.to_s,
    localEndpoint: @local_endpoint.to_h,
    timestamp: @timestamp,
    duration: @duration,
    debug: @debug
  }
  h[:parentId] = @span_id.parent_id.to_s unless @span_id.parent_id.nil?
  h[:kind] = @kind unless @kind.nil?
  h[:remoteEndpoint] = @remote_endpoint.to_h unless @remote_endpoint.nil?
  h[:annotations] = @annotations.map(&:to_h) unless @annotations.empty?
  h[:tags] = @tags unless @tags.empty?
  h[:shared] = true if @span_id.shared && @kind == Kind::SERVER
  h
end