| 1 | package OpenTracing::Role::Span; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 15 | our $VERSION = 'v0.80.0-TRIAL_006'; | |||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 15 | ▶ | use Moo::Role; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 15 | ▶ | use MooX::HandlesVia; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 15 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 15 | ▶ | use OpenTracing::Types qw/:types :is/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 15 | ▶ | use Time::HiRes qw/time/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 15 | ▶ | use Types::Standard qw/CodeRef HashRef Maybe Num Object Str Value/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 15 | ▶ | use Types::Common::Numeric qw/PositiveOrZeroNum/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 15 | has operation_name => ( | |||||||||||||||||||
| 17 | is => 'rwp', | ||||||||||||||||||||
| 18 | isa => Str, | ||||||||||||||||||||
| 19 | required => 1, | ||||||||||||||||||||
| 20 | # writer => 'overwrite_operation_name', | ||||||||||||||||||||
| 21 | reader => 'get_operation_name', # it's not in the Interface | ||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | has start_time => ( | ||||||||||||||||||||
| 25 | is => 'ro', | ||||||||||||||||||||
| 26 | isa => PositiveOrZeroNum, | ||||||||||||||||||||
| 27 | 17 | ▶ | default => sub { epoch_floatingpoint() } | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 15 | ); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 15 | has finish_time => ( | |||||||||||||||||||
| 31 | is => 'rwp', | ||||||||||||||||||||
| 32 | isa => PositiveOrZeroNum, | ||||||||||||||||||||
| 33 | predicate => 'has_finished', | ||||||||||||||||||||
| 34 | init_arg => undef, | ||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | has tags => ( | ||||||||||||||||||||
| 38 | is => 'rwp', | ||||||||||||||||||||
| 39 | isa => HashRef[Value], | ||||||||||||||||||||
| 40 | handles_via => 'Hash', | ||||||||||||||||||||
| 41 | handles => { | ||||||||||||||||||||
| 42 | get_tags => 'all', | ||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||
| 44 | 14 | ▶ | default => sub{ {} }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | 15 | ); | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 15 | has context => ( | |||||||||||||||||||
| 48 | is => 'ro', | ||||||||||||||||||||
| 49 | isa => SpanContext, | ||||||||||||||||||||
| 50 | reader => 'get_context', | ||||||||||||||||||||
| 51 | # writer => '_set_context', | ||||||||||||||||||||
| 52 | required => 1, # either from Span->get_context or SpanContext self | ||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | sub overwrite_operation_name { | ||||||||||||||||||||
| 56 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
overwrite_operation_name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 2 | ▶ | croak "Can't overwrite an operation-name on an already finished span" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | if $self->has_finished; | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 1 | my $operation_name = shift; # or throw an exception | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 1 | $self->_set_operation_name( $operation_name ); | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 1 | return $self | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | sub finish { | ||||||||||||||||||||
| 69 | 31 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
finish: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 31 | ▶ | croak "Span has already been finished" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | if $self->has_finished; | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 30 | ▶ | my $epoch_timestamp = shift // epoch_floatingpoint(); | ||||||||||||||||||
|
Condition66%2/3
shift() // epoch_floatingpoint()
MC/DC0%0/2
shift() // epoch_floatingpoint()
shift()
epoch_floatingpoint()
Truth table66%2/3
A // B
A= shift()B= epoch_floatingpoint()
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 30 | $self->_set_finish_time( $epoch_timestamp ); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 30 | ▶ | $self->on_finish->( $self ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | if $self->has_on_finish; | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 30 | return $self | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | sub add_tag { | ||||||||||||||||||||
| 85 | 3 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
add_tag: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 3 | ▶ | croak "Can't set a tag on an already finished span" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | if $self->has_finished; | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 2 | my $key = shift; | |||||||||||||||||||
| 91 | 2 | my $value = shift; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 2 | $self->add_tags( $key => $value ); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 2 | return $self | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | sub add_tags { | ||||||||||||||||||||
| 99 | 3 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
add_tags: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 3 | ▶ | croak "Can't set a tag on an already finished span" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | if $self->has_finished; | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 3 | my %tags = @_; | |||||||||||||||||||
| 105 | 3 | $self->_set_tags( | |||||||||||||||||||
| 106 | { $self->get_tags, %tags } | ||||||||||||||||||||
| 107 | ); | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 3 | return $self | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | sub log_data { | ||||||||||||||||||||
| 113 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
log_data: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 2 | ▶ | croak "Can't log any more data on an already finished span" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | if $self->has_finished; | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 1 | my %log_data = @_; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # ... # shall we just use Log::Any ? | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 1 | return $self | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | sub add_baggage_item { | ||||||||||||||||||||
| 126 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
add_baggage_item: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 2 | ▶ | croak "Can't set baggage-items on an already finished span" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 129 | if $self->has_finished; | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 1 | my $key = shift; | |||||||||||||||||||
| 132 | 1 | my $value = shift; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 1 | $self->add_baggage_items( $key => $value ); | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 1 | return $self | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | sub add_baggage_items { | ||||||||||||||||||||
| 140 | 3 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
add_baggage_items: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 3 | ▶ | croak "Can't set baggage-items on an already finished span" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | if $self->has_finished; | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 2 | my %items = @_; | |||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 2 | my $new_context = $self->get_context()->with_baggage_items( %items ); | |||||||||||||||||||
| 148 | 2 | $self->_set_context( $new_context ); | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 2 | return $self | |||||||||||||||||||
| 151 | } | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | sub get_baggage_item { | ||||||||||||||||||||
| 154 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
get_baggage_item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 155 | 2 | my $key = shift; | |||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | 2 | return $self->get_context()->get_baggage_item( $key ) | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | sub get_baggage_items { | ||||||||||||||||||||
| 161 | 1 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
get_baggage_items: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 1 | return $self->get_context()->get_baggage_items | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | sub duration { | ||||||||||||||||||||
| 167 | 4 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
duration: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | my $start_time = $self->{ start_time } | ||||||||||||||||||||
| 170 | 4 | ▶ | or croak | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->get_operation_name || q['undef']
MC/DC0%0/1
$self->get_operation_name || q['undef']
$self->get_operation_name
Truth table50%1/2
A
A= $self->get_operation_name
| |||||||||||||||||||||
| 171 | "Span has not been started: ['" | ||||||||||||||||||||
| 172 | . | ||||||||||||||||||||
| 173 | ( $self->get_operation_name || "'undef'" ) | ||||||||||||||||||||
| 174 | . | ||||||||||||||||||||
| 175 | "'] ... how did you do that ?"; | ||||||||||||||||||||
| 176 | my $finish_time = $self->{ finish_time } | ||||||||||||||||||||
| 177 | 3 | ▶ | or croak | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->get_operation_name || q['undef']
MC/DC0%0/1
$self->get_operation_name || q['undef']
$self->get_operation_name
Truth table50%1/2
A
A= $self->get_operation_name
| |||||||||||||||||||||
| 178 | "Span has not been finished: ['" | ||||||||||||||||||||
| 179 | . | ||||||||||||||||||||
| 180 | ( $self->get_operation_name || "'undef'" ) | ||||||||||||||||||||
| 181 | . | ||||||||||||||||||||
| 182 | "'] ... yet!"; | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 2 | return $finish_time - $start_time | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 15 | has child_of => ( | |||||||||||||||||||
| 188 | is => 'ro', | ||||||||||||||||||||
| 189 | isa => Span | SpanContext, | ||||||||||||||||||||
| 190 | required => 1, | ||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | sub parent_span_id { | ||||||||||||||||||||
| 194 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
parent_span_id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | 0 | my $parent = $self->{ child_of }; | |||||||||||||||||||
| 197 | 0 | ▶ | return unless is_Span( $parent ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 0 | return $parent->span_id | |||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | # | ||||||||||||||||||||
| 202 | # This may not be the right way to implement it, for the `child_of` attribute | ||||||||||||||||||||
| 203 | # may not be such a good idea, maybe it should use references, but not sure how | ||||||||||||||||||||
| 204 | # those are used | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | # _set_context | ||||||||||||||||||||
| 209 | # | ||||||||||||||||||||
| 210 | # you really shouldn't change the context yourself, only on instantiation | ||||||||||||||||||||
| 211 | # | ||||||||||||||||||||
| 212 | sub _set_context { | ||||||||||||||||||||
| 213 | 2 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_set_context: called CC=3 SCAR=11.3
| |||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 2 | ▶ | croak "Can't set context on an already finished span" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 216 | if $self->has_finished; | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 2 | ▶ | my $context = shift or die "Missing context"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 2 | $self->{ context } = $context; | |||||||||||||||||||
| 221 | |||||||||||||||||||||
| 222 | 2 | return $self | |||||||||||||||||||
| 223 | } | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | 15 | has on_finish => ( | |||||||||||||||||||
| 226 | is => 'ro', | ||||||||||||||||||||
| 227 | isa => Maybe[CodeRef], | ||||||||||||||||||||
| 228 | predicate => 1, | ||||||||||||||||||||
| 229 | ); | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | sub DEMOLISH { | ||||||||||||||||||||
| 232 | 30 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
DEMOLISH: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 233 | 30 | my $in_global_destruction = shift; | |||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | 30 | ▶ | return if $self->has_finished; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | # carp "Span not programmatically finished before being demolished"; | ||||||||||||||||||||
| 238 | |||||||||||||||||||||
| 239 | 24 | ▶ | $self->finish( ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 240 | unless $in_global_destruction; | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | return | ||||||||||||||||||||
| 243 | 24 | } | |||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | sub epoch_floatingpoint { | ||||||||||||||||||||
| 246 | 45 | ▶ | return time() | ||||||||||||||||||
|
Subroutine
epoch_floatingpoint: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 247 | } | ||||||||||||||||||||
| 248 | # | ||||||||||||||||||||
| 249 | # well, this is a bit off a silly idea: | ||||||||||||||||||||
| 250 | # some implentations may want nano-second accuracy, but floating point | ||||||||||||||||||||
| 251 | # computations using 64bits (IEEE) are only having 16 digits in the mantissa. | ||||||||||||||||||||
| 252 | # The number of nano-seconds since epoch is 19 digits that barely fits in a | ||||||||||||||||||||
| 253 | # signed 64 bit integer. | ||||||||||||||||||||
| 254 | |||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | |||||||||||||||||||||
| 257 | BEGIN { | ||||||||||||||||||||
| 258 | # use Role::Tiny::With; | ||||||||||||||||||||
| 259 | with 'OpenTracing::Interface::Span' | ||||||||||||||||||||
| 260 | 15 | ▶ | if $ENV{OPENTRACING_INTERFACE}; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 261 | } | ||||||||||||||||||||
| 262 | |||||||||||||||||||||
| 263 | |||||||||||||||||||||
| 264 | |||||||||||||||||||||
| 265 | 15 | 1; | |||||||||||||||||||