Metrics-Any

 view release on metacpan or  search on metacpan

lib/Metrics/Any/AdapterBase/Stored.pm  view on Meta::CPAN

{
   my $self = shift;
   my ( $type, $handle ) = @_;

   my $metric = $self->{$handle};
   $metric->{type} eq $type or
      croak "$handle is not a $type metric";

   return $metric;
}

sub _labelset
{
   my $self = shift;
   my ( $handle, @labelvalues ) = @_;

   my $metric = $self->{$handle};

   my $labels = $metric->{labels} or return "";

   return join "\0", map { "$labels->[$_]:$labelvalues[$_]" } 0 .. $#$labels;
}

=head2 walk

   $stored->walk( $code )

      $code->( $type, $name, $labels, $value )

Given a CODE reference, this method invokes it once per labelset of every
stored metric.

For each labelset, C<$type> will give the metric type (as a string, either
C<counter>, C<distribution>, C<gauge> or C<timer>), C<$name> gives the name
it was registered with, C<$labels> will be a reference to an even-sized array
containing label names and values.

For counter and gauge metrics, C<$value> will be a numerical scalar giving the
current value. For distribution and timer metrics, C<$value> will be whatever
the implementing class's corresponding C<store_distribution> or C<store_timer>
method returns for them.

=cut

sub walk
{
   my $self = shift;
   my ( $code ) = @_;

   if( my $cbs = $self->{"\0batch_callbacks"} ) {
      foreach my $cb ( @$cbs ) { $cb->() }
   }

   foreach my $handle ( sort keys %$self ) {
      next if $handle =~ m/^\0/;

      my $metric = $self->{$handle};
      my $values = $metric->{values};

      foreach my $labelset ( sort keys %$values ) {
         my @labels = map { split m/:/, $_, 2 } split m/\0/, $labelset;

         $code->( $metric->{type}, $metric->{name}, \@labels, $values->{$labelset} );
      }
   }
}

=head2 clear_values

   $stored->clear_values

Clears all of the metric storage. Every labelset of every metric is deleted.
The metric definitions themselves remain.

=cut

sub clear_values
{
   my $self = shift;

   $_->{values} = {} for values %$self;
}

sub make_counter { shift->_make( counter => @_ ) }

sub inc_counter_by
{
   my $self = shift;
   my ( $handle, $amount, @labelvalues ) = @_;

   my $metric = $self->_metric( counter => $handle );

   $metric->{values}{ $self->_labelset( $handle, @labelvalues ) } += $amount;
}

sub make_distribution { shift->_make( distribution => @_ ) }

sub report_distribution
{
   my $self = shift;
   my ( $handle, $amount, @labelvalues ) = @_;

   my $metric = $self->_metric( distribution => $handle );

   my $values = $metric->{values};
   my $key = $self->_labelset( $handle, @labelvalues );

   $values->{$key} = $self->store_distribution( $values->{$key}, $amount );
}

sub make_gauge { shift->_make( gauge => @_ ) }

sub inc_gauge_by
{
   my $self = shift;
   my ( $handle, $amount, @labelvalues ) = @_;

   my $metric = $self->_metric( gauge => $handle );

   $metric->{values}{ $self->_labelset( $handle, @labelvalues ) } += $amount;
}



( run in 0.721 second using v1.01-cache-2.11-cpan-71847e10f99 )