File Coverage

blib/lib/OpenTracing/Any.pm
Criterion Covered Total %
statement 27 27 100.0
branch 4 4 100.0
condition n/a
subroutine 7 7 100.0
pod n/a
total 38 38 100.0


line stmt bran cond sub pod time code
1             package OpenTracing::Any;
2              
3 2     2   468 use strict;
  2         5  
  2         51  
4 2     2   8 use warnings;
  2         3  
  2         84  
5              
6             our $VERSION = '1.003'; # VERSION
7             our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8              
9 2     2   414 no indirect;
  2         934  
  2         12  
10 2     2   609 use utf8;
  2         15  
  2         12  
11              
12             =encoding utf8
13              
14             =head1 NAME
15              
16             OpenTracing::Any - application tracing
17              
18             =head1 SYNOPSIS
19              
20             use OpenTracing::Any qw($tracer);
21             {
22             my $span = $tracer->span(operation_name => 'whatever');
23             $span->add_tag(xyz => 'abc');
24             sleep 3;
25             }
26             # at this point the span will be finished and have an approximate timing of ~3s
27              
28             =head1 DESCRIPTION
29              
30             This provides a tracer to the current package. By default it will be given the
31             package variable name C<< $tracer >>, but you can override this by providing a
32             different name:
33              
34             use OpenTracing::Any qw($renamed_tracer_variable);
35             $renamed_tracer_variable->span(...);
36              
37             See L for more details on available methods.
38              
39             See also: L.
40              
41             =cut
42              
43 2     2   763 use OpenTracing;
  2         8  
  2         266  
44              
45             sub import {
46 7     7   14573 my ($class, @args) = @_;
47 7 100       34 die 'too many parameters when loading OpenTracing::Any - expects a single variable name'
48             if @args > 1;
49              
50             # Normally we'd expect the caller to provide a variable name - but if they don't,
51             # '$tracer' seems as good a default as any
52 6         12 my ($injected_variable) = (@args, '$tracer');
53 6 100       54 my ($bare_name) = $injected_variable =~ /^\$(\w+)$/
54             or die 'invalid injected variable name ' . $injected_variable;
55              
56 5         12 my ($pkg) = caller;
57 5         14 my $fully_qualified = $pkg . '::' . $bare_name;
58             {
59 2     2   15 no strict 'refs';
  2         3  
  2         168  
  5         8  
60             # This mostly does what we'd want if we're called at compiletime before any code actually tries
61             # to use the injected variable - but as soon as the compiler sees $SomeModule::tracer it'll happily
62             # tell the symbol table about it and trigger this check. Thus, it's currently disabled, and
63             # since Log::Any also skips the check it seems we're in good company.
64             # require B;
65             # die $pkg . ' already has a variable called ' . $injected_variable if B::svref_2object(\*$fully_qualified)->SV->$*;
66 5         20 *{$fully_qualified} = \(OpenTracing->global_tracer());
  5         282  
67             }
68             }
69              
70             1;
71              
72             __END__