blib/lib/MFab/Plugins/Datadog.pm

filter: Statementstmt 69.8% 95 / 136 Branchbran 37.5% 15 / 40 Conditioncond 28.0% 7 / 25 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 85.0% 17 / 20 Podpod 100% 12 / 12 totaltotal 58.8% 146 / 248 CC 34 SCAR 26.4
CC
34
Cov
58%
endSpan
42.0
startSpan
30.0
submitDatadog
25.2
CRAP 14.0
↑ Summary
1 package MFab::Plugins::Datadog 1.0;
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 MFab::Plugins::Datadog - Mojolicious plugin for Datadog APM integration
8  
9 =head1 SYNOPSIS
10  
11 # In your Mojolicious application
12 push(@{ $self->plugins->namespaces }, 'MFab::Plugins');
13  
14 $app->plugin('Datadog', {
15 enabled => "true",
16 service => "MyApp",
17 serviceEnv => "production"
18 });
19  
20 =head1 DESCRIPTION
21  
22 This module provides seamless integration between Mojolicious web applications and Datadog's Application Performance Monitoring (APM) system. It automatically instruments your Mojolicious application to send distributed traces and metrics to Datadog, giving you visibility into:
23  
24 =over 4
25  
26 =item * HTTP request/response cycles
27  
28 =item * Route matching and dispatch timing
29  
30 =item * Controller action execution
31  
32 =item * Custom application spans
33  
34 =back
35  
36 The plugin automatically propagates trace context across service boundaries using Datadog's trace headers, enabling distributed tracing across your microservices architecture.
37  
38 =head1 CUSTOM SPANS
39  
40 When you want to measure a specific function, you can use the startSpan/endSpan functions to mark the start and end points:
41  
42 sub customHandler {
43 my($c) = @_;
44 my $span = startSpan($c->tx, "TestCode::customHandler", "customHandler");
45 # do work here
46 sleep(10);
47 endSpan($span);
48 }
49  
50 =head1 NOTES
51  
52 =over 4
53  
54 =item * If the worker is killed through a heartbeat failure, the spans for that worker won't be sent
55  
56 =item * Websockets only generate a mojolicious-transaction span
57  
58 =back
59  
60 =head1 FUNCTIONS
61  
62 =cut
63  
64 2 ▶use Mojo::Base qw(Mojolicious::Plugin -signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 2 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶use Crypt::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use Math::Pari;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 our @EXPORT_OK = qw(startSpan endSpan);
72  
73 # Keep track of all outstanding transactions
74 2 my(%transactions);
75  
76 =head2 register()
77  
78 Register Mojolicious plugin and hook into the application - called by the Mojolicious framework
79  
80 It accepts the following config items
81  
82 =over 4
83  
84 =item * "datadogHost" - the datadog agent host, also looks in the ENV for "DD_AGENT_HOST", defaults to "localhost"
85  
86 =item * "enabled" - if "true", traces are sent to datadog, also looks in the ENV for "DD_TRACE_ENABLED", defaults to "false"
87  
88 =item * "service" - the value to send to datadog for the service name, defaults to "MFab::Plugins::Datadog"
89  
90 =item * "serviceEnv" - the value to send to datadog for the service environment, defaults to "test"
91  
92 =back
93  
94 =cut
95  
96 1 ▶sub register ($self, $app, $args) {
Subroutine register: called CC=4 SCAR=14.1
Pod documented
97 1 ▶ if(not $args->{service}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"service"}) 1 0 1
98 1 $args->{service} = "MFab::Plugins::Datadog";
99 }
100 1 $args->{datadogHost} = configItem($args->{datadogHost}, "DD_AGENT_HOST", "localhost");
101 1 $args->{enabled} = configItem($args->{enabled}, "DD_TRACE_ENABLED", "false") eq "true";
102 1 $args->{datadogURL} = "http://".$args->{datadogHost}.":8126/v0.3/traces";
103 1 ▶ $args->{serviceEnv} = $args->{serviceEnv} || "test";
Condition50%1/2
$args->{'serviceEnv'} || 'test'
l!l
01
MC/DC0%0/1
$args->{'serviceEnv'} || 'test'
$args->{'serviceEnv'}
Truth table50%1/2
A
A= $args->{'serviceEnv'}
Aresult
11
00
104  
105 1 ▶ if($args->{enabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"enabled"}) 1 0 1
106 1 $app->hook(around_action => \&aroundActionHook);
107 1 $app->hook(after_dispatch => \&afterDispatchHook);
108 1 ▶ $app->hook(after_build_tx => sub ($tx, $app) { afterBuildTxHook($tx, $app, $args) });
Subroutine __ANON__: called CC=1 SCAR=0.0
109 }
110 }
111  
112 =head2 datadogId()
113  
114 Generate a 64 bit integer that JSON::XS will serialize as an integer
115  
116 =cut
117  
118 6 ▶sub datadogId () {
Subroutine datadogId: called CC=1 SCAR=0.0
Pod documented
119 6 my $id = Crypt::Random::makerandom(Size => 64, Strength => 0);
120 6 return Math::Pari::pari2iv($id);
121 }
122  
123 =head2 configItem($config_host)
124  
125 Get the config item from: the app setting, the environment variable, or use the default
126  
127 =cut
128  
129 2 ▶sub configItem ($appSetting, $envName, $default) {
Subroutine configItem: called CC=3 SCAR=11.4
Pod documented
130 2 ▶ if(not $appSetting) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($appSetting) 1 1 2
131 1 ▶ $appSetting = $ENV{$envName} || $default;
Condition33%1/3
$ENV{$envName} || $default
l!l&&r!l&&!r
010
MC/DC0%0/2
$ENV{$envName} || $default
$ENV{$envName} $default
Truth table33%1/3
A || B
A= $ENV{$envName}B= $default
ABresult
1X1
011
000
132 }
133 2 return $appSetting;
134 }
135  
136 =head2 setTraceId($c, $connection_data)
137  
138 Set the traceid in the connection data
139  
140 =cut
141  
142 3 ▶sub setTraceId ($tx, $connection_data) {
Subroutine setTraceId: called CC=3 SCAR=11.2
Pod documented
143 3 ▶ if(defined $connection_data->{traceid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connection_data->{"traceid"}) 2 1 3
144 2 return;
145 }
146  
147 1 $connection_data->{traceid} = $tx->req->headers->header("x-datadog-trace-id");
148 1 ▶ if($connection_data->{traceid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection_data->{'traceid'}) { } 0 1 1
149 0 $connection_data->{traceid} = int($connection_data->{traceid});
150 } else {
151 1 $connection_data->{traceid} = datadogId();
152 1 $tx->req->headers->header("x-datadog-trace-id" => $connection_data->{traceid});
153 }
154 }
155  
156 =head2 aroundActionHook()
157  
158 The around_action hook - wrapped around the action
159  
160 =cut
161  
162 1 ▶sub aroundActionHook ($next, $c, $action, $last) {
Subroutine aroundActionHook: called CC=4 SCAR=14.3
Pod documented
163 1 ▶ my $connection_data = $transactions{$c->tx} || {};
Condition50%1/2
$transactions{$c->tx} || {}
l!l
10
MC/DC0%0/1
$transactions{$c->tx} || {}
$transactions{$c->tx}
Truth table50%1/2
A
A= $transactions{$c->tx}
Aresult
11
00
164  
165 1 $connection_data->{action_start} = [gettimeofday()];
166 1 $connection_data->{action_spanid} = datadogId();
167 1 $connection_data->{current_spanid} = $connection_data->{dispatch_spanid};
168 1 $connection_data->{after_dispatch} = 0;
169  
170 1 setTraceId($c->tx, $connection_data);
171  
172 1 $connection_data->{parentid} = $c->tx->req->headers->header("x-datadog-parent-id");
173 1 ▶ if($connection_data->{parentid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection_data->{"parentid"}) 0 1 1
174 0 $connection_data->{parentid} = int($connection_data->{parentid});
175 }
176 1 $c->tx->req->headers->header("x-datadog-parent-id" => $connection_data->{action_spanid});
177  
178 1 my $retval = $next->();
179  
180 1 $connection_data->{action_duration} = tv_interval($connection_data->{action_start});
181 1 $connection_data->{is_sync} = $connection_data->{after_dispatch};
182  
183 1 my $route = $c->match->endpoint;
184 # "/" doesn't have a pattern
185 1 ▶ $connection_data->{pattern} = $route->pattern->unparsed || $c->req->url;
Condition33%1/3
$route->pattern->unparsed || $c->req->url
l!l&&r!l&&!r
010
MC/DC0%0/2
$route->pattern->unparsed || $c->req->url
$route->pattern->unparsed $c->req->url
Truth table33%1/3
A || B
A= $route->pattern->unparsedB= $c->req->url
ABresult
1X1
011
000
186  
187 1 return $retval;
188 }
189  
190 =head2 afterDispatchHook()
191  
192 The after_dispatch hook - called after the request is finished the sync stage of processing, more async processing can happen after this
193  
194 =cut
195  
196 1 ▶sub afterDispatchHook ($c) {
Subroutine afterDispatchHook: called CC=3 SCAR=11.6
Pod documented
197 1 ▶ my $connection_data = $transactions{$c->tx} || {};
Condition50%1/2
$transactions{$c->tx} || {}
l!l
10
MC/DC0%0/1
$transactions{$c->tx} || {}
$transactions{$c->tx}
Truth table50%1/2
A
A= $transactions{$c->tx}
Aresult
11
00
198  
199 1 ▶ if(not defined($connection_data->{action_start})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $connection_data->{"action_start"}) 0 1 1
200 0 $connection_data->{action_start} = [gettimeofday()];
201 }
202  
203 1 setTraceId($c->tx, $connection_data);
204  
205 1 $connection_data->{after_dispatch} = 1;
206 1 $connection_data->{current_spanid} = $connection_data->{dispatch_spanid};
207 1 $connection_data->{dispatch_duration} = tv_interval($connection_data->{action_start});
208 }
209  
210 =head2 afterBuildTxHook()
211  
212 The after_build_tx hook - called after the transaction is built but before it is parsed
213  
214 =cut
215  
216 1 ▶sub afterBuildTxHook ($tx, $app, $args) {
Subroutine afterBuildTxHook: called CC=1 SCAR=0.0
Pod documented
217 1 my $connection_data = {
218 spans => [],
219 };
220 1 $transactions{$tx} = $connection_data;
221 1 $connection_data->{tx_spanid} = datadogId();
222 1 $connection_data->{dispatch_spanid} = datadogId();
223 1 $connection_data->{current_spanid} = $connection_data->{tx_spanid};
224 1 $connection_data->{build_tx_start} = [gettimeofday()];
225  
226 1 ▶ $tx->on(finish => sub ($tx) {
Subroutine __ANON__: called CC=1 SCAR=0.0
227 # websockets skip dispatch & action hooks
228 1 setTraceId($tx, $connection_data);
229 1 $connection_data->{url} = $tx->req->url->path;
230 1 $connection_data->{method} = $tx->req->method;
231 1 $connection_data->{code} = $tx->res->code;
232 1 $connection_data->{tx_duration} = tv_interval($connection_data->{build_tx_start});
233 1 submitDatadog($app, $connection_data, $args);
234 1 $transactions{$tx} = undef;
235 1 });
236 }
237  
238 =head2 startSpan($tx, $name, $resource, [$parent_id])
239  
240 Start a new span, associates it to the transaction via $tx
241  
242 =cut
243  
244 0 ▶sub startSpan ($tx, $name, $resource, $parent_id = undef) {
Subroutine startSpan: not called CC=5 SCAR=34.0
Pod documented
245 # we don't have a transaction, so we can't send this span
246 0 ▶ if(not defined($tx)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tx) 0 0 0
247 return {
248 0 "no_tx" => 1,
249 };
250 }
251 0 ▶ my $connection_data = $transactions{$tx} || {};
Condition0%0/2
$transactions{$tx} || {}
l!l
00
MC/DC0%0/1
$transactions{$tx} || {}
$transactions{$tx}
Truth table0%0/2
A
A= $transactions{$tx}
Aresult
11
00
252 my $span = {
253 "name" => $name,
254 "resource" => $resource,
255 "start" => [gettimeofday()],
256 "span_id" => datadogId(),
257 "parent_id" => $parent_id || $connection_data->{current_spanid},
258 0 ▶ "type" => "web",
Condition0%0/3
$parent_id || $connection_data->{'current_spanid'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$parent_id || $connection_data->{'current_spanid'}
$parent_id $connection_data->{'current_spanid'}
Truth table0%0/3
A || B
A= $parent_idB= $connection_data->{'current_spanid'}
ABresult
1X1
011
000
259 "meta" => {},
260 };
261 0 push(@{$connection_data->{spans}}, $span);
262 0 return $span;
263 }
264  
265 =head2 endSpan($span, [$error_message])
266  
267 End a span, optional error message
268  
269 =cut
270  
271 0 ▶sub endSpan ($span, $error_message = undef) {
Subroutine endSpan: not called CC=6 SCAR=37.4
Pod documented
272 0 ▶ if($span->{no_tx}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($span->{"no_tx"}) 0 0 0
273 0 return;
274 }
275 # we've already submitted this span, likely due to a timeout
276 0 ▶ if($span->{meta}{"mojolicious.unclosed"} or ref($span->{start}) ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if ($span->{"meta"}{"mojolicious.unclosed"} or ref $span->{"start"} ne "ARRAY") 0 0 0
Condition0%0/3
$span->{"meta"}{"mojolicious.unclosed"} or ref $span->{"start"} ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
$span->{"meta"}{"mojolicious.unclosed"} or ref $span->{"start"} ne "ARRAY"
$span->{"meta"}{"mojolicious.unclosed"} ref $span->{"start"} ne "ARRAY"
Truth table0%0/3
A or B
A= $span->{"meta"}{"mojolicious.unclosed"}B= ref $span->{"start"} ne "ARRAY"
ABresult
1X1
011
000
277 0 return;
278 }
279 0 $span->{duration} = durationToDatadog(tv_interval($span->{start}));
280 0 $span->{start} = timestampToDatadog($span->{start});
281 0 ▶ if($error_message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error_message) 0 0 0
282 0 $span->{error} = 1;
283 0 $span->{meta} = { "error.message" => "$error_message" };
284 }
285 }
286  
287 =head2 timestampToDatadog($timestamp)
288  
289 Datadog wants number of nanoseconds since the epoch
290  
291 =cut
292  
293 3 ▶sub timestampToDatadog ($timestamp) {
Subroutine timestampToDatadog: called CC=2 SCAR=8.1
Pod documented
294 3 ▶ if (not defined($timestamp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $timestamp) 0 3 3
295 0 return undef;
296 }
297 3 return $timestamp->[0] * 1000000000 + $timestamp->[1] * 1000;
298 }
299  
300 =head2 durationToDatadog($duration)
301  
302 Datadog wants duration in nanoseconds
303  
304 =cut
305  
306 3 ▶sub durationToDatadog ($duration) {
Subroutine durationToDatadog: called CC=2 SCAR=8.1
Pod documented
307 3 ▶ if (not defined($duration)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $duration) 0 3 3
308 0 return undef;
309 }
310 3 return int($duration * 1000000000);
311 }
312  
313 =head2 submitDatadog($app, $connection_data, $args)
314  
315 Submit spans to datadog agent
316  
317 =cut
318  
319 1 ▶sub submitDatadog ($app, $connection_data, $args) {
Subroutine submitDatadog: called CC=9 SCAR=32.3
Pod documented
320 1 ▶ my $pattern = $connection_data->{pattern} || $connection_data->{url};
Condition33%1/3
$connection_data->{'pattern'} || $connection_data->{'url'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$connection_data->{'pattern'} || $connection_data->{'url'}
$connection_data->{'pattern'} $connection_data->{'url'}
Truth table33%1/3
A || B
A= $connection_data->{'pattern'}B= $connection_data->{'url'}
ABresult
1X1
011
000
321 1 ▶ my $is_sync = $connection_data->{is_sync} || 0;
Condition50%1/2
$connection_data->{'is_sync'} || 0
l!l
10
MC/DC0%0/1
$connection_data->{'is_sync'} || 0
$connection_data->{'is_sync'}
Truth table50%1/2
A
A= $connection_data->{'is_sync'}
Aresult
11
00
322 my %meta = (
323 "env" => $args->{serviceEnv},
324 "api.endpoint.route" => $pattern,
325 "http.path_group" => $connection_data->{pattern},
326 "http.method" => $connection_data->{method},
327 "http.url_details.path" => $connection_data->{url},
328 1 "process_id" => "$$",
329 "language" => "perl",
330 "mojolicious.sync" => "$is_sync",
331 );
332 1 ▶ if(defined($connection_data->{code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connection_data->{"code"}) 1 0 1
333 1 $meta{"http.status_code"} = "".$connection_data->{code};
334 }
335  
336 1 my @spans = @{$connection_data->{spans}};
337  
338 1 for my $span (@spans) {
339 0 $span->{meta}{env} = $meta{env};
340 0 $span->{meta}{process_id} = "$$";
341 0 $span->{meta}{language} = "perl";
342 0 $span->{trace_id} = $connection_data->{traceid};
343 0 ▶ if(not defined($span->{duration})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $span->{"duration"}) 0 0 0
344 0 $span->{duration} = durationToDatadog(tv_interval($span->{start}));
345 0 $span->{start} = timestampToDatadog($span->{start});
346 0 $span->{meta}{"mojolicious.unclosed"} = "true";
347 0 $span->{error} = 1;
348 0 $span->{meta} = { "error.message" => "Span was not finished when it was sent to the platform" };
349 }
350  
351 0 $span->{service} = $args->{service};
352 }
353  
354 1 ▶ if(defined($connection_data->{action_duration})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connection_data->{"action_duration"}) 1 0 1
355 push(@spans,
356 {
357 "duration" => durationToDatadog($connection_data->{action_duration}),
358 "meta" => \%meta,
359 "name" => "mojolicious-action",
360 "resource" => $pattern,
361 "service" => $args->{service},
362 "span_id" => $connection_data->{action_spanid},
363 "start" => timestampToDatadog($connection_data->{action_start}),
364 "trace_id" => $connection_data->{traceid},
365 "parent_id" => $connection_data->{tx_spanid},
366 1 "type" => "web",
367 });
368 }
369  
370 1 ▶ if($connection_data->{after_dispatch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection_data->{"after_dispatch"}) 1 0 1
371 push(@spans,
372 {
373 "duration" => durationToDatadog($connection_data->{dispatch_duration}),
374 "meta" => \%meta,
375 "name" => "mojolicious-dispatch",
376 "resource" => $pattern,
377 "service" => $args->{service},
378 "span_id" => $connection_data->{dispatch_spanid},
379 "start" => timestampToDatadog($connection_data->{action_start}),
380 "trace_id" => $connection_data->{traceid},
381 "parent_id" => $connection_data->{tx_spanid},
382 1 "type" => "web",
383 }
384 );
385 }
386  
387 my $tx_span = {
388 "duration" => durationToDatadog($connection_data->{tx_duration}),
389 "meta" => \%meta,
390 "name" => "mojolicious-transaction",
391 "resource" => $pattern,
392 "service" => $args->{service},
393 "span_id" => $connection_data->{tx_spanid},
394 "start" => timestampToDatadog($connection_data->{build_tx_start}),
395 "trace_id" => $connection_data->{traceid},
396 1 "type" => "web",
397 };
398 1 ▶ if($connection_data->{parentid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($connection_data->{"parentid"}) 0 1 1
399 0 $tx_span->{"parent_id"} = $connection_data->{parentid};
400 }
401 1 push(@spans, $tx_span);
402  
403 0 ▶ $app->ua->put($args->{datadogURL}, json => [ \@spans ], sub ($ua, $tx) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
404 0 ▶ if($tx->res->is_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->is_error) 0 0 0
405 0 $app->log->error("HTTP Error sending to datadog: ".$tx->res->code." ".$tx->res->body);
406 0 return;
407 }
408 # Errors without a HTTP status code like connection refused & timeout
409 0 ▶ if($tx->res->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->res->error) 0 0 0
410 0 $app->log->error("Error sending to datadog: ".$tx->res->error->{message});
411 0 return;
412 }
413 1 });
414 }
415  
416 2 1;
↑ Summary