-
Notifications
You must be signed in to change notification settings - Fork 45
Remove aws-xray-sdk #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove aws-xray-sdk #171
Changes from all commits
e14dd83
2cd0228
83d7914
b3b8a35
ed5f65d
999a735
8072ed4
117209f
ab29cee
6139e39
15374e0
331ab13
5d098a1
ded2793
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
Component,Origin,License,Copyright | ||
aws-xray-sdk-python,github.com/aws/aws-xray-sdk-python,Apache-2.0, | ||
flake8,gitlab.com/pycqa/flake8,MIT,"Copyright (C) 2011-2013 Tarek Ziade <[email protected]>. Copyright (C) 2012-2016 Ian Cordasco <[email protected]>." | ||
nose2,github.com/nose-devs/nose2,BSD-2-Clause,"Copyright (c) 2012, Jason Pellerin. All rights reserved." | ||
requests,github.com/kennethreitz/requests,Apache-2.0,"Copyright 2018 Kenneth Reitz" | ||
wrapt,github.com/GrahamDumpleton/wrapt,BSD-2-Clause,"Copyright (c) 2013-2019, Graham Dumpleton" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import os | ||
import logging | ||
import json | ||
import binascii | ||
import time | ||
import socket | ||
|
||
from datadog_lambda.constants import XrayDaemon, XraySubsegment, TraceContextSource | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_xray_host_port(adress): | ||
if adress == "": | ||
logger.debug("X-Ray daemon env var not set, not sending sub-segment") | ||
return None | ||
parts = adress.split(":") | ||
if len(parts) <= 1: | ||
logger.debug("X-Ray daemon env var not set, not sending sub-segment") | ||
return None | ||
port = int(parts[1]) | ||
host = parts[0] | ||
return (host, port) | ||
|
||
|
||
def send(host_port_tuple, payload): | ||
sock = None | ||
try: | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
sock.setblocking(0) | ||
sock.connect(host_port_tuple) | ||
sock.send(payload.encode("utf-8")) | ||
except Exception as e_send: | ||
logger.error("Error occurred submitting to xray daemon: %s", str(e_send)) | ||
try: | ||
sock.close() | ||
except Exception as e_close: | ||
logger.error("Error while closing the socket: %s", str(e_close)) | ||
|
||
|
||
def build_segment_payload(payload): | ||
if payload is None: | ||
return None | ||
return '{"format": "json", "version": 1}' + "\n" + payload | ||
|
||
|
||
def parse_xray_header(raw_trace_id): | ||
# Example: Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;Sampled=1 | ||
logger.debug("Reading trace context from env var %s", raw_trace_id) | ||
if len(raw_trace_id) == 0: | ||
return None | ||
parts = raw_trace_id.split(";") | ||
if len(parts) != 3: | ||
return None | ||
root = parts[0].replace("Root=", "") | ||
parent = parts[1].replace("Parent=", "") | ||
sampled = parts[2].replace("Sampled=", "") | ||
if ( | ||
len(root) == len(parts[0]) | ||
or len(parent) == len(parts[1]) | ||
or len(sampled) == len(parts[2]) | ||
): | ||
return None | ||
return { | ||
"parent_id": parent, | ||
"trace_id": root, | ||
"sampled": sampled, | ||
"source": TraceContextSource.XRAY, | ||
} | ||
|
||
|
||
def generate_random_id(): | ||
return binascii.b2a_hex(os.urandom(8)).decode("utf-8") | ||
|
||
|
||
def build_segment(context, key, metadata): | ||
|
||
segment = json.dumps( | ||
{ | ||
"id": generate_random_id(), | ||
"trace_id": context["trace_id"], | ||
"parent_id": context["parent_id"], | ||
"name": XraySubsegment.NAME, | ||
"start_time": time.time(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just want to verify that time.time() returns in the correct unit: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html . I believe there might be a bug in the node version where this timestamp is off by an order of magnitude. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've checked at https://github.com/aws/aws-xray-sdk-python/blob/894b419518e14b5cb2a75293ed4da17f87b2a7c0/aws_xray_sdk/core/models/entity.py#L39 and it seems correct? |
||
"end_time": time.time(), | ||
"type": "subsegment", | ||
"metadata": { | ||
XraySubsegment.NAMESPACE: { | ||
key: metadata, | ||
} | ||
}, | ||
} | ||
) | ||
return segment | ||
|
||
|
||
def send_segment(key, metadata): | ||
host_port_tuple = get_xray_host_port( | ||
os.environ.get(XrayDaemon.XRAY_DAEMON_ADDRESS, "") | ||
) | ||
if host_port_tuple is None: | ||
return None | ||
context = parse_xray_header( | ||
os.environ.get(XrayDaemon.XRAY_TRACE_ID_HEADER_NAME, "") | ||
) | ||
if context is None: | ||
logger.debug( | ||
"Failed to create segment since it was not possible to get trace context from header" | ||
) | ||
return None | ||
segment = build_segment(context, key, metadata) | ||
segment_payload = build_segment_payload(segment) | ||
send(host_port_tuple, segment_payload) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requests was previously removed