From e147587a45da2f45ff238547a3d491fe13d7041a Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Thu, 23 Jul 2020 13:10:54 +0200 Subject: [PATCH 1/2] SDK: Add tests to validate custom service names --- tests/opentracing/__init__.py | 0 tests/opentracing/test_ot_span.py | 54 ++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/opentracing/__init__.py diff --git a/tests/opentracing/__init__.py b/tests/opentracing/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/opentracing/test_ot_span.py b/tests/opentracing/test_ot_span.py index 955dc480..1e8c3771 100644 --- a/tests/opentracing/test_ot_span.py +++ b/tests/opentracing/test_ot_span.py @@ -6,14 +6,17 @@ import opentracing from uuid import UUID from instana.util import to_json -from instana.singletons import tracer +from instana.singletons import agent, tracer +from ..helpers import get_first_span_by_filter PY2 = sys.version_info[0] == 2 PY3 = sys.version_info[0] == 3 + class TestOTSpan(unittest.TestCase): def setUp(self): """ Clear all spans before a test run """ + agent.options.service_name = None opentracing.tracer = tracer recorder = opentracing.tracer.recorder recorder.clear_spans() @@ -215,3 +218,52 @@ def test_tag_names(self): json_data = to_json(test_span) assert(json_data) + def test_custom_service_name(self): + # Set a custom service name + agent.options.service_name = "custom_service_name" + + with tracer.start_active_span('entry_span') as scope: + scope.span.set_tag('span.kind', 'server') + scope.span.set_tag(u'type', 'entry_span') + + with tracer.start_active_span('intermediate_span', child_of=scope.span) as exit_scope: + exit_scope.span.set_tag(u'type', 'intermediate_span') + + with tracer.start_active_span('exit_span', child_of=scope.span) as exit_scope: + exit_scope.span.set_tag('span.kind', 'client') + exit_scope.span.set_tag(u'type', 'exit_span') + + spans = tracer.recorder.queued_spans() + assert len(spans) == 3 + + filter = lambda span: span.n == "sdk" and span.data['sdk']['name'] == "entry_span" + entry_span = get_first_span_by_filter(spans, filter) + assert (entry_span) + + filter = lambda span: span.n == "sdk" and span.data['sdk']['name'] == "intermediate_span" + intermediate_span = get_first_span_by_filter(spans, filter) + assert (intermediate_span) + + filter = lambda span: span.n == "sdk" and span.data['sdk']['name'] == "exit_span" + exit_span = get_first_span_by_filter(spans, filter) + assert (exit_span) + + # Custom service name should be set on ENTRY spans and none other + assert(entry_span) + assert(len(entry_span.data['sdk']['custom']['tags']) == 2) + assert(entry_span.data['sdk']['custom']['tags']['type'] == 'entry_span') + assert(entry_span.data['service'] == 'custom_service_name') + assert(entry_span.k == 1) + + assert(intermediate_span) + assert(len(intermediate_span.data['sdk']['custom']['tags']) == 1) + assert(intermediate_span.data['sdk']['custom']['tags']['type'] == 'intermediate_span') + assert("service" not in intermediate_span.data) + assert(intermediate_span.k == 3) + + assert(exit_span) + assert(len(exit_span.data['sdk']['custom']['tags']) == 2) + assert(exit_span.data['sdk']['custom']['tags']['type'] == 'exit_span') + assert("service" not in intermediate_span.data) + assert(exit_span.k == 2) + From 62ecf9b9b69fd8d7176414c30bf1792f96aa37f1 Mon Sep 17 00:00:00 2001 From: Peter Giacomo Lombardo Date: Thu, 23 Jul 2020 15:48:44 +0200 Subject: [PATCH 2/2] Refactor Tornado tests --- tests/apps/__init__.py | 16 ---------------- tests/apps/tornado_server/__init__.py | 16 ++++++++++++++++ tests/apps/{tornado.py => tornado_server/app.py} | 6 +----- tests/frameworks/test_tornado_client.py | 14 ++++++++++---- tests/frameworks/test_tornado_server.py | 13 +++++++++++-- 5 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 tests/apps/tornado_server/__init__.py rename tests/apps/{tornado.py => tornado_server/app.py} (92%) diff --git a/tests/apps/__init__.py b/tests/apps/__init__.py index 05110183..60eee1c6 100644 --- a/tests/apps/__init__.py +++ b/tests/apps/__init__.py @@ -4,7 +4,6 @@ import threading if 'GEVENT_TEST' not in os.environ and 'CASSANDRA_TEST' not in os.environ: - if sys.version_info >= (3, 5, 3): # Background RPC application # @@ -31,19 +30,4 @@ print("Starting background aiohttp server...") aio_server.start() - if sys.version_info >= (3, 5, 3): - # Background Tornado application - from .tornado import run_server - - # Spawn our background Tornado app that the tests will throw - # requests at. - tornado_server = threading.Thread(target=run_server) - tornado_server.daemon = True - tornado_server.name = "Background Tornado server" - print("Starting background Tornado server...") - tornado_server.start() - - # from .celery import start as start_celery - # start_celery() - time.sleep(1) diff --git a/tests/apps/tornado_server/__init__.py b/tests/apps/tornado_server/__init__.py new file mode 100644 index 00000000..84452d1d --- /dev/null +++ b/tests/apps/tornado_server/__init__.py @@ -0,0 +1,16 @@ +import os +import sys +from ...helpers import testenv +from ..utils import launch_background_thread + +app_thread = None + +if app_thread is None and sys.version_info >= (3, 5, 3) and 'GEVENT_TEST' not in os.environ and 'CASSANDRA_TEST' not in os.environ: + testenv["tornado_port"] = 10813 + testenv["tornado_server"] = ("http://127.0.0.1:" + str(testenv["tornado_port"])) + + # Background Tornado application + from .app import run_server + + app_thread = launch_background_thread(run_server, "Tornado") + diff --git a/tests/apps/tornado.py b/tests/apps/tornado_server/app.py similarity index 92% rename from tests/apps/tornado.py rename to tests/apps/tornado_server/app.py index a567fbaf..33e1e6e5 100644 --- a/tests/apps/tornado.py +++ b/tests/apps/tornado_server/app.py @@ -10,11 +10,7 @@ import asyncio -from ..helpers import testenv - - -testenv["tornado_port"] = 10813 -testenv["tornado_server"] = ("http://127.0.0.1:" + str(testenv["tornado_port"])) +from ...helpers import testenv class Application(tornado.web.Application): diff --git a/tests/frameworks/test_tornado_client.py b/tests/frameworks/test_tornado_client.py index 2bb2e2c6..d020968d 100644 --- a/tests/frameworks/test_tornado_client.py +++ b/tests/frameworks/test_tornado_client.py @@ -1,19 +1,19 @@ from __future__ import absolute_import +import time import asyncio import unittest import tornado from tornado.httpclient import AsyncHTTPClient +from instana.singletons import tornado_tracer -from instana.singletons import async_tracer, tornado_tracer, agent - +import tests.apps.tornado_server from ..helpers import testenv from nose.plugins.skip import SkipTest raise SkipTest("Non deterministic tests TBR") - class TestTornadoClient(unittest.TestCase): def setUp(self): @@ -39,6 +39,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -97,6 +98,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -154,8 +156,8 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) + time.sleep(0.5) spans = self.recorder.queued_spans() - self.assertEqual(4, len(spans)) server301_span = spans[0] @@ -227,6 +229,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -287,6 +290,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -347,6 +351,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -404,6 +409,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) diff --git a/tests/frameworks/test_tornado_server.py b/tests/frameworks/test_tornado_server.py index 9620c945..ffd160d2 100644 --- a/tests/frameworks/test_tornado_server.py +++ b/tests/frameworks/test_tornado_server.py @@ -1,15 +1,16 @@ from __future__ import absolute_import +import time import asyncio import aiohttp import unittest -import time import tornado from tornado.httpclient import AsyncHTTPClient -from instana.singletons import async_tracer, agent +import tests.apps.tornado_server +from instana.singletons import async_tracer, agent from ..helpers import testenv, get_first_span_by_name, get_first_span_by_filter @@ -51,6 +52,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -183,6 +185,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -202,6 +205,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(4, len(spans)) @@ -280,6 +284,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -343,6 +348,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -407,6 +413,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -471,6 +478,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans)) @@ -542,6 +550,7 @@ async def test(): response = tornado.ioloop.IOLoop.current().run_sync(test) + time.sleep(0.5) spans = self.recorder.queued_spans() self.assertEqual(3, len(spans))