Class: PMetric::Collector::Influx

Inherits:
Base
  • Object
show all
Defined in:
lib/pmetric/collector/influx.rb

Overview

InfluxDB Collector for Pmetrics.

Constant Summary collapse

PRECISIONS =
{
  's' => Proc.new { Time.now.utc.to_i },
  'ms' => Proc.new { (Time.now.utc * 1000.0).to_i },
  'ns' => Proc.new { (Time.now.utc.to_r * 10**9).to_i }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts: {}, tags: nil, logger: nil) ⇒ Influx

Returns a new instance of Influx.

Parameters:

  • database (String)

    InfluxDB database to write to.

  • client_opts (Hash)

    InfluxDB client options.

  • tags (Hash) (defaults to: nil)

    Default tags to be added to every event.

  • logger (Logger) (defaults to: nil)

    InfluxDB client logger. This affects global InfluxDB logging.



17
18
19
20
21
22
23
24
# File 'lib/pmetric/collector/influx.rb', line 17

def initialize(opts: {}, tags: nil, logger: nil)
  @opts = opts
  @tags = Hash(tags).freeze
  @precision = (opts[:time_precision] || 'ns').freeze

  require 'influxdb'
  InfluxDB::Logging.logger = InfluxLogger.new(logger)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/pmetric/collector/influx.rb', line 11

def logger
  @logger
end

#optsObject (readonly)

Returns the value of attribute opts.



11
12
13
# File 'lib/pmetric/collector/influx.rb', line 11

def opts
  @opts
end

#precisionObject (readonly)

Returns the value of attribute precision.



11
12
13
# File 'lib/pmetric/collector/influx.rb', line 11

def precision
  @precision
end

#tagsObject (readonly)

Returns the value of attribute tags.



11
12
13
# File 'lib/pmetric/collector/influx.rb', line 11

def tags
  @tags
end

Instance Method Details

#increment(metric, fields: {}, tags: {}) ⇒ Object

Delegates to #write, adding ‘value: 1` to the fields. Passing your own `value` field option is pointless here.

Parameters:

  • metric (String)

    The item/event being measured.

  • fields (Hash) (defaults to: {})

    Additional fields being measured.

  • tags (Hash) (defaults to: {})

    Additional tags for the event.



32
33
34
35
# File 'lib/pmetric/collector/influx.rb', line 32

def increment(metric, fields: {}, tags: {})
  fields[:value] = 1
  write(metric, fields: fields, tags: tags)
end

#write(metric, fields:, tags: {}) ⇒ Object

Writes metrics to InfluxDB.

Parameters:

  • metric (String)

    The item/event being measured.

  • fields (Hash)

    The fields being measured.

  • tags (Hash) (defaults to: {})

    Additional tags for the event.



42
43
44
45
46
47
48
49
50
# File 'lib/pmetric/collector/influx.rb', line 42

def write(metric, fields:, tags: {})
  data = {
    values: fields,
    tags: tags.merge(@tags),
    timestamp: PRECISIONS[@precision].call
  }

  client.write_point(metric, data)
end