File Coverage

blib/lib/OpenTracing.pm
Criterion Covered Total %
statement 37 38 97.3
branch n/a
condition n/a
subroutine 13 14 92.8
pod 2 2 100.0
total 52 54 96.3


line stmt bran cond sub pod time code
1             package OpenTracing;
2             # ABSTRACT: supporting for application process monitoring, as defined by opentracing.io
3              
4 2     2   14 use strict;
  2         4  
  2         61  
5 2     2   10 use warnings;
  2         10  
  2         90  
6              
7             our $VERSION = '1.006';
8             our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
9              
10 2     2   10 no indirect;
  2         3  
  2         9  
11 2     2   94 use utf8;
  2         5  
  2         30  
12              
13             =encoding utf8
14              
15             =head1 NAME
16              
17             OpenTracing - support for L 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
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 of the official specification.
33              
34             =head1 Alternative Perl implementations
35              
36             Please note that there is a B OpenTracing implementation
37             in L - 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,
41             and your code is primarily synchronous, then L
42             would be a good target.
43              
44             If you have async code - particularly anything based on L
45             or plain Ls, as used heavily in the L framework, then
46             L 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, currently in beta.
52              
53             There is a separate implementation in L which will be
54             tracking the progress of this project. The L API should
55             remain compatible and existing code which uses the DSL, or the tracer+span
56             interfaces provided by L 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. Eventually, L
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
70              
71             =item * L
72              
73             =item * L
74              
75             =back
76              
77             =head2 Tracing
78              
79             Collecting trace data is similar to a logging module such as L.
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 instance in the C<< $tracer >>
85             package variable. You can then use this to create L:
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 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 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, for example,
121             all HTTP queries will be traced as if you'd wrapped every C/C/etc. method
122             with tracing code.
123              
124             Most of those third-party integrations are in separate distributions, search for
125             C 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
142              
143             If you're using Kubernetes, you likely have Jæger available - this is available
144             via C if you're running L 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, designed to work with
153             code that uses the L 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 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.
174              
175             =head2 More information
176              
177             See the following classes for more information:
178              
179             =over 4
180              
181             =item * L
182              
183             =item * L
184              
185             =item * L
186              
187             =item * L
188              
189             =item * L
190              
191             =back
192              
193             =cut
194              
195 2     2   1009 use OpenTracing::Tag;
  2         4  
  2         63  
196 2     2   1140 use OpenTracing::Log;
  2         5  
  2         62  
197 2     2   867 use OpenTracing::Span;
  2         4  
  2         74  
198 2     2   858 use OpenTracing::SpanProxy;
  2         5  
  2         75  
199 2     2   873 use OpenTracing::SpanContext;
  2         6  
  2         72  
200 2     2   881 use OpenTracing::Process;
  2         5  
  2         87  
201 2     2   901 use OpenTracing::Tracer;
  2         5  
  2         82  
202 2     2   14 use OpenTracing::Reference;
  2         3  
  2         228  
203              
204             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 and L.
215              
216             =cut
217              
218 5     5 1 12 sub global_tracer { $TRACER }
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, see
230             L instead.
231              
232             =cut
233              
234 0     0 1   sub set_global_tracer { $TRACER = $_[1] }
235              
236             1;
237              
238             __END__