Module: LogBrew::QueueCarrier

Defined in:
lib/logbrew/queue_carrier.rb

Overview

Bounded W3C carrier shared by explicit and framework-owned queue adapters.

Constant Summary collapse

KEY =
"logbrew".freeze
VERSION =
1
MAX_ENQUEUED_AT_MS =
9_007_199_254_740_991
MAX_QUEUE_WAIT_MS =
604_800_000

Class Method Summary collapse

Class Method Details

.child_context(carrier) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/logbrew/queue_carrier.rb', line 34

def child_context(carrier)
  parsed = carrier && Traceparent.parse(carrier.fetch("traceparent"))
  return Trace.create_root if parsed.nil?

  Trace.create(
    trace_id: parsed.trace_id,
    span_id: Trace.generate_span_id,
    parent_span_id: parsed.parent_span_id,
    trace_flags: parsed.trace_flags
  )
end

.create(context, enqueued_at_ms: wall_time_ms) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/logbrew/queue_carrier.rb', line 13

def create(context, enqueued_at_ms: wall_time_ms)
  {
    "version" => VERSION,
    "traceparent" => Trace.create_headers(context).fetch("traceparent"),
    "enqueuedAtMs" => enqueued_at_ms
  }
end

.queue_wait_ms(carrier) ⇒ Object



46
47
48
49
50
# File 'lib/logbrew/queue_carrier.rb', line 46

def queue_wait_ms(carrier)
  return if carrier.nil?

  [[wall_time_ms - carrier.fetch("enqueuedAtMs"), 0].max, MAX_QUEUE_WAIT_MS].min
end

.read(value) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logbrew/queue_carrier.rb', line 21

def read(value)
  keys = %w[enqueuedAtMs traceparent version]
  return unless value.is_a?(Hash) && value.size == keys.length && keys.all? { |key| value.key?(key) }
  return unless value["version"] == VERSION
  return unless value["traceparent"].is_a?(String) && value["traceparent"].bytesize <= 55
  return unless value["enqueuedAtMs"].is_a?(Integer) && value["enqueuedAtMs"].between?(0, MAX_ENQUEUED_AT_MS)

  Traceparent.parse(value["traceparent"])
  value
rescue SdkError
  nil
end

.wall_time_msObject



52
53
54
# File 'lib/logbrew/queue_carrier.rb', line 52

def wall_time_ms
  (Time.now.to_f * 1000.0).floor
end