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
  @metadata    = 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

Returns Set this attribute in order to override the destination of these Honeycomb events (defaults to Client::API_HOST).

Returns:

  • (String)

    Set this attribute in order to override the destination of these Honeycomb events (defaults to Client::API_HOST).



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

def api_host
  @api_host
end

#dataHash<String=>any> (readonly)

Returns the fields added to this event.

Returns:

  • (Hash<String=>any>)

    the fields added to this event



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

def data
  @data
end

#datasetString

Returns the Honeycomb dataset this event is destined for (defaults to the Builder‘s dataset, which in turn defaults to the Client’s dataset).

Returns:

  • (String)

    the Honeycomb dataset this event is destined for (defaults to the Builder‘s dataset, which in turn defaults to the Client’s dataset)



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

def dataset
  @dataset
end

#metadataObject

Returns Set this attribute to any Object you might need to identify this Event as it is returned to the responses queue (e.g. tag an Event with an internal ID in order to retry something specific on failure).

Returns:

  • (Object)

    Set this attribute to any Object you might need to identify this Event as it is returned to the responses queue (e.g. tag an Event with an internal ID in order to retry something specific on failure).



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

def 
  @metadata
end

#sample_rateFixnum

Returns Set this attribute to indicate that it represents sample_rate number of events (e.g. setting this to 10 will result in a 1-in-10 chance of it being successfully emitted to Honeycomb, and the Honeycomb query engine will interpret it as representative of 10 events).

Returns:

  • (Fixnum)

    Set this attribute to indicate that it represents sample_rate number of events (e.g. setting this to 10 will result in a 1-in-10 chance of it being successfully emitted to Honeycomb, and the Honeycomb query engine will interpret it as representative of 10 events)



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

def sample_rate
  @sample_rate
end

#timestampTime

Returns Set this attribute in order to override the timestamp associated with the event (defaults to the Time.now at Event creation).

Returns:

  • (Time)

    Set this attribute in order to override the timestamp associated with the event (defaults to the Time.now at Event creation)



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

def timestamp
  @timestamp
end

#writekeyString

Returns the Honeycomb API key with which to authenticate this request.

Returns:

  • (String)

    the Honeycomb API key with which to authenticate this request



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
  })

Parameters:

  • newdata (Hash<String=>any>)

    field->value mapping.

Returns:

  • (self)

    this event.



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

Parameters:

  • name (String)
  • val (any)

Returns:

  • (self)

    this event.



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

Returns:

  • (self)

    this event.



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

Returns:

  • (self)

    this event.



123
124
125
126
127
128
129
130
131
# File 'lib/libhoney/event.rb', line 123

def send_presampled
  if Thread.current[:libhoney_transmitting]
    @libhoney.send_dropped_response(self, 'dropped event generated during transmission')
  else
    @libhoney.send_event(self)
  end

  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

Parameters:

  • name (String)

    the name of the field to add to the event

Returns:

  • (self)

    this event.



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