DataDog/Agent.pm

filter: Statementstmt 57.1% 32 / 56 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 2 totaltotal 54.0% 40 / 74 CC 2 SCAR 6.9
CC
2
Cov
55%
_http_post_struct_as_json
6.0
_build_user_agent
2.0
_build_uri
2.0
CRAP 2.0
« DataDog/Span.pm ↑ Summary DataDog/Utils.pm »
1 package OpenTracing::Implementation::DataDog::Agent;
2  
3 =head1 NAME
4  
5 OpenTracing::Implementation::DataDog::Agent - A Client that sends off the data
6  
7 =head1 SYNOPSIS
8  
9 use alias OpenTracing::Implementation::DataDog::Agent;
10
11 my $dd_agent = Agent->new(
12 user_agent => LWP::UserAgent->new();
13 host => 'localhost',
14 port => '8126',
15 path => 'v0.3/traces',
16 ); # these are defaults
17  
18 and later:
19  
20 $dd_agent->send_span( $span );
21  
22 =cut
23  
24 2 our $VERSION = 'v0.30.1';
25  
26 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use MooX::Attribute::ENV;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use PerlX::Maybe qw/maybe provided/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use Types::Standard qw/HasMethods/;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36  
37  
38 2 has user_agent => (
39 is => 'lazy',
40 isa => HasMethods[qw/request/],
41 );
42  
43 sub _build_user_agent {
44 0 ▶ return LWP::UserAgent->new( )
Subroutine _build_user_agent: not called CC=1 SCAR=6.9
45 }
46  
47  
48  
49 2 has host => (
50 is => 'ro',
51 env_key => 'DD_AGENT_HOST',
52 default => 'localhost',
53 );
54  
55  
56  
57 2 has port => (
58 is => 'ro',
59 env_key => 'DD_TRACE_AGENT_PORT',
60 default => '8126',
61 );
62  
63  
64  
65 2 has path => (
66 is => 'ro',
67 default => 'v0.3/traces',
68 );
69  
70  
71  
72 2 has uri => (
73 is => 'lazy',
74 init_arg => undef,
75 );
76  
77 sub _build_uri {
78 0 ▶ my $self = shift;
Subroutine _build_uri: not called CC=1 SCAR=6.9
79
80 0 return "http://$self->{ host }:$self->{ port }/$self->{ path }"
81 }
82  
83  
84  
85 2 has _json_encoder => (
86 is => 'lazy',
87 init_arg => undef,
88 handles => { json_encode => 'encode' },
89 );
90  
91 sub _build__json_encoder {
92 0 ▶ JSON()->new->utf8->canonical->pretty
Subroutine _build__json_encoder: not called CC=1 SCAR=6.9
93 }
94  
95  
96  
97 sub send_span {
98 0 ▶ my $self = shift;
Subroutine send_span: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my $span = shift;
100
101 0 my $data = __PACKAGE__->to_struct( $span );
102
103 0 my $resp = $self->_http_post_struct_as_json( [[ $data ]] );
104
105 0 return $resp->is_success
106 }
107  
108  
109  
110 # to_struct
111 #
112 # Gather required data from the span and it's context, tags and baggage items.
113 # this data structure is specific for sending it through the DataDog agent and
114 # therefore can not be a intance method of the DataDog::Span object.
115 #
116 sub to_struct {
117 0 ▶ my $class = shift;
Subroutine to_struct: not called CC=1 SCAR=6.9
Pod undocumented
118 0 my $span = shift;
119
120 0 my $context = $span->get_context();
121
122 0 my $meta_data = {
123 $span->get_tags,
124 $context->get_baggage_items,
125 };
126
127 0 my $data = {
128 trace_id => $context->trace_id,
129 span_id => $span->span_id,
130 resource => $context->resource_name,
131 service => $context->service_name,
132
133 maybe
134 type => $context->service_type,
135
136 name => $span->operation_name,
137 start => $span->nano_seconds_start_time(),
138 duration => $span->nano_seconds_duration(),
139
140 maybe
141 parent_id => $span->parent_span_id(),
142
143 # error => ... ,
144
145 provided %$meta_data,
146 meta => $meta_data,
147
148 # metrics => ... ,
149 };
150
151 # TODO: use Hash::Ordered, so we can control what will be the first item in
152 # the long string of JSON text. But this needs investigation on how
153 # this behaves with JSON
154
155 0 return $data
156 }
157  
158  
159  
160 sub _http_post_struct_as_json {
161 0 ▶ my $self = shift;
Subroutine _http_post_struct_as_json: not called CC=2 SCAR=17.9
162 0 my $struct = shift;
163
164 0 my $encoded_data = $self->json_encode($struct);
165 0 do { warn "$encoded_data\n" }
166 0 ▶ if $ENV{OPENTRACING_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"OPENTRACING_DEBUG"}) 0 0 0
167
168
169 0 my $header = ['Content-Type' => 'application/json; charset=UTF-8'];
170 0 my $rqst = HTTP::Request->new( 'POST', $self->uri, $header, $encoded_data );
171
172 0 my $resp = $self->user_agent->request( $rqst );
173
174 0 return $resp;
175 }
176  
177  
178  
179 2 1;
« DataDog/Span.pm ↑ Summary DataDog/Utils.pm »