Type-safe serialisation to InfluxDB's line protocol.
Measurements are Moose objects based on this
library. Tags and fields are attributes on the object which use the Tagset
and/or Fieldset
traits.
Create a class:
package SomeMeasurement;
use Moose;
extends 'InfluxDB::LineProtocol::Typed';
has my_tag => (is => 'ro', isa => 'Str', traits => [ qw(InfluxDB Tagset) ]);
has my_field => (is => 'ro', isa => 'Num', traits => [ qw(InfluxDB Fieldset) ]);
has my_both => (is => 'ro', isa => 'Str', traits => [ qw(InfluxDB Tagset Fieldset) ]);
no Moose;
__PACKAGE__->meta->make_immutable();
1;
Then, initialise the class, set the attributes and use data2line()
to retrieve
the line for submission to InfluxDB's HTTP API:
use SomeMeasurement;
my $measurement = SomeMeasurement->new({
measurement => 'Measurement name here',
my_tag => 'Some tag here',
my_field => -5.01,
my_both => 'Hello, world',
});
my $line = $measurement->data2line();
You can use several of Moose's built-in data types:
InfluxDB v2.0+ supports unsigned integers. Unsigned integers are a little more complex and require an extra trait:
has my_unsigned_integer => (is => 'ro', isa => 'Int', traits => [ qw(InfluxDB Fieldset Unsigned) ]);
Please note that attempting to use unsigned integers for older versions of InfluxDB breaks data submission with an "invalid number" error.
The generated line should be submitted to your InfluxDB API using either a client library such as InfluxDB::HTTP or a user agent like Mojo::UserAgent or LWP::UserAgent.