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