ForgeOps-Tracker

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

readings and falls off the end needs nothing more (a test runs exactly that in a child `perl`); call
`flush_metrics()` if it might exit another way (`POSIX::_exit`). Every entry is stored as it was
captured (a signup is a row, not a running total), so a count or sum you compute later is exact. Both
are a no-op when the client isn't enabled for the environment.

A delivery that fails for now (a network error, a timeout, a 5xx, a 408 or a 429) keeps every entry
for the next flush, and an entry captured while a delivery is in flight is kept too (a test pins this
with a gated delivery on a second thread). A batch the server rejects (any other 4xx, such as the 403
a plan without the feature answers) is dropped rather than sent again, and so is one that has failed
5 flushes in a row. The buffer holds at most 1000 entries per kind and drops further ones until a
flush lets go of them. A NaN, infinite or non-numeric value is dropped at capture. Requires a ForgeOps
plan that includes custom metrics / infrastructure monitoring.

## What changed

ForgeOps can show what changed in your system next to the errors and slowdowns that followed it.
Two ways in:

**Record a change yourself** when something changes that no deploy captures, like a feature flag
flipped, a config value edited, or a migration run by hand:

lib/ForgeOps/Tracker/MetricBuffer.pm  view on Meta::CPAN

# Three deliberate differences from the Ruby buffers:
#
# - A flush snapshots the first N entries and, on success, removes exactly those N, instead of
#   resetting the whole list, so an entry recorded while the request is in flight (the lock is not
#   held around the network call) is kept for the next flush rather than lost.
# - The buffer is capped at MAX_ENTRIES, and once full further entries are dropped until a flush
#   lets go of them: an unreachable server fails every flush, and an uncapped buffer would keep
#   growing until the batch is given up on (see DeliveryStatus::settle). Dropping the newest rather than the oldest keeps the
#   entries a flush is delivering at the front of the list, which is what makes removing exactly those
#   afterward exact.
# - A NaN or infinite value is dropped at record time: JSON::PP would encode it as the invalid JSON
#   token NaN/Inf and the server would reject the whole batch.
#
# Perl's ithreads share nothing by default (see PerformanceFlusher's own comment), so the entries live
# in a threads::shared array of JSON strings: a flat shared array sidesteps sharing nested structures.
# The background thread is started lazily on the first record, like DeliveryQueue's. An END block
# flushes whatever is left when the program ends normally, which is what a short-lived cron script that
# captures a few readings and falls off the end relies on.
use constant MAX_ENTRIES => 1000;

my @live_buffers;

t/metric_buffer.t  view on Meta::CPAN

    );
    ok($buffer->record({ metric_name => 'signup', value => 1 }));
    ok($buffer->record({ metric_name => 'payment', value => 49.5 }));
    $buffer->flush;

    is_deeply([@delivered], [2]);
    like($seen_stamp, qr/\A\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ\z/);
    $buffer->_clear;
};

subtest 'keeps a negative value, drops NaN, infinite and non-numeric ones' => sub {
    my (@delivered, @outcomes) :shared;
    my $buffer = recording_buffer(\@delivered, \@outcomes);
    ok($buffer->record({ metric_name => 'refund', value => -12 }), 'a refund is a real metric');
    ok(!$buffer->record({ metric_name => 'nan', value => 'NaN' }));
    ok(!$buffer->record({ metric_name => 'inf', value => 'Inf' }));
    ok(!$buffer->record({ metric_name => 'str', value => 'twelve' }));
    ok(!$buffer->record({ metric_name => 'undef', value => undef }));
    ok($buffer->record({ metric_name => 'sci', value => '1.5e3' }));
    is($buffer->count, 2);
    $buffer->_clear;
};

subtest 'a failed delivery keeps every entry for the next flush' => sub {
    my (@delivered, @outcomes) :shared;



( run in 1.214 second using v1.01-cache-2.11-cpan-60884a8df76 )