1#!/usr/bin/perl
2
3use v5.14;  # package NAME {BLOCK}
4use warnings;
5
6use Test::More;
7
8use Metrics::Any '$metrics';
9
10package Metrics::Any::Adapter::localTestAdapter {
11   use base "Metrics::Any::AdapterBase::Stored";
12
13   # push to an array
14   sub store_distribution
15   {
16      my ( undef, $storage, $amount ) = @_;
17      push @$storage, $amount;
18      return $storage;
19   }
20
21   # summarize
22   sub store_timer
23   {
24      my ( undef, $storage, $duration ) = @_;
25      $storage->{total} += $duration;
26      $storage->{count} += 1;
27      return $storage;
28   }
29}
30
31use Metrics::Any::Adapter 'localTestAdapter';
32
33{
34   $metrics->make_counter( counter => );
35   $metrics->make_distribution( distribution => );
36   $metrics->make_gauge( gauge => );
37   $metrics->make_timer( timer => );
38
39   $metrics->inc_counter( counter => );
40
41   $metrics->report_distribution( distribution => 1 );
42   $metrics->report_distribution( distribution => 3 );
43   $metrics->report_distribution( distribution => 5 );
44
45   $metrics->inc_gauge_by( gauge => 5 );
46
47   $metrics->report_timer( timer => 2 );
48   $metrics->report_timer( timer => 4 );
49
50   # Also test labels
51
52   $metrics->make_counter( labeled_counter =>
53      labels => [qw( x y )],
54   );
55   $metrics->inc_counter( labeled_counter => { x => 10, y => 20 } );
56
57   my @walkdata;
58   Metrics::Any::Adapter->adapter->walk( sub {
59      my ( $type, $name, $labels, $value ) = @_;
60      push @walkdata, [ $type, $name, $labels, $value ];
61   } );
62
63   is_deeply( \@walkdata, [
64      [ counter => counter => [], 1 ],
65
66      [ distribution => distribution => [], [ 1, 3, 5 ] ],
67
68      [ gauge => gauge => [], 5 ],
69
70      [ counter => labeled_counter => [ x => 10, y => 20 ], 1 ],
71
72      [ timer => timer => [], { total => 6, count => 2 } ],
73   ], 'metrics to ->walk' );
74}
75
76done_testing;
77