Class: AllStak::Modules::Span

Inherits:
Object
  • Object
show all
Defined in:
lib/allstak/modules/tracing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trace_id:, span_id:, parent_span_id:, operation:, description:, service:, environment:, tags:, start_time_millis:, on_finish:, release: "", sampled: true) ⇒ Span

Returns a new instance of Span.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/allstak/modules/tracing.rb', line 135

def initialize(trace_id:, span_id:, parent_span_id:, operation:, description:,
               service:, environment:, tags:, start_time_millis:, on_finish:,
               release: "", sampled: true)
  @trace_id = trace_id
  @span_id = span_id
  @parent_span_id = parent_span_id
  @operation = operation
  @description = description
  @service = service
  @environment = environment
  @release = release
  @tags = tags.dup
  @start_time_millis = start_time_millis
  @end_time_millis = nil
  @status = "ok"
  @finished = false
  @sampled = sampled
  @on_finish = on_finish
end

Instance Attribute Details

#span_idObject (readonly)

Returns the value of attribute span_id.



133
134
135
# File 'lib/allstak/modules/tracing.rb', line 133

def span_id
  @span_id
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



133
134
135
# File 'lib/allstak/modules/tracing.rb', line 133

def trace_id
  @trace_id
end

Instance Method Details

#finish(status = "ok") ⇒ Object



173
174
175
176
177
178
179
# File 'lib/allstak/modules/tracing.rb', line 173

def finish(status = "ok")
  return if @finished
  @finished = true
  @status = Tracing::VALID_STATUSES.include?(status) ? status : "ok"
  @end_time_millis = (Time.now.to_f * 1000).to_i
  @on_finish.call(self)
end

#finished?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/allstak/modules/tracing.rb', line 169

def finished?
  @finished
end

#sampled?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/allstak/modules/tracing.rb', line 155

def sampled?
  @sampled
end

#set_description(description) ⇒ Object



164
165
166
167
# File 'lib/allstak/modules/tracing.rb', line 164

def set_description(description)
  @description = description
  self
end

#set_tag(key, value) ⇒ Object



159
160
161
162
# File 'lib/allstak/modules/tracing.rb', line 159

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

#to_hObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/allstak/modules/tracing.rb', line 181

def to_h
  end_ms = @end_time_millis || (Time.now.to_f * 1000).to_i
  {
    traceId: @trace_id,
    spanId: @span_id,
    parentSpanId: @parent_span_id,
    operation: @operation,
    description: @description,
    status: @status,
    durationMs: end_ms - @start_time_millis,
    startTimeMillis: @start_time_millis,
    endTimeMillis: end_ms,
    service: @service,
    environment: @environment,
    release: @release,
    tags: @tags
  }
end