| 1 | 14 | ▶ | use Object::Pad ':experimental(init_expr)'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | # ABSTRACT: A context class for OpenTelemetry | ||||||||||
| 3 | |||||||||||
| 4 | package | ||||||||||
| 5 | OpenTelemetry::Context::Key; | ||||||||||
| 6 | |||||||||||
| 7 | 14 | our $VERSION = '0.040'; | |||||||||
| 8 | |||||||||||
| 9 | class OpenTelemetry::Context::Key { | ||||||||||
| 10 | 14 | ▶ | use UUID::URandom 'create_uuid'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 14 | field $name :param; | |||||||||
| 13 | 133 | ▶ | field $string :reader; | ||||||||
|
Subroutine
string: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 14 | ADJUST { $string = $name . '-' . unpack( 'H*', create_uuid ) }; | |||||||||
| 16 | } | ||||||||||
| 17 | |||||||||||
| 18 | 14 | package OpenTelemetry::Context; | |||||||||
| 19 | |||||||||||
| 20 | 14 | our $VERSION = '0.040'; | |||||||||
| 21 | |||||||||||
| 22 | 38 | ▶ | sub key ( $, $name ) { | ||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 23 | 38 | OpenTelemetry::Context::Key->new( name => $name ); | |||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | class OpenTelemetry::Context { | ||||||||||
| 27 | 14 | ▶ | use Carp qw( carp croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 14 | ▶ | use List::Util qw( pairs all ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 14 | ▶ | use OpenTelemetry::X; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 14 | ▶ | use isa 'OpenTelemetry::Context::Key'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 14 | field $data; | |||||||||
| 34 | |||||||||||
| 35 | 67 | ▶ | sub BUILDARGS ( $class, %args ) { | ||||||||
|
Subroutine
BUILDARGS: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 36 | 67 | ▶ | carp 'The OpenTelemetry::Context constructor no longer takes arguments' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | if %args; | ||||||||||
| 38 | 67 | return; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | method $init ( %data ) { | ||||||||||
| 42 | $data = \%data; | ||||||||||
| 43 | $self; | ||||||||||
| 44 | 14 | } | |||||||||
| 45 | |||||||||||
| 46 | 78 | ▶ | method get ( $key ) { | ||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 47 | 78 | ▶ | croak +OpenTelemetry::X->create( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | Invalid => 'Keys in a context object must be instances of OpenTelemetry::Context::Key', | ||||||||||
| 49 | ) unless isa_OpenTelemetry_Context_Key $key; | ||||||||||
| 50 | |||||||||||
| 51 | 77 | $data->{ $key->string }; | |||||||||
| 52 | 14 | } | |||||||||
| 53 | |||||||||||
| 54 | 46 | ▶ | method set ( @pairs ) { | ||||||||
|
Subroutine
set: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 55 | 46 | my %pairs; | |||||||||
| 56 | 46 | for ( pairs @pairs ) { | |||||||||
| 57 | 49 | ▶ | croak +OpenTelemetry::X->create( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | Invalid => 'Keys in a context object must be instances of OpenTelemetry::Context::Key', | ||||||||||
| 59 | ) unless isa_OpenTelemetry_Context_Key $_->[0]; | ||||||||||
| 60 | |||||||||||
| 61 | 48 | $pairs{ $_->[0]->string } = $_->[1]; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 45 | OpenTelemetry::Context->new->$init( %$data, %pairs ); | |||||||||
| 65 | 14 | } | |||||||||
| 66 | |||||||||||
| 67 | 5 | ▶ | method delete ( @keys ) { | ||||||||
|
Subroutine
delete: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 68 | 5 | my @strings; | |||||||||
| 69 | 5 | for (@keys) { | |||||||||
| 70 | 7 | ▶ | croak +OpenTelemetry::X->create( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | Invalid => 'Keys in a context object must be instances of OpenTelemetry::Context::Key', | ||||||||||
| 72 | ) unless isa_OpenTelemetry_Context_Key $_; | ||||||||||
| 73 | |||||||||||
| 74 | 6 | push @strings, $_->string; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 4 | my %copy = %$data; | |||||||||
| 78 | 4 | delete @copy{@strings}; | |||||||||
| 79 | |||||||||||
| 80 | 4 | OpenTelemetry::Context->new->$init(%copy); | |||||||||
| 81 | 14 | } | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | # Context management | ||||||||||
| 85 | 14 | { | |||||||||
| 86 | 14 | ▶ | use Sentinel; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 87 | |||||||||||
| 88 | 14 | ▶ | use isa 'OpenTelemetry::Context'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 89 | |||||||||||
| 90 | 14 | my $current = OpenTelemetry::Context->new; | |||||||||
| 91 | 93 | ▶ | sub current :lvalue { | ||||||||
|
Subroutine
current: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 92 | sentinel( | ||||||||||
| 93 | 122 | ▶ | get => sub { $current }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 94 | 45 | ▶ | set => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 95 | 45 | ▶ | croak +OpenTelemetry::X->create( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | Invalid => 'Current context must be an instance of OpenTelemetry::Context, received instead ' . ref $_[0], | ||||||||||
| 97 | ) unless isa_OpenTelemetry_Context $_[0]; | ||||||||||
| 98 | |||||||||||
| 99 | 44 | $current = $_[0]; | |||||||||
| 100 | }, | ||||||||||
| 101 | 93 | ); | |||||||||
| 102 | } | ||||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 14 | 1; | |||||||||