Skip to content

Commit bca3a1d

Browse files
author
Joshua Wood
committed
Hash fingerprint for notice payload.
1 parent 097fca7 commit bca3a1d

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ By default, we group errors in two ways:
198198
1. "Strict" grouping generates a fingerprint using a hash of the error
199199
class, component (if available), and the entire backtrace. When the
200200
backtrace changes, a new error is created by Honeybadger.
201-
2. "Loose" grouping uses only the error class, component (if available),
202-
and the first line of the application trace.
201+
2. "Loose" grouping uses the error class, component (if available), and
202+
the application trace.
203203

204204
You can choose to use strict or loose grouping from your Honeybadger
205205
project settings page. If you want to use your own grouping strategy,
@@ -213,7 +213,7 @@ we'll use that for grouping errors instead of the default:
213213
# See lib/honeybadger/notice.rb for the options that are available
214214
# on the notice object
215215
config.fingerprint do |notice|
216-
Digest::SHA1.hexdigest([notice[:error_class], notice[:component], notice[:backtrace].to_s].join(':'))
216+
[notice[:error_class], notice[:component], notice[:backtrace].to_s].join(':')
217217
end
218218

219219
end
@@ -223,6 +223,9 @@ directly:
223223

224224
Honeybadger.notify(StandardError.new('oh noes!'), :fingerprint => 'asdf')
225225

226+
The fingerprint can be any Ruby object that responds to #to_s, and will
227+
be sent to Honeybadger as a SHA1 hash.
228+
226229
*Please note that to make use of this option, you must have **strict**
227230
grouping disabled on your project settings page.*
228231

lib/honeybadger.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'net/http'
22
require 'net/https'
33
require 'json'
4+
require 'digest'
45
require 'logger'
56

67
require 'honeybadger/configuration'

lib/honeybadger/configuration.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ def async
267267

268268
# Public: Generate custom fingerprint (optional)
269269
#
270-
# block - An optional block returning calculated fingerprint
270+
# block - An optional block returning object responding to #to_s
271271
#
272272
# Examples
273273
#
274274
# config.fingerprint = Proc.new { |notice| ... }
275275
#
276276
# config.fingerprint do |notice|
277-
# Digest::SHA1.hexdigest([notice[:error_class], notice[:component], notice[:backtrace].to_s].join(':'))
277+
# [notice[:error_class], notice[:component], notice[:backtrace].to_s].join(':')
278278
# end
279279
#
280280
# Returns configured fingerprint generator (should respond to #call(notice))

lib/honeybadger/notice.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def initialize(args)
103103
self.environment_name = args[:environment_name]
104104
self.cgi_data = args[:cgi_data] || args[:rack_env]
105105
self.backtrace = Backtrace.parse(exception_attribute(:backtrace, caller), :filters => self.backtrace_filters)
106-
self.fingerprint = generated_fingerprint
106+
self.fingerprint = hashed_fingerprint
107107
self.error_class = exception_attribute(:error_class) {|exception| exception.class.name }
108108
self.error_message = exception_attribute(:error_message, 'Notification') do |exception|
109109
"#{exception.class.name}: #{exception.message}"
@@ -300,14 +300,21 @@ def clean_rack_request_data
300300
end
301301
end
302302

303-
def generated_fingerprint
303+
def fingerprint_from_args
304304
if args[:fingerprint].respond_to?(:call)
305305
args[:fingerprint].call(self)
306306
else
307307
args[:fingerprint]
308308
end
309309
end
310310

311+
def hashed_fingerprint
312+
fingerprint = fingerprint_from_args
313+
if fingerprint && fingerprint.respond_to?(:to_s)
314+
Digest::SHA1.hexdigest(fingerprint.to_s)
315+
end
316+
end
317+
311318
def extract_source_from_backtrace
312319
if backtrace.lines.empty?
313320
nil

test/unit/notice_test.rb

+9-4
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,19 @@ def stub_request(attrs = {})
8686
assert_equal nil, notice.fingerprint
8787
end
8888

89-
should "accepts fingerprint as string" do
89+
should "accept fingerprint as string" do
9090
notice = build_notice({ :fingerprint => 'foo' })
91-
assert_equal 'foo', notice.fingerprint
91+
assert_equal '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', notice.fingerprint
9292
end
9393

94-
should "accepts fingerprint responding to #call" do
94+
should "accept fingerprint responding to #call" do
9595
notice = build_notice({ :fingerprint => mock(:call => 'foo') })
96-
assert_equal 'foo', notice.fingerprint
96+
assert_equal '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', notice.fingerprint
97+
end
98+
99+
should "accept fingerprint using #to_s" do
100+
notice = build_notice({ :fingerprint => mock(:to_s => 'foo') })
101+
assert_equal '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', notice.fingerprint
97102
end
98103
end
99104

0 commit comments

Comments
 (0)