| 1 | package OpenTracing; | ||
| 2 | # ABSTRACT: supporting for application process monitoring, as defined by opentracing.io | ||
| 3 | |||
| 4 | 7 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 7 | our $VERSION = '1.006'; | |
| 8 | 7 | our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY | |
| 9 | |||
| 10 | 7 | ▶ | no indirect; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 7 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | =encoding utf8 | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | OpenTracing - support for L<https://opentracing.io> application tracing | ||
| 18 | |||
| 19 | =head1 DESCRIPTION | ||
| 20 | |||
| 21 | The OpenTracing standard provides a way to profile and monitor applications | ||
| 22 | across different components and services. | ||
| 23 | |||
| 24 | It's defined by the following specification: | ||
| 25 | |||
| 26 | L<https://github.com/opentracing/specification/blob/master/specification.md> | ||
| 27 | |||
| 28 | and has several "semantic conventions" which provide a common way to | ||
| 29 | include details for common components such as databases, caches and web | ||
| 30 | applications. | ||
| 31 | |||
| 32 | This module currently implements B<version 1.1> of the official specification. | ||
| 33 | |||
| 34 | =head1 Alternative Perl implementations | ||
| 35 | |||
| 36 | Please note that there is a B<separate, independent> OpenTracing implementation | ||
| 37 | in L<OpenTracing::Interface> - it is well-documented and actively maintained, | ||
| 38 | depending on your requirements it may be a better fit. | ||
| 39 | |||
| 40 | If you want good support for frameworks such as L<CGI::Application>, | ||
| 41 | and your code is primarily synchronous, then L<OpenTracing::Interface> | ||
| 42 | would be a good target. | ||
| 43 | |||
| 44 | If you have async code - particularly anything based on L<Future::AsyncAwait> | ||
| 45 | or plain L<Future>s, as used heavily in the L<IO::Async> framework, then | ||
| 46 | L<OpenTracing> may be the better option. | ||
| 47 | |||
| 48 | =head1 OpenTelemetry or OpenTracing? | ||
| 49 | |||
| 50 | The OpenTracing initiative is eventually likely to end up as part of | ||
| 51 | L<https://opentelemetry.io/>, currently in beta. | ||
| 52 | |||
| 53 | There is a separate implementation in L<OpenTelemetry> which will be | ||
| 54 | tracking the progress of this project. The L<OpenTracing::Any> API should | ||
| 55 | remain compatible and existing code which uses the DSL, or the tracer+span | ||
| 56 | interfaces provided by L<OpenTracing::Any> will continue to work even | ||
| 57 | if the OpenTracing upstream project is deprecated by the OpenTelemetry | ||
| 58 | project. | ||
| 59 | |||
| 60 | In short: for now, I'd use L<OpenTracing::Any>. Eventually, L<OpenTelemetry::API> | ||
| 61 | should be interchangeable. | ||
| 62 | |||
| 63 | =head2 How to use this | ||
| 64 | |||
| 65 | There are 3 parts to this: | ||
| 66 | |||
| 67 | =over 4 | ||
| 68 | |||
| 69 | =item * L<add tracing to your code|/Tracing> | ||
| 70 | |||
| 71 | =item * L<set up an opentracing service|/Tracers> | ||
| 72 | |||
| 73 | =item * L<have the top-level application(s) send traces to that service|/Application> | ||
| 74 | |||
| 75 | =back | ||
| 76 | |||
| 77 | =head2 Tracing | ||
| 78 | |||
| 79 | Collecting trace data is similar to a logging module such as L<Log::Any>. | ||
| 80 | Add this line to any module where you want to include tracing information: | ||
| 81 | |||
| 82 | use OpenTracing::Any qw($tracer); | ||
| 83 | |||
| 84 | This will give you an L<OpenTracing::Tracer> instance in the C<< $tracer >> | ||
| 85 | package variable. You can then use this to create L<spans|OpenTracing::Span>: | ||
| 86 | |||
| 87 | my $span = $tracer->span( | ||
| 88 | name => 'example' | ||
| 89 | ); | ||
| 90 | |||
| 91 | The span will be closed automatically when it drops out of scope, and | ||
| 92 | that action will cause the timing to be recorded ready for sending to | ||
| 93 | the OpenTracing server. | ||
| 94 | |||
| 95 | You could also use L<OpenTracing::DSL> for an alternative way to trace blocks of code: | ||
| 96 | |||
| 97 | use OpenTracing::DSL qw(:v1); | ||
| 98 | |||
| 99 | trace { | ||
| 100 | print 'operation starts here'; | ||
| 101 | sleep 2; | ||
| 102 | print 'end of operation'; | ||
| 103 | } operation_name => 'example'; | ||
| 104 | |||
| 105 | This passes the new span as the first parameter to the block, allowing tags | ||
| 106 | for example: | ||
| 107 | |||
| 108 | trace { | ||
| 109 | my $span = shift; | ||
| 110 | $span->tag('request.type' => 'example'); | ||
| 111 | ... | ||
| 112 | }; | ||
| 113 | |||
| 114 | The name defaults to the current sub/method. See L<OpenTracing::DSL> for | ||
| 115 | more details. | ||
| 116 | |||
| 117 | =head3 Integration | ||
| 118 | |||
| 119 | For some common modules and services there are integrations which automatically create | ||
| 120 | spans for operations. If you load L<OpenTracing::Integration::HTTP::Tiny>, for example, | ||
| 121 | all HTTP queries will be traced as if you'd wrapped every C<get>/C<post>/etc. method | ||
| 122 | with tracing code. | ||
| 123 | |||
| 124 | Most of those third-party integrations are in separate distributions, search for | ||
| 125 | C<OpenTracing::Integration> on CPAN for available options. | ||
| 126 | |||
| 127 | If you're feeling lucky, you might also want to add this to your top-level application code: | ||
| 128 | |||
| 129 | use OpenTracing::Integration qw(:all); | ||
| 130 | |||
| 131 | This will go through the list of all modules currently loaded and attempt to | ||
| 132 | enable any matching integrations. | ||
| 133 | |||
| 134 | =head2 Tracers | ||
| 135 | |||
| 136 | Once you have tracing in your code, you'll need to send the traces to an | ||
| 137 | OpenTracing-compatible service, which will collect and present the traces. | ||
| 138 | |||
| 139 | At the time of writing, there is an incomplete list here: | ||
| 140 | |||
| 141 | L<https://opentracing.io/docs/supported-tracers/> | ||
| 142 | |||
| 143 | If you're using Kubernetes, you likely have Jæger available - this is available | ||
| 144 | via C<microk8s.enable jaeger> if you're running L<https://microk8s.io/> for example. | ||
| 145 | |||
| 146 | =head2 Application | ||
| 147 | |||
| 148 | The top-level code (applications, dæmons, cron jobs, microservices, etc.) will need | ||
| 149 | to register a tracer implementation and configure it with the service details, so | ||
| 150 | that the collected data has somewhere to go. | ||
| 151 | |||
| 152 | One such tracer implementation is L<Net::Async::OpenTracing>, designed to work with | ||
| 153 | code that uses the L<IO::Async> event loop. | ||
| 154 | |||
| 155 | use IO::Async::Loop; | ||
| 156 | use Net::Async::OpenTracing; | ||
| 157 | my $loop = IO::Async::Loop->new; | ||
| 158 | $loop->add( | ||
| 159 | my $target = Net::Async::OpenTracing->new( | ||
| 160 | host => 'localhost', | ||
| 161 | port => 6832, | ||
| 162 | protocol => 'jaeger', | ||
| 163 | ) | ||
| 164 | ); | ||
| 165 | OpenTracing->global_tracer->register($target); | ||
| 166 | |||
| 167 | See the L<module documentation|Net::Async::OpenTracing> for more details on the options. | ||
| 168 | |||
| 169 | =head2 Logging | ||
| 170 | |||
| 171 | Log messages can be attached to spans. | ||
| 172 | |||
| 173 | Currently, the recommended way to do this is via L<Log::Any::Adapter::OpenTracing>. | ||
| 174 | |||
| 175 | =head2 More information | ||
| 176 | |||
| 177 | See the following classes for more information: | ||
| 178 | |||
| 179 | =over 4 | ||
| 180 | |||
| 181 | =item * L<OpenTracing::DSL> | ||
| 182 | |||
| 183 | =item * L<OpenTracing::Span> | ||
| 184 | |||
| 185 | =item * L<OpenTracing::SpanProxy> | ||
| 186 | |||
| 187 | =item * L<OpenTracing::Log> | ||
| 188 | |||
| 189 | =item * L<OpenTracing::Process> | ||
| 190 | |||
| 191 | =back | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | 7 | ▶ | use OpenTracing::Tag; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 196 | 7 | ▶ | use OpenTracing::Log; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 197 | 7 | ▶ | use OpenTracing::Span; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 198 | 7 | ▶ | use OpenTracing::SpanProxy; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 199 | 7 | ▶ | use OpenTracing::SpanContext; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 200 | 7 | ▶ | use OpenTracing::Process; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 201 | 7 | ▶ | use OpenTracing::Tracer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 202 | 7 | ▶ | use OpenTracing::Reference; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 203 | |||
| 204 | 7 | our $TRACER = OpenTracing::Tracer->new; | |
| 205 | |||
| 206 | =head1 METHODS | ||
| 207 | |||
| 208 | =head2 global_tracer | ||
| 209 | |||
| 210 | Returns the default tracer instance. | ||
| 211 | |||
| 212 | my $span = OpenTracing->global_tracer->span(name => 'test'); | ||
| 213 | |||
| 214 | This is the same instance used by L<OpenTracing::Any> and L<OpenTracing::DSL>. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | 8 | ▶ | sub global_tracer { $TRACER } |
|
Subroutine
global_tracer: called CC=1 SCAR=0.0
Pod
documented
| |||
| 219 | |||
| 220 | =head2 set_global_tracer | ||
| 221 | |||
| 222 | Replaces the current global tracer with the given one. | ||
| 223 | |||
| 224 | OpenTracing->set_global_tracer($tracer); | ||
| 225 | |||
| 226 | Note that a typical application would only need a single instance, and the | ||
| 227 | default should normally be good enough. | ||
| 228 | |||
| 229 | B<If you want to set up where the traces should go>, see | ||
| 230 | L<OpenTracing::Tracer/register> instead. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | 0 | ▶ | sub set_global_tracer { $TRACER = $_[1] } |
|
Subroutine
set_global_tracer: not called CC=1 SCAR=6.9
Pod
documented
| |||
| 235 | |||
| 236 | 7 | 1; | |
| 237 | |||
| 238 | __END__ | ||