| 1 | package OpenTracing::Integration::System; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 1 | our $VERSION = '1.006'; # VERSION | |||||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY | |||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | ▶ | no indirect; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | =encoding utf8 | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 NAME | ||||||||||||||
| 15 | |||||||||||||||
| 16 | OpenTracing::Integration::System - support tracing system calls | ||||||||||||||
| 17 | |||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||
| 19 | |||||||||||||||
| 20 | To create trace each time a system is called: | ||||||||||||||
| 21 | |||||||||||||||
| 22 | use OpenTracing::Any qw($tracer); | ||||||||||||||
| 23 | use OpenTracing::Integration qw(System); | ||||||||||||||
| 24 | system("ls"); | ||||||||||||||
| 25 | |||||||||||||||
| 26 | Additionally, this modules supports span context propagation using the OPENTRACING_CONTEXT environment variable. | ||||||||||||||
| 27 | Any call to system will fill the environment variable OPENTRACING_CONTEXT=<traceid>:<id> in order to propagate | ||||||||||||||
| 28 | down the context to other programs. | ||||||||||||||
| 29 | |||||||||||||||
| 30 | Of course, this requires that the child process implements extracting the span from that variable. | ||||||||||||||
| 31 | Luckily, this perl module can do that for you using: | ||||||||||||||
| 32 | |||||||||||||||
| 33 | use OpenTracing::Any qw($tracer); | ||||||||||||||
| 34 | use OpenTracing::Integration qw(System); | ||||||||||||||
| 35 | |||||||||||||||
| 36 | my $span_context = OpenTracing::Integration::System->extract_context(); | ||||||||||||||
| 37 | my $span; | ||||||||||||||
| 38 | if ($span_context) | ||||||||||||||
| 39 | { | ||||||||||||||
| 40 | $span = $tracer->span(operation_name => 'main', references => [$tracer->child_of($span_context)]); | ||||||||||||||
| 41 | } | ||||||||||||||
| 42 | else | ||||||||||||||
| 43 | { | ||||||||||||||
| 44 | $span = $tracer->span(operation_name => 'main'); | ||||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||||||
| 48 | |||||||||||||||
| 49 | See L<OpenTracing::Integration> for more details. | ||||||||||||||
| 50 | |||||||||||||||
| 51 | =cut | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 1 | ▶ | use Syntax::Keyword::Try; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 54 | 1 | ▶ | use Role::Tiny::With; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 55 | 1 | ▶ | use OpenTracing::Any qw($tracer); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 56 | |||||||||||||||
| 57 | 1 | with qw(OpenTracing::Integration); | |||||||||||||
| 58 | |||||||||||||||
| 59 | 1 | my $loaded; | |||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | ▶ | sub load { | ||||||||||||
|
Subroutine
load: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 62 | 0 | my ($class, $load_deps) = @_; | |||||||||||||
| 63 | 0 | ▶ | unless($loaded++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | ▶ | *CORE::GLOBAL::system = sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||
| 66 | 0 | my @args = @_; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | my $span = $tracer->span(operation_name => "system"); | |||||||||||||
| 69 | 0 | $span->tag('command' => join(" ", @args)); | |||||||||||||
| 70 | 0 | $class->inject_context($span); | |||||||||||||
| 71 | |||||||||||||||
| 72 | 0 | CORE::system(@args); | |||||||||||||
| 73 | |||||||||||||||
| 74 | 0 | ▶ | if ($? == -1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 75 | 0 | my $message = "failed to execute: $!"; | |||||||||||||
| 76 | 0 | $span->tag('error' => 'true'); | |||||||||||||
| 77 | 0 | $span->log($message); | |||||||||||||
| 78 | } elsif ($? & 127) { | ||||||||||||||
| 79 | 0 | ▶ | my $message = sprintf "child died with signal %d, %s coredump", ($? & 127), ($? & 128) ? 'with' : 'without'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 80 | 0 | $span->tag('error' => 'true'); | |||||||||||||
| 81 | 0 | $span->log($message); | |||||||||||||
| 82 | } else { | ||||||||||||||
| 83 | 0 | my $exitcode = ($? >> 8); | |||||||||||||
| 84 | 0 | $span->tag('exitcode' => $exitcode); | |||||||||||||
| 85 | 0 | ▶ | if ($exitcode > 0) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | { | ||||||||||||||
| 87 | 0 | $span->tag('error' => 'true'); | |||||||||||||
| 88 | 0 | $span->log("exitcode greater than 0"); | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | |||||||||||||||
| 93 | 0 | return $?; | |||||||||||||
| 94 | 0 | }; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 0 | ▶ | sub inject_context { | ||||||||||||
|
Subroutine
inject_context: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 99 | 0 | my ($class, $span) = @_; | |||||||||||||
| 100 | |||||||||||||||
| 101 | # Propagate down the context to child process | ||||||||||||||
| 102 | 0 | my %result = %{ $tracer->inject($span->span) }; | |||||||||||||
| 103 | 0 | $ENV{OPENTRACING_CONTEXT} = $result{trace_id} . ":" . $result{id}; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | ▶ | sub extract_context { | ||||||||||||
|
Subroutine
extract_context: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||
| 107 | 0 | my $class = shift; | |||||||||||||
| 108 | 0 | ▶ | return unless $ENV{OPENTRACING_CONTEXT}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | |||||||||||||||
| 110 | 0 | my ($trace_id, $id) = split(':', $ENV{OPENTRACING_CONTEXT}); | |||||||||||||
| 111 | 0 | delete $ENV{OPENTRACING_CONTEXT}; | |||||||||||||
| 112 | |||||||||||||||
| 113 | 0 | return $tracer->extract({ | |||||||||||||
| 114 | trace_id => $trace_id, | ||||||||||||||
| 115 | id => $id | ||||||||||||||
| 116 | }); | ||||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 1 | 1; | |||||||||||||
| 120 | |||||||||||||||
| 121 | __END__ | ||||||||||||||