1
1
from datadog_lambda .pb .span_pb2 import Span
2
2
from datadog_lambda .pb .trace_pb2 import APITrace
3
3
from datadog_lambda .pb .trace_payload_pb2 import TracePayload
4
+ from datadog_lambda import __version__
5
+ import urllib2
4
6
5
7
6
8
class TraceConnection :
7
9
def __init__ (self , rootURL , apiKey ):
8
10
self ._traceURL = "https://trace.agent.{}/api/v0.2/traces" .format (rootURL )
11
+ self ._apiKey = apiKey
9
12
10
- def send_traces (traces ):
11
- pass
12
-
13
- def convert_trace (traces ):
14
- pass
13
+ def send_traces (self , spans ):
14
+ trace_payload = convert_trace_to_protobuf_payload (spans )
15
+ data = trace_payload .SerializeToString ()
16
+ user_agent = "aws_lambda/{}/1 (http://localhost)" .format (__version__ )
17
+ cont_len = len (data )
18
+ headers = {
19
+ "Content-Type" : "application/x-protobuf" ,
20
+ "Content-Encoding" : "identity" ,
21
+ "DD-Api-Key" : self ._apiKey ,
22
+ "User-Agent" : user_agent ,
23
+ "Content-Length" : cont_len ,
24
+ }
25
+ try :
26
+ request = urllib2 .Request (self ._traceURL , data , headers )
27
+ response = urllib2 .urlopen (request )
28
+ except urllib2 .HTTPError as e :
29
+ print ("request to {} failed with error {}" .format (self ._traceURL , e ))
15
30
16
31
17
32
def convert_trace_to_protobuf_payload (trace ):
@@ -25,14 +40,18 @@ def convert_trace_to_protobuf_payload(trace):
25
40
else :
26
41
span_groups [trace_id ] = span_group
27
42
43
+ parent_id = None
44
+ if parent_id in span :
45
+ parent_id = int (span ["parent_id" ])
46
+
28
47
span_group .append (
29
48
Span (
30
49
service = span ["service" ],
31
50
name = span ["name" ],
32
51
resource = span ["resource" ],
33
52
traceID = trace_id ,
34
53
spanID = int (span ["span_id" ]),
35
- parentID = int ( span [ " parent_id" ]) ,
54
+ parentID = parent_id ,
36
55
start = span ["start" ],
37
56
duration = span ["duration" ],
38
57
error = span ["error" ],
0 commit comments