| 1 | package OpenTracing::Role::SpanContext; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | OpenTracing::Role::SpanContext - Role for OpenTracing implementations. | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | package OpenTracing::Implementation::MyBackendService::SpanContext | ||||||||||
| 10 | |||||||||||
| 11 | use Moo; | ||||||||||
| 12 | |||||||||||
| 13 | ... | ||||||||||
| 14 | |||||||||||
| 15 | with 'OpenTracing::Role::SpanContext' | ||||||||||
| 16 | |||||||||||
| 17 | 1; | ||||||||||
| 18 | |||||||||||
| 19 | =cut | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | 14 | our $VERSION = '0.08_003'; | |||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | |||||||||||
| 27 | 14 | ▶ | use Moo::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 14 | ▶ | use MooX::HandlesVia; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 14 | ▶ | use Sub::Trigger::Lock; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | 14 | ▶ | use Types::Standard qw/HashRef Str/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | |||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||
| 37 | |||||||||||
| 38 | This is a role for OpenTracing implenetations that are compliant with the | ||||||||||
| 39 | L<OpenTracing::Interface>. | ||||||||||
| 40 | |||||||||||
| 41 | It has been suggested that an object that implements the OpenTracing SpanContext | ||||||||||
| 42 | interface SHOULD be immutable, to avoid lifetime issues. Therefore, the | ||||||||||
| 43 | attributes are read/write-protected. Any changes tried to make, will trigger a | ||||||||||
| 44 | L<Sub::Trigger::Lock> exception. | ||||||||||
| 45 | |||||||||||
| 46 | The only way to 'mutate' the bagage items, is by using L<with_baggage_item> or | ||||||||||
| 47 | L<with_baggage_items>. | ||||||||||
| 48 | |||||||||||
| 49 | Most likely, the L<new> constructor would only be called during the extraction | ||||||||||
| 50 | phase. Depending on the framework the OpenTracing implementation is being used | ||||||||||
| 51 | for, it will be initialised with request depenent information. From there on, | ||||||||||
| 52 | additional bagage-items can be added. | ||||||||||
| 53 | |||||||||||
| 54 | Implementors should be aware of the immutable desired behavbior and should use | ||||||||||
| 55 | methods like C<with_...> to clone this object with new values, rather than just | ||||||||||
| 56 | updating any values of the the attributes. | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | # baggage_items | ||||||||||
| 63 | # | ||||||||||
| 64 | has baggage_items => ( | ||||||||||
| 65 | is => 'rwp', | ||||||||||
| 66 | isa => HashRef[Str], | ||||||||||
| 67 | handles_via => 'Hash', | ||||||||||
| 68 | handles => { | ||||||||||
| 69 | # get_baggage_item => 'get', | ||||||||||
| 70 | get_baggage_items => 'all', | ||||||||||
| 71 | }, | ||||||||||
| 72 | 1 | ▶ | default => sub{ {} }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 14 | trigger => Lock, | |||||||||
| 74 | ); | ||||||||||
| 75 | |||||||||||
| 76 | |||||||||||
| 77 | # XXX: trigger and $obj->get_baggage_item( 'foo' ) do not play well together | ||||||||||
| 78 | # feels like a bug in Moo or Sub::Trigger::Lock | ||||||||||
| 79 | # | ||||||||||
| 80 | sub get_baggage_item { | ||||||||||
| 81 | 6 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_baggage_item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 82 | 6 | my $item_key = shift; | |||||||||
| 83 | |||||||||||
| 84 | 6 | return { $self->get_baggage_items() }->{ $item_key } | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | # with_baggage_item | ||||||||||
| 90 | # | ||||||||||
| 91 | # creates a clone of the current object, with new kew/value pair added | ||||||||||
| 92 | # | ||||||||||
| 93 | sub with_baggage_item { | ||||||||||
| 94 | 1 | ▶ | my ( $self, $key, $value ) = @_; | ||||||||
|
Subroutine
with_baggage_item: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 95 | |||||||||||
| 96 | 1 | $self->_clone( | |||||||||
| 97 | baggage_items => { $self->get_baggage_items(), $key => $value }, | ||||||||||
| 98 | ); | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | |||||||||||
| 102 | # with_baggage_items | ||||||||||
| 103 | # | ||||||||||
| 104 | # creates a clone of the current object, with list of kew/value pairs added | ||||||||||
| 105 | # | ||||||||||
| 106 | sub with_baggage_items { | ||||||||||
| 107 | 3 | ▶ | my ( $self, %args ) = @_; | ||||||||
|
Subroutine
with_baggage_items: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 108 | |||||||||||
| 109 | 3 | $self->_clone( | |||||||||
| 110 | baggage_items => { $self->get_baggage_items(), %args }, | ||||||||||
| 111 | ); | ||||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | # _clone | ||||||||||
| 116 | # | ||||||||||
| 117 | # Creates a shallow clone of the object, which is fine | ||||||||||
| 118 | # | ||||||||||
| 119 | sub _clone { | ||||||||||
| 120 | 4 | ▶ | my ( $self, @args ) = @_; | ||||||||
|
Subroutine
_clone: called CC=1 SCAR=0.0
| |||||||||||
| 121 | 4 | ref( $self )->new( %$self, @args ); | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | BEGIN { | ||||||||||
| 126 | # use Role::Tiny::With; | ||||||||||
| 127 | with 'OpenTracing::Interface::SpanContext' | ||||||||||
| 128 | 14 | ▶ | if $ENV{OPENTRACING_INTERFACE}; | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | |||||||||||
| 132 | |||||||||||
| 133 | =head1 ATTRIBUTES | ||||||||||
| 134 | |||||||||||
| 135 | |||||||||||
| 136 | |||||||||||
| 137 | =head2 baggage_items | ||||||||||
| 138 | |||||||||||
| 139 | |||||||||||
| 140 | |||||||||||
| 141 | =head1 METHODS | ||||||||||
| 142 | |||||||||||
| 143 | |||||||||||
| 144 | |||||||||||
| 145 | =head2 get_baggage_item | ||||||||||
| 146 | |||||||||||
| 147 | Returns a single value for a given key. | ||||||||||
| 148 | |||||||||||
| 149 | =head2 get_baggage_items | ||||||||||
| 150 | |||||||||||
| 151 | Returns a hash that contains all key/value pairs for the current baggage items. | ||||||||||
| 152 | By returning a hash and not a reference, it purposefully makes it hard to mutate | ||||||||||
| 153 | any of the key/value pairs in the baggage_items. | ||||||||||
| 154 | |||||||||||
| 155 | =head2 with_baggage_item | ||||||||||
| 156 | |||||||||||
| 157 | Creates a clone of the current object, with new kew/value pair added. | ||||||||||
| 158 | |||||||||||
| 159 | =head2 with_baggage_items | ||||||||||
| 160 | |||||||||||
| 161 | Creates a clone of the current object, with list of kew/value pairs added. | ||||||||||
| 162 | |||||||||||
| 163 | |||||||||||
| 164 | |||||||||||
| 165 | =head1 SEE ALSO | ||||||||||
| 166 | |||||||||||
| 167 | =over | ||||||||||
| 168 | |||||||||||
| 169 | =item L<OpenTracing::Role> | ||||||||||
| 170 | |||||||||||
| 171 | =item L<OpenTracing::Implementation> | ||||||||||
| 172 | |||||||||||
| 173 | =item L<OpenTracing::Interface::SpanContext> | ||||||||||
| 174 | |||||||||||
| 175 | =back | ||||||||||
| 176 | |||||||||||
| 177 | =cut | ||||||||||
| 178 | |||||||||||
| 179 | |||||||||||
| 180 | 14 | 1; | |||||||||