OpenTelemetry/Attributes.pm

filter: Statementstmt 100% 70 / 70 Branchbran 100% 12 / 12 Conditioncond 88.8% 8 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 97.2% 106 / 109 CC 11 SCAR 18.3
CC
11
Cov
99%
set
11.0
BEGIN
1.0
BEGIN
1.0
CRAP 6.2
« OpenTelemetry/Instrumentation.pm ↑ Summary OpenTelemetry/Propagator/TraceContext/TraceParent.pm »
1 2 ▶use Object::Pad ':experimental(init_expr)';
Subroutine BEGIN: called CC=1 SCAR=0.0
2 # ABSTRACT: A class encapsulating attribute validation for OpenTelemetry
3  
4 package OpenTelemetry::Attributes;
5  
6 2 our $VERSION = '0.040';
7  
8 class OpenTelemetry::AttributeMap {
9 2 ▶ use Log::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶ use OpenTelemetry::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 my $logger = OpenTelemetry::Common::internal_logger;
13  
14 2 ▶ use List::Util qw( any pairs );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶ use Ref::Util qw( is_hashref is_arrayref );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 field $max_fields :param = undef;
19 2 field $max_field_length :param = undef;
20 21 field $dropped_fields :reader = 0;
21 21 ▶ field $data = {};
Subroutine dropped_fields: called CC=1 SCAR=0.0
22  
23 2 ADJUSTPARAMS ($params) {
24 $self->set( %{ delete $params->{data} // {} } );
25 }
26  
27 2 method $validate_attribute_value ( $value ) {
28 # Attribute values cannot be undefined but logging this is noisy
29 return unless defined $value;
30  
31 if ( is_arrayref $value ) {
32 if ( any { ref } @$value ) {
33 $logger->trace('Attribute values that are lists cannot themselves hold references');
34 return;
35 }
36  
37 # Make sure we do not store the same reference that was
38 # passed as a value, since the list on the other side of
39 # that reference can be modified without going through
40 # our checks
41 $value = $max_field_length ? [
42 map {
43 defined ? substr( $_, 0, $max_field_length ) : $_
44 } @$value
45 ] : [ @$value ];
46 }
47 elsif ( ref $value ) {
48 $logger->trace('Attribute values cannot be references');
49 return;
50 }
51 elsif ( $max_field_length ) {
52 $value = substr $value, 0, $max_field_length;
53 }
54  
55 ( 1, $value );
56 2 }
57  
58 27 ▶ method set ( %args ) {
Subroutine set: called CC=11 SCAR=24.0
59 27 my $recorded = 0;
60 27 for ( pairs %args ) {
61 27 my ( $key, $value ) = @$_;
62  
63 27 ▶ $key ||= do {
Condition66%2/3
$key ||= do { $logger->debugf("Attribute names should not be empty. Setting to 'null' instead"); "null" }
l!l&&r!l&&!r
2610
MC/DC0%0/2
$key ||= do { $logger->debugf("Attribute names should not be empty. Setting to 'null' instead"); "null" }
$key do { $logger->debugf("Attribute names should not be empty. Setting to 'null' instead"); "null" }
Truth table66%2/3
A ||= B
A= $keyB= do { $logger->debugf("Attribute names should not be empty. Setting to 'null' instead"); "null" }
ABresult
1X1
011
000
64 1 $logger->debugf("Attribute names should not be empty. Setting to 'null' instead");
65 1 'null';
66 };
67  
68 27 my $fields = scalar %$data;
69 27 ▶ $fields++ unless exists $data->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->{$key} 22 5 27
70  
71 27 ▶ next if $max_fields && $fields > $max_fields;
Branch: 1 branch
BranchTrueFalseTotal
if $max_fields and $fields > $max_fields 3 24 27
Condition100%3/3
$max_fields and $fields > $max_fields
!ll&&!rl&&r
2043
MC/DC100%2/2
$max_fields and $fields > $max_fields
$max_fields $fields > $max_fields
Truth table100%3/3
A and B
A= $max_fieldsB= $fields > $max_fields
ABresult
0X0
100
111
72  
73 24 my $ok;
74 24 ( $ok, $value ) = $self->$validate_attribute_value($value);
75  
76 24 ▶ next unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 4 20 24
77  
78 20 $recorded++;
79 20 $data->{$key} = $value;
80 }
81  
82 27 my $dropped = +( keys %args ) - $recorded;
83  
84 27 ▶ $logger->debugf(
Branch: 3 branches
BranchTrueFalseTotal
if $logger->is_debug and $dropped > 0 6 21 27
$dropped > 1 ? : 1 5 6
$max_fields ? : 3 3 6
Condition100%3/3
$logger->is_debug and $dropped > 0
!ll&&!rl&&r
8136
MC/DC100%2/2
$logger->is_debug and $dropped > 0
$logger->is_debug $dropped > 0
Truth table100%3/3
A and B
A= $logger->is_debugB= $dropped > 0
ABresult
0X0
100
111
85 'Dropped %s attribute entr%s because %s invalid%s',
86 $dropped,
87 $dropped > 1 ? ( 'ies', 'they were' ) : ( 'y', 'it was' ),
88 $max_fields
89 ? " or would have exceeded field limit ($max_fields)" : '',
90 ) if $logger->is_debug && $dropped > 0;
91  
92 27 $dropped_fields += $dropped;
93  
94 27 return $self;
95 2 }
96  
97 26 ▶ method to_hash () {
Subroutine to_hash: called CC=1 SCAR=0.0
98 26 dclone $data;
99 2 }
100 }
101  
102 2 role OpenTelemetry::Attributes {
103 2 field $attributes;
104  
105 ADJUSTPARAMS ( $params ) {
106 $attributes = OpenTelemetry::AttributeMap->new(
107 data => delete $params->{attributes} // {},
108 max_fields => delete $params->{attribute_count_limit},
109 max_field_length => delete $params->{attribute_length_limit},
110 );
111 }
112  
113 21 ▶ method dropped_attributes () { $attributes->dropped_fields }
Subroutine dropped_attributes: called CC=1 SCAR=0.0
Pod documented
114  
115 26 ▶ method attributes () { $attributes->to_hash }
Subroutine attributes: called CC=1 SCAR=0.0
116 2 }
« OpenTelemetry/Instrumentation.pm ↑ Summary OpenTelemetry/Propagator/TraceContext/TraceParent.pm »