Skip to content

Commit e0ce951

Browse files
authored
Improved JSON encoding, Kind handling & SDK span support (#132)
* Lowercase keys in json encoding * Add note explaining why these fields are capitalized * Better SDKSpan building * Add tests for k field * Remove unused imports
1 parent 08e612f commit e0ce951

File tree

5 files changed

+59
-12
lines changed

5 files changed

+59
-12
lines changed

instana/agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ def start(self, e):
5151

5252
def to_json(self, o):
5353
def extractor(o):
54-
return {k: v for k, v in o.__dict__.items() if v is not None}
54+
return {k.lower(): v for k, v in o.__dict__.items() if v is not None}
5555

5656
try:
5757
return json.dumps(o, default=extractor, sort_keys=False, separators=(',', ':')).encode()
58-
except Exception as e:
58+
except:
5959
logger.debug("to_json", exc_info=True)
6060

6161
def is_timed_out(self):

instana/json_span.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ def __init__(self, **kwds):
122122

123123
class SDKData(object):
124124
name = None
125+
126+
# Since 'type' and 'return' are a Python builtin and a reserved keyword respectively, these keys (all keys) are
127+
# lower-case'd in json encoding. See Agent.to_json
125128
Type = None
126-
arguments = None
127129
Return = None
130+
131+
arguments = None
128132
custom = None
129133

130134
def __init__(self, **kwds):

instana/recorder.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class InstanaRecorder(SpanRecorder):
3333

3434
entry_kind = ["entry", "server", "consumer"]
3535
exit_kind = ["exit", "client", "producer"]
36+
3637
queue = queue.Queue()
3738

3839
def __init__(self):
@@ -208,11 +209,16 @@ def build_sdk_span(self, span):
208209
logs=self.collect_logs(span))
209210

210211
sdk_data = SDKData(name=span.operation_name,
211-
custom=custom_data)
212+
custom=custom_data,
213+
Type=self.get_span_kind_as_string(span))
214+
215+
if "arguments" in span.tags:
216+
sdk_data.arguments = span.tags["arguments"]
212217

213-
sdk_data.Type = self.get_span_kind(span)
214-
data = Data(service=instana.singletons.agent.sensor.options.service_name,
215-
sdk=sdk_data)
218+
if "return" in span.tags:
219+
sdk_data.Return = span.tags["return"]
220+
221+
data = Data(service=instana.singletons.agent.sensor.options.service_name, sdk=sdk_data)
216222
entity_from = {'e': instana.singletons.agent.from_.pid,
217223
'h': instana.singletons.agent.from_.agentUuid}
218224

@@ -222,6 +228,7 @@ def build_sdk_span(self, span):
222228
s=span.context.span_id,
223229
ts=int(round(span.start_time * 1000)),
224230
d=int(round(span.duration * 1000)),
231+
k=self.get_span_kind_as_int(span),
225232
n="sdk",
226233
f=entity_from,
227234
data=data)
@@ -246,16 +253,40 @@ def get_http_host_name(self, span):
246253

247254
return "localhost"
248255

249-
def get_span_kind(self, span):
250-
kind = ""
256+
def get_span_kind_as_string(self, span):
257+
"""
258+
Will retrieve the `span.kind` tag and return the appropriate string value for the Instana backend or
259+
None if the tag is set to something we don't recognize.
260+
261+
:param span: The span to search for the `span.kind` tag
262+
:return: String
263+
"""
264+
kind = None
251265
if "span.kind" in span.tags:
252266
if span.tags["span.kind"] in self.entry_kind:
253267
kind = "entry"
254268
elif span.tags["span.kind"] in self.exit_kind:
255269
kind = "exit"
256270
else:
257271
kind = "intermediate"
272+
return kind
258273

274+
def get_span_kind_as_int(self, span):
275+
"""
276+
Will retrieve the `span.kind` tag and return the appropriate integer value for the Instana backend or
277+
None if the tag is set to something we don't recognize.
278+
279+
:param span: The span to search for the `span.kind` tag
280+
:return: Integer
281+
"""
282+
kind = None
283+
if "span.kind" in span.tags:
284+
if span.tags["span.kind"] in self.entry_kind:
285+
kind = 1
286+
elif span.tags["span.kind"] in self.exit_kind:
287+
kind = 2
288+
else:
289+
kind = 3
259290
return kind
260291

261292
def collect_logs(self, span):

tests/test_ot_span.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,18 @@ def test_span_kind(self):
128128

129129
span = spans[4]
130130
assert_equals('intermediate', span.data.sdk.Type)
131+
132+
span = spans[0]
133+
assert_equals(1, span.k)
134+
135+
span = spans[1]
136+
assert_equals(1, span.k)
137+
138+
span = spans[2]
139+
assert_equals(2, span.k)
140+
141+
span = spans[3]
142+
assert_equals(2, span.k)
143+
144+
span = spans[4]
145+
assert_equals(3, span.k)

tests/test_ot_tracer.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import opentracing
2-
from nose.tools import assert_equals
3-
4-
from instana.singletons import tracer
52

63

74
def test_tracer_basics():

0 commit comments

Comments
 (0)