| 1 | 2 | ▶ | use Object::Pad ':experimental(init_expr)'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | # ABSTRACT: An OpenTelemetry Protocol span exporter | ||||||||||||
| 3 | |||||||||||||
| 4 | package OpenTelemetry::Exporter::OTLP; | ||||||||||||
| 5 | |||||||||||||
| 6 | 2 | our $VERSION = '0.022'; | |||||||||||
| 7 | |||||||||||||
| 8 | 2 | ▶ | class OpenTelemetry::Exporter::OTLP :does(OpenTelemetry::Exporter) { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Feature::Compat::Try; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use HTTP::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use Module::Runtime 'require_module'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 2 | ▶ | use OpenTelemetry::Common qw( config maybe_timeout timeout_timestamp ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 2 | ▶ | use OpenTelemetry::Constants -trace_export; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 2 | ▶ | use OpenTelemetry::Context; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 2 | ▶ | use OpenTelemetry::Trace; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 2 | ▶ | use OpenTelemetry::X; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 2 | ▶ | use Syntax::Keyword::Dynamically; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 2 | ▶ | use Syntax::Keyword::Match; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 2 | ▶ | use Time::HiRes 'sleep'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 2 | ▶ | use Time::Piece; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 2 | ▶ | use URL::Encode 'url_decode'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | |||||||||||||
| 24 | 2 | my $CAN_USE_PROTOBUF = eval { | |||||||||||
| 25 | 2 | require Google::ProtocolBuffers::Dynamic; | |||||||||||
| 26 | 0 | 1; | |||||||||||
| 27 | }; | ||||||||||||
| 28 | |||||||||||||
| 29 | 2 | ▶ | my $PROTOCOL = $CAN_USE_PROTOBUF ? 'http/protobuf' : 'http/json'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | |||||||||||||
| 31 | 2 | ▶ | my $COMPRESSION = eval { | ||||||||||
|
Condition50%1/2
eval {
do {
require Compress::Zlib;
"gzip"
}
} // "none"
MC/DC0%0/1
eval {
do {
require Compress::Zlib;
"gzip"
}
} // "none"
eval {
do {
require Compress::Zlib;
"gzip"
}
}
Truth table50%1/2
A
A= eval {
do {
require Compress::Zlib;
"gzip"
}
}
| |||||||||||||
| 32 | 2 | require Compress::Zlib; | |||||||||||
| 33 | 2 | 'gzip'; | |||||||||||
| 34 | } // 'none'; | ||||||||||||
| 35 | |||||||||||||
| 36 | 2 | my $logger = OpenTelemetry::Common::internal_logger; | |||||||||||
| 37 | |||||||||||||
| 38 | 2 | ▶ | use Metrics::Any '$metrics', strict => 1, | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 39 | 2 | name_prefix => [qw( otel exporter otlp )]; | |||||||||||
| 40 | |||||||||||||
| 41 | 2 | $metrics->make_counter( 'success', | |||||||||||
| 42 | name => [qw( success )], | ||||||||||||
| 43 | description => 'Number of times the export process succeeded', | ||||||||||||
| 44 | ); | ||||||||||||
| 45 | |||||||||||||
| 46 | 2 | $metrics->make_counter( 'failure', | |||||||||||
| 47 | name => [qw( failure )], | ||||||||||||
| 48 | description => 'Number of times the export process failed', | ||||||||||||
| 49 | labels => [qw( reason )], | ||||||||||||
| 50 | ); | ||||||||||||
| 51 | |||||||||||||
| 52 | 2 | $metrics->make_distribution( 'uncompressed', | |||||||||||
| 53 | name => [qw( message uncompressed size )], | ||||||||||||
| 54 | description => 'Size of exporter payload before compression', | ||||||||||||
| 55 | units => 'bytes', | ||||||||||||
| 56 | ); | ||||||||||||
| 57 | |||||||||||||
| 58 | 2 | $metrics->make_distribution( 'compressed', | |||||||||||
| 59 | name => [qw( message compressed size )], | ||||||||||||
| 60 | description => 'Size of exporter payload after compression', | ||||||||||||
| 61 | units => 'bytes', | ||||||||||||
| 62 | ); | ||||||||||||
| 63 | |||||||||||||
| 64 | 2 | $metrics->make_timer( 'request', | |||||||||||
| 65 | name => [qw( request duration )], | ||||||||||||
| 66 | description => 'Duration of the export request', | ||||||||||||
| 67 | labels => [qw( status )], | ||||||||||||
| 68 | ); | ||||||||||||
| 69 | |||||||||||||
| 70 | 2 | field $stopped; | |||||||||||
| 71 | 2 | field $ua; | |||||||||||
| 72 | 2 | field $endpoint; | |||||||||||
| 73 | 2 | field $compression; | |||||||||||
| 74 | 2 | field $encoder; | |||||||||||
| 75 | 2 | field $retries; | |||||||||||
| 76 | |||||||||||||
| 77 | ADJUSTPARAMS ($params) { | ||||||||||||
| 78 | $endpoint = delete $params->{endpoint} | ||||||||||||
| 79 | // config('EXPORTER_OTLP_TRACES_ENDPOINT') | ||||||||||||
| 80 | // do { | ||||||||||||
| 81 | my $base = config('EXPORTER_OTLP_ENDPOINT') | ||||||||||||
| 82 | // 'http://localhost:4318'; | ||||||||||||
| 83 | |||||||||||||
| 84 | ( $base =~ s|/+$||r ) . '/v1/traces'; | ||||||||||||
| 85 | }; | ||||||||||||
| 86 | |||||||||||||
| 87 | $compression = delete $params->{compression} | ||||||||||||
| 88 | 2 | ▶ | // config(<EXPORTER_OTLP_{TRACES_,}COMPRESSION>) | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 89 | // $COMPRESSION; | ||||||||||||
| 90 | |||||||||||||
| 91 | $retries = delete $params->{retries} // 5; | ||||||||||||
| 92 | |||||||||||||
| 93 | my $timeout = delete $params->{timeout} | ||||||||||||
| 94 | // config(<EXPORTER_OTLP_{TRACES_,}TIMEOUT>) | ||||||||||||
| 95 | // 10; | ||||||||||||
| 96 | |||||||||||||
| 97 | my $headers = delete $params->{headers} | ||||||||||||
| 98 | // config(<EXPORTER_OTLP_{TRACES_,}HEADERS>) | ||||||||||||
| 99 | // {}; | ||||||||||||
| 100 | |||||||||||||
| 101 | $headers = { | ||||||||||||
| 102 | map { | ||||||||||||
| 103 | my ( $k, $v ) = map url_decode($_), split '=', $_, 2; | ||||||||||||
| 104 | $k =~ s/^\s+|\s+$//g; | ||||||||||||
| 105 | $v =~ s/^\s+|\s+$//g; | ||||||||||||
| 106 | $k => $v; | ||||||||||||
| 107 | } split ',', $headers | ||||||||||||
| 108 | } unless ref $headers; | ||||||||||||
| 109 | |||||||||||||
| 110 | die OpenTelemetry::X->create( | ||||||||||||
| 111 | Invalid => "invalid URL for OTLP exporter: $endpoint" | ||||||||||||
| 112 | ) unless "$endpoint" =~ m|^https?://|; | ||||||||||||
| 113 | |||||||||||||
| 114 | die OpenTelemetry::X->create( | ||||||||||||
| 115 | Unsupported => "unsupported compression key for OTLP exporter: $compression" | ||||||||||||
| 116 | ) unless $compression =~ /^(?:gzip|none)$/; | ||||||||||||
| 117 | |||||||||||||
| 118 | $headers->{'Content-Encoding'} = $compression unless $compression eq 'none'; | ||||||||||||
| 119 | |||||||||||||
| 120 | $encoder = do { | ||||||||||||
| 121 | my $protocol = delete $params->{protocol} | ||||||||||||
| 122 | // config('EXPORTER_OTLP_PROTOCOL') | ||||||||||||
| 123 | // $PROTOCOL; | ||||||||||||
| 124 | |||||||||||||
| 125 | die OpenTelemetry::X->create( | ||||||||||||
| 126 | Unsupported => "unsupported protocol for OTLP exporter: $protocol", | ||||||||||||
| 127 | ) unless $protocol =~ /^http\/(protobuf|json)$/; | ||||||||||||
| 128 | |||||||||||||
| 129 | my $class = 'OpenTelemetry::Exporter::OTLP::Encoder::'; | ||||||||||||
| 130 | $class .= 'Protobuf' if $1 eq 'protobuf'; | ||||||||||||
| 131 | $class .= 'JSON' if $1 eq 'json'; | ||||||||||||
| 132 | |||||||||||||
| 133 | try { | ||||||||||||
| 134 | require_module $class; | ||||||||||||
| 135 | $class->new; | ||||||||||||
| 136 | } | ||||||||||||
| 137 | catch ($e) { | ||||||||||||
| 138 | $logger->warn( | ||||||||||||
| 139 | 'Could not load OTLP encoder class. Defaulting to JSON', | ||||||||||||
| 140 | { class => $class, error => $e }, | ||||||||||||
| 141 | ); | ||||||||||||
| 142 | |||||||||||||
| 143 | require OpenTelemetry::Exporter::OTLP::Encoder::JSON; | ||||||||||||
| 144 | OpenTelemetry::Exporter::OTLP::Encoder::JSON->new; | ||||||||||||
| 145 | } | ||||||||||||
| 146 | }; | ||||||||||||
| 147 | |||||||||||||
| 148 | my %ssl_options; | ||||||||||||
| 149 | { | ||||||||||||
| 150 | my $ca = delete $params->{certificate} // config(qw( | ||||||||||||
| 151 | EXPORTER_OTLP_TRACES_CERTIFICATE | ||||||||||||
| 152 | EXPORTER_OTLP_CERTIFICATE | ||||||||||||
| 153 | )); | ||||||||||||
| 154 | |||||||||||||
| 155 | my $key = delete $params->{client_key} // config(qw( | ||||||||||||
| 156 | EXPORTER_OTLP_TRACES_CLIENT_KEY | ||||||||||||
| 157 | EXPORTER_OTLP_CLIENT_KEY | ||||||||||||
| 158 | )); | ||||||||||||
| 159 | |||||||||||||
| 160 | my $cert = delete $params->{client_certificate} // config(qw( | ||||||||||||
| 161 | EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE | ||||||||||||
| 162 | EXPORTER_OTLP_CLIENT_CERTIFICATE | ||||||||||||
| 163 | )); | ||||||||||||
| 164 | |||||||||||||
| 165 | $ssl_options{SSL_ca_file} = $ca if $ca; | ||||||||||||
| 166 | $ssl_options{SSL_key_file} = $key if $key; | ||||||||||||
| 167 | $ssl_options{SSL_cert_file} = $cert if $cert; | ||||||||||||
| 168 | }; | ||||||||||||
| 169 | |||||||||||||
| 170 | $ua = HTTP::Tiny->new( | ||||||||||||
| 171 | timeout => $timeout, | ||||||||||||
| 172 | agent => "OTel-OTLP-Exporter-Perl/$VERSION", | ||||||||||||
| 173 | default_headers => { | ||||||||||||
| 174 | %$headers, | ||||||||||||
| 175 | 'Content-Type' => $encoder->content_type, | ||||||||||||
| 176 | }, | ||||||||||||
| 177 | %ssl_options ? ( SSL_options => \%ssl_options ) : (), | ||||||||||||
| 178 | ); | ||||||||||||
| 179 | } | ||||||||||||
| 180 | |||||||||||||
| 181 | 2 | method $maybe_backoff ( $attempts, $after = undef ) { | |||||||||||
| 182 | $after //= 0; # Breaks tests in Perls under 5.38 if in signature | ||||||||||||
| 183 | |||||||||||||
| 184 | return if $attempts > $retries; | ||||||||||||
| 185 | |||||||||||||
| 186 | my $sleep; | ||||||||||||
| 187 | try { | ||||||||||||
| 188 | my $date = Time::Piece->strptime($after, '%a, %d %b %Y %T %Z'); | ||||||||||||
| 189 | $sleep = ( $date - localtime )->seconds; | ||||||||||||
| 190 | } | ||||||||||||
| 191 | catch($e) { | ||||||||||||
| 192 | die $e unless $e =~ /^Error parsing time/; | ||||||||||||
| 193 | $sleep = $after if $after > 0; | ||||||||||||
| 194 | } | ||||||||||||
| 195 | $sleep //= int rand 2 ** $attempts; | ||||||||||||
| 196 | |||||||||||||
| 197 | sleep $sleep + rand; | ||||||||||||
| 198 | |||||||||||||
| 199 | return 1; | ||||||||||||
| 200 | 2 | } | |||||||||||
| 201 | |||||||||||||
| 202 | method $send_request ( $data, $timeout ) { | ||||||||||||
| 203 | my %request = ( content => $data ); | ||||||||||||
| 204 | |||||||||||||
| 205 | $metrics->report_distribution( | ||||||||||||
| 206 | uncompressed => length $request{content}, | ||||||||||||
| 207 | ); | ||||||||||||
| 208 | |||||||||||||
| 209 | if ( $compression eq 'gzip' ) { | ||||||||||||
| 210 | require Compress::Zlib; | ||||||||||||
| 211 | $request{content} = Compress::Zlib::memGzip($request{content}); | ||||||||||||
| 212 | |||||||||||||
| 213 | unless ($request{content}) { | ||||||||||||
| 214 | OpenTelemetry->handle_error( | ||||||||||||
| 215 | message => "Error compressing data: $Compress::Zlib::gzerrno" | ||||||||||||
| 216 | ); | ||||||||||||
| 217 | |||||||||||||
| 218 | $metrics->inc_counter( | ||||||||||||
| 219 | failure => [ reason => 'zlib_error' ], | ||||||||||||
| 220 | ); | ||||||||||||
| 221 | |||||||||||||
| 222 | return TRACE_EXPORT_FAILURE; | ||||||||||||
| 223 | } | ||||||||||||
| 224 | |||||||||||||
| 225 | $metrics->report_distribution( | ||||||||||||
| 226 | compressed => length $request{content}, | ||||||||||||
| 227 | ); | ||||||||||||
| 228 | } | ||||||||||||
| 229 | |||||||||||||
| 230 | my $start = timeout_timestamp; | ||||||||||||
| 231 | my $attempts = 0; | ||||||||||||
| 232 | while (1) { | ||||||||||||
| 233 | my $remaining = maybe_timeout $timeout, $start; | ||||||||||||
| 234 | return TRACE_EXPORT_TIMEOUT if $timeout && !$remaining; | ||||||||||||
| 235 | |||||||||||||
| 236 | # We are changing the state of the user-agent here | ||||||||||||
| 237 | # There doesn't seem to be another way to do this. | ||||||||||||
| 238 | # As long as this exporter is running with the Batch | ||||||||||||
| 239 | # processor, it should only be processing one request | ||||||||||||
| 240 | # at a time, so this should not be a problem. | ||||||||||||
| 241 | $ua->timeout($remaining); | ||||||||||||
| 242 | |||||||||||||
| 243 | my $request_start = timeout_timestamp; | ||||||||||||
| 244 | my $res = $ua->post( $endpoint, \%request ); | ||||||||||||
| 245 | my $request_end = timeout_timestamp; | ||||||||||||
| 246 | |||||||||||||
| 247 | $metrics->report_timer( | ||||||||||||
| 248 | request => $request_end - $request_start, | ||||||||||||
| 249 | [ status => $res->{status} ], | ||||||||||||
| 250 | ); | ||||||||||||
| 251 | |||||||||||||
| 252 | if ( $res->{success} ) { | ||||||||||||
| 253 | $metrics->inc_counter('success'); | ||||||||||||
| 254 | return TRACE_EXPORT_SUCCESS; | ||||||||||||
| 255 | } | ||||||||||||
| 256 | |||||||||||||
| 257 | match ( $res->{status} : =~ ) { | ||||||||||||
| 258 | case( m/^ 599 $/x ) { | ||||||||||||
| 259 | my $reason = do { | ||||||||||||
| 260 | match ( $res->{content} : =~ ) { | ||||||||||||
| 261 | case(m/^Timed out/) { 'timeout' } | ||||||||||||
| 262 | case(m/^Could not connect /) { 'socket_error' } | ||||||||||||
| 263 | case(m/^Could not .* socket /) { 'socket_error' } | ||||||||||||
| 264 | case(m/^Socket closed /) { 'socket_error' } | ||||||||||||
| 265 | case(m/^Wide character in write/) { 'socket_error' } | ||||||||||||
| 266 | case(m/^Error halting .* SSL /) { 'ssl_error' } | ||||||||||||
| 267 | case(m/^SSL connection failed /) { 'ssl_error' } | ||||||||||||
| 268 | case(m/^Unexpected end of stream/) { 'eof_error' } | ||||||||||||
| 269 | case(m/^Cannot parse/) { 'parse_error' } | ||||||||||||
| 270 | default { | ||||||||||||
| 271 | $metrics->inc_counter( | ||||||||||||
| 272 | failure => [ reason => $res->{status} ], | ||||||||||||
| 273 | ); | ||||||||||||
| 274 | |||||||||||||
| 275 | OpenTelemetry->handle_error( | ||||||||||||
| 276 | exception => $res->{content}, | ||||||||||||
| 277 | message => 'Unhandled error sending OTLP request', | ||||||||||||
| 278 | ); | ||||||||||||
| 279 | |||||||||||||
| 280 | return TRACE_EXPORT_FAILURE; | ||||||||||||
| 281 | } | ||||||||||||
| 282 | } | ||||||||||||
| 283 | }; | ||||||||||||
| 284 | |||||||||||||
| 285 | $metrics->inc_counter( failure => [ reason => $reason ] ); | ||||||||||||
| 286 | |||||||||||||
| 287 | redo if $self->$maybe_backoff( ++$attempts ); | ||||||||||||
| 288 | } | ||||||||||||
| 289 | case( m/^(?: 4 | 5 ) \d{2} $/ax ) { | ||||||||||||
| 290 | my $code = $res->{status}; | ||||||||||||
| 291 | |||||||||||||
| 292 | $metrics->inc_counter( failure => [ reason => $code ] ); | ||||||||||||
| 293 | |||||||||||||
| 294 | if ( $CAN_USE_PROTOBUF ) { | ||||||||||||
| 295 | try { | ||||||||||||
| 296 | require OpenTelemetry::Proto; | ||||||||||||
| 297 | |||||||||||||
| 298 | my $status = OTel::Google::RPC::Status | ||||||||||||
| 299 | ->decode($res->{content}); | ||||||||||||
| 300 | |||||||||||||
| 301 | OpenTelemetry->handle_error( | ||||||||||||
| 302 | exception => $status->encode_json, | ||||||||||||
| 303 | message => 'OTLP exporter received an RPC error status', | ||||||||||||
| 304 | ); | ||||||||||||
| 305 | } | ||||||||||||
| 306 | catch($e) { | ||||||||||||
| 307 | OpenTelemetry->handle_error( | ||||||||||||
| 308 | exception => $e, | ||||||||||||
| 309 | message => 'Unexpected error decoding RPC status in OTLP exporter', | ||||||||||||
| 310 | ); | ||||||||||||
| 311 | } | ||||||||||||
| 312 | } | ||||||||||||
| 313 | |||||||||||||
| 314 | my $after = ( $code == 429 || $code == 503 ) | ||||||||||||
| 315 | ? $res->{headers}{'retry-after'} | ||||||||||||
| 316 | : undef; | ||||||||||||
| 317 | |||||||||||||
| 318 | # As-per https://opentelemetry.io/docs/specs/otlp/#failures-1 | ||||||||||||
| 319 | redo if ( $code == 429 | ||||||||||||
| 320 | || $code == 502 | ||||||||||||
| 321 | || $code == 503 | ||||||||||||
| 322 | || $code == 504 | ||||||||||||
| 323 | ) && $self->$maybe_backoff( ++$attempts, $after ); | ||||||||||||
| 324 | } | ||||||||||||
| 325 | } | ||||||||||||
| 326 | |||||||||||||
| 327 | return TRACE_EXPORT_FAILURE; | ||||||||||||
| 328 | } | ||||||||||||
| 329 | 2 | } | |||||||||||
| 330 | |||||||||||||
| 331 | 4 | ▶ | method export ( $data, $timeout = undef ) { | ||||||||||
|
Subroutine
export: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||
| 332 | 4 | ▶ | return TRACE_EXPORT_FAILURE if $stopped; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 333 | 4 | ▶ | return unless @$data; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 334 | |||||||||||||
| 335 | 4 | try { | |||||||||||
| 336 | 4 | dynamically OpenTelemetry::Context->current | |||||||||||
| 337 | = OpenTelemetry::Trace->untraced_context; | ||||||||||||
| 338 | |||||||||||||
| 339 | 4 | my $request = $encoder->encode($data); | |||||||||||
| 340 | 4 | my $result = $self->$send_request( $request, $timeout ); | |||||||||||
| 341 | |||||||||||||
| 342 | 4 | $metrics->inc_counter('success'); | |||||||||||
| 343 | |||||||||||||
| 344 | 4 | return $result; | |||||||||||
| 345 | } | ||||||||||||
| 346 | catch($e) { | ||||||||||||
| 347 | 0 | warn "Could not export data: $e"; | |||||||||||
| 348 | 0 | return TRACE_EXPORT_FAILURE; | |||||||||||
| 349 | } | ||||||||||||
| 350 | 2 | } | |||||||||||
| 351 | |||||||||||||
| 352 | 0 | ▶ | async method shutdown ( $timeout = undef ) { | ||||||||||
|
Subroutine
shutdown: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 353 | 0 | $stopped = 1; | |||||||||||
| 354 | 0 | TRACE_EXPORT_SUCCESS; | |||||||||||
| 355 | 2 | } | |||||||||||
| 356 | |||||||||||||
| 357 | 0 | ▶ | async method force_flush ( $timeout = undef ) { | ||||||||||
|
Subroutine
force_flush: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 358 | 0 | TRACE_EXPORT_SUCCESS; | |||||||||||
| 359 | 2 | } | |||||||||||
| 360 | 2 | } | |||||||||||