File Coverage

blib/lib/OpenTelemetry/SDK/Exporter/Console.pm
Criterion Covered Total %
statement 56 56 100.0
branch 3 4 75.0
condition n/a
subroutine 12 12 100.0
pod 0 3 0.0
total 71 75 94.6


line stmt bran cond sub pod time code
1 2     2   172245 use Object::Pad ':experimental(init_expr)';
  2         9888  
  2         15  
2             # ABSTRACT: An OpenTelemetry span exporter that prints to the console
3              
4             package OpenTelemetry::SDK::Exporter::Console;
5              
6             our $VERSION = '0.028';
7              
8             class OpenTelemetry::SDK::Exporter::Console
9             :does(OpenTelemetry::Exporter)
10 2     2   1625 {
  2         1355  
  2         191  
11 2     2   571 use Future::AsyncAwait;
  2         17468  
  2         15  
12 2     2   648 use OpenTelemetry::Constants -trace_export;
  2         8610  
  2         22  
13              
14 2     2   2448 use feature 'say';
  2         4  
  2         4299  
15              
16             field $encoder :param //= undef;
17             field $handle :param = \*STDERR;
18             field $stopped;
19              
20             ADJUST {
21             $encoder //= do {
22             my ( $format, @options ) = split /,/,
23             $ENV{OTEL_PERL_EXPORTER_CONSOLE_FORMAT} // 'data-dumper';
24              
25             my %options = map split( /=/, $_, 2 ), @options;
26              
27             if ( lc $format eq 'json' ) {
28             require JSON::MaybeXS;
29             my $json = JSON::MaybeXS->new(
30             # Defaults
31             canonical => 1,
32             utf8 => 1,
33              
34             # User overrides
35             %options,
36             );
37              
38             sub { $json->encode(@_) };
39             }
40             else {
41             sub {
42             require Data::Dumper;
43              
44             # Defaults
45             local $Data::Dumper::Indent = 0;
46             local $Data::Dumper::Terse = 1;
47             local $Data::Dumper::Sortkeys = 1;
48              
49             my $dumper = Data::Dumper->new([@_]);
50             $dumper->$_( $options{$_} ) for keys %options;
51             $dumper->Dump;
52             };
53             }
54             };
55             }
56              
57 4     4   8 my sub dump_event ($event) {
  4         10  
  4         6  
58             {
59 4         21 timestamp => $event->timestamp,
60             name => $event->name,
61             attributes => $event->attributes,
62             dropped_attributes => $event->dropped_attributes,
63             }
64             }
65              
66 4     4   7 my sub dump_link ($link) {
  4         8  
  4         7  
67             {
68 4         20 trace_id => $link->context->hex_trace_id,
69             span_id => $link->context->hex_span_id,
70             attributes => $link->attributes,
71             dropped_attributes => $link->dropped_attributes,
72             }
73             }
74              
75 4     4   7 my sub dump_status ($status) {
  4         8  
  4         8  
76             {
77 4         20 code => $status->code,
78             description => $status->description,
79             }
80             }
81              
82 4     4   8 my sub dump_scope ($scope) {
  4         10  
  4         6  
83             {
84 4         20 name => $scope->name,
85             version => $scope->version,
86             }
87             }
88              
89 6     6 0 12589 method export ( $spans, $timeout = undef ) {
  6         34  
  6         15  
  6         14  
  6         11  
90 6 100       25 return TRACE_EXPORT_FAILURE if $stopped;
91              
92              
93 5         16 for my $span (@$spans) {
94 4         22 my $resource = $span->resource;
95              
96 4 50       21 my $encoded = $encoder->({
97             attributes => $span->attributes,
98             end_timestamp => $span->end_timestamp,
99             events => [ map dump_event($_), $span->events ],
100             instrumentation_scope => dump_scope($span->instrumentation_scope),
101             kind => $span->kind,
102             links => [ map dump_link($_), $span->links ],
103             name => $span->name,
104             parent_span_id => $span->hex_parent_span_id,
105             resource => $resource ? $resource->attributes : {},
106             span_id => $span->hex_span_id,
107             start_timestamp => $span->start_timestamp,
108             status => dump_status($span->status),
109             dropped_attributes => $span->dropped_attributes,
110             dropped_events => $span->dropped_events,
111             dropped_links => $span->dropped_links,
112             trace_flags => $span->trace_flags->flags,
113             trace_id => $span->hex_trace_id,
114             trace_state => $span->trace_state->to_string,
115             });
116 4         488 chomp $encoded;
117 4         26 say $handle $encoded;
118             }
119              
120 5         38 TRACE_EXPORT_SUCCESS;
121             }
122              
123 1     1 0 535 async method shutdown ( $timeout = undef ) {
  1         3  
  1         6  
  1         4  
  1         2  
124 1         3 $stopped = 1;
125 1         10 TRACE_EXPORT_SUCCESS;
126             }
127              
128 2     2 0 592 async method force_flush ( $timeout = undef ) { TRACE_EXPORT_SUCCESS }
  2         5  
  2         11  
  2         5  
  2         4  
  2         39  
129             }