| 1 | use Object::Pad ':experimental(init_expr)'; | ||
| 2 | # ABSTRACT: A class encapsulating attribute validation for OpenTelemetry | ||
| 3 | |||
| 4 | package OpenTelemetry::Attributes; | ||
| 5 | |||
| 6 | our $VERSION = '0.039'; | ||
| 7 | |||
| 8 | class OpenTelemetry::AttributeMap { | ||
| 9 | use Log::Any; | ||
| 10 | use OpenTelemetry::Common (); | ||
| 11 | |||
| 12 | my $logger = OpenTelemetry::Common::internal_logger; | ||
| 13 | |||
| 14 | use List::Util qw( any pairs ); | ||
| 15 | use Ref::Util qw( is_hashref is_arrayref ); | ||
| 16 | use Storable 'dclone'; | ||
| 17 | |||
| 18 | field $max_fields :param = undef; | ||
| 19 | field $max_field_length :param = undef; | ||
| 20 | field $dropped_fields :reader = 0; | ||
| 21 | field $data = {}; | ||
| 22 | |||
| 23 | ADJUSTPARAMS ($params) { | ||
| 24 | $self->set( %{ delete $params->{data} // {} } ); | ||
| 25 | } | ||
| 26 | |||
| 27 | 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 | } | ||
| 57 | |||
| 58 | method set ( %args ) { | ||
| 59 | my $recorded = 0; | ||
| 60 | for ( pairs %args ) { | ||
| 61 | my ( $key, $value ) = @$_; | ||
| 62 | |||
| 63 | $key ||= do { | ||
| 64 | $logger->debugf("Attribute names should not be empty. Setting to 'null' instead"); | ||
| 65 | 'null'; | ||
| 66 | }; | ||
| 67 | |||
| 68 | my $fields = scalar %$data; | ||
| 69 | $fields++ unless exists $data->{$key}; | ||
| 70 | |||
| 71 | next if $max_fields && $fields > $max_fields; | ||
| 72 | |||
| 73 | my $ok; | ||
| 74 | ( $ok, $value ) = $self->$validate_attribute_value($value); | ||
| 75 | |||
| 76 | next unless $ok; | ||
| 77 | |||
| 78 | $recorded++; | ||
| 79 | $data->{$key} = $value; | ||
| 80 | } | ||
| 81 | |||
| 82 | my $dropped = +( keys %args ) - $recorded; | ||
| 83 | |||
| 84 | $logger->debugf( | ||
| 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 | $dropped_fields += $dropped; | ||
| 93 | |||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | method to_hash () { | ||
| 98 | dclone $data; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | role OpenTelemetry::Attributes { | ||
| 103 | 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 | method dropped_attributes () { $attributes->dropped_fields } | ||
| 114 | |||
| 115 | method attributes () { $attributes->to_hash } | ||
| 116 | } |