Skip to content

Commit 2997612

Browse files
authored
Do not record non-sampled spans (#109)
Fixes #108
1 parent 1765cbd commit 2997612

File tree

4 files changed

+57
-17
lines changed

4 files changed

+57
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
CHANGELOG.md
22

3+
<a name="4.4.8"></a>
4+
## [4.4.8](https://github.com/lightstep/lightstep-tracer-python/compare/4.4.7...4.4.8)
5+
* Do not record non-sampled spans (#108)
6+
37
<a name="4.4.7"></a>
48
## [4.4.7](https://github.com/lightstep/lightstep-tracer-python/compare/4.4.6...4.4.7)
59
* Cast all carrier values to string (#106)

lightstep/http_connection.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ def report(self, *args, **kwargs):
3030
with self._lock:
3131
try:
3232
report.auth.access_token = auth.access_token
33-
headers = {"Content-Type": "application/octet-stream",
34-
"Accept": "application/octet-stream",
35-
"Lightstep-Access-Token": auth.access_token}
33+
headers = {
34+
"Content-Type": "application/octet-stream",
35+
"Accept": "application/octet-stream",
36+
"Lightstep-Access-Token": auth.access_token
37+
}
3638

3739
r = requests.post(
3840
self._collector_url,

lightstep/recorder.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,19 @@ def __init__(self,
7171
self._finest("Initialized with Tracer runtime: {0}", (self._runtime,))
7272
secure = collector_encryption != 'none' # the default is 'tls'
7373
self._collector_url = util._collector_url_from_hostport(
74-
secure,
75-
collector_host,
76-
collector_port,
77-
self.use_thrift)
74+
secure,
75+
collector_host,
76+
collector_port,
77+
self.use_thrift
78+
)
7879
self._timeout_seconds = timeout_seconds
7980
self._auth = self.converter.create_auth(access_token)
8081
self._mutex = threading.Lock()
8182
self._span_records = []
8283
self._max_span_records = max_span_records
8384

8485
self._disabled_runtime = False
85-
86+
8687
atexit.register(self.shutdown)
8788

8889
self._periodic_flush_seconds = periodic_flush_seconds
@@ -99,7 +100,7 @@ def __init__(self,
99100
def _maybe_init_flush_thread(self):
100101
"""Start a periodic flush mechanism for this recorder if:
101102
102-
1. periodic_flush_seconds > 0, and
103+
1. periodic_flush_seconds > 0, and
103104
2. self._flush_thread is None, indicating that we have not yet
104105
initialized the background flush thread.
105106
@@ -132,7 +133,7 @@ def record_span(self, span):
132133
133134
Will drop a previously-added span if the limit has been reached.
134135
"""
135-
if self._disabled_runtime:
136+
if self._disabled_runtime or not span.context.sampled:
136137
return
137138

138139
# Lazy-init the flush loop (if need be).
@@ -241,7 +242,7 @@ def _flush_periodically(self):
241242
def _flush_worker(self, connection):
242243
"""Use the given connection to transmit the current logs and spans as a
243244
report request."""
244-
if connection == None:
245+
if connection is None:
245246
return False
246247

247248
# If the connection is not ready, try reestablishing it. If that
@@ -268,8 +269,9 @@ def _flush_worker(self, connection):
268269

269270
except Exception as e:
270271
self._fine(
271-
"Caught exception during report: {0}, stack trace: {1}",
272-
(e, traceback.format_exc()))
272+
"Caught exception during report: {0}, stack trace: {1}",
273+
(e, traceback.format_exc())
274+
)
273275
self._restore_spans(report_request)
274276
return False
275277

tests/recorder_test.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,39 @@ def recorder(request):
5555
yield lightstep.recorder.Recorder(**runtime_args)
5656

5757

58+
def test_non_sampled_span_thrift(recorder):
59+
60+
mock_connection = MockConnection()
61+
mock_connection.open()
62+
63+
non_sampled_span = BasicSpan(
64+
lightstep.tracer._LightstepTracer(False, recorder, None),
65+
operation_name="non_sampled",
66+
context=SpanContext(trace_id=1, span_id=1, sampled=False),
67+
start_time=time.time(),
68+
)
69+
non_sampled_span.finish()
70+
71+
sampled_span = BasicSpan(
72+
lightstep.tracer._LightstepTracer(False, recorder, None),
73+
operation_name="sampled",
74+
context=SpanContext(trace_id=1, span_id=2, sampled=True),
75+
start_time=time.time(),
76+
)
77+
sampled_span.finish()
78+
recorder.record_span(non_sampled_span)
79+
recorder.record_span(sampled_span)
80+
81+
recorder.flush(mock_connection)
82+
83+
if recorder.use_thrift:
84+
for span_record in mock_connection.reports[0].span_records:
85+
assert span_record.span_name == "sampled"
86+
else:
87+
for span in mock_connection.reports[0].spans:
88+
assert span.operation_name == "sampled"
89+
90+
5891
def test_default_tags_set_correctly(recorder):
5992
mock_connection = MockConnection()
6093
mock_connection.open()
@@ -80,7 +113,7 @@ def test_default_tags_set_correctly(recorder):
80113
"access_token": "{your_access_token}",
81114
"component_name": "python/runtime_test",
82115
"periodic_flush_seconds": 0,
83-
"tags": {"lightstep.hostname": "hostname",},
116+
"tags": {"lightstep.hostname": "hostname"},
84117
}
85118
new_recorder = lightstep.recorder.Recorder(**runtime_args)
86119
for tag in new_recorder._runtime.tags:
@@ -119,7 +152,7 @@ def test_shutdown_twice(recorder):
119152
recorder.shutdown()
120153
recorder.shutdown()
121154
except Exception as error:
122-
self.fail("Unexpected exception raised: {}".format(error))
155+
pytest.fail("Unexpected exception raised: {}".format(error))
123156

124157

125158
# ------------
@@ -225,7 +258,7 @@ def test_exception_formatting(recorder):
225258
assert len(recorder._span_records) == 1
226259
assert recorder.flush(mock_connection)
227260
spans = recorder.converter.get_span_records(mock_connection.reports[1])
228-
261+
229262
if hasattr(spans[0], "log_records"):
230263
assert len(spans[0].log_records) == 1
231264
assert len(spans[0].log_records[0].fields) == 3
@@ -251,4 +284,3 @@ def test_exception_formatting(recorder):
251284
assert field.string_value == ""
252285
else:
253286
raise AttributeError("unexpected field: %s".format(field.key))
254-

0 commit comments

Comments
 (0)