Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions tests/apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -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)
16 changes: 16 additions & 0 deletions tests/apps/tornado_server/__init__.py
Original file line number Diff line number Diff line change
@@ -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")

6 changes: 1 addition & 5 deletions tests/apps/tornado.py → tests/apps/tornado_server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 10 additions & 4 deletions tests/frameworks/test_tornado_client.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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))
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down
13 changes: 11 additions & 2 deletions tests/frameworks/test_tornado_server.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand All @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down
Empty file added tests/opentracing/__init__.py
Empty file.
54 changes: 53 additions & 1 deletion tests/opentracing/test_ot_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)