diff --git a/instana/agent.py b/instana/agent.py index 2cbe245f..31df37d6 100644 --- a/instana/agent.py +++ b/instana/agent.py @@ -51,11 +51,11 @@ def start(self, e): def to_json(self, o): def extractor(o): - return {k: v for k, v in o.__dict__.items() if v is not None} + return {k.lower(): v for k, v in o.__dict__.items() if v is not None} try: return json.dumps(o, default=extractor, sort_keys=False, separators=(',', ':')).encode() - except Exception as e: + except: logger.debug("to_json", exc_info=True) def is_timed_out(self): diff --git a/instana/json_span.py b/instana/json_span.py index edb12545..40ea2ca5 100644 --- a/instana/json_span.py +++ b/instana/json_span.py @@ -122,9 +122,13 @@ def __init__(self, **kwds): class SDKData(object): name = None + + # Since 'type' and 'return' are a Python builtin and a reserved keyword respectively, these keys (all keys) are + # lower-case'd in json encoding. See Agent.to_json Type = None - arguments = None Return = None + + arguments = None custom = None def __init__(self, **kwds): diff --git a/instana/recorder.py b/instana/recorder.py index 5e302480..d009280e 100644 --- a/instana/recorder.py +++ b/instana/recorder.py @@ -33,6 +33,7 @@ class InstanaRecorder(SpanRecorder): entry_kind = ["entry", "server", "consumer"] exit_kind = ["exit", "client", "producer"] + queue = queue.Queue() def __init__(self): @@ -208,11 +209,16 @@ def build_sdk_span(self, span): logs=self.collect_logs(span)) sdk_data = SDKData(name=span.operation_name, - custom=custom_data) + custom=custom_data, + Type=self.get_span_kind_as_string(span)) + + if "arguments" in span.tags: + sdk_data.arguments = span.tags["arguments"] - sdk_data.Type = self.get_span_kind(span) - data = Data(service=instana.singletons.agent.sensor.options.service_name, - sdk=sdk_data) + if "return" in span.tags: + sdk_data.Return = span.tags["return"] + + data = Data(service=instana.singletons.agent.sensor.options.service_name, sdk=sdk_data) entity_from = {'e': instana.singletons.agent.from_.pid, 'h': instana.singletons.agent.from_.agentUuid} @@ -222,6 +228,7 @@ def build_sdk_span(self, span): s=span.context.span_id, ts=int(round(span.start_time * 1000)), d=int(round(span.duration * 1000)), + k=self.get_span_kind_as_int(span), n="sdk", f=entity_from, data=data) @@ -246,8 +253,15 @@ def get_http_host_name(self, span): return "localhost" - def get_span_kind(self, span): - kind = "" + def get_span_kind_as_string(self, span): + """ + Will retrieve the `span.kind` tag and return the appropriate string value for the Instana backend or + None if the tag is set to something we don't recognize. + + :param span: The span to search for the `span.kind` tag + :return: String + """ + kind = None if "span.kind" in span.tags: if span.tags["span.kind"] in self.entry_kind: kind = "entry" @@ -255,7 +269,24 @@ def get_span_kind(self, span): kind = "exit" else: kind = "intermediate" + return kind + def get_span_kind_as_int(self, span): + """ + Will retrieve the `span.kind` tag and return the appropriate integer value for the Instana backend or + None if the tag is set to something we don't recognize. + + :param span: The span to search for the `span.kind` tag + :return: Integer + """ + kind = None + if "span.kind" in span.tags: + if span.tags["span.kind"] in self.entry_kind: + kind = 1 + elif span.tags["span.kind"] in self.exit_kind: + kind = 2 + else: + kind = 3 return kind def collect_logs(self, span): diff --git a/tests/test_ot_span.py b/tests/test_ot_span.py index 20996e06..da1e9e63 100644 --- a/tests/test_ot_span.py +++ b/tests/test_ot_span.py @@ -128,3 +128,18 @@ def test_span_kind(self): span = spans[4] assert_equals('intermediate', span.data.sdk.Type) + + span = spans[0] + assert_equals(1, span.k) + + span = spans[1] + assert_equals(1, span.k) + + span = spans[2] + assert_equals(2, span.k) + + span = spans[3] + assert_equals(2, span.k) + + span = spans[4] + assert_equals(3, span.k) diff --git a/tests/test_ot_tracer.py b/tests/test_ot_tracer.py index 1dc9a25c..8eaccb97 100644 --- a/tests/test_ot_tracer.py +++ b/tests/test_ot_tracer.py @@ -1,7 +1,4 @@ import opentracing -from nose.tools import assert_equals - -from instana.singletons import tracer def test_tracer_basics():