Class: PMetric::Collector::Influx
- 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
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
-
#increment(metric, fields: {}, tags: {}) ⇒ Object
Delegates to #write, adding ‘value: 1` to the fields.
-
#initialize(opts: {}, tags: nil, logger: nil) ⇒ Influx
constructor
A new instance of Influx.
-
#write(metric, fields:, tags: {}) ⇒ Object
Writes metrics to InfluxDB.
Constructor Details
#initialize(opts: {}, tags: nil, logger: nil) ⇒ Influx
Returns a new instance of Influx.
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().freeze @precision = (opts[:time_precision] || 'ns').freeze require 'influxdb' InfluxDB::Logging.logger = InfluxLogger.new(logger) end |
Instance Attribute Details
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/pmetric/collector/influx.rb', line 11 def logger @logger end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
11 12 13 |
# File 'lib/pmetric/collector/influx.rb', line 11 def opts @opts end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
11 12 13 |
# File 'lib/pmetric/collector/influx.rb', line 11 def precision @precision end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
11 12 13 |
# File 'lib/pmetric/collector/influx.rb', line 11 def @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.
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: ) end |
#write(metric, fields:, tags: {}) ⇒ Object
Writes metrics to InfluxDB.
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: .merge(@tags), timestamp: PRECISIONS[@precision].call } client.write_point(metric, data) end |