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. The data itself is a ruby hash.

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.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/libhoney/event.rb', line 14

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| self.add_field(k, v) }
  dyn_fields.each { |k, v| self.add_field(k, v.call) }
  
  self
end

Instance Attribute Details

#api_hostObject

Returns the value of attribute api_host.



6
7
8
# File 'lib/libhoney/event.rb', line 6

def api_host
  @api_host
end

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/libhoney/event.rb', line 9

def data
  @data
end

#datasetObject

Returns the value of attribute dataset.



6
7
8
# File 'lib/libhoney/event.rb', line 6

def dataset
  @dataset
end

#metadataObject

Returns the value of attribute metadata.



7
8
9
# File 'lib/libhoney/event.rb', line 7

def 
  @metadata
end

#sample_rateObject

Returns the value of attribute sample_rate.



6
7
8
# File 'lib/libhoney/event.rb', line 6

def sample_rate
  @sample_rate
end

#timestampObject

Returns the value of attribute timestamp.



7
8
9
# File 'lib/libhoney/event.rb', line 7

def timestamp
  @timestamp
end

#writekeyObject

Returns the value of attribute writekey.



6
7
8
# File 'lib/libhoney/event.rb', line 6

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.



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

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.



55
56
57
58
# File 'lib/libhoney/event.rb', line 55

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

#sendself

sends this event to honeycomb

Returns:

  • (self)

    this event.



80
81
82
83
84
85
86
87
88
# File 'lib/libhoney/event.rb', line 80

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

  self.send_presampled()
end

#send_presampledself

sends a presampled event to honeycomb

Returns:

  • (self)

    this event.

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
# File 'lib/libhoney/event.rb', line 93

def send_presampled
  raise ArgumentError.new("No metrics added to event. Won't send empty event.")         if self.data.length == 0
  raise ArgumentError.new("No APIHost for Honeycomb. Can't send to the Great Unknown.") if self.api_host == ""
  raise ArgumentError.new("No WriteKey specified. Can't send event.")                   if self.writekey == ""
  raise ArgumentError.new("No Dataset for Honeycomb. Can't send datasetless.")          if self.dataset == ""

  @libhoney.send_event(self)
  self
end

#with_timer(name, &block) ⇒ 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.



68
69
70
71
72
73
74
75
# File 'lib/libhoney/event.rb', line 68

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