libhoney 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6f7c1f1580ba76af6061acd5a3913cb858085d07
4
- data.tar.gz: 60c311e244c43d1bc644b514d41e61e706cdebae
2
+ SHA256:
3
+ metadata.gz: 930fb89e75aad3624d26e929b09d4b900a88df25ad4c0462eedd85ea5efe1091
4
+ data.tar.gz: 0f7584e0ca46508f3da2d74523c15126f78f230312c865c2facadcac49dcb187
5
5
  SHA512:
6
- metadata.gz: 01281419d356b74b4af7d48b6cfe6fb9db346ec384f0e7326cb2eec11baba6e917f2620c47077e148f228caf86da5f3232db00b63a01fa660660b8f67675ba73
7
- data.tar.gz: 68d452bc863044c530a0a36718484635d893cdf29a1c89b823edcbcda56e9faf3b85407df3c35393fe1e9ac02b2524de1cda49f98c21adc424ea15bf04b97017
6
+ metadata.gz: f3990fcde2ad49d87eb9503ef89ca42cc7ab03c6c6ec2527bd1ec84062496a8e3c514e7b53c09ef5a278390f778afb807a3728617e0e00de89f1b7d0c1494285
7
+ data.tar.gz: 4bee236b58007911ad8c1e0ced9754bc127a1629aa330903c7a40ed8452d0d8c3274ed0e2b444ae04965137a50a72ce6198e9a278fbbb2a4af5fbccba60719f5
data/README.md CHANGED
@@ -59,6 +59,34 @@ honeycomb.close
59
59
 
60
60
  You can find a more complete example demonstrating usage in [`example/fact.rb`](example/fact.rb)
61
61
 
62
+ ## Testing instrumented code
63
+
64
+ Once you've instrumented your code to send events to Honeycomb, you may want to
65
+ consider writing tests that verify your code is producing the events you expect,
66
+ annotating them with the right information, etc. That way, if your code changes
67
+ and breaks the instrumentation, you'll find out straight away, instead of at 3am
68
+ when you need that data available for debugging!
69
+
70
+ To support this use case, libhoney provides a
71
+ [`TestClient`](http://www.rubydoc.info/gems/libhoney/Libhoney/TestClient) which
72
+ you can swap in for the usual `Client`. Example usage:
73
+
74
+ ```ruby
75
+ fakehoney = Libhoney::TestClient.new
76
+
77
+ my_app = MyApp.new(..., fakehoney, ...)
78
+ my_app.do_stuff
79
+
80
+ expect(fakehoney.events.size).to eq 3
81
+
82
+ first_event = fakehoney.events[0]
83
+ expect(first_event.data['hovercraft_contents']).to eq 'Eels'
84
+ ```
85
+
86
+ For more detail see the docs for
87
+ [`TestClient`](http://www.rubydoc.info/gems/libhoney/Libhoney/TestClient) and
88
+ [`Event`](http://www.rubydoc.info/gems/libhoney/Libhoney/Event).
89
+
62
90
  ## Contributions
63
91
 
64
92
  Features, bug fixes and other changes to libhoney are gladly accepted. Please
@@ -53,12 +53,14 @@ module Libhoney
53
53
  # @param dataset [String] the dataset you want
54
54
  # @param sample_rate [Fixnum] cause libhoney to send 1 out of sampleRate events. overrides the libhoney instance's value.
55
55
  # @param api_host [String] the base url to send events to
56
+ # @param transmission [Object] transport used to actually send events. If nil (the default), will be lazily initialized with a {TransmissionClient} on first event send.
56
57
  # @param block_on_send [Boolean] if more than pending_work_capacity events are written, block sending further events
57
58
  # @param block_on_responses [Boolean] if true, block if there is no thread reading from the response queue
58
59
  def initialize(writekey: '',
59
60
  dataset: '',
60
61
  sample_rate: 1,
61
62
  api_host: 'https://api.honeycomb.io/',
63
+ transmission: nil,
62
64
  block_on_send: false,
63
65
  block_on_responses: false,
64
66
  max_batch_size: 50,
@@ -83,7 +85,7 @@ module Libhoney
83
85
  @max_concurrent_batches = max_concurrent_batches
84
86
  @pending_work_capacity = pending_work_capacity
85
87
  @responses = SizedQueue.new(2 * @pending_work_capacity)
86
- @tx = nil
88
+ @tx = transmission
87
89
  @lock = Mutex.new
88
90
 
89
91
  self
@@ -203,6 +205,23 @@ module Libhoney
203
205
  def should_drop(sample_rate)
204
206
  rand(1..sample_rate) != 1
205
207
  end
208
+ end
206
209
 
210
+ # A client with the network stubbed out for testing purposes. Does not
211
+ # actually send any events to Honeycomb; instead, records events for later
212
+ # inspection.
213
+ #
214
+ # @note This class is intended for use in tests, for example if you want to
215
+ # verify what events your instrumented code is sending. Use in
216
+ # production is not recommended.
217
+ class TestClient < Client
218
+ def initialize(*args, **kwargs)
219
+ super(*args, transmission: MockTransmissionClient.new, **kwargs)
220
+ end
221
+
222
+ # @return [Array<Event>] the recorded events
223
+ def events
224
+ @tx.events
225
+ end
207
226
  end
208
227
  end
@@ -1,11 +1,11 @@
1
1
  module Libhoney
2
2
  ##
3
3
  # This is the event object that you can fill up with data.
4
- # The data itself is a ruby hash.
5
4
  class Event
6
5
  attr_accessor :writekey, :dataset, :sample_rate, :api_host
7
6
  attr_accessor :timestamp, :metadata
8
7
 
8
+ # @return [Hash<String=>any>] the fields in this event
9
9
  attr_reader :data
10
10
 
11
11
  # @api private
@@ -92,10 +92,6 @@ module Libhoney
92
92
  # @return [self] this event.
93
93
  def send_presampled
94
94
  raise ArgumentError.new("No metrics added to event. Won't send empty event.") if self.data.length == 0
95
- raise ArgumentError.new("No APIHost for Honeycomb. Can't send to the Great Unknown.") if self.api_host == ""
96
- raise ArgumentError.new("No WriteKey specified. Can't send event.") if self.writekey == ""
97
- raise ArgumentError.new("No Dataset for Honeycomb. Can't send datasetless.") if self.dataset == ""
98
-
99
95
  @libhoney.send_event(self)
100
96
  self
101
97
  end
@@ -0,0 +1,26 @@
1
+ module Libhoney
2
+ # For testing use: a mock version of TransmissionClient that retains all
3
+ # events in an in-memory queue for inspection (and does not send them to
4
+ # Honeycomb, or perform any network activity).
5
+ #
6
+ # @note This class is intended for use in tests, for example if you want to
7
+ # verify what events your instrumented code is sending. Use in
8
+ # production is not recommended.
9
+ class MockTransmissionClient
10
+ def initialize
11
+ @events = []
12
+ end
13
+
14
+ # @return [Array<Event>] the recorded events
15
+ attr_reader :events
16
+
17
+ # Records an event
18
+ def add(event)
19
+ @events.push(event)
20
+ end
21
+
22
+ # Does nothing.
23
+ def close(drain)
24
+ end
25
+ end
26
+ end
@@ -26,6 +26,10 @@ module Libhoney
26
26
  end
27
27
 
28
28
  def add(event)
29
+ raise ArgumentError, "No APIHost for Honeycomb. Can't send to the Great Unknown." if event.api_host == ""
30
+ raise ArgumentError, "No WriteKey specified. Can't send event." if event.writekey == ""
31
+ raise ArgumentError, "No Dataset for Honeycomb. Can't send datasetless." if event.dataset == ""
32
+
29
33
  begin
30
34
  @send_queue.enq(event, !@block_on_send)
31
35
  rescue ThreadError
@@ -1,3 +1,3 @@
1
1
  module Libhoney
2
- VERSION = "1.3.2"
2
+ VERSION = "1.4.0"
3
3
  end
data/lib/libhoney.rb CHANGED
@@ -3,3 +3,4 @@ require 'libhoney/version'
3
3
  require 'libhoney/builder'
4
4
  require 'libhoney/response'
5
5
  require 'libhoney/transmission'
6
+ require 'libhoney/mock_transmission'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libhoney
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Honeycomb.io Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-10 00:00:00.000000000 Z
11
+ date: 2017-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -127,6 +127,7 @@ files:
127
127
  - lib/libhoney/builder.rb
128
128
  - lib/libhoney/client.rb
129
129
  - lib/libhoney/event.rb
130
+ - lib/libhoney/mock_transmission.rb
130
131
  - lib/libhoney/response.rb
131
132
  - lib/libhoney/transmission.rb
132
133
  - lib/libhoney/version.rb
@@ -151,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
151
152
  version: '0'
152
153
  requirements: []
153
154
  rubyforge_project:
154
- rubygems_version: 2.6.14
155
+ rubygems_version: 2.7.2
155
156
  signing_key:
156
157
  specification_version: 4
157
158
  summary: send data to Honeycomb