Module: Arcp::Trace

Defined in:
lib/arcp/trace.rb

Defined Under Namespace

Classes: Context

Constant Summary collapse

KEY =
:arcp_trace_context

Class Method Summary collapse

Class Method Details

.currentObject



13
14
15
# File 'lib/arcp/trace.rb', line 13

def current
  Fiber[KEY] || Context.new(trace_id: nil, span_id: nil, attributes: {}.freeze)
end

.current=(ctx) ⇒ Object



17
18
19
# File 'lib/arcp/trace.rb', line 17

def current=(ctx)
  Fiber[KEY] = ctx
end

.in_span(name, attributes: {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/arcp/trace.rb', line 36

def in_span(name, attributes: {}, &)
  tracer = begin
    require 'opentelemetry'
    OpenTelemetry.tracer_provider.tracer('arcp')
  rescue LoadError
    nil
  end

  if tracer
    tracer.in_span(name, attributes: attributes, &)
  else
    with(attributes: attributes, &)
  end
end

.new_trace_idString

Returns new 32-hex trace id.

Returns:

  • (String)

    new 32-hex trace id.



34
# File 'lib/arcp/trace.rb', line 34

def new_trace_id = Arcp::Ids.trace_id

.with(trace_id: nil, span_id: nil, attributes: {}) ⇒ Object



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

def with(trace_id: nil, span_id: nil, attributes: {})
  prev = current
  Fiber[KEY] = Context.new(
    trace_id: trace_id || prev.trace_id || Arcp::Ids.trace_id,
    span_id: span_id || Arcp::Ids.span_id,
    attributes: prev.attributes.merge(attributes).freeze
  )
  yield current
ensure
  Fiber[KEY] = prev
end