From 58b1a6cbbee8261ee9edb0e779cf96cc75a683ac Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 18 Feb 2019 12:08:23 +0100 Subject: [PATCH 1/5] Lowercase keys in json encoding --- instana/agent.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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): From 7d88ae95af2528a19602b339ec7e7df072c09426 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 18 Feb 2019 12:08:49 +0100 Subject: [PATCH 2/5] Add note explaining why these fields are capitalized --- instana/json_span.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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): From 2fc436029d0660134e21a6a4f8e23285a4aa908a Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 18 Feb 2019 12:09:40 +0100 Subject: [PATCH 3/5] Better SDKSpan building --- instana/recorder.py | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) 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): From ba4b4c50c809033469159981e25085835fbc9610 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 18 Feb 2019 12:10:03 +0100 Subject: [PATCH 4/5] Add tests for k field --- tests/test_ot_span.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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) From e7194588f9a080524b17c8bdb765dfbf2a822098 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Mon, 18 Feb 2019 12:10:34 +0100 Subject: [PATCH 5/5] Remove unused imports --- tests/test_ot_tracer.py | 3 --- 1 file changed, 3 deletions(-) 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():