Class: Libhoney::Builder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(libhoney, parent_builder, fields = {}, dyn_fields = {}) ⇒ Builder

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 Builder.

See Also:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/libhoney/builder.rb', line 10

def initialize(libhoney, parent_builder, fields = {}, dyn_fields = {})
  @libhoney = libhoney

  @fields     = {}
  @dyn_fields = {}

  unless parent_builder.nil?
    @writekey    = parent_builder.writekey
    @dataset     = parent_builder.dataset
    @sample_rate = parent_builder.sample_rate
    @api_host    = parent_builder.api_host

    @fields.merge!(parent_builder.fields)
    @dyn_fields.merge!(parent_builder.dyn_fields)
  end

  @fields.merge!(fields)
  @dyn_fields.merge!(dyn_fields)
end

Instance Attribute Details

#api_hostObject

Returns the value of attribute api_host.



5
6
7
# File 'lib/libhoney/builder.rb', line 5

def api_host
  @api_host
end

#datasetObject

Returns the value of attribute dataset.



5
6
7
# File 'lib/libhoney/builder.rb', line 5

def dataset
  @dataset
end

#dyn_fieldsObject

Returns the value of attribute dyn_fields.



5
6
7
# File 'lib/libhoney/builder.rb', line 5

def dyn_fields
  @dyn_fields
end

#fieldsObject

Returns the value of attribute fields.



5
6
7
# File 'lib/libhoney/builder.rb', line 5

def fields
  @fields
end

#sample_rateObject

Returns the value of attribute sample_rate.



5
6
7
# File 'lib/libhoney/builder.rb', line 5

def sample_rate
  @sample_rate
end

#writekeyObject

Returns the value of attribute writekey.



5
6
7
# File 'lib/libhoney/builder.rb', line 5

def writekey
  @writekey
end

Instance Method Details

#add(data) ⇒ self

adds a group of field->values to the events created from this builder.

Examples:

using an object

honey = Libhoney::Client.new(...)
builder = honey.builder
builder.add {
  :responseTime_ms => 100,
  :httpStatusCode => 200
}

Parameters:

  • data (Hash<String=>any>)

    field->value mapping.

Returns:

  • (self)

    this Builder instance.



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

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

#add_dynamic_field(name, proc) ⇒ self

adds a single field->dynamic value function, which is invoked to supply values when events are created from this builder.

Examples:

builder.add_dynamic_field("process_heapUsed", Proc.new { Thread.list.select {|thread| thread.status == "run"}.count })

Parameters:

  • name (string)

    the name of the field to add to events.

  • proc (#call)

    the function called to generate the value for this field.

Returns:

  • (self)

    this Builder instance.



65
66
67
# File 'lib/libhoney/builder.rb', line 65

def add_dynamic_field(name, proc)
  @dyn_fields[name] = proc
end

#add_field(name, val) ⇒ self

adds a single field->value mapping to the events created from this builder.

Examples:

builder.add_field("responseTime_ms", 100)

Parameters:

  • name (string)
  • val (any)

Returns:

  • (self)

    this Builder instance.



53
54
55
56
# File 'lib/libhoney/builder.rb', line 53

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

#builder(fields = {}, dyn_fields = {}) ⇒ Builder

creates and returns a clone of this builder, merged with fields and dyn_fields passed as arguments.

Examples:

no additional fields/dyn_field

another_builder = the_builder.builder

additional fields/dyn_field

anotherBuilder = the_builder.builder({ :request_id => @request_id },
                                     { :active_threads => Proc.new { Thread.list.select {|thread| thread.status == "run"}.count } });

Parameters:

  • fields (Hash<String=>any>) (defaults to: {})

    a field->value mapping to merge into the new builder.

  • dyn_fields (Hash<String=>#call>) (defaults to: {})

    a field->dynamic function mapping to merge into the new builder.

Returns:



115
116
117
# File 'lib/libhoney/builder.rb', line 115

def builder(fields = {}, dyn_fields = {})
  Builder.new(@libhoney, self, fields, dyn_fields)
end

#eventEvent

creates and returns a new Event containing all fields/dyn_fields from this builder, that can be further fleshed out and sent on its own.

Examples:

adding data at send-time

ev = builder.event
ev.add_field("additionalField", value)
ev.send

Returns:

  • (Event)

    an Event instance



101
102
103
# File 'lib/libhoney/builder.rb', line 101

def event
  Event.new(@libhoney, self, @fields, @dyn_fields)
end

#send_now(data = {}) ⇒ self

Deprecated.

Creates and sends an event, including all global builder fields/dyn_fields, as well as anything in the optional data parameter.

Equivalent to:

ev = builder.event
ev.add(data)
ev.send

May be removed in a future major release

Examples:

empty send_now

builder.send_now # sends just the data that has been added via add/add_field/add_dynamic_field.

adding data at send-time

builder.send_now {
  :additionalField => value
}

Parameters:

  • data (Hash<String=>any>) (defaults to: {})

    optional field->value mapping to add to the event sent.

Returns:

  • (self)

    this Builder instance



87
88
89
90
91
92
# File 'lib/libhoney/builder.rb', line 87

def send_now(data = {})
  ev = event
  ev.add(data)
  ev.send
  self
end