Metrics-Any
view release on metacpan or search on metacpan
lib/Metrics/Any/Collector.pm view on Meta::CPAN
}
Because not every adapter may implement this mode, instrumented code should be
prepared to fall back on the regular API to report its counters.
my $evcounter = 0;
my $use_batch = $metrics->add_batch_mode_callback( sub {
$metrics->inc_counter_by( events => $evcounter );
$evcounter = 0;
} );
sub do_thing {
$use_batch ? $evcounter++ : $metrics->inc_counter( events => );
...
}
Each adapter implementation should document if and how it handles batch mode.
=head1 ENVIRONMENT
=head2 METRICS_ANY_DISABLE
I<Since version 0.07.>
Provides a list of packages and namespaces in which to disable L<Metrics::Any>
reporting entirely.
This variable gives a comma-separated list of name patterns. Patterns may end
with C<::*>, where they will match any package whose name starts with that
prefix, or they may be literal package names. If any code in matching packages
attempts to use L<Metrics::Any::Collector> to report metrics, that code will
be given a C<Null> adapter, and no metrics will be reported from here.
For example, to disable the metrics that C<Net::Async::HTTP::Server> itself
creates when exporting Prometheus metrics:
$ METRICS_ANY_DISABLE=Net::Async::HTTP::Server ./program.pl
=cut
# Not public API; used by Metrics::Any::import_into
sub new
{
my $class = shift;
my ( $package, %args ) = @_;
return bless {
package => $package,
adapter => undef,
deferred => [],
name_prefix => $args{name_prefix},
metrics => {},
strict => $args{strict} // 1,
}, $class;
}
my %disable_for_package;
my %disable_for_namespace;
if( my $val = $ENV{METRICS_ANY_DISABLE} ) {
foreach my $pattern ( split m/,/, $val ) {
if( $pattern =~ s/\*$// ) {
$pattern =~ s/::$//;
$disable_for_namespace{$pattern} = 1;
}
else {
$disable_for_package{$pattern} = 1;
}
}
require Metrics::Any::Adapter::Null;
}
sub _enabled_for_package
{
my ( $pkg ) = @_;
return 0 if $disable_for_package{$pkg};
return 1 unless %disable_for_namespace;
do {
return 0 if $disable_for_namespace{$pkg};
} while( $pkg =~ s/::[^:]+// );
return 1;
}
sub adapter
{
my $self = shift;
return $self->{adapter} if $self->{adapter};
my $adapter = $self->{adapter} =
( _enabled_for_package( $self->{package} ) ? Metrics::Any::Adapter->adapter
: Metrics::Any::Adapter::Null->new );
foreach my $call ( @{ $self->{deferred} } ) {
my ( $method, @args ) = @$call;
$adapter->$method( @args );
}
undef $self->{deferred};
return $adapter;
}
sub _adapter_call
{
my $self = shift;
my ( $method, @args ) = @_;
if( $self->{adapter} ) {
$self->{adapter}->$method( @args );
}
else {
push @{ $self->{deferred} }, [ $method, @args ];
}
}
sub _metricname
{
my $self = shift;
my ( $suffix ) = @_;
( run in 0.701 second using v1.01-cache-2.11-cpan-71847e10f99 )