55 lines
1.3 KiB
Ruby
55 lines
1.3 KiB
Ruby
class Metrics::Client
|
|
include HasColorLogger
|
|
|
|
REPORT = !Rails.env.test?
|
|
|
|
def self.singleton
|
|
@singleton ||= Metrics::Client.new
|
|
end
|
|
def self.singleton=(instance)
|
|
@singleton = instance
|
|
end
|
|
|
|
private
|
|
|
|
def initialize(default_tags: {})
|
|
unless REPORT
|
|
logger.warn "not reporting metrics for '#{Rails.env.to_s.bold}' environment"
|
|
return
|
|
end
|
|
|
|
host = Rails.application.config.x.influxdb.host || raise("no host")
|
|
bucket = Rails.application.config.x.influxdb.bucket || raise("no bucket")
|
|
|
|
@client = InfluxDB2::Client.new(
|
|
host, "",
|
|
org: "",
|
|
bucket: bucket,
|
|
precision: InfluxDB2::WritePrecision::MILLISECOND,
|
|
use_ssl: false,
|
|
)
|
|
|
|
write_options = InfluxDB2::WriteOptions.new(
|
|
write_type: InfluxDB2::WriteType::BATCHING,
|
|
batch_size: 100, flush_interval: 5_000,
|
|
max_retries: 3, max_retry_delay: 15_000,
|
|
exponential_base: 2,
|
|
)
|
|
|
|
point_settings = InfluxDB2::PointSettings.new(default_tags: default_tags)
|
|
@writer = @client.create_write_api(
|
|
write_options: write_options,
|
|
point_settings: point_settings,
|
|
)
|
|
end
|
|
|
|
public
|
|
|
|
def write_point(caller, name, tags: {}, fields: {})
|
|
return unless REPORT
|
|
@writer.write(data: { name: name, tags: tags, fields: fields })
|
|
rescue => e
|
|
logger.error("error writing influxdb metric: #{e}")
|
|
end
|
|
end
|