This file is indexed.

/usr/share/perl5/Net/Statsd.pm is in libnet-statsd-perl 0.12-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package Net::Statsd;
{
  $Net::Statsd::VERSION = '0.12';
}

# ABSTRACT: Perl client for Etsy's statsd daemon

use strict;
use warnings;
use Carp ();
use IO::Socket ();

our $HOST = 'localhost';
our $PORT = 8125;

my $SOCK;
my $SOCK_PEER;



sub timing {
    my ($name, $time, $sample_rate) = @_;

    if (! defined $sample_rate) {
        $sample_rate = 1;
    }

    my $stats = {
        $name => sprintf "%d|ms", $time
    };

    return Net::Statsd::send($stats, $sample_rate);
}


sub increment {
    my ($stats, $sample_rate) = @_;

    return Net::Statsd::update_stats($stats, 1, $sample_rate);
}

*inc = *increment;


sub decrement {
    my ($stats, $sample_rate) = @_;

    return Net::Statsd::update_stats($stats, -1, $sample_rate);
}

*dec = *decrement;


sub update_stats {
    my ($stats, $delta, $sample_rate) = @_;

    if (! defined $delta) {
        $delta = 1;
    }

    if (! defined $sample_rate) {
        $sample_rate = 1;
    }

    if (! ref $stats) {
        $stats = [ $stats ];
    }
    elsif (ref $stats eq 'HASH') {
        Carp::croak("Usage: update_stats(\$str, ...) or update_stats(\\\@list, ...)");
    }

    my %data = map { $_ => sprintf "%s|c", $delta } @{ $stats };

    return Net::Statsd::send(\%data, $sample_rate)
}


sub gauge {
    my $stats = {};

    while (my($name, $value) = splice(@_, 0, 2)) {
        $value = 0 unless defined $value;
        # Didn't use '%d' because values might be floats
        push @{ $stats->{$name} }, sprintf("%s|g", $value);
    }

    return Net::Statsd::send($stats, 1);
}


sub send {
    my ($data, $sample_rate) = @_;

    my $sampled_data = _sample_data($data, $sample_rate);

    # No sampled_data can happen when:
    # 1) No $data came in
    # 2) Sample rate was low enough that we don't want to send events
    if (! $sampled_data) {
        return;
    }

    # Cache the socket to avoid dns and socket creation overheads
    # (this boosts performance from ~6k to >60k sends/sec)
    if (!$SOCK || !$SOCK_PEER || "$HOST:$PORT" ne $SOCK_PEER) {

        $SOCK = IO::Socket::INET->new(
            Proto    => 'udp',
            PeerAddr => $HOST,
            PeerPort => $PORT,
        ) or do {
            Carp::carp("Net::Statsd can't create a socket to $HOST:$PORT: $!")
                unless our $_warn_once->{"$HOST:$PORT"}++;
            return
        };
        $SOCK_PEER = "$HOST:$PORT";

        # We don't want to die if Net::Statsd::send() doesn't work...
        # We could though:
        #
        # or die "Could not create UDP socket: $!\n";
    }

    my $all_sent = 1;

    keys %{ $sampled_data }; # reset iterator
    while (my ($stat, $value) = each %{ $sampled_data }) {
        my $packet;
        if (ref $value eq 'ARRAY') {
            # https://github.com/etsy/statsd/blob/master/docs/metric_types.md#multi-metric-packets
            $packet = join("\n", map { "$stat:$_" } @{ $value });
        }
        else {
            # Single value as scalar
            $packet = "$stat:$value";
        }
        # send() returns the number of characters sent, or undef on error.
        my $r = CORE::send($SOCK, $packet, 0);
        if (!defined $r) {
            #warn "Net::Statsd send error: $!";
            $all_sent = 0;
        }
        elsif ($r != length($packet)) {
            #warn "Net::Statsd send truncated: $!";
            $all_sent = 0;
        }
    }

    return $all_sent;
}


sub _sample_data {
    my ($data, $sample_rate) = @_;

    if (! $data || ref $data ne 'HASH') {
        Carp::croak("No data?");
    }

    if (! defined $sample_rate) {
        $sample_rate = 1;
    }

    # Sample rate > 1 doesn't make sense though
    if ($sample_rate >= 1) {
        return $data;
    }

    my $sampled_data;

    # Perform sampling here, so that clients using Net::Statsd
    # don't have to do it every time. This is the same
    # implementation criteria used in the other statsd client libs
    #
    # If rand() doesn't trigger, then no data will be sent
    # to the statsd server, which is what we want.

    if (rand() <= $sample_rate) {
        while (my ($stat, $value) = each %{ $data }) {
            # Uglier, but if there's no data to be sampled,
            # we get a clean undef as returned value
            $sampled_data ||= {};

            # Multi-metric packet:
            # https://github.com/etsy/statsd/blob/master/docs/metric_types.md#multi-metric-packets
            if (ref $value eq 'ARRAY') {
                foreach my $v ( @{ $value } ) {
                    push @{ $sampled_data->{$stat} }, sprintf("%s|@%s", $v, $sample_rate);
                }
            }
            # Single value as scalar
            else {
                $sampled_data->{$stat} = sprintf "%s|@%s", $value, $sample_rate;
            }
        }
    }

    return $sampled_data;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Net::Statsd - Perl client for Etsy's statsd daemon

=head1 VERSION

version 0.12

=head1 SYNOPSIS

    # Configure where to send events
    # That's where your statsd daemon is listening.
    $Net::Statsd::HOST = 'localhost';    # Default
    $Net::Statsd::PORT = 8125;           # Default

    #
    # Keep track of events as counters
    #
    Net::Statsd::increment('site.logins');
    Net::Statsd::increment('database.connects');

    #
    # Log timing of events, ex. db queries
    #
    use Time::HiRes;
    my $start_time = [ Time::HiRes::gettimeofday ];

    # do the complex database query
    # note: time value sent to timing should
    # be in milliseconds.
    Net::Statsd::timing(
        'database.complexquery',
        Time::HiRes::tv_interval($start_time) * 1000
    );

    #
    # Log metric values
    #
    Net::Statsd::gauge('core.temperature' => 55);

=head1 DESCRIPTION

This module implement a UDP client for the B<statsd> statistics
collector daemon in use at Etsy.com.

You want to use this module to track statistics in your Perl
application, such as how many times a certain event occurs
(user logins in a web application, or database queries issued),
or you want to time and then graph how long certain events take,
like database queries execution time or time to download a
certain file, etc...

If you're uncertain whether you'd want to use this module or
statsd, then you can read some background information here:

    http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/

The github repository for statsd is:

    http://github.com/etsy/statsd

By default the client will try to send statistic metrics to
C<localhost:8125>, but you can change the default hostname and port
with:

    $Net::Statsd::HOST = 'your.statsd.hostname.net';
    $Net::Statsd::PORT = 9999;

just after including the C<Net::Statsd> module.

=head1 ABOUT SAMPLING

A note about sample rate: A sample rate of < 1 instructs this
library to send only the specified percentage of the samples to
the server. As such, the application code should call this module
for every occurence of each metric and allow this library to
determine which specific measurements to deliver, based on the
sample_rate value. (e.g. a sample rate of 0.5 would indicate that
approximately only half of the metrics given to this module would
actually be sent to statsd).

=head1 FUNCTIONS

=head2 C<timing($name, $time, $sample_rate = 1)>

Log timing information.
B<Time is assumed to be in milliseconds (ms)>.

    Net::Statsd::timing('some.timer', 500);

=head2 C<increment($counter, $sample_rate=1)>

=head2 C<increment(\@counter, $sample_rate=1)>

Increments one or more stats counters

    # +1 on 'some.int'
    Net::Statsd::increment('some.int');

    # 0.5 = 50% sampling
    Net::Statsd::increment('some.int', 0.5);

To increment more than one counter at a time,
you can B<pass an array reference>:

    Net::Statsd::increment(['grue.dinners', 'room.lamps'], 1);

B<You can also use "inc()" instead of "increment()" to type less>.

=head2 C<decrement($counter, $sample_rate=1)>

Same as increment, but decrements. Yay.

    Net::Statsd::decrement('some.int')

B<You can also use "dec()" instead of "decrement()" to type less>.

=head2 C<update_stats($stats, $delta=1, $sample_rate=1)>

Updates one or more stats counters by arbitrary amounts

    Net::Statsd::update_stats('some.int', 10)

equivalent to:

    Net::Statsd::update_stats('some.int', 10, 1)

A sampling rate less than 1 means only update the stats
every x number of times (0.1 = 10% of the times).

=head2 C<gauge($name, $value)>

Log arbitrary values, as a temperature, or server load.

    Net::Statsd::gauge('core.temperature', 55);

Statsd interprets gauge values with C<+> or C<-> sign as increment/decrement.
Therefore, to explicitly set a gauge to a negative number it has to be set
to zero first.

However, if either the zero or the actual negative value is lost in UDP
transport to statsd server because of e.g. network congestion or packet loss,
your gauge will become skewed.

To ensure network problems will not skew your data, C<Net::Statsd::gauge()>
supports packing multiple values in single UDP packet sent to statsd:

    Net::Statsd::gauge(
        'core.temperature' => 55,
        'freezer.temperature' => -18
    );

Make sure you don't supply too many values, or you might risk exceeding the
MTU of the network interface and cause the resulting UDP packet to be dropped.

In general, a safe limit should be 512 bytes. Related to the example
above, C<core.temperature> of 55 will be likely packed as a string:

    core.temperature:55|g

which is 21 characters, plus a newline used as delimiter (22).
Using this example, you can pack at least 20 distinct gauge values without
problems. That will result in a UDP message of 440 bytes (22 times 20),
which is well below the I<safe> threshold of 512.

In reality, if the communication happens on a local interface, or over
a 10G link, you are allowed much more than that.

=head2 C<send(\%data, $sample_rate = 1)>

Squirt the metrics over UDP.

    Net::Statsd::send({ 'some.int' => 1 });

=head2 C<_sample_data(\%data, $sample_rate = 1)>

B<This method is used internally, it's not part of the public interface.>

Takes care of transforming a hash of metrics data into
a B<sampled> hash of metrics data, according to the given
C<$sample_rate>.

If C<$sample_rate == 1>, then sampled data is exactly the
incoming data.

If C<$sample_rate = 0.2>, then every metric value will be I<marked>
with the given sample rate, so the Statsd server will automatically
scale it. For example, with a sample rate of 0.2, the metric values
will be multiplied by 5.

=head1 AUTHOR

Cosimo Streppone <cosimo@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Cosimo Streppone.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut