Class: Logtide::Hub

Inherits:
Object
  • Object
show all
Defined in:
lib/logtide/hub.rb

Overview

Binds a Client to a stack of Scopes (spec 001 terminology, 004 section 4). Captures merge the current scope; with_scope/clone provide isolation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, scope = Scope.new) ⇒ Hub

Returns a new instance of Hub.



10
11
12
13
# File 'lib/logtide/hub.rb', line 10

def initialize(client, scope = Scope.new)
  @client = client
  @scope_stack = [scope]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/logtide/hub.rb', line 15

def client
  @client
end

Instance Method Details

#add_breadcrumb(breadcrumb) ⇒ Object



58
59
60
# File 'lib/logtide/hub.rb', line 58

def add_breadcrumb(breadcrumb)
  current_scope.add_breadcrumb(breadcrumb)
end

#capture_exception(exception, **opts) ⇒ Object



52
53
54
55
56
# File 'lib/logtide/hub.rb', line 52

def capture_exception(exception, **opts)
  return unless @client

  @client.capture_exception(exception, scope: current_scope, **opts)
end

#capture_log(level, message, metadata = nil, **opts) ⇒ Object



46
47
48
49
50
# File 'lib/logtide/hub.rb', line 46

def capture_log(level, message,  = nil, **opts)
  return unless @client

  @client.capture_log(level, message, , scope: current_scope, **opts)
end

#cloneObject



42
43
44
# File 'lib/logtide/hub.rb', line 42

def clone
  Hub.new(@client, current_scope.clone)
end

#close(timeout = nil) ⇒ Object



97
98
99
# File 'lib/logtide/hub.rb', line 97

def close(timeout = nil)
  @client&.close(*[timeout].compact)
end

#configure_scope {|current_scope| ... } ⇒ Object

Yields:



37
38
39
40
# File 'lib/logtide/hub.rb', line 37

def configure_scope
  yield current_scope
  self
end

#current_scopeObject



17
18
19
# File 'lib/logtide/hub.rb', line 17

def current_scope
  @scope_stack.last
end

#flush(timeout = nil) ⇒ Object



93
94
95
# File 'lib/logtide/hub.rb', line 93

def flush(timeout = nil)
  @client&.flush(*[timeout].compact)
end

#pop_scopeObject



26
27
28
# File 'lib/logtide/hub.rb', line 26

def pop_scope
  @scope_stack.pop if @scope_stack.size > 1
end

#push_scopeObject



21
22
23
24
# File 'lib/logtide/hub.rb', line 21

def push_scope
  @scope_stack.push(current_scope.clone)
  current_scope
end

#start_span(name, **opts) ⇒ Object

Start a span and make it the active span. With a block, the span is finished and the previous active span restored automatically.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/logtide/hub.rb', line 64

def start_span(name, **opts)
  return block_given? ? yield(nil) : nil unless @client

  span = @client.start_span(name, scope: current_scope, **opts)
  previous = current_scope.span
  current_scope.set_span(span)
  return span unless block_given?

  begin
    yield span
  ensure
    span.finish
    current_scope.set_span(previous)
  end
end

#trace_propagation_headersObject

Headers to inject into an outbound HTTP request so the trace continues across services (spec 005 section 2). Empty when there is no trace context. Never emits X-Trace-ID.



83
84
85
86
87
88
89
90
91
# File 'lib/logtide/hub.rb', line 83

def trace_propagation_headers
  scope = current_scope
  trace_id = scope.span&.trace_id || scope.trace_id
  span_id = scope.span&.span_id || scope.span_id
  return {} unless trace_id && span_id

  sampled = scope.span ? scope.span.sampled? : true
  { "traceparent" => Tracing::Propagation.format_traceparent(trace_id: trace_id, span_id: span_id, sampled: sampled) }
end

#with_scopeObject



30
31
32
33
34
35
# File 'lib/logtide/hub.rb', line 30

def with_scope
  push_scope
  yield current_scope
ensure
  pop_scope
end