Class: OpenTrace::InstrumentationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/opentrace/instrumentation_context.rb

Constant Summary collapse

FIBER_KEY =
:opentrace_buffer

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.capture_rulesObject

Optional capture rules instance. Set via configuration.



101
102
103
# File 'lib/opentrace/instrumentation_context.rb', line 101

def capture_rules
  @capture_rules
end

Class Method Details

.active?Boolean

Returns true if there is a buffer on the current Fiber.

Returns:

  • (Boolean)


79
80
81
# File 'lib/opentrace/instrumentation_context.rb', line 79

def active?
  !Fiber[FIBER_KEY].nil?
end

.buffer_poolObject

── Singleton resources (lazy-initialized) ──



85
86
87
# File 'lib/opentrace/instrumentation_context.rb', line 85

def buffer_pool
  @buffer_pool ||= BufferPool.new
end

.current_bufferObject

Returns the current Fiber’s RequestBuffer, or nil.



74
75
76
# File 'lib/opentrace/instrumentation_context.rb', line 74

def current_buffer
  Fiber[FIBER_KEY]
end

.memory_guardObject



89
90
91
# File 'lib/opentrace/instrumentation_context.rb', line 89

def memory_guard
  @memory_guard ||= MemoryGuard.new
end

.reset!Object

Reset singletons (for testing).



94
95
96
97
98
# File 'lib/opentrace/instrumentation_context.rb', line 94

def reset!
  @buffer_pool = nil
  @memory_guard = nil
  @capture_rules = nil
end

.setup(env: nil, job: nil) ⇒ Object

── Setup ──

Initializes a capture context for the current Fiber. Called at the start of a request (with env:) or job (with job:).

Checks out a RequestBuffer from the global BufferPool, sets it on the current Fiber, and marks the event_type.

Returns the buffer (for callers that need it).



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

def setup(env: nil, job: nil)
  buf = buffer_pool.checkout

  buf.event_type = if env
                     :http_request
                   elsif job
                     :job_perform
                   end

  Fiber[FIBER_KEY] = buf
  buf
end

.teardown(status: nil, duration_ms: nil, error: false) ⇒ Object

── Teardown ──

Finalizes the capture context. Resolves capture level (via CaptureRules if configured), applies MemoryGuard effective level, produces the document, checks the buffer back into the pool, and clears the Fiber local.

Returns the document Hash. The caller is responsible for enqueueing.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/opentrace/instrumentation_context.rb', line 42

def teardown(status: nil, duration_ms: nil, error: false)
  buf = Fiber[FIBER_KEY]
  return nil unless buf

  # Resolve capture level
  capture_level = resolve_capture_level(
    buf, status: status, duration_ms: duration_ms, error: error
  )

  # Apply memory guard — may downgrade under pressure
  capture_level = memory_guard.effective_level(capture_level)

  # Normalize: MemoryGuard returns :none when exceeded, but RequestBuffer
  # only understands :minimal / :standard / :full. Map :none to :minimal.
  capture_level = :minimal if capture_level == :none

  # Build domain overrides from capture rules (if configured)
  domain_overrides = {}

  # Produce the document
  doc = buf.to_document(capture_level: capture_level, domain_overrides: domain_overrides)

  # Return buffer to pool and clear Fiber local
  buffer_pool.checkin(buf)
  Fiber[FIBER_KEY] = nil

  doc
end