Class: Libhoney::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/libhoney/event.rb

Overview

This is the event object that you can fill up with data.

Examples:

Override the default timestamp on an event

evt = libhoney.event
evt.add_fields(useful_fields)
evt.timestamp = Time.now
evt.send

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(libhoney, builder, fields = {}, dyn_fields = {}) ⇒ Event

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Event.

See Also:



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/libhoney/event.rb', line 47

def initialize(libhoney, builder, fields = {}, dyn_fields = {})
  @libhoney    = libhoney
  @writekey    = builder.writekey
  @dataset     = builder.dataset
  @sample_rate = builder.sample_rate
  @api_host    = builder.api_host
  @timestamp   = Time.now
      = nil

  @data = {}
  fields.each { |k, v| add_field(k, v) }
  dyn_fields.each { |k, v| add_field(k, v.call) }
end

Instance Attribute Details

#api_hostString



28
29
30
# File 'lib/libhoney/event.rb', line 28

def api_host
  @api_host
end

#dataHash<String=>any> (readonly)



42
43
44
# File 'lib/libhoney/event.rb', line 42

def data
  @data
end

#datasetString



18
19
20
# File 'lib/libhoney/event.rb', line 18

def dataset
  @dataset
end

#metadataObject



34
35
36
# File 'lib/libhoney/event.rb', line 34

def 
  
end

#sample_rateFixnum



24
25
26
# File 'lib/libhoney/event.rb', line 24

def sample_rate
  @sample_rate
end

#timestampTime



39
40
41
# File 'lib/libhoney/event.rb', line 39

def timestamp
  @timestamp
end

#writekeyString



13
14
15
# File 'lib/libhoney/event.rb', line 13

def writekey
  @writekey
end

Instance Method Details

#add(newdata) ⇒ self

adds a group of field->values to this event.

Examples:

using an object

builder.event
  .add({
    :responseTime_ms => 100,
    :httpStatusCode => 200
  })


71
72
73
74
# File 'lib/libhoney/event.rb', line 71

def add(newdata)
  @data.merge!(newdata)
  self
end

#add_field(name, val) ⇒ self

adds a single field->value mapping to this event.

Examples:

builder.event
  .add_field("responseTime_ms", 100)
  .send


85
86
87
88
# File 'lib/libhoney/event.rb', line 85

def add_field(name, val)
  @data[name] = val
  self
end

#sendself

sends this event to Honeycomb



110
111
112
113
114
115
116
117
118
# File 'lib/libhoney/event.rb', line 110

def send
  # discard if sampling rate says so
  if @libhoney.should_drop(sample_rate)
    @libhoney.send_dropped_response(self, 'event dropped due to sampling')
    return
  end

  send_presampled
end

#send_presampledself

sends a presampled event to Honeycomb



123
124
125
126
# File 'lib/libhoney/event.rb', line 123

def send_presampled
  @libhoney.send_event(self)
  self
end

#with_timer(name) ⇒ self

times the execution of a block and adds a field containing the duration in milliseconds

Examples:

event.with_timer "task_ms" do
  # something time consuming
end


98
99
100
101
102
103
104
105
# File 'lib/libhoney/event.rb', line 98

def with_timer(name)
  start = Time.now
  yield
  duration = Time.now - start
  # report in ms
  add_field(name, duration * 1000)
  self
end