Module: Traces::Backend::Test::Interface

Defined in:
lib/traces/backend/test.rb

Overview

The test backend interface.

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/traces/backend/test.rb', line 87

def active?
  !!Fiber.current.traces_backend_context
end

#trace(name, attributes: nil, &block) ⇒ Object

Trace the given block of code and validate the interface usage.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/traces/backend/test.rb', line 49

def trace(name, attributes: nil, &block)
  unless block_given?
    raise ArgumentError, "No block given!"
  end
  
  unless name.is_a?(String)
    raise ArgumentError, "Invalid name (must be String): #{name.inspect}!"
  end
  
  context = Context.nested(Fiber.current.traces_backend_context)
  
  span = Span.new(context)
  
  # Ensure the attributes are valid and follow the requirements:
  attributes&.each do |key, value|
    span[key] = value
  end
  
  Fiber.current.traces_backend_context = context
  
  if block.arity.zero?
    yield
  else
    yield span
  end
end

#trace_contextObject

Get a trace context from the current execution scope.



82
83
84
# File 'lib/traces/backend/test.rb', line 82

def trace_context
  Fiber.current.traces_backend_context
end

#trace_context=(context) ⇒ Object

Assign a trace context to the current execution scope.



77
78
79
# File 'lib/traces/backend/test.rb', line 77

def trace_context= context
  Fiber.current.traces_backend_context = context
end