Class: Logister::Reporter

Inherits:
Object
  • Object
show all
Defined in:
lib/logister/reporter.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Reporter

Returns a new instance of Reporter.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logister/reporter.rb', line 12

def initialize(configuration)
  @configuration = configuration
  @client        = Client.new(configuration)

  # Pre-build values that are static for the lifetime of this reporter so
  # they are not allocated on every report_error / report_metric call.
  @static_context = {
    environment: @configuration.environment,
    service:     @configuration.service,
    release:     @configuration.release
  }.freeze

  # Normalise ignore_environments once into a frozen Set of Strings so
  # ignored_environment? never allocates a mapped Array.
  @ignored_envs = Set.new(@configuration.ignore_environments.map(&:to_s)).freeze

  # Cache the current-environment String to avoid repeated .to_s calls.
  @current_env = @configuration.environment.to_s.freeze

  # Compile the app-root stripping Regexp once; Dir.pwd is a syscall that
  # always returns a new String — do it exactly once here.
  app_root = Dir.pwd.to_s.freeze
  @app_root_re = /\A#{Regexp.escape(app_root)}\//.freeze

  # Register shutdown hook. Guard with a flag so multiple Reporter instances
  # (created by repeated Logister.configure calls) each shut down cleanly
  # without re-registering more handlers.
  @shutdown_registered = false
  register_shutdown_hook
end

Instance Method Details

#clear_userObject



248
249
250
# File 'lib/logister/reporter.rb', line 248

def clear_user
  Thread.current[:logister_user] = nil
end

#flush(timeout: 2) ⇒ Object



252
253
254
# File 'lib/logister/reporter.rb', line 252

def flush(timeout: 2)
  @client.flush(timeout: timeout)
end

#report_check_in(slug:, status: 'ok', expected_interval_seconds: 300, duration_ms: nil, context: {}, level: nil, environment: nil, release: nil, occurred_at: nil, trace_id: nil, request_id: nil) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/logister/reporter.rb', line 199

def report_check_in(
  slug:,
  status: 'ok',
  expected_interval_seconds: 300,
  duration_ms: nil,
  context: {},
  level: nil,
  environment: nil,
  release: nil,
  occurred_at: nil,
  trace_id: nil,
  request_id: nil
)
  return false if ignored_environment?

  payload = build_payload(
    event_type:  'check_in',
    level:       level || (status.to_s == 'error' ? 'error' : 'info'),
    message:     slug,
    fingerprint: metric_fingerprint("checkin:#{slug}"),
    context:     context.merge(
      check_in_slug: slug,
      check_in_status: status,
      expected_interval_seconds: expected_interval_seconds,
      duration_ms: duration_ms,
      environment: environment,
      release: release,
      trace_id: trace_id,
      request_id: request_id
    ).compact
  )
  payload[:occurred_at] = normalize_timestamp(occurred_at) if occurred_at

  payload = apply_before_notify(payload)
  return false unless payload

  @client.publish(payload)
end

#report_error(exception, context: {}, tags: {}, level: 'error', fingerprint: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/logister/reporter.rb', line 43

def report_error(exception, context: {}, tags: {}, level: 'error', fingerprint: nil)
  return false if ignored_exception?(exception)
  return false if ignored_path?(context)

  merged_context = default_error_context.merge(context)
  user = current_user_context
  merged_context[:user] = user if user

  payload = build_payload(
    event_type:  'error',
    level:       level,
    message:     "#{exception.class}: #{exception.message}",
    fingerprint: fingerprint || default_fingerprint(exception),
    context:     merged_context.merge(
      exception: exception_context(exception),
      tags: tags
    )
  )

  payload = apply_before_notify(payload)
  return false unless payload

  @client.publish(payload)
end

#report_log(message:, level: 'info', context: {}, tags: {}, fingerprint: nil) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/logister/reporter.rb', line 181

def report_log(message:, level: 'info', context: {}, tags: {}, fingerprint: nil)
  return false if ignored_environment?
  return false if ignored_path?(context)

  payload = build_payload(
    event_type:  'log',
    level:       level,
    message:     message,
    fingerprint: fingerprint || metric_fingerprint("log:#{message.to_s.lines.first}"),
    context:     context.merge(tags: tags)
  )

  payload = apply_before_notify(payload)
  return false unless payload

  @client.publish(payload)
end

#report_metric(message:, value: nil, unit: nil, level: 'info', context: {}, tags: {}, fingerprint: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/logister/reporter.rb', line 68

def report_metric(message:, value: nil, unit: nil, level: 'info', context: {}, tags: {}, fingerprint: nil)
  return false if ignored_environment?
  return false if ignored_path?(context)

  metric_context = context.merge(tags: tags)
  if value
    metric_context = metric_context.merge(
      metric: {
        name: message,
        value: value,
        unit: unit
      }.compact,
      value: value,
      unit: unit
    ).compact
  end

  payload = build_payload(
    event_type:  'metric',
    level:       level,
    message:     message,
    fingerprint: fingerprint || metric_fingerprint(message),
    context:     metric_context
  )

  payload = apply_before_notify(payload)
  return false unless payload

  @client.publish(payload)
end

#report_span(name:, duration_ms:, trace_id: nil, request_id: nil, span_id: nil, parent_span_id: nil, kind: 'internal', status: nil, started_at: nil, ended_at: nil, context: {}, tags: {}, fingerprint: nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/logister/reporter.rb', line 122

def report_span(
  name:,
  duration_ms:,
  trace_id: nil,
  request_id: nil,
  span_id: nil,
  parent_span_id: nil,
  kind: 'internal',
  status: nil,
  started_at: nil,
  ended_at: nil,
  context: {},
  tags: {},
  fingerprint: nil
)
  return false if ignored_environment?
  return false if ignored_path?(context)

  started_at ||= Time.now.utc - (duration_ms.to_f / 1000.0)
  span_id ||= SecureRandom.hex(8)
  trace_id ||= span_id

  payload = build_payload(
    event_type: 'span',
    level: status.to_s == 'error' ? 'error' : 'info',
    message: name,
    fingerprint: fingerprint || metric_fingerprint("span:#{kind}:#{name}"),
    context: context.merge(
      name: name,
      trace_id: trace_id,
      request_id: request_id,
      span_id: span_id,
      parent_span_id: parent_span_id,
      span_kind: kind,
      kind: kind,
      status: status,
      duration_ms: duration_ms,
      started_at: normalize_timestamp(started_at),
      ended_at: ended_at && normalize_timestamp(ended_at),
      tags: tags
    ).compact
  )

  payload[:started_at] = normalize_timestamp(started_at)
  payload[:ended_at] = normalize_timestamp(ended_at) if ended_at
  payload[:duration_ms] = duration_ms
  payload[:trace_id] = trace_id
  payload[:request_id] = request_id if request_id
  payload[:span_id] = span_id
  payload[:parent_span_id] = parent_span_id if parent_span_id
  payload[:kind] = kind
  payload[:status] = status if status

  payload = apply_before_notify(payload)
  return false unless payload

  @client.publish(payload)
end

#report_transaction(name:, duration_ms:, level: 'info', context: {}, tags: {}, fingerprint: nil, status: nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/logister/reporter.rb', line 99

def report_transaction(name:, duration_ms:, level: 'info', context: {}, tags: {}, fingerprint: nil, status: nil)
  return false if ignored_environment?
  return false if ignored_path?(context)

  payload = build_payload(
    event_type:  'transaction',
    level:       level,
    message:     name,
    fingerprint: fingerprint || metric_fingerprint("tx:#{name}"),
    context:     context.merge(
      transaction_name: name,
      duration_ms: duration_ms,
      status: status,
      tags: tags
    ).compact
  )

  payload = apply_before_notify(payload)
  return false unless payload

  @client.publish(payload)
end

#set_user(id: nil, email: nil, name: nil, **extra) ⇒ Object

Store user info for the current thread so it is automatically attached to every error reported during this request.

Logister.set_user(id: current_user.id, email: current_user.email, name: current_user.name)


243
244
245
246
# File 'lib/logister/reporter.rb', line 243

def set_user(id: nil, email: nil, name: nil, **extra)
  ctx = { id: id, email: email, name: name }.merge(extra).compact
  Thread.current[:logister_user] = ctx.empty? ? nil : ctx
end

#shutdownObject



256
257
258
# File 'lib/logister/reporter.rb', line 256

def shutdown
  @client.shutdown
end