Class: Logtide::Hub
- Inherits:
-
Object
- Object
- Logtide::Hub
- 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
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #add_breadcrumb(breadcrumb) ⇒ Object
- #capture_exception(exception, **opts) ⇒ Object
- #capture_log(level, message, metadata = nil, **opts) ⇒ Object
- #clone ⇒ Object
- #close(timeout = nil) ⇒ Object
- #configure_scope {|current_scope| ... } ⇒ Object
- #current_scope ⇒ Object
- #flush(timeout = nil) ⇒ Object
-
#initialize(client, scope = Scope.new) ⇒ Hub
constructor
A new instance of Hub.
- #pop_scope ⇒ Object
- #push_scope ⇒ Object
-
#start_span(name, **opts) ⇒ Object
Start a span and make it the active span.
-
#trace_propagation_headers ⇒ Object
Headers to inject into an outbound HTTP request so the trace continues across services (spec 005 section 2).
- #with_scope ⇒ Object
Constructor Details
Instance Attribute Details
#client ⇒ Object (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 () current_scope.() 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, , = nil, **opts) return unless @client @client.capture_log(level, , , scope: current_scope, **opts) end |
#clone ⇒ Object
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
37 38 39 40 |
# File 'lib/logtide/hub.rb', line 37 def configure_scope yield current_scope self end |
#current_scope ⇒ Object
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_scope ⇒ Object
26 27 28 |
# File 'lib/logtide/hub.rb', line 26 def pop_scope @scope_stack.pop if @scope_stack.size > 1 end |
#push_scope ⇒ Object
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_headers ⇒ Object
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_scope ⇒ Object
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 |