File Coverage

blib/lib/OpenTracing/GlobalTracer.pm
Criterion Covered Total %
statement 25 38 65.7
branch 3 16 18.7
condition 1 5 20.0
subroutine 6 9 66.6
pod 2 3 66.6
total 37 71 52.1


line stmt bran cond sub pod time code
1             package OpenTracing::GlobalTracer;
2              
3 1     1   121378 use strict;
  1         3  
  1         29  
4 1     1   6 use warnings;
  1         2  
  1         36  
5              
6             our $VERSION = '0.04';
7              
8 1     1   6 use Carp;
  1         2  
  1         46  
9 1     1   523 use Module::Load;
  1         1198  
  1         6  
10              
11             my $TRACER;
12              
13             my $GLOBALTRACER_DEFAULT =
14             $ENV{OPENTRACING_GLOBALTRACER_DEFAULT}
15             ||
16             'OpenTracing::Implementation::NoOp::Tracer';
17              
18             eval { load $GLOBALTRACER_DEFAULT };
19             carp "GlobalTracer can't find default implementation [$GLOBALTRACER_DEFAULT]"
20             if $@;
21              
22              
23              
24             sub import {
25 1     1   10 my $class = shift;
26            
27 1 50       5 croak "OpenTracing::GlobalTracer has too many parameters when loading"
28             if @_ > 1;
29            
30 1   50     6 my $injected_variable = shift // '$TRACER';
31             # Normally we'd expect the caller to provide a variable name - but if they
32             # don't, `$tracer` seems as good a default as any
33            
34 1 50       8 my ($bare_name) = $injected_variable =~ /^\$(\w+)$/
35             or croak "invalid injected variable name [$injected_variable]";
36            
37 1         4 my ($package_name) = caller;
38 1         27 my $fully_qualified_name = $package_name . '::' . $bare_name;
39             {
40 1     1   204 no strict 'refs';
  1         2  
  1         220  
  1         2  
41            
42             # This mostly does what we'd want if we're called at compiletime before
43             # any code actually tries to use the injected variable - but as soon as
44             # the compiler sees $SomeModule::tracer it'll happily tell the symbol
45             # table about it and trigger this check. Thus, it's currently disabled,
46             # and since Log::Any also skips the check it seems we're in good
47             # company.
48             #
49             # require B;
50             # croak "$package_name already has a variable called:$injected_variable"
51             # if B::svref_2object(\*$fully_qualified_name)->SV->$*;
52 1         6 *$fully_qualified_name = \$TRACER;
53             }
54            
55             carp "$fully_qualified_name installed"
56 1 50       5 if $ENV{OPENTRACING_DEBUG};
57            
58             return
59 1         13 }
60              
61              
62              
63             sub set_global_tracer {
64 0     0 1   my $class = shift;
65 0           my $tracer = shift;
66            
67             carp "Overwriting existing GlobalTracer"
68 0 0         if $ENV{OPENTRACING_DEBUG};
69             carp "GlobalTracer is cleared."
70 0 0 0       if $ENV{OPENTRACING_DEBUG} and not defined $tracer;
71            
72 0           $TRACER = $tracer;
73            
74             return
75 0           }
76              
77              
78              
79             sub get_global_tracer {
80            
81 0 0   0 1   return $TRACER
82             if defined $TRACER;
83            
84             carp "GlobalTracer is using defaulted implementation"
85 0 0         if $ENV{OPENTRACING_DEBUG};
86            
87 0           my $tracer = eval { $GLOBALTRACER_DEFAULT->new( ) };
  0            
88 0 0         croak "GlobalTracer can't instantiate tracer [$GLOBALTRACER_DEFAULT]"
89             if $@;
90            
91 0           return $tracer
92             }
93              
94              
95              
96 0     0 0   sub is_registered { defined $TRACER }
97              
98              
99              
100             1;