| 1 | package Redis::OpenTracing; | ||||||||||
| 2 | |||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 6 | ▶ | use syntax 'maybe'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 6 | our $VERSION = 'v0.3.0'; | |||||||||
| 9 | |||||||||||
| 10 | 6 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 6 | ▶ | use Types::Standard qw/HashRef Maybe Object Str Value is_Str/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 6 | ▶ | use OpenTracing::GlobalTracer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | ▶ | use Scalar::Util 'blessed'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 6 | ▶ | use Carp qw/croak/ ; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 6 | has 'redis' => ( | |||||||||
| 18 | is => 'ro', | ||||||||||
| 19 | isa => Object, # beyond current scope to detect if it is a Redis like client | ||||||||||
| 20 | required => 1, | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 6 | has '_redis_client_class_name' => ( | |||||||||
| 26 | is => 'lazy', | ||||||||||
| 27 | isa => Str, | ||||||||||
| 28 | ); | ||||||||||
| 29 | |||||||||||
| 30 | 4 | ▶ | sub _build__redis_client_class_name { | ||||||||
|
Subroutine
_build__redis_client_class_name: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 4 | blessed( shift->redis ) | |||||||||
| 32 | }; | ||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | 13 | ▶ | sub _operation_name { | ||||||||
|
Subroutine
_operation_name: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 13 | my ( $self, $method_name ) = @_; | |||||||||
| 38 | |||||||||||
| 39 | 13 | return $self->_redis_client_class_name . '::' . $method_name; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | |||||||||||
| 44 | has 'tags' => ( | ||||||||||
| 45 | is => 'ro', | ||||||||||
| 46 | isa => HashRef[Value], | ||||||||||
| 47 | 3 | ▶ | default => sub { {} }, # an empty HashRef | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 6 | ); | |||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | 6 | our $AUTOLOAD; # keep 'use strict' happy | |||||||||
| 53 | |||||||||||
| 54 | 13 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=1 SCAR=0.0
| |||||||||||
| 55 | 13 | my ($self) = @_; | |||||||||
| 56 | |||||||||||
| 57 | 13 | my $method_call = do { $_ = $AUTOLOAD; s/.*:://; $_ }; | |||||||||
| 58 | 13 | my $component_name = $self->_redis_client_class_name( ); | |||||||||
| 59 | 13 | my $db_statement = uc($method_call); | |||||||||
| 60 | 13 | my $operation_name = $self->_operation_name( $method_call ); | |||||||||
| 61 | |||||||||||
| 62 | 14 | ▶ | my $method_wrap = sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 63 | 14 | my $self = shift; | |||||||||
| 64 | my $scope = _global_tracer_start_active_span( | ||||||||||
| 65 | $operation_name, | ||||||||||
| 66 | tags => { | ||||||||||
| 67 | 'component' => $component_name, | ||||||||||
| 68 | |||||||||||
| 69 | 14 | %{ $self->tags( ) }, | |||||||||
| 70 | |||||||||||
| 71 | 'db.statement' => $db_statement, | ||||||||||
| 72 | 'db.type' => 'redis', | ||||||||||
| 73 | 'span.kind' => 'client', | ||||||||||
| 74 | |||||||||||
| 75 | }, | ||||||||||
| 76 | ); | ||||||||||
| 77 | |||||||||||
| 78 | 14 | my $result; | |||||||||
| 79 | 14 | my $wantarray = wantarray(); | |||||||||
| 80 | |||||||||||
| 81 | 14 | my $ok = eval { | |||||||||
| 82 | 14 | ▶ | if ($wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 3 | $result = [ $self->redis->$method_call(@_) ]; | |||||||||
| 84 | } else { | ||||||||||
| 85 | 11 | $result = $self->redis->$method_call(@_); | |||||||||
| 86 | }; | ||||||||||
| 87 | 13 | 1; | |||||||||
| 88 | }; | ||||||||||
| 89 | 14 | my $error = $@; | |||||||||
| 90 | |||||||||||
| 91 | 14 | ▶ | if ( $ok ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 13 | $scope->close() | |||||||||
| 93 | } else { | ||||||||||
| 94 | 1 | $scope->get_span()->add_tags( | |||||||||
| 95 | generate_error_tags( $db_statement, $error ) | ||||||||||
| 96 | ); | ||||||||||
| 97 | 1 | $scope->close(); | |||||||||
| 98 | 1 | croak $error; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 13 | ▶ | return $wantarray ? @$result : $result; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 13 | }; | |||||||||
| 103 | |||||||||||
| 104 | # Save this method for future calls | ||||||||||
| 105 | 6 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 106 | 13 | *$AUTOLOAD = $method_wrap; | |||||||||
| 107 | |||||||||||
| 108 | 13 | goto $method_wrap; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | 14 | ▶ | sub _global_tracer_start_active_span { | ||||||||
|
Subroutine
_global_tracer_start_active_span: called CC=1 SCAR=0.0
| |||||||||||
| 114 | 14 | my $operation_name = shift; | |||||||||
| 115 | 14 | my @args = @_; | |||||||||
| 116 | |||||||||||
| 117 | 14 | return OpenTracing::GlobalTracer->get_global_tracer()->start_active_span( | |||||||||
| 118 | $operation_name, | ||||||||||
| 119 | @args, | ||||||||||
| 120 | ); | ||||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | 1 | ▶ | sub generate_error_tags { | ||||||||
|
Subroutine
generate_error_tags: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 126 | 1 | my ( $db_statement, $error ) = @_; | |||||||||
| 127 | |||||||||||
| 128 | 1 | my $error_message = $error; | |||||||||
| 129 | 1 | chomp $error_message; | |||||||||
| 130 | |||||||||||
| 131 | 1 | my $error_kind = "REDIS_EXCEPTION"; | |||||||||
| 132 | # my $error_kind = sprintf("REDIS_EXCEPTION_%s", | ||||||||||
| 133 | # $db_statement, | ||||||||||
| 134 | # ); | ||||||||||
| 135 | # | ||||||||||
| 136 | return ( | ||||||||||
| 137 | 1 | 'error' => 1, | |||||||||
| 138 | 'message' => $error_message, | ||||||||||
| 139 | 'error.kind' => $error_kind, | ||||||||||
| 140 | ); | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | |||||||||||
| 144 | |||||||||||
| 145 | 0 | ▶ | sub DESTROY { } # we don't want this to be dispatched | ||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 146 | |||||||||||
| 147 | |||||||||||
| 148 | |||||||||||
| 149 | 6 | 1; | |||||||||