Class: Logtide::Client

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

Overview

Owns configuration and the transport, and runs the capture pipeline: merge scope + global metadata, stamp the sdk, run event processors and before_send, sample, then enqueue. Public capture methods are fire-and-forget and never raise for transport problems (spec 001).

Constant Summary collapse

LEVELS =
%w[debug info warn error critical].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, batcher: nil, span_batcher: nil, sampler: -> { rand }) ⇒ Client

Returns a new instance of Client.



26
27
28
29
30
31
32
# File 'lib/logtide/client.rb', line 26

def initialize(configuration, batcher: nil, span_batcher: nil, sampler: -> { rand })
  @configuration = configuration
  @metrics = Metrics.new
  @sampler = sampler
  @batcher = batcher || build_batcher
  @span_batcher = span_batcher
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



24
25
26
# File 'lib/logtide/client.rb', line 24

def configuration
  @configuration
end

Instance Method Details

#capture_exception(exception, message: nil, level: "error", metadata: nil, scope: nil, hint: nil, trace_id: nil, span_id: nil, time: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/logtide/client.rb', line 42

def capture_exception(exception, message: nil, level: "error", metadata: nil,
                      scope: nil, hint: nil, trace_id: nil, span_id: nil, time: nil)
  structured = StructuredException.serialize(exception, include_stacktrace: @configuration.attach_stacktrace)
  hint = (hint || {}).merge(exception: exception)
  dispatch(
    level: normalize_level(level), message: message || exception.message.to_s,
    metadata: , scope: scope, hint: hint, exception: structured,
    trace_id: trace_id, span_id: span_id, time: time
  )
end

#capture_log(level, message, metadata = nil, scope: nil, hint: nil, trace_id: nil, span_id: nil, time: nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/logtide/client.rb', line 34

def capture_log(level, message,  = nil, scope: nil, hint: nil,
                trace_id: nil, span_id: nil, time: nil)
  dispatch(
    level: normalize_level(level), message: message, metadata: ,
    scope: scope, hint: hint || {}, trace_id: trace_id, span_id: span_id, time: time
  )
end

#close(timeout = @configuration.flush_timeout) ⇒ Object



73
74
75
76
# File 'lib/logtide/client.rb', line 73

def close(timeout = @configuration.flush_timeout)
  @batcher.close(timeout)
  @span_batcher&.close(timeout)
end

#flush(timeout = @configuration.flush_timeout) ⇒ Object



68
69
70
71
# File 'lib/logtide/client.rb', line 68

def flush(timeout = @configuration.flush_timeout)
  @batcher.flush(timeout)
  @span_batcher&.flush(timeout)
end

#metricsObject



78
79
80
# File 'lib/logtide/client.rb', line 78

def metrics
  @metrics.snapshot
end

#reset_metricsObject



82
83
84
# File 'lib/logtide/client.rb', line 82

def reset_metrics
  @metrics.reset
end

#start_span(name, scope:, kind: :internal) ⇒ Object

Start a span. The returned span becomes the active span on the given scope (the caller is responsible for finishing it). Sampling is decided once per trace at the root span (spec 005 section 5).



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/logtide/client.rb', line 56

def start_span(name, scope:, kind: :internal)
  parent = scope&.span
  trace_id = parent&.trace_id || scope&.trace_id || Tracing.generate_trace_id
  sampled = parent ? parent.sampled? : sample_trace?

  Tracing::Span.new(
    name: name, trace_id: trace_id, span_id: Tracing.generate_span_id,
    parent_span_id: parent&.span_id || scope&.span_id, kind: kind,
    sampled: sampled, reporter: ->(span) { export_span(span) }
  )
end