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.



11
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
# File 'lib/logister/reporter.rb', line 11

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



158
159
160
# File 'lib/logister/reporter.rb', line 158

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

#flush(timeout: 2) ⇒ Object



162
163
164
# File 'lib/logister/reporter.rb', line 162

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) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/logister/reporter.rb', line 126

def report_check_in(slug:, status: 'ok', expected_interval_seconds: 300, duration_ms: nil, context: {}, level: 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
    ).compact
  )

  payload = apply_before_notify(payload)
  return false unless payload

  @client.publish(payload)
end

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



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

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/logister/reporter.rb', line 108

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:, level: 'info', context: {}, tags: {}, fingerprint: nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/logister/reporter.rb', line 67

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

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

  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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/logister/reporter.rb', line 85

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)


153
154
155
156
# File 'lib/logister/reporter.rb', line 153

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



166
167
168
# File 'lib/logister/reporter.rb', line 166

def shutdown
  @client.shutdown
end