| 1 | 2 | ▶ | use Object::Pad ':experimental(init_expr)'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | # ABSTRACT: An OpenTelemetry span exporter that prints to the console | ||||||||||||||
| 3 | |||||||||||||||
| 4 | package OpenTelemetry::SDK::Exporter::Console; | ||||||||||||||
| 5 | |||||||||||||||
| 6 | 2 | our $VERSION = '0.101'; | |||||||||||||
| 7 | |||||||||||||||
| 8 | class OpenTelemetry::SDK::Exporter::Console | ||||||||||||||
| 9 | :does(OpenTelemetry::Exporter) | ||||||||||||||
| 10 | 1 | ▶ | { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 2 | ▶ | use Data::Dumper; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 2 | ▶ | use OpenTelemetry::Constants -trace_export; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 2 | ▶ | use isa qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | OpenTelemetry::SDK::Logs::LogRecord | ||||||||||||||
| 17 | OpenTelemetry::SDK::Trace::Span::Readable | ||||||||||||||
| 18 | 2 | ); | |||||||||||||
| 19 | |||||||||||||||
| 20 | 2 | ▶ | use feature 'say'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | field $encoder :param //= undef; | ||||||||||||||
| 23 | 2 | field $handle :param = \*STDERR; | |||||||||||||
| 24 | 2 | field $stopped; | |||||||||||||
| 25 | |||||||||||||||
| 26 | ADJUST { | ||||||||||||||
| 27 | $encoder //= do { | ||||||||||||||
| 28 | my ( $format, @options ) = split /,/, | ||||||||||||||
| 29 | $ENV{OTEL_PERL_EXPORTER_CONSOLE_FORMAT} // 'data-dumper'; | ||||||||||||||
| 30 | |||||||||||||||
| 31 | my %options = map split( /=/, $_, 2 ), @options; | ||||||||||||||
| 32 | |||||||||||||||
| 33 | if ( lc $format eq 'json' ) { | ||||||||||||||
| 34 | require JSON::MaybeXS; | ||||||||||||||
| 35 | my $json = JSON::MaybeXS->new( | ||||||||||||||
| 36 | # Defaults | ||||||||||||||
| 37 | canonical => 1, | ||||||||||||||
| 38 | utf8 => 1, | ||||||||||||||
| 39 | |||||||||||||||
| 40 | # User overrides | ||||||||||||||
| 41 | %options, | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | sub { $json->encode(@_) }; | ||||||||||||||
| 45 | } | ||||||||||||||
| 46 | else { | ||||||||||||||
| 47 | sub { | ||||||||||||||
| 48 | require Data::Dumper; | ||||||||||||||
| 49 | |||||||||||||||
| 50 | # Defaults | ||||||||||||||
| 51 | local $Data::Dumper::Indent = 0; | ||||||||||||||
| 52 | local $Data::Dumper::Terse = 1; | ||||||||||||||
| 53 | local $Data::Dumper::Sortkeys = 1; | ||||||||||||||
| 54 | |||||||||||||||
| 55 | my $dumper = Data::Dumper->new([@_]); | ||||||||||||||
| 56 | $dumper->$_( $options{$_} ) for keys %options; | ||||||||||||||
| 57 | $dumper->Dump; | ||||||||||||||
| 58 | }; | ||||||||||||||
| 59 | } | ||||||||||||||
| 60 | }; | ||||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 2 | ▶ | my sub dump_span_event ($event) { | ||||||||||||
|
Subroutine
dump_span_event: called CC=1 SCAR=0.0
| |||||||||||||||
| 64 | { | ||||||||||||||
| 65 | 1 | timestamp => $event->timestamp, | |||||||||||||
| 66 | name => $event->name, | ||||||||||||||
| 67 | attributes => $event->attributes, | ||||||||||||||
| 68 | dropped_attributes => $event->dropped_attributes, | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | |||||||||||||||
| 72 | 1 | ▶ | my sub dump_span_link ($link) { | ||||||||||||
|
Subroutine
dump_span_link: called CC=1 SCAR=0.0
| |||||||||||||||
| 73 | { | ||||||||||||||
| 74 | 1 | trace_id => $link->context->hex_trace_id, | |||||||||||||
| 75 | span_id => $link->context->hex_span_id, | ||||||||||||||
| 76 | attributes => $link->attributes, | ||||||||||||||
| 77 | dropped_attributes => $link->dropped_attributes, | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | ▶ | my sub dump_span_status ($status) { | ||||||||||||
|
Subroutine
dump_span_status: called CC=1 SCAR=0.0
| |||||||||||||||
| 82 | { | ||||||||||||||
| 83 | 1 | code => $status->code, | |||||||||||||
| 84 | description => $status->description, | ||||||||||||||
| 85 | } | ||||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 2 | ▶ | my sub dump_scope ($scope) { | ||||||||||||
|
Subroutine
dump_scope: called CC=1 SCAR=0.0
| |||||||||||||||
| 89 | { | ||||||||||||||
| 90 | 2 | name => $scope->name, | |||||||||||||
| 91 | version => $scope->version, | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | 1 | ▶ | my sub dump_span ($span) { | ||||||||||||
|
Subroutine
dump_span: called CC=2 SCAR=7.2
| |||||||||||||||
| 96 | 1 | my $resource = $span->resource; | |||||||||||||
| 97 | return { | ||||||||||||||
| 98 | 1 | ▶ | attributes => $span->attributes, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 99 | dropped_attributes => $span->dropped_attributes, | ||||||||||||||
| 100 | dropped_events => $span->dropped_events, | ||||||||||||||
| 101 | dropped_links => $span->dropped_links, | ||||||||||||||
| 102 | end_timestamp => $span->end_timestamp, | ||||||||||||||
| 103 | events => [ map dump_span_event($_), $span->events ], | ||||||||||||||
| 104 | instrumentation_scope => dump_scope($span->instrumentation_scope), | ||||||||||||||
| 105 | kind => $span->kind, | ||||||||||||||
| 106 | links => [ map dump_span_link($_), $span->links ], | ||||||||||||||
| 107 | name => $span->name, | ||||||||||||||
| 108 | parent_span_id => $span->hex_parent_span_id, | ||||||||||||||
| 109 | resource => $resource ? $resource->attributes : {}, | ||||||||||||||
| 110 | span_id => $span->hex_span_id, | ||||||||||||||
| 111 | start_timestamp => $span->start_timestamp, | ||||||||||||||
| 112 | status => dump_span_status($span->status), | ||||||||||||||
| 113 | trace_flags => $span->trace_flags->flags, | ||||||||||||||
| 114 | trace_id => $span->hex_trace_id, | ||||||||||||||
| 115 | trace_state => $span->trace_state->to_string, | ||||||||||||||
| 116 | }; | ||||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 1 | ▶ | my sub dump_log ($log) { | ||||||||||||
|
Subroutine
dump_log: called CC=2 SCAR=7.2
| |||||||||||||||
| 120 | 1 | my $resource = $log->resource; | |||||||||||||
| 121 | return { | ||||||||||||||
| 122 | 1 | ▶ | attributes => $log->attributes, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 123 | body => $log->body, | ||||||||||||||
| 124 | dropped_attributes => $log->dropped_attributes, | ||||||||||||||
| 125 | flags => $log->trace_flags->flags, | ||||||||||||||
| 126 | instrumentation_scope => dump_scope($log->instrumentation_scope), | ||||||||||||||
| 127 | observed_timestamp => $log->observed_timestamp, | ||||||||||||||
| 128 | resource => $resource ? $resource->attributes : {}, | ||||||||||||||
| 129 | severity_number => 0+$log->severity_number, | ||||||||||||||
| 130 | severity_text => $log->severity_text, | ||||||||||||||
| 131 | span_id => $log->hex_span_id, | ||||||||||||||
| 132 | timestamp => $log->timestamp, | ||||||||||||||
| 133 | trace_id => $log->hex_trace_id, | ||||||||||||||
| 134 | }; | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | 4 | ▶ | method export ( $batch, $timeout = undef ) { | ||||||||||||
|
Subroutine
export: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 138 | 4 | ▶ | return TRACE_EXPORT_FAILURE if $stopped; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | |||||||||||||||
| 140 | 3 | for my $item (@$batch) { | |||||||||||||
| 141 | # FIXME: This multiple dispatch is annoying. We could move | ||||||||||||||
| 142 | # this to different "export_$type"-style methods, but then | ||||||||||||||
| 143 | # we would still need to be able to configure what method | ||||||||||||||
| 144 | # to call (eg. in the batch exporter, the call to the function | ||||||||||||||
| 145 | # is in the base class, not the metric-specific or log-specific | ||||||||||||||
| 146 | # implementation). This might be tricky. | ||||||||||||||
| 147 | # It might be that we want a placeholder LogRecord class in | ||||||||||||||
| 148 | # the API distribution (like we have for spans) so we can | ||||||||||||||
| 149 | # check for subclasses of that, and not require people to | ||||||||||||||
| 150 | # subclass the SDK. This would allow other people to provide | ||||||||||||||
| 151 | # their own implementations of a LogRecord as libraries too. | ||||||||||||||
| 152 | # That seems like a reasonable plan. | ||||||||||||||
| 153 | 2 | my $data; | |||||||||||||
| 154 | 2 | ▶ | if ( isa_OpenTelemetry_SDK_Logs_LogRecord $item ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 155 | 1 | $data = dump_log($item); | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | elsif ( isa_OpenTelemetry_SDK_Trace_Span_Readable $item ) { | ||||||||||||||
| 158 | 1 | $data = dump_span($item); | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | |||||||||||||||
| 161 | 2 | chomp( my $encoded = $encoder->($data) ); | |||||||||||||
| 162 | 2 | say $handle $encoded; | |||||||||||||
| 163 | } | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 3 | TRACE_EXPORT_SUCCESS; | |||||||||||||
| 166 | 2 | } | |||||||||||||
| 167 | |||||||||||||||
| 168 | 2 | ▶ | async method shutdown ( $timeout = undef ) { | ||||||||||||
|
Subroutine
shutdown: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 169 | 2 | $stopped = 1; | |||||||||||||
| 170 | 2 | TRACE_EXPORT_SUCCESS; | |||||||||||||
| 171 | 2 | } | |||||||||||||
| 172 | |||||||||||||||
| 173 | 2 | ▶ | async method force_flush ( $timeout = undef ) { TRACE_EXPORT_SUCCESS } | ||||||||||||
|
Subroutine
force_flush: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 174 | 2 | } | |||||||||||||