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
42
43
44
# 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,
    repository:  @configuration.repository,
    commit_sha:  @configuration.commit_sha,
    branch:      @configuration.branch
  }.compact.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



285
286
287
# File 'lib/logister/reporter.rb', line 285

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

#flush(timeout: 2) ⇒ Object



289
290
291
# File 'lib/logister/reporter.rb', line 289

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

#record_deployment(release: nil, environment: nil, repository: nil, commit_sha: nil, branch: nil, deployed_at: nil, pull_request_number: nil, pull_request_url: nil, release_tag: nil, release_url: nil, compare_url: nil, workflow_run_url: nil, deployment_url: nil) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/logister/reporter.rb', line 241

def record_deployment(
  release: nil,
  environment: nil,
  repository: nil,
  commit_sha: nil,
  branch: nil,
  deployed_at: nil,
  pull_request_number: nil,
  pull_request_url: nil,
  release_tag: nil,
  release_url: nil,
  compare_url: nil,
  workflow_run_url: nil,
  deployment_url: nil
)
  payload = {
    release: release || @configuration.release,
    environment: environment || @configuration.environment,
    repository: repository || @configuration.repository,
    commit_sha: commit_sha || @configuration.commit_sha,
    branch: branch || @configuration.branch,
    deployed_at: deployed_at && normalize_timestamp(deployed_at),
    pull_request_number: pull_request_number,
    pull_request_url: pull_request_url,
    release_tag: release_tag,
    release_url: release_url,
    compare_url: compare_url,
    workflow_run_url: workflow_run_url,
    deployment_url: deployment_url
  }.compact

  @client.publish_deployment(payload)
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



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
237
238
239
# File 'lib/logister/reporter.rb', line 202

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



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

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



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

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



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
98
99
100
# File 'lib/logister/reporter.rb', line 71

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



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
180
181
182
# File 'lib/logister/reporter.rb', line 125

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



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

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)


280
281
282
283
# File 'lib/logister/reporter.rb', line 280

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



293
294
295
# File 'lib/logister/reporter.rb', line 293

def shutdown
  @client.shutdown
end