Adapters/Otel.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 161 CC 13 SCAR 30.8
CC
13
Cov
0%
emit
42.0
_payload
42.0
_attribute
6.0
CRAP 21.8
1 package Praxo::Adapters::Otel;
2  
3 use strict;
4 use warnings;
5 use HTTP::Tiny ();
6 use Time::HiRes ();
7 use Praxo::Errors qw(praxo_error);
8 use Praxo::Json ();
9  
10 my $MAX_BODY = 512;
11  
12 sub new {
13 my ($class, %args) = @_;
14 return bless {
15 endpoint => $args{endpoint} // '',
16 service_name => $args{service_name} // 'praxo-core',
17 allowed_data_keys => $args{allowed_data_keys} // [],
18 transport => $args{transport},
19 clock => $args{clock},
20 timeout => $args{timeout} // 10,
21 }, $class;
22 }
23  
24 sub emit {
25 my ($self, $event) = @_;
26 praxo_error('OTEL_URL', 'otel: endpoint is required')
27 if $self->{endpoint} eq '';
28 my $nanos = $self->{clock}
29 ? $self->{clock}->()
30 : int(Time::HiRes::gettimeofday() * 1_000_000_000);
31 my $body = Praxo::Json::stringify($self->_payload($event, $nanos));
32 my $headers = { 'content-type' => 'application/json' };
33 my $response = $self->{transport}
34 ? $self->{transport}->($self->{endpoint}, $body, $headers)
35 : $self->_post($body, $headers);
36 my $status = $response->{status} // 0;
37 return if $status >= 200 && $status < 300;
38 my $text = $response->{body} // '';
39 $text = substr($text, 0, $MAX_BODY) if length($text) > $MAX_BODY;
40 praxo_error('OTEL_HTTP', "otel: HTTP $status: $text");
41 }
42  
43 sub _payload {
44 my ($self, $event, $nanos) = @_;
45 my $stamp = "$nanos";
46 my $data = ref($event->{data}) eq 'HASH' ? $event->{data} : {};
47 my $key = join('|',
48 $event->{sessionId} // '',
49 $event->{runId} // '',
50 $event->{type} // '',
51 $stamp,
52 Praxo::Json::stringify($data),
53 );
54 my $trace_id = _stable_id("trace-a:$key") . _stable_id("trace-b:$key")
55 . _stable_id("trace-c:$key") . _stable_id("trace-d:$key");
56 my $span_id = _stable_id("span-a:$key") . _stable_id("span-b:$key");
57 my @attributes = (
58 _attribute('praxo.event.type', $event->{type} // ''),
59 );
60 push @attributes, _attribute('praxo.session.id', $event->{sessionId})
61 if ($event->{sessionId} // '') ne '';
62 push @attributes, _attribute('praxo.run.id', $event->{runId})
63 if ($event->{runId} // '') ne '';
64 push @attributes, _attribute('praxo.event.at', $stamp);
65 for my $name (@{ $self->{allowed_data_keys} }) {
66 next unless exists $data->{$name};
67 push @attributes,
68 _attribute("praxo.event.data.$name", _text($data->{$name}));
69 }
70 my $service = $self->{service_name} eq ''
71 ? 'praxo-core'
72 : $self->{service_name};
73 return {
74 resourceSpans => [
75 {
76 resource => {
77 attributes => [
78 _attribute('service.name', $service),
79 ],
80 },
81 scopeSpans => [
82 {
83 scope => { name => 'praxo-core' },
84 spans => [
85 {
86 traceId => $trace_id,
87 spanId => $span_id,
88 name => 'praxo.' . ($event->{type} // ''),
89 kind => 1,
90 startTimeUnixNano => $stamp,
91 endTimeUnixNano => $stamp,
92 attributes => \@attributes,
93 },
94 ],
95 },
96 ],
97 },
98 ],
99 };
100 }
101  
102 sub _attribute {
103 my ($key, $value) = @_;
104 return {
105 key => $key,
106 value => { stringValue => defined $value ? "$value" : '' },
107 };
108 }
109  
110 sub _text {
111 my ($value) = @_;
112 return $value if defined $value && !ref $value;
113 return Praxo::Json::stringify($value);
114 }
115  
116 sub _stable_id {
117 my ($text) = @_;
118 my $hash = 0x811c9dc5;
119 for my $byte (unpack 'C*', $text) {
120 $hash ^= $byte;
121 $hash = ($hash * 0x01000193) & 0xFFFFFFFF;
122 }
123 return sprintf('%08x', $hash);
124 }
125  
126 sub _post {
127 my ($self, $body, $headers) = @_;
128 my $http = HTTP::Tiny->new(timeout => $self->{timeout});
129 my $response = $http->post(
130 $self->{endpoint},
131 { headers => $headers, content => $body },
132 );
133 return {
134 status => $response->{status} // 0,
135 body => $response->{content} // '',
136 };
137 }
138  
139 1;