-
Notifications
You must be signed in to change notification settings - Fork 0
/
deterministic_sampling.rb
86 lines (65 loc) · 1.86 KB
/
deterministic_sampling.rb
1
2
3
4
5
6
7
8
9
10
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
puts <<-MSG
In order to drop entire traces at a certain rate, you should use
Honeycomb::DeterministicSampler. This uses a hash function on the trace ID to
make a deterministic but well-distributed choice as to whether the event should
be dropped or not at the given rate.
Here, we monkey-patch the (normally autogenerated) trace ID in two separate
traces. As it so happens, some human-friendly names hash such that we get the
desired results:
should_sample(2, 'drop') #=> false
should_sample(2, 'send') #=> true
Every event in the first trace will be dropped (so its URL won't work), and
every event in the second trace will be sent (so its URL will work, and there
won't be any orphans).
MSG
module Sampler
extend Honeycomb::DeterministicSampler
def self.sample(fields)
rate = 2
if should_sample(rate, fields['trace.trace_id'])
puts "Send event: #{JSON.pretty_generate(fields)}\n\n"
[true, rate]
else
puts "Drop event: #{JSON.pretty_generate(fields)}\n\n"
[false, rate]
end
end
end
Honeycomb.configure do |config|
config.write_key = ENV['HONEYCOMB_WRITE_KEY']
config.dataset = 'exsampling'
config.sample_hook do |fields|
Sampler.sample(fields)
end
end
Honeycomb.start_span(name: 'root') do
Honeycomb.current_trace.instance_variable_set(:@id, 'drop')
puts trace_url
puts
sleep 1
Honeycomb.start_span(name: 'child 1') do
sleep 1
Honeycomb.start_span(name: 'grandchild') do
sleep 1
end
end
Honeycomb.start_span(name: 'child 2') do
sleep 1
end
end
puts
Honeycomb.start_span(name: 'root') do
Honeycomb.current_trace.instance_variable_set(:@id, 'send')
puts trace_url
puts
sleep 1
Honeycomb.start_span(name: 'child 1') do
sleep 1
Honeycomb.start_span(name: 'grandchild') do
sleep 1
end
end
Honeycomb.start_span(name: 'child 2') do
sleep 1
end
end