InfluxDB-Client-Simple
view release on metacpan or search on metacpan
lib/InfluxDB/Client/Simple.pm view on Meta::CPAN
}
=head2 send_data ($measurement, \%tags, \%fields, [$timestamp, [%options]])
Write data to the influxDB after converting them into LineProtocol format.
(call write() underneath)
$measurement is the name to be used for measurement
\%tags is the tag set associated to this datapoint
\%fields are the field set associated to this datapoint
$timestamp is an optional timestamp value expected to be in the database precision format (default is nanosecond)
\%options are also optional
%options can have the following keys:
=over
=item *
database - The database to be queried on the InfluxDB server
=item *
retention_policy - The retention policy to be used (if different from the default one)
=item *
precision - The precision used in the data (if diffectent from the default 'ns')
=back
Returns a hashref whose keys are:
=over
=item *
raw - The raw response from the server (obviously empty when using UDP)
=item *
error - The error message returned by the server (empty on success)
=back
=cut
sub send_data {
my $self = shift;
my $measurement = shift;
my $tags = shift;
my $fields = shift;
my $timestamp = shift;
my %options = @_;
return $self->write( _line_protocol( $measurement, $tags, $fields, $timestamp ), %options );
}
sub _get_influxdb_http_api_uri {
my ( $self, $endpoint ) = @_;
die "Missing argument 'endpoint'" if !$endpoint;
my $uri = URI->new();
$uri->scheme('http');
$uri->host( $self->{host} );
$uri->port( $self->{port} );
$uri->path($endpoint);
return $uri;
}
# Blatantly stolen from InfluxDB::LineProtocol
sub _format_value {
my $k = shift;
my $v = shift;
if ( $v =~ /^(-?\d+)(?:i?)$/ ) {
$v = $1 . 'i';
}
elsif ( $v =~ /^[Ff](?:ALSE|alse)?$/ ) {
$v = 'FALSE';
}
elsif ( $v =~ /^[Tt](?:RUE|rue)?$/ ) {
$v = 'TRUE';
}
elsif ( $v =~ /^-?\d+(?:\.\d+)?(?:e(?:-|\+)?\d+)?$/ ) {
# pass it on, no mod
}
else {
# string actually, but this should be quoted differently?
$v =~ s/(["\\])/\\$1/g;
$v = '"' . $v . '"';
}
return $v;
}
sub _line_protocol {
my $measurement = shift;
my $tags = shift;
my $fields = shift;
my $timestamp = shift;
# sort and encode (LineProtocol) tags
my @tags;
foreach my $k ( sort keys %$tags ) {
my $v = $tags->{$k};
next unless defined($v);
$k =~ s/([,\s])/\\$1/g;
$v =~ s/([,\s])/\\$1/g;
push( @tags, $k . '=' . $v );
}
my $tag_string = join( ',', @tags );
# sort and encode (LineProtocol) fields
my @fields;
foreach my $k ( sort keys %$fields ) {
my $v = $fields->{$k} || '';
my $esc_k = $k;
$esc_k =~ s/([,\s])/\\$1/g;
my $esc_v = _format_value( $k, $v );
push( @fields, $esc_k . '=' . $esc_v );
}
my $field_string = join( ',', @fields );
( run in 0.649 second using v1.01-cache-2.11-cpan-524268b4103 )