File Coverage

blib/lib/Redis/OpenTracing.pm
Criterion Covered Total %
statement 42 42 100.0
branch n/a
condition n/a
subroutine 12 13 92.3
pod n/a
total 54 55 98.1


line stmt bran cond sub pod time code
1             package Redis::OpenTracing;
2              
3 6     6   2193886 use strict;
  6         55  
  6         178  
4 6     6   74 use warnings;
  6         18  
  6         165  
5              
6 6     6   1768 use syntax 'maybe';
  6         90875  
  6         65  
7              
8             our $VERSION = 'v0.2.2';
9              
10 6     6   17688 use Moo;
  6         28587  
  6         35  
11 6     6   9229 use Types::Standard qw/HashRef Maybe Object Str Value is_Str/;
  6         489838  
  6         113  
12              
13 6     6   20629 use OpenTracing::AutoScope;
  6         75466  
  6         219  
14 6     6   53 use Scalar::Util 'blessed';
  6         18  
  6         2216  
15              
16              
17              
18             has 'redis' => (
19             is => 'ro',
20             isa => Object, # beyond current scope to detect if it is a Redis like client
21             required => 1,
22             );
23              
24              
25              
26             has '_redis_client_class_name' => (
27             is => 'lazy',
28             isa => Str,
29             );
30              
31             sub _build__redis_client_class_name {
32 4     4   164 blessed( shift->redis )
33             };
34              
35              
36              
37             sub _operation_name {
38 12     12   57 my ( $self, $method_name ) = @_;
39            
40 12         200 return $self->_redis_client_class_name . '::' . $method_name;
41             }
42              
43              
44              
45             has 'tags' => (
46             is => 'ro',
47             isa => HashRef[Value],
48             default => sub { {} }, # an empty HashRef
49             );
50              
51              
52              
53             our $AUTOLOAD; # keep 'use strict' happy
54              
55             sub AUTOLOAD {
56 12     12   31502 my ($self) = @_;
57            
58 12         24 my $method_call = do { $_ = $AUTOLOAD; s/.*:://; $_ };
  12         46  
  12         80  
  12         33  
59 12         234 my $component_name = $self->_redis_client_class_name( );
60 12         254 my $db_statement = uc($method_call);
61 12         38 my $operation_name = $self->_operation_name( $method_call );
62            
63             my $method_wrap = sub {
64 13     13   867 my $self = shift;
65             OpenTracing::AutoScope->start_guarded_span(
66             $operation_name,
67             tags => {
68             'component' => $component_name,
69            
70 13         36 %{ $self->tags( ) },
  13         138  
71            
72             'db.statement' => $db_statement,
73             'db.type' => 'redis',
74             'span.kind' => 'client',
75            
76             },
77             );
78            
79 13         28895 return $self->redis->$method_call(@_);
80 12         152 };
81            
82             # Save this method for future calls
83 6     6   56 no strict 'refs';
  6         21  
  6         574  
84 12         48 *$AUTOLOAD = $method_wrap;
85            
86 12         33 goto $method_wrap;
87             }
88              
89              
90              
91       0     sub DESTROY { } # we don't want this to be dispatched
92              
93              
94              
95             1;