diff --git a/.tekton/.currency/scripts/generate_report.py b/.tekton/.currency/scripts/generate_report.py index da306ee43..ce001be6c 100644 --- a/.tekton/.currency/scripts/generate_report.py +++ b/.tekton/.currency/scripts/generate_report.py @@ -1,10 +1,11 @@ # Standard Libraries -import re import json +import re + +import pandas as pd # Third Party import requests -import pandas as pd from bs4 import BeautifulSoup from kubernetes import client, config @@ -117,7 +118,9 @@ def process_taskrun_logs( match = re.search("Successfully installed .* (starlette-[^\s]+)", logs) tekton_ci_output += f"{match[1]}\n" elif task_name == "python-tracer-unittest-googlecloud-task": - match = re.search("Successfully installed .* (google-cloud-storage-[^\s]+)", logs) + match = re.search( + "Successfully installed .* (google-cloud-storage-[^\s]+)", logs + ) tekton_ci_output += f"{match[1]}\n" elif task_name == "python-tracer-unittest-default-task": for line in logs.splitlines(): @@ -140,7 +143,10 @@ def get_tekton_ci_output(): core_v1_client = client.CoreV1Api() task_name = "python-tracer-unittest-gevent-starlette-task" - taskrun_filter = lambda tr: tr["status"]["conditions"][0]["type"] == "Succeeded" + + def taskrun_filter(tr): + return tr["status"]["conditions"][0]["type"] == "Succeeded" + starlette_taskruns = get_taskruns(namespace, task_name, taskrun_filter) tekton_ci_output = process_taskrun_logs( @@ -148,10 +154,13 @@ def get_tekton_ci_output(): ) task_name = "python-tracer-unittest-googlecloud-task" - taskrun_filter = ( - lambda tr: tr["metadata"]["name"].endswith("unittest-googlecloud-0") - and tr["status"]["conditions"][0]["type"] == "Succeeded" - ) + + def taskrun_filter(tr): + return ( + tr["metadata"]["name"].endswith("unittest-googlecloud-0") + and tr["status"]["conditions"][0]["type"] == "Succeeded" + ) + googlecloud_taskruns = get_taskruns(namespace, task_name, taskrun_filter) tekton_ci_output = process_taskrun_logs( @@ -159,10 +168,13 @@ def get_tekton_ci_output(): ) task_name = "python-tracer-unittest-default-task" - taskrun_filter = ( - lambda tr: tr["metadata"]["name"].endswith("unittest-default-3") - and tr["status"]["conditions"][0]["type"] == "Succeeded" - ) + + def taskrun_filter(tr): + return ( + tr["metadata"]["name"].endswith("unittest-default-3") + and tr["status"]["conditions"][0]["type"] == "Succeeded" + ) + default_taskruns = get_taskruns(namespace, task_name, taskrun_filter) tekton_ci_output = process_taskrun_logs( diff --git a/bin/announce_release_on_slack.py b/bin/announce_release_on_slack.py index 2c6625dd3..4a276265a 100755 --- a/bin/announce_release_on_slack.py +++ b/bin/announce_release_on_slack.py @@ -10,8 +10,12 @@ def ensure_environment_variables_are_present(): - required_env_vars = ('GITHUB_RELEASE_TAG', 'GITHUB_TOKEN', - 'SLACK_BOT_TOKEN', 'SLACK_CHANNEL_ID_RELEASES') + required_env_vars = ( + "GITHUB_RELEASE_TAG", + "GITHUB_TOKEN", + "SLACK_BOT_TOKEN", + "SLACK_CHANNEL_ID_RELEASES", + ) for v in required_env_vars: if not os.environ.get(v): @@ -33,7 +37,8 @@ def get_gh_release_info_text_with_token(release_tag, access_token): f"Tag: {release.tag_name}\n" f"Created at: {release.created_at}\n" f"Published at: {release.published_at}\n" - f"{release.body}\n") + f"{release.body}\n" + ) logging.info(msg) return msg @@ -42,8 +47,10 @@ def get_gh_release_info_text_with_token(release_tag, access_token): def post_on_slack_channel(slack_token, slack_channel_id, message_text): api_url = "https://slack.com/api/chat.postMessage" - headers = {"Authorization": f"Bearer {slack_token}", - "Content-Type": "application/json"} + headers = { + "Authorization": f"Bearer {slack_token}", + "Content-Type": "application/json", + } body = {"channel": slack_channel_id, "text": message_text} response = requests.post(api_url, headers=headers, data=json.dumps(body)) @@ -52,7 +59,7 @@ def post_on_slack_channel(slack_token, slack_channel_id, message_text): if response_data["ok"]: logging.info("Message sent successfully!") else: - logging.fatal("Error sending message: %s", response_data['error']) + logging.fatal("Error sending message: %s", response_data["error"]) def main(): @@ -61,12 +68,13 @@ def main(): logging.basicConfig(level=logging.INFO) ensure_environment_variables_are_present() - msg = get_gh_release_info_text_with_token(os.environ['GITHUB_RELEASE_TAG'], - os.environ['GITHUB_TOKEN']) + msg = get_gh_release_info_text_with_token( + os.environ["GITHUB_RELEASE_TAG"], os.environ["GITHUB_TOKEN"] + ) - post_on_slack_channel(os.environ['SLACK_BOT_TOKEN'], - os.environ['SLACK_CHANNEL_ID_RELEASES'], - msg) + post_on_slack_channel( + os.environ["SLACK_BOT_TOKEN"], os.environ["SLACK_CHANNEL_ID_RELEASES"], msg + ) if __name__ == "__main__": diff --git a/bin/aws-lambda/build_and_publish_lambda_layer.py b/bin/aws-lambda/build_and_publish_lambda_layer.py index 41ea5b1cc..dee9c38b8 100755 --- a/bin/aws-lambda/build_and_publish_lambda_layer.py +++ b/bin/aws-lambda/build_and_publish_lambda_layer.py @@ -11,19 +11,22 @@ import distutils.spawn from subprocess import call, check_call, check_output, CalledProcessError, DEVNULL -for profile in ('china', 'non-china'): +for profile in ("china", "non-china"): try: - check_call(['aws', 'configure', 'list', '--profile', profile], stdout=DEVNULL) + check_call(["aws", "configure", "list", "--profile", profile], stdout=DEVNULL) except CalledProcessError: raise ValueError( f"Please ensure, that your aws configuration includes a profile called '{profile}'" - "and has the 'access_key' and 'secret_key' configured for the respective regions") + "and has the 'access_key' and 'secret_key' configured for the respective regions" + ) # Either -dev or -prod must be specified (and nothing else) -if len(sys.argv) != 2 or (('-dev' not in sys.argv) and ('-prod' not in sys.argv)): - raise ValueError('Please specify -dev or -prod to indicate which type of layer to build.') +if len(sys.argv) != 2 or (("-dev" not in sys.argv) and ("-prod" not in sys.argv)): + raise ValueError( + "Please specify -dev or -prod to indicate which type of layer to build." + ) -dev_mode = '-dev' in sys.argv +dev_mode = "-dev" in sys.argv # Disable aws CLI pagination os.environ["AWS_PAGER"] = "" @@ -48,7 +51,7 @@ if "PYTHONPATH" not in os.environ: local_env["PYTHONPATH"] = os.getcwd() -build_directory = os.getcwd() + '/build/lambda/python' +build_directory = os.getcwd() + "/build/lambda/python" if os.path.isdir(build_directory): print("===> Cleaning build pre-existing directory: %s" % build_directory) @@ -58,7 +61,18 @@ os.makedirs(build_directory, exist_ok=True) print("===> Installing Instana and dependencies into build directory") -call(["pip", "install", "-q", "-U", "-t", os.getcwd() + '/build/lambda/python', "instana"], env=local_env) +call( + [ + "pip", + "install", + "-q", + "-U", + "-t", + os.getcwd() + "/build/lambda/python", + "instana", + ], + env=local_env, +) print("===> Manually copying in local dev code") shutil.rmtree(build_directory + "/instana") @@ -69,86 +83,134 @@ zip_filename = "instana-py-layer-%s.zip" % timestamp os.chdir(os.getcwd() + "/build/lambda/") -call(["zip", "-q", "-r", zip_filename, "./python", "-x", "*.pyc", "./python/pip*", "./python/setuptools*", "./python/wheel*"]) - -fq_zip_filename = os.getcwd() + '/%s' % zip_filename +call( + [ + "zip", + "-q", + "-r", + zip_filename, + "./python", + "-x", + "*.pyc", + "./python/pip*", + "./python/setuptools*", + "./python/wheel*", + ] +) + +fq_zip_filename = os.getcwd() + "/%s" % zip_filename aws_zip_filename = "fileb://%s" % fq_zip_filename print("Zipfile should be at: ", fq_zip_filename) cn_regions = [ - 'cn-north-1', - 'cn-northwest-1', - ] + "cn-north-1", + "cn-northwest-1", +] if dev_mode: - target_regions = ['us-west-1'] + target_regions = ["us-west-1"] LAYER_NAME = "instana-py-dev" else: - target_regions = [ - 'af-south-1', - 'ap-east-1', - 'ap-northeast-1', - 'ap-northeast-2', - 'ap-northeast-3', - 'ap-south-1', - 'ap-south-2', - 'ap-southeast-1', - 'ap-southeast-2', - 'ap-southeast-3', - 'ap-southeast-4', - 'ca-central-1', - 'ca-west-1', - 'cn-north-1', - 'cn-northwest-1', - 'eu-central-1', - 'eu-central-2', - 'eu-north-1', - 'eu-south-1', - 'eu-south-2', - 'eu-west-1', - 'eu-west-2', - 'eu-west-3', - 'il-central-1', - 'me-central-1', - 'me-south-1', - 'sa-east-1', - 'us-east-1', - 'us-east-2', - 'us-west-1', - 'us-west-2' - ] + "af-south-1", + "ap-east-1", + "ap-northeast-1", + "ap-northeast-2", + "ap-northeast-3", + "ap-south-1", + "ap-south-2", + "ap-southeast-1", + "ap-southeast-2", + "ap-southeast-3", + "ap-southeast-4", + "ca-central-1", + "ca-west-1", + "cn-north-1", + "cn-northwest-1", + "eu-central-1", + "eu-central-2", + "eu-north-1", + "eu-south-1", + "eu-south-2", + "eu-west-1", + "eu-west-2", + "eu-west-3", + "il-central-1", + "me-central-1", + "me-south-1", + "sa-east-1", + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", + ] LAYER_NAME = "instana-python" published = dict() for region in target_regions: print("===> Uploading layer to AWS %s " % region) - profile = 'china' if region in cn_regions else 'non-china' - - response = check_output(["aws", "--region", region, "lambda", "publish-layer-version", - "--description", - "Provides Instana tracing and monitoring of AWS Lambda functions built with Python", - "--license-info", "MIT", "--output", "json", - "--layer-name", LAYER_NAME, "--zip-file", aws_zip_filename, - "--compatible-runtimes", "python3.8", "python3.9", "python3.10", "python3.11", "python3.12", - "--profile", profile]) + profile = "china" if region in cn_regions else "non-china" + + response = check_output( + [ + "aws", + "--region", + region, + "lambda", + "publish-layer-version", + "--description", + "Provides Instana tracing and monitoring of AWS Lambda functions built with Python", + "--license-info", + "MIT", + "--output", + "json", + "--layer-name", + LAYER_NAME, + "--zip-file", + aws_zip_filename, + "--compatible-runtimes", + "python3.8", + "python3.9", + "python3.10", + "python3.11", + "python3.12", + "--profile", + profile, + ] + ) json_data = json.loads(response) - version = json_data['Version'] + version = json_data["Version"] print("===> Uploaded version is %s" % version) if dev_mode is False: print("===> Making layer public...") - response = check_output(["aws", "--region", region, "lambda", "add-layer-version-permission", - "--layer-name", LAYER_NAME, "--version-number", str(version), - "--statement-id", "public-permission-all-accounts", - "--principal", "*", - "--action", "lambda:GetLayerVersion", - "--output", "text", - "--profile", profile]) - - published[region] = json_data['LayerVersionArn'] + response = check_output( + [ + "aws", + "--region", + region, + "lambda", + "add-layer-version-permission", + "--layer-name", + LAYER_NAME, + "--version-number", + str(version), + "--statement-id", + "public-permission-all-accounts", + "--principal", + "*", + "--action", + "lambda:GetLayerVersion", + "--output", + "text", + "--profile", + profile, + ] + ) + + published[region] = json_data["LayerVersionArn"] print("===> Published list:") diff --git a/bin/create_general_release.py b/bin/create_general_release.py index d9fc3ac6a..3b065f9f0 100755 --- a/bin/create_general_release.py +++ b/bin/create_general_release.py @@ -14,7 +14,7 @@ if len(sys.argv) != 2: raise ValueError('Please specify the version to release. e.g. "1.27.1"') -if sys.argv[1] in ['-h', '--help']: +if sys.argv[1] in ["-h", "--help"]: filename = os.path.basename(__file__) print("Usage: %s " % filename) print("Exampe: %s 1.27.1" % filename) @@ -30,23 +30,36 @@ sys.exit(1) version = sys.argv[1] -semantic_version = 'v' + version +semantic_version = "v" + version title = version -body = """ +body = ( + """ This release includes the following fixes & improvements: * Available on PyPI: https://pypi.python.org/pypi/instana/%s -""" % version +""" + % version +) -response = check_output(["gh", "release", "create", semantic_version, - "-d", # draft - "-R", "instana/python-sensor", - "-t", semantic_version, - "-n", body]) +response = check_output( + [ + "gh", + "release", + "create", + semantic_version, + "-d", # draft + "-R", + "instana/python-sensor", + "-t", + semantic_version, + "-n", + body, + ] +) print("If there weren't any failures, the draft release is available at:") diff --git a/bin/create_lambda_release.py b/bin/create_lambda_release.py index dc2e209db..47f4cf666 100755 --- a/bin/create_lambda_release.py +++ b/bin/create_lambda_release.py @@ -15,7 +15,7 @@ if len(sys.argv) != 2: raise ValueError('Please specify the layer version to release. e.g. "14"') -if sys.argv[1] in ['-h', '--help']: +if sys.argv[1] in ["-h", "--help"]: filename = os.path.basename(__file__) print("Usage: %s " % filename) print("Exampe: %s 14" % filename) @@ -31,52 +31,66 @@ sys.exit(1) regions = [ - 'af-south-1', - 'ap-east-1', - 'ap-northeast-1', - 'ap-northeast-2', - 'ap-northeast-3', - 'ap-south-1', - 'ap-south-2', - 'ap-southeast-1', - 'ap-southeast-2', - 'ap-southeast-3', - 'ap-southeast-4', - 'ca-central-1', - 'ca-west-1', - 'cn-north-1', - 'cn-northwest-1', - 'eu-central-1', - 'eu-central-2', - 'eu-north-1', - 'eu-south-1', - 'eu-south-2', - 'eu-west-1', - 'eu-west-2', - 'eu-west-3', - 'il-central-1', - 'me-central-1', - 'me-south-1', - 'sa-east-1', - 'us-east-1', - 'us-east-2', - 'us-west-1', - 'us-west-2' - ] + "af-south-1", + "ap-east-1", + "ap-northeast-1", + "ap-northeast-2", + "ap-northeast-3", + "ap-south-1", + "ap-south-2", + "ap-southeast-1", + "ap-southeast-2", + "ap-southeast-3", + "ap-southeast-4", + "ca-central-1", + "ca-west-1", + "cn-north-1", + "cn-northwest-1", + "eu-central-1", + "eu-central-2", + "eu-north-1", + "eu-south-1", + "eu-south-2", + "eu-west-1", + "eu-west-2", + "eu-west-3", + "il-central-1", + "me-central-1", + "me-south-1", + "sa-east-1", + "us-east-1", + "us-east-2", + "us-west-1", + "us-west-2", +] version = sys.argv[1] -semantic_version = 'v' + version +semantic_version = "v" + version title = "AWS Lambda Layer %s" % semantic_version -body = '| AWS Region | ARN |\n' -body += '| :-- | :-- |\n' +body = "| AWS Region | ARN |\n" +body += "| :-- | :-- |\n" for region in regions: - body += "| %s | arn:aws:lambda:%s:410797082306:layer:instana-python:%s |\n" % (region, region, version) + body += "| %s | arn:aws:lambda:%s:410797082306:layer:instana-python:%s |\n" % ( + region, + region, + version, + ) -response = check_output(["gh", "api", "repos/:owner/:repo/releases", "--method=POST", - "-F", ("tag_name=%s" % semantic_version), - "-F", "name=%s" % title, - "-F", "body=%s" % body]) +response = check_output( + [ + "gh", + "api", + "repos/:owner/:repo/releases", + "--method=POST", + "-F", + ("tag_name=%s" % semantic_version), + "-F", + "name=%s" % title, + "-F", + "body=%s" % body, + ] +) json_data = json.loads(response) diff --git a/example/asyncio/aioclient.py b/example/asyncio/aioclient.py index fb8696759..c0fa1444b 100644 --- a/example/asyncio/aioclient.py +++ b/example/asyncio/aioclient.py @@ -10,14 +10,15 @@ async def test(): while True: await asyncio.sleep(2) - with async_tracer.start_active_span('JobRunner'): + with async_tracer.start_active_span("JobRunner"): async with aiohttp.ClientSession() as session: - # aioserver exposes /, /401, /500 & /publish - async with session.get("http://localhost:5102/publish?secret=iloveyou") as response: - print(response.status) + # aioserver exposes /, /401, /500 & /publish + async with session.get( + "http://localhost:5102/publish?secret=iloveyou" + ) as response: + print(response.status) loop = asyncio.get_event_loop() loop.run_until_complete(test()) loop.run_forever() - diff --git a/example/asyncio/aioserver.py b/example/asyncio/aioserver.py index bf8db2638..14af43055 100644 --- a/example/asyncio/aioserver.py +++ b/example/asyncio/aioserver.py @@ -3,6 +3,7 @@ import os import asyncio + # TODO: Change asynqp to aio-pika once it is fully supported import asynqp from aiohttp import web @@ -14,7 +15,7 @@ RABBITMQ_HOST = "localhost" -class RabbitUtil(): +class RabbitUtil: def __init__(self, loop): self.loop = loop self.loop.run_until_complete(self.connect()) @@ -22,43 +23,51 @@ def __init__(self, loop): @asyncio.coroutine def connect(self): # connect to the RabbitMQ broker - self.connection = yield from asynqp.connect(RABBITMQ_HOST, 5672, username='guest', password='guest') + self.connection = yield from asynqp.connect( + RABBITMQ_HOST, 5672, username="guest", password="guest" + ) # Open a communications channel self.channel = yield from self.connection.open_channel() # Create a queue and an exchange on the broker - self.exchange = yield from self.channel.declare_exchange('test.exchange', 'direct') - self.queue = yield from self.channel.declare_queue('test.queue') + self.exchange = yield from self.channel.declare_exchange( + "test.exchange", "direct" + ) + self.queue = yield from self.channel.declare_queue("test.queue") # Bind the queue to the exchange, so the queue will get messages published to the exchange - yield from self.queue.bind(self.exchange, 'routing.key') + yield from self.queue.bind(self.exchange, "routing.key") yield from self.queue.purge() @asyncio.coroutine def publish_msg(request): - msg = asynqp.Message({'hello': 'world'}) - rabbit_util.exchange.publish(msg, 'routing.key') - rabbit_util.exchange.publish(msg, 'routing.key') - rabbit_util.exchange.publish(msg, 'routing.key') - rabbit_util.exchange.publish(msg, 'routing.key') + msg = asynqp.Message({"hello": "world"}) + rabbit_util.exchange.publish(msg, "routing.key") + rabbit_util.exchange.publish(msg, "routing.key") + rabbit_util.exchange.publish(msg, "routing.key") + rabbit_util.exchange.publish(msg, "routing.key") msg = yield from rabbit_util.queue.get() - return web.Response(text='Published 4 messages. Got 1. %s' % str(msg)) + return web.Response(text="Published 4 messages. Got 1. %s" % str(msg)) async def say_hello(request): - return web.Response(text='Hello, world') + return web.Response(text="Hello, world") async def four_hundred_one(request): - return web.HTTPUnauthorized(reason="I must simulate errors.", text="Simulated server error.") + return web.HTTPUnauthorized( + reason="I must simulate errors.", text="Simulated server error." + ) async def five_hundred(request): - return web.HTTPInternalServerError(reason="I must simulate errors.", text="Simulated server error.") + return web.HTTPInternalServerError( + reason="I must simulate errors.", text="Simulated server error." + ) loop = asyncio.new_event_loop() @@ -67,15 +76,14 @@ async def five_hundred(request): rabbit_util = RabbitUtil(loop) app = web.Application(debug=False) -app.add_routes([web.get('/', say_hello)]) -app.add_routes([web.get('/401', four_hundred_one)]) -app.add_routes([web.get('/500', five_hundred)]) -app.add_routes([web.get('/publish', publish_msg)]) +app.add_routes([web.get("/", say_hello)]) +app.add_routes([web.get("/401", four_hundred_one)]) +app.add_routes([web.get("/500", five_hundred)]) +app.add_routes([web.get("/publish", publish_msg)]) runner = web.AppRunner(app) loop.run_until_complete(runner.setup()) -site = web.TCPSite(runner, 'localhost', 5102) +site = web.TCPSite(runner, "localhost", 5102) loop.run_until_complete(site.start()) loop.run_forever() - diff --git a/example/autoprofile/app.py b/example/autoprofile/app.py index 1ed9cd03c..972f5441f 100644 --- a/example/autoprofile/app.py +++ b/example/autoprofile/app.py @@ -7,10 +7,10 @@ import sys import os -sys.path.append('../..') -os.environ['INSTANA_DEBUG'] = 'yes' -os.environ['INSTANA_AUTOPROFILE'] = 'yes' -import instana +sys.path.append("../..") +os.environ["INSTANA_DEBUG"] = "yes" +os.environ["INSTANA_AUTOPROFILE"] = "yes" + # Simulate CPU intensive work def simulate_cpu(): @@ -27,14 +27,15 @@ def simulate_mem_leak(): for j in range(0, 1800): mem2 = [] for i in range(0, 1000): - obj1 = {'v': random.randint(0, 1000000)} + obj1 = {"v": random.randint(0, 1000000)} mem1.append(obj1) - obj2 = {'v': random.randint(0, 1000000)} + obj2 = {"v": random.randint(0, 1000000)} mem2.append(obj2) time.sleep(1) + threading.Thread(target=simulate_mem_leak).start() @@ -48,13 +49,14 @@ def lock_wait(): while True: lock.acquire() - + threading.Thread(target=lock_wait).start() time.sleep(1) lock.release() time.sleep(1) + threading.Thread(target=simulate_lock).start() diff --git a/example/carry_context.py b/example/carry_context.py index 8a37c236a..e6245f3e0 100644 --- a/example/carry_context.py +++ b/example/carry_context.py @@ -15,6 +15,7 @@ uvloop.install() + async def launch_async_calls(parent_span): """ Method to launch a series (1 currently) of asynchronous http calls @@ -24,19 +25,20 @@ async def launch_async_calls(parent_span): # Now that we are inside of the event loop, first thing to do is to initialize # the tracing context using _and_ the asynchronous tracer - with async_tracer.start_active_span('launch_async_calls', child_of=parent_span): + with async_tracer.start_active_span("launch_async_calls", child_of=parent_span): async with aiohttp.ClientSession() as session: session.get("http://127.0.0.1/api/v2/endpoint/1") session.get("http://127.0.0.1/api/v2/endpoint/2") session.get("http://127.0.0.1/api/v2/endpoint/3") + # # Synchronous application code such as from inside a Django or Flask handler # # Start an ENTRY span in our synchronous execution scope with tracer.start_active_span("launch_uvloop") as sync_scope: - sync_scope.span.set_tag('span.kind', 'entry') + sync_scope.span.set_tag("span.kind", "entry") # You can also retrieve the currently active span with: # tracer.active_span @@ -44,4 +46,3 @@ async def launch_async_calls(parent_span): # Launch our requests asynchronously # Enter the event loop and pass in the parent tracing context (sync_scope) manually asyncio.run(launch_async_calls(sync_scope.span)) - diff --git a/example/opentracing_vanilla.py b/example/opentracing_vanilla.py index 8a836dc9c..3ecf8c124 100644 --- a/example/opentracing_vanilla.py +++ b/example/opentracing_vanilla.py @@ -9,21 +9,25 @@ # Loop continuously with a 2 second sleep to generate traces while True: - with opentracing.tracer.start_active_span('universe') as escope: - escope.span.set_tag('http.method', 'GET') - escope.span.set_tag('http.url', '/users') - escope.span.set_tag('span.kind', 'entry') + with opentracing.tracer.start_active_span("universe") as escope: + escope.span.set_tag("http.method", "GET") + escope.span.set_tag("http.url", "/users") + escope.span.set_tag("span.kind", "entry") - with opentracing.tracer.start_active_span('black-hole', child_of=escope.span) as dbscope: - dbscope.span.set_tag('db.instance', 'users') - dbscope.span.set_tag('db.statement', 'SELECT * FROM user_table') - time.sleep(.1) - dbscope.span.set_tag('db.type', 'mysql') - dbscope.span.set_tag('db.user', 'mysql_login') - dbscope.span.set_tag('span.kind', 'exit') + with opentracing.tracer.start_active_span( + "black-hole", child_of=escope.span + ) as dbscope: + dbscope.span.set_tag("db.instance", "users") + dbscope.span.set_tag("db.statement", "SELECT * FROM user_table") + time.sleep(0.1) + dbscope.span.set_tag("db.type", "mysql") + dbscope.span.set_tag("db.user", "mysql_login") + dbscope.span.set_tag("span.kind", "exit") - with opentracing.tracer.start_active_span('space-dust', child_of=escope.span) as iscope: - iscope.span.log_kv({'message': 'All seems ok'}) + with opentracing.tracer.start_active_span( + "space-dust", child_of=escope.span + ) as iscope: + iscope.span.log_kv({"message": "All seems ok"}) - escope.span.set_tag('http.status_code', 200) - time.sleep(.2) + escope.span.set_tag("http.status_code", 200) + time.sleep(0.2) diff --git a/example/simple.py b/example/simple.py index a18765a9b..23c0b0981 100644 --- a/example/simple.py +++ b/example/simple.py @@ -10,18 +10,18 @@ import opentracing as ot import opentracing.ext.tags as ext -os.environ['INSTANA_SERVICE_NAME'] = "🦄 Stan ❤️s Python 🦄" +os.environ["INSTANA_SERVICE_NAME"] = "🦄 Stan ❤️s Python 🦄" def main(argv): - while (True): + while True: time.sleep(2) simple() time.sleep(200) def simple(): - with ot.tracer.start_active_span('asteroid') as pscope: + with ot.tracer.start_active_span("asteroid") as pscope: pscope.span.set_tag(ext.COMPONENT, "Python simple example app") pscope.span.set_tag(ext.SPAN_KIND, ext.SPAN_KIND_RPC_SERVER) pscope.span.set_tag(ext.PEER_HOSTNAME, "localhost") @@ -31,16 +31,16 @@ def simple(): pscope.span.set_tag("Pete's RequestId", "0xdeadbeef") pscope.span.set_tag("X-Peter-Header", "👀") pscope.span.set_tag("X-Job-Id", "1947282") - time.sleep(.2) + time.sleep(0.2) - with ot.tracer.start_active_span('spacedust', child_of=pscope.span) as cscope: + with ot.tracer.start_active_span("spacedust", child_of=pscope.span) as cscope: cscope.span.set_tag(ext.SPAN_KIND, ext.SPAN_KIND_RPC_CLIENT) cscope.span.set_tag(ext.PEER_HOSTNAME, "localhost") cscope.span.set_tag(ext.HTTP_URL, "/python/simple/two") cscope.span.set_tag(ext.HTTP_METHOD, "POST") cscope.span.set_tag(ext.HTTP_STATUS_CODE, 204) cscope.span.set_baggage_item("someBaggage", "someValue") - time.sleep(.1) + time.sleep(0.1) if __name__ == "__main__": diff --git a/example/xmlrpc/rpcclient.py b/example/xmlrpc/rpcclient.py index 7660572c9..f2234ed71 100644 --- a/example/xmlrpc/rpcclient.py +++ b/example/xmlrpc/rpcclient.py @@ -8,7 +8,7 @@ while True: time.sleep(2) - with opentracing.tracer.start_active_span('RPCJobRunner') as rscope: + with opentracing.tracer.start_active_span("RPCJobRunner") as rscope: rscope.span.set_tag("span.kind", "entry") rscope.span.set_tag("http.url", "http://jobkicker.instana.com/runrpcjob") rscope.span.set_tag("http.method", "GET") @@ -20,10 +20,11 @@ scope.span.set_tag("rpc.call", "dance") carrier = dict() - opentracing.tracer.inject(scope.span.context, opentracing.Format.HTTP_HEADERS, carrier) + opentracing.tracer.inject( + scope.span.context, opentracing.Format.HTTP_HEADERS, carrier + ) with xmlrpc.client.ServerProxy("http://localhost:8261/") as proxy: - result = proxy.dance("NOW!", carrier) scope.span.set_tag("result", result) diff --git a/example/xmlrpc/rpcserver.py b/example/xmlrpc/rpcserver.py index 001851293..812ab3d3e 100644 --- a/example/xmlrpc/rpcserver.py +++ b/example/xmlrpc/rpcserver.py @@ -9,7 +9,7 @@ def dance(payload, carrier): ctx = opentracing.tracer.extract(opentracing.Format.HTTP_HEADERS, carrier) - with opentracing.tracer.start_active_span('RPCServer', child_of=ctx) as scope: + with opentracing.tracer.start_active_span("RPCServer", child_of=ctx) as scope: scope.span.set_tag("span.kind", "entry") scope.span.set_tag("rpc.call", "dance") scope.span.set_tag("rpc.host", "rpc-api.instana.com:8261") @@ -20,4 +20,4 @@ def dance(payload, carrier): server = SimpleXMLRPCServer(("localhost", 8261)) print("Listening on port 8261...") server.register_function(dance, "dance") -server.serve_forever() \ No newline at end of file +server.serve_forever() diff --git a/src/instana/__init__.py b/src/instana/__init__.py index b9bc30ec9..0a3eac28e 100644 --- a/src/instana/__init__.py +++ b/src/instana/__init__.py @@ -102,13 +102,13 @@ def key_to_bool(k: str) -> bool: def get_aws_lambda_handler() -> Tuple[str, str]: """ - For instrumenting AWS Lambda, users specify their original lambda handler - in the LAMBDA_HANDLER environment variable. This function searches for and + For instrumenting AWS Lambda, users specify their original lambda handler + in the LAMBDA_HANDLER environment variable. This function searches for and parses that environment variable or returns the defaults. The default handler value for AWS Lambda is 'lambda_function.lambda_handler' - which equates to the function "lambda_handler in a file named - lambda_function.py" or in Python terms + which equates to the function "lambda_handler in a file named + lambda_function.py" or in Python terms "from lambda_function import lambda_handler" """ handler_module = "lambda_function" @@ -200,8 +200,8 @@ def boot_agent() -> None: storage, # noqa: F401 ) from instana.instrumentation.tornado import ( - client, # noqa: F401 - server, # noqa: F401 + client, # noqa: F401, F811 + server, # noqa: F401, F811 ) # Hooks diff --git a/src/instana/__main__.py b/src/instana/__main__.py index 83a7a2fd9..149fcf18f 100644 --- a/src/instana/__main__.py +++ b/src/instana/__main__.py @@ -7,6 +7,7 @@ The console is disabled by default unless the ipython package is installed. """ + import os import sys @@ -28,10 +29,14 @@ import IPython except ImportError: print("This console is not enabled by default.") - print("IPython not installed. To use this debug console do: 'pip install ipython'\n") + print( + "IPython not installed. To use this debug console do: 'pip install ipython'\n" + ) else: print("Welcome to the Instana console.\n") - print("This is a simple IPython console with the Instana Python Sensor pre-loaded.\n") + print( + "This is a simple IPython console with the Instana Python Sensor pre-loaded.\n" + ) if "INSTANA_DEBUG" not in os.environ: print("If you want debug output of this sensors' activity run instead:\n") diff --git a/src/instana/agent/aws_eks_fargate.py b/src/instana/agent/aws_eks_fargate.py index d404c3f95..f90fa8ad0 100644 --- a/src/instana/agent/aws_eks_fargate.py +++ b/src/instana/agent/aws_eks_fargate.py @@ -4,8 +4,7 @@ The Instana agent (for AWS EKS Fargate) that manages monitoring state and reporting that data. """ -import os -import time + from instana.options import EKSFargateOptions from instana.collector.aws_eks_fargate import EKSFargateCollector from instana.collector.helpers.eks.process import get_pod_name @@ -16,7 +15,8 @@ class EKSFargateAgent(BaseAgent): - """ In-process agent for AWS Fargate """ + """In-process agent for AWS Fargate""" + def __init__(self): super(EKSFargateAgent, self).__init__() @@ -29,15 +29,20 @@ def __init__(self): # Update log level (if INSTANA_LOG_LEVEL was set) self.update_log_level() - logger.info("Stan is on the EKS Pod on AWS Fargate scene. Starting Instana instrumentation version: %s", VERSION) + logger.info( + "Stan is on the EKS Pod on AWS Fargate scene. Starting Instana instrumentation version: %s", + VERSION, + ) if self._validate_options(): self._can_send = True self.collector = EKSFargateCollector(self) self.collector.start() else: - logger.warning("Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. " - "We will not be able to monitor this Pod.") + logger.warning( + "Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. " + "We will not be able to monitor this Pod." + ) def can_send(self): """ @@ -52,7 +57,7 @@ def get_from_structure(self): @return: dict() """ - return {'hl': True, 'cp': 'k8s', 'e': self.podname} + return {"hl": True, "cp": "k8s", "e": self.podname} def report_data_payload(self, payload): """ @@ -67,15 +72,20 @@ def report_data_payload(self, payload): self.report_headers["X-Instana-Host"] = self.podname self.report_headers["X-Instana-Key"] = self.options.agent_key - response = self.client.post(self.__data_bundle_url(), - data=to_json(payload), - headers=self.report_headers, - timeout=self.options.timeout, - verify=self.options.ssl_verify, - proxies=self.options.endpoint_proxy) + response = self.client.post( + self.__data_bundle_url(), + data=to_json(payload), + headers=self.report_headers, + timeout=self.options.timeout, + verify=self.options.ssl_verify, + proxies=self.options.endpoint_proxy, + ) if not 200 <= response.status_code < 300: - logger.info("report_data_payload: Instana responded with status code %s", response.status_code) + logger.info( + "report_data_payload: Instana responded with status code %s", + response.status_code, + ) except Exception as exc: logger.debug("report_data_payload: connection error (%s)", type(exc)) return response @@ -84,7 +94,9 @@ def _validate_options(self): """ Validate that the options used by this Agent are valid. e.g. can we report data? """ - return self.options.endpoint_url is not None and self.options.agent_key is not None + return ( + self.options.endpoint_url is not None and self.options.agent_key is not None + ) def __data_bundle_url(self): """ diff --git a/src/instana/agent/aws_fargate.py b/src/instana/agent/aws_fargate.py index c38024c42..3689dcde2 100644 --- a/src/instana/agent/aws_fargate.py +++ b/src/instana/agent/aws_fargate.py @@ -5,7 +5,7 @@ The Instana agent (for AWS Fargate) that manages monitoring state and reporting that data. """ -import time + from instana.options import AWSFargateOptions from instana.collector.aws_fargate import AWSFargateCollector from ..log import logger @@ -15,7 +15,8 @@ class AWSFargateAgent(BaseAgent): - """ In-process agent for AWS Fargate """ + """In-process agent for AWS Fargate""" + def __init__(self): super(AWSFargateAgent, self).__init__() @@ -27,15 +28,20 @@ def __init__(self): # Update log level (if INSTANA_LOG_LEVEL was set) self.update_log_level() - logger.info("Stan is on the AWS Fargate scene. Starting Instana instrumentation version: %s", VERSION) + logger.info( + "Stan is on the AWS Fargate scene. Starting Instana instrumentation version: %s", + VERSION, + ) if self._validate_options(): self._can_send = True self.collector = AWSFargateCollector(self) self.collector.start() else: - logger.warning("Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. " - "We will not be able monitor this AWS Fargate cluster.") + logger.warning( + "Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. " + "We will not be able monitor this AWS Fargate cluster." + ) def can_send(self): """ @@ -49,7 +55,7 @@ def get_from_structure(self): Retrieves the From data that is reported alongside monitoring data. @return: dict() """ - return {'hl': True, 'cp': 'aws', 'e': self.collector.get_fq_arn()} + return {"hl": True, "cp": "aws", "e": self.collector.get_fq_arn()} def report_data_payload(self, payload): """ @@ -64,15 +70,20 @@ def report_data_payload(self, payload): self.report_headers["X-Instana-Host"] = self.collector.get_fq_arn() self.report_headers["X-Instana-Key"] = self.options.agent_key - response = self.client.post(self.__data_bundle_url(), - data=to_json(payload), - headers=self.report_headers, - timeout=self.options.timeout, - verify=self.options.ssl_verify, - proxies=self.options.endpoint_proxy) + response = self.client.post( + self.__data_bundle_url(), + data=to_json(payload), + headers=self.report_headers, + timeout=self.options.timeout, + verify=self.options.ssl_verify, + proxies=self.options.endpoint_proxy, + ) if not 200 <= response.status_code < 300: - logger.info("report_data_payload: Instana responded with status code %s", response.status_code) + logger.info( + "report_data_payload: Instana responded with status code %s", + response.status_code, + ) except Exception as exc: logger.debug("report_data_payload: connection error (%s)", type(exc)) return response @@ -81,7 +92,9 @@ def _validate_options(self): """ Validate that the options used by this Agent are valid. e.g. can we report data? """ - return self.options.endpoint_url is not None and self.options.agent_key is not None + return ( + self.options.endpoint_url is not None and self.options.agent_key is not None + ) def __data_bundle_url(self): """ diff --git a/src/instana/agent/base.py b/src/instana/agent/base.py index 3c6f63eaf..262b37637 100644 --- a/src/instana/agent/base.py +++ b/src/instana/agent/base.py @@ -4,13 +4,15 @@ """ Base class for all the agent flavors """ + import logging import requests from ..log import logger class BaseAgent(object): - """ Base class for all agent flavors """ + """Base class for all agent flavors""" + client = None options = None @@ -18,13 +20,14 @@ def __init__(self): self.client = requests.Session() def update_log_level(self): - """ Uses the value in to update the global logger """ - if self.options is None or self.options.log_level not in [logging.DEBUG, - logging.INFO, - logging.WARN, - logging.ERROR]: + """Uses the value in to update the global logger""" + if self.options is None or self.options.log_level not in [ + logging.DEBUG, + logging.INFO, + logging.WARN, + logging.ERROR, + ]: logger.warning("BaseAgent.update_log_level: Unknown log level set") return logger.setLevel(self.options.log_level) - diff --git a/src/instana/agent/google_cloud_run.py b/src/instana/agent/google_cloud_run.py index 59d6d9a09..6eaea1086 100644 --- a/src/instana/agent/google_cloud_run.py +++ b/src/instana/agent/google_cloud_run.py @@ -5,7 +5,7 @@ The Instana agent (for GCR) that manages monitoring state and reporting that data. """ -import time + from instana.options import GCROptions from instana.collector.google_cloud_run import GCRCollector from instana.log import logger @@ -15,7 +15,7 @@ class GCRAgent(BaseAgent): - """ In-process agent for Google Cloud Run """ + """In-process agent for Google Cloud Run""" def __init__(self, service, configuration, revision): super(GCRAgent, self).__init__() @@ -28,15 +28,20 @@ def __init__(self, service, configuration, revision): # Update log level (if INSTANA_LOG_LEVEL was set) self.update_log_level() - logger.info("Stan is on the AWS Fargate scene. Starting Instana instrumentation version: %s", VERSION) + logger.info( + "Stan is on the AWS Fargate scene. Starting Instana instrumentation version: %s", + VERSION, + ) if self._validate_options(): self._can_send = True self.collector = GCRCollector(self, service, configuration, revision) self.collector.start() else: - logger.warning("Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. " - "We will not be able monitor this GCR cluster.") + logger.warning( + "Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. " + "We will not be able monitor this GCR cluster." + ) def can_send(self): """ @@ -50,7 +55,7 @@ def get_from_structure(self): Retrieves the From data that is reported alongside monitoring data. @return: dict() """ - return {'hl': True, 'cp': 'gcp', 'e': self.collector.get_instance_id()} + return {"hl": True, "cp": "gcp", "e": self.collector.get_instance_id()} def report_data_payload(self, payload): """ @@ -63,19 +68,25 @@ def report_data_payload(self, payload): self.report_headers = { "Content-Type": "application/json", "X-Instana-Host": "gcp:cloud-run:revision:{revision}".format( - revision=self.collector.revision), - "X-Instana-Key": self.options.agent_key + revision=self.collector.revision + ), + "X-Instana-Key": self.options.agent_key, } - response = self.client.post(self.__data_bundle_url(), - data=to_json(payload), - headers=self.report_headers, - timeout=self.options.timeout, - verify=self.options.ssl_verify, - proxies=self.options.endpoint_proxy) + response = self.client.post( + self.__data_bundle_url(), + data=to_json(payload), + headers=self.report_headers, + timeout=self.options.timeout, + verify=self.options.ssl_verify, + proxies=self.options.endpoint_proxy, + ) if response.status_code >= 400: - logger.info("report_data_payload: Instana responded with status code %s", response.status_code) + logger.info( + "report_data_payload: Instana responded with status code %s", + response.status_code, + ) except Exception as exc: logger.debug("report_data_payload: connection error (%s)", type(exc)) return response @@ -84,7 +95,9 @@ def _validate_options(self): """ Validate that the options used by this Agent are valid. e.g. can we report data? """ - return self.options.endpoint_url is not None and self.options.agent_key is not None + return ( + self.options.endpoint_url is not None and self.options.agent_key is not None + ) def __data_bundle_url(self): """ diff --git a/src/instana/agent/host.py b/src/instana/agent/host.py index b4943cdbb..9e29eba84 100644 --- a/src/instana/agent/host.py +++ b/src/instana/agent/host.py @@ -24,7 +24,8 @@ class AnnounceData(object): - """ The Announce Payload """ + """The Announce Payload""" + pid = 0 agentUuid = "" @@ -38,6 +39,7 @@ class HostAgent(BaseAgent): parts it handles are the announce state and the collection and reporting of metrics and spans to the Instana Host agent. """ + AGENT_DISCOVERY_PATH = "com.instana.plugin.python.discovery" AGENT_DATA_PATH = "com.instana.plugin.python.%d" @@ -54,7 +56,10 @@ def __init__(self): # Update log level from what Options detected self.update_log_level() - logger.info("Stan is on the scene. Starting Instana instrumentation version: %s", VERSION) + logger.info( + "Stan is on the scene. Starting Instana instrumentation version: %s", + VERSION, + ) self.collector = HostCollector(self) self.machine = TheMachine(self) @@ -124,24 +129,29 @@ def set_from(self, res_data): @return: None """ if "secrets" in res_data: - self.options.secrets_matcher = res_data['secrets']['matcher'] - self.options.secrets_list = res_data['secrets']['list'] + self.options.secrets_matcher = res_data["secrets"]["matcher"] + self.options.secrets_list = res_data["secrets"]["list"] if "extraHeaders" in res_data: if self.options.extra_http_headers is None: - self.options.extra_http_headers = res_data['extraHeaders'] + self.options.extra_http_headers = res_data["extraHeaders"] else: - self.options.extra_http_headers.extend(res_data['extraHeaders']) - logger.info("Will also capture these custom headers: %s", self.options.extra_http_headers) + self.options.extra_http_headers.extend(res_data["extraHeaders"]) + logger.info( + "Will also capture these custom headers: %s", + self.options.extra_http_headers, + ) - self.announce_data = AnnounceData(pid=res_data['pid'], agentUuid=res_data['agentUuid']) + self.announce_data = AnnounceData( + pid=res_data["pid"], agentUuid=res_data["agentUuid"] + ) def get_from_structure(self): """ Retrieves the From data that is reported alongside monitoring data. @return: dict() """ - return {'e': self.announce_data.pid, 'h': self.announce_data.agentUuid} + return {"e": self.announce_data.pid, "h": self.announce_data.agentUuid} def is_agent_listening(self, host, port): """ @@ -157,10 +167,14 @@ def is_agent_listening(self, host, port): logger.debug("Instana host agent found on %s:%d", host, port) result = True else: - logger.debug("The attempt to connect to the Instana host "\ - "agent on %s:%d has failed with an unexpected " \ - "status code. Expected HTTP 200 but received: %d", - host, port, response.status_code) + logger.debug( + "The attempt to connect to the Instana host " + "agent on %s:%d has failed with an unexpected " + "status code. Expected HTTP 200 but received: %d", + host, + port, + response.status_code, + ) except Exception: logger.debug("Instana Host Agent not found on %s:%d", host, port) return result @@ -171,10 +185,12 @@ def announce(self, discovery): """ try: url = self.__discovery_url() - response = self.client.put(url, - data=to_json(discovery), - headers={"Content-Type": "application/json"}, - timeout=0.8) + response = self.client.put( + url, + data=to_json(discovery), + headers={"Content-Type": "application/json"}, + timeout=0.8, + ) except Exception as exc: logger.debug("announce: connection error (%s)", type(exc)) return None @@ -183,7 +199,9 @@ def announce(self, discovery): self.last_seen = datetime.now() if response.status_code != 200: - logger.debug("announce: response status code (%s) is NOT 200", response.status_code) + logger.debug( + "announce: response status code (%s) is NOT 200", response.status_code + ) return None if isinstance(response.content, bytes): @@ -193,19 +211,19 @@ def announce(self, discovery): try: payload = json.loads(raw_json) - except json.JSONDecodeError as e: + except json.JSONDecodeError: logger.debug("announce: response is not JSON: (%s)", raw_json) return None - if not hasattr(payload, 'get'): + if not hasattr(payload, "get"): logger.debug("announce: response payload has no fields: (%s)", payload) return None - if not payload.get('pid'): + if not payload.get("pid"): logger.debug("announce: response payload has no pid: (%s)", payload) return None - if not payload.get('agentUuid'): + if not payload.get("agentUuid"): logger.debug("announce: response payload has no agentUuid: (%s)", payload) return None @@ -221,11 +239,12 @@ def log_message_to_host_agent(self, message): payload["m"] = message url = self.__agent_logger_url() - response = self.client.post(url, - data=to_json(payload), - headers={"Content-Type": "application/json", - "X-Log-Level": "INFO"}, - timeout=0.8) + response = self.client.post( + url, + data=to_json(payload), + headers={"Content-Type": "application/json", "X-Log-Level": "INFO"}, + timeout=0.8, + ) if 200 <= response.status_code <= 204: self.last_seen = datetime.now() @@ -253,37 +272,43 @@ def report_data_payload(self, payload): response = None try: # Report spans (if any) - span_count = len(payload['spans']) + span_count = len(payload["spans"]) if span_count > 0: logger.debug("Reporting %d spans", span_count) - response = self.client.post(self.__traces_url(), - data=to_json(payload['spans']), - headers={"Content-Type": "application/json"}, - timeout=0.8) + response = self.client.post( + self.__traces_url(), + data=to_json(payload["spans"]), + headers={"Content-Type": "application/json"}, + timeout=0.8, + ) if response is not None and 200 <= response.status_code <= 204: self.last_seen = datetime.now() # Report profiles (if any) - profile_count = len(payload['profiles']) + profile_count = len(payload["profiles"]) if profile_count > 0: logger.debug("Reporting %d profiles", profile_count) - response = self.client.post(self.__profiles_url(), - data=to_json(payload['profiles']), - headers={"Content-Type": "application/json"}, - timeout=0.8) + response = self.client.post( + self.__profiles_url(), + data=to_json(payload["profiles"]), + headers={"Content-Type": "application/json"}, + timeout=0.8, + ) if response is not None and 200 <= response.status_code <= 204: self.last_seen = datetime.now() # Report metrics - metric_count = len(payload['metrics']) + metric_count = len(payload["metrics"]) if metric_count > 0: metric_bundle = payload["metrics"]["plugins"][0]["data"] - response = self.client.post(self.__data_url(), - data=to_json(metric_bundle), - headers={"Content-Type": "application/json"}, - timeout=0.8) + response = self.client.post( + self.__data_url(), + data=to_json(metric_bundle), + headers={"Content-Type": "application/json"}, + timeout=0.8, + ) if response is not None and 200 <= response.status_code <= 204: self.last_seen = datetime.now() @@ -297,7 +322,11 @@ def report_data_payload(self, payload): except urllib3.exceptions.MaxRetryError: pass except Exception as exc: - logger.debug("report_data_payload: Instana host agent connection error (%s)", type(exc), exc_info=True) + logger.debug( + "report_data_payload: Instana host agent connection error (%s)", + type(exc), + exc_info=True, + ) return response def handle_agent_tasks(self, task): @@ -310,21 +339,23 @@ def handle_agent_tasks(self, task): if task["action"] == "python.source": payload = get_py_source(task["args"]["file"]) else: - message = "Unrecognized action: %s. An newer Instana package may be required " \ - "for this. Current version: %s" % (task["action"], VERSION) + message = ( + "Unrecognized action: %s. An newer Instana package may be required " + "for this. Current version: %s" % (task["action"], VERSION) + ) payload = {"error": message} else: payload = {"error": "Instana Python: No action specified in request."} self.__task_response(task["messageId"], payload) - def diagnostics(self): """ Helper function to dump out state. """ try: import threading + dt_format = "%Y-%m-%d %H:%M:%S" logger.warning("====> Instana Python Language Agent Diagnostics <====") @@ -349,14 +380,25 @@ def diagnostics(self): logger.warning("----> Collector <----") logger.warning("Collector: %s", self.collector) - logger.warning("is_collector_thread_running?: %s", self.collector.is_reporting_thread_running()) - logger.warning("background_report_lock.locked?: %s", self.collector.background_report_lock.locked()) + logger.warning( + "is_collector_thread_running?: %s", + self.collector.is_reporting_thread_running(), + ) + logger.warning( + "background_report_lock.locked?: %s", + self.collector.background_report_lock.locked(), + ) logger.warning("ready_to_start: %s", self.collector.ready_to_start) logger.warning("reporting_thread: %s", self.collector.reporting_thread) logger.warning("report_interval: %s", self.collector.report_interval) - logger.warning("should_send_snapshot_data: %s", self.collector.should_send_snapshot_data()) + logger.warning( + "should_send_snapshot_data: %s", + self.collector.should_send_snapshot_data(), + ) logger.warning("spans in queue: %s", self.collector.span_queue.qsize()) - logger.warning("thread_shutdown is_set: %s", self.collector.thread_shutdown.is_set()) + logger.warning( + "thread_shutdown is_set: %s", self.collector.thread_shutdown.is_set() + ) logger.warning("----> Threads <----") logger.warning("Threads: %s", threading.enumerate()) @@ -372,52 +414,85 @@ def __task_response(self, message_id, data): try: payload = json.dumps(data) - logger.debug("Task response is %s: %s", self.__response_url(message_id), payload) + logger.debug( + "Task response is %s: %s", self.__response_url(message_id), payload + ) - response = self.client.post(self.__response_url(message_id), - data=payload, - headers={"Content-Type": "application/json"}, - timeout=0.8) + response = self.client.post( + self.__response_url(message_id), + data=payload, + headers={"Content-Type": "application/json"}, + timeout=0.8, + ) except Exception as exc: - logger.debug("__task_response: Instana host agent connection error (%s)", type(exc)) + logger.debug( + "__task_response: Instana host agent connection error (%s)", type(exc) + ) return response def __discovery_url(self): """ URL for announcing to the host agent """ - return "http://%s:%s/%s" % (self.options.agent_host, self.options.agent_port, self.AGENT_DISCOVERY_PATH) + return "http://%s:%s/%s" % ( + self.options.agent_host, + self.options.agent_port, + self.AGENT_DISCOVERY_PATH, + ) def __data_url(self): """ URL for posting metrics to the host agent. Only valid when announced. """ path = self.AGENT_DATA_PATH % self.announce_data.pid - return "http://%s:%s/%s" % (self.options.agent_host, self.options.agent_port, path) + return "http://%s:%s/%s" % ( + self.options.agent_host, + self.options.agent_port, + path, + ) def __traces_url(self): """ URL for posting traces to the host agent. Only valid when announced. """ path = "com.instana.plugin.python/traces.%d" % self.announce_data.pid - return "http://%s:%s/%s" % (self.options.agent_host, self.options.agent_port, path) + return "http://%s:%s/%s" % ( + self.options.agent_host, + self.options.agent_port, + path, + ) def __profiles_url(self): """ URL for posting profiles to the host agent. Only valid when announced. """ path = "com.instana.plugin.python/profiles.%d" % self.announce_data.pid - return "http://%s:%s/%s" % (self.options.agent_host, self.options.agent_port, path) + return "http://%s:%s/%s" % ( + self.options.agent_host, + self.options.agent_port, + path, + ) def __response_url(self, message_id): """ URL for responding to agent requests. """ - path = "com.instana.plugin.python/response.%d?messageId=%s" % (int(self.announce_data.pid), message_id) - return "http://%s:%s/%s" % (self.options.agent_host, self.options.agent_port, path) + path = "com.instana.plugin.python/response.%d?messageId=%s" % ( + int(self.announce_data.pid), + message_id, + ) + return "http://%s:%s/%s" % ( + self.options.agent_host, + self.options.agent_port, + path, + ) def __agent_logger_url(self): """ URL for logging messages to the discovered host agent. """ - return "http://%s:%s/%s" % (self.options.agent_host, self.options.agent_port, "com.instana.agent.logger") + return "http://%s:%s/%s" % ( + self.options.agent_host, + self.options.agent_port, + "com.instana.agent.logger", + ) diff --git a/src/instana/autoprofile/runtime.py b/src/instana/autoprofile/runtime.py index e296e1036..80179113b 100644 --- a/src/instana/autoprofile/runtime.py +++ b/src/instana/autoprofile/runtime.py @@ -9,6 +9,7 @@ if TYPE_CHECKING: from types import FrameType + class RuntimeInfo(object): OS_LINUX = sys.platform.startswith("linux") OS_DARWIN = sys.platform == "darwin" diff --git a/src/instana/collector/helpers/fargate/docker.py b/src/instana/collector/helpers/fargate/docker.py index e654772d0..f1aa1b3b6 100644 --- a/src/instana/collector/helpers/fargate/docker.py +++ b/src/instana/collector/helpers/fargate/docker.py @@ -1,7 +1,8 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2020 -""" Module to handle the collection of Docker metrics in AWS Fargate """ +"""Module to handle the collection of Docker metrics in AWS Fargate""" + from __future__ import division from ....log import logger from ..base import BaseHelper @@ -9,7 +10,8 @@ class DockerHelper(BaseHelper): - """ This class acts as a helper to collect Docker snapshot and metric information """ + """This class acts as a helper to collect Docker snapshot and metric information""" + def __init__(self, collector): super(DockerHelper, self).__init__(collector) @@ -44,14 +46,16 @@ def collect_metrics(self, **kwargs): with_snapshot = kwargs.get("with_snapshot", False) # Metrics - self._collect_container_metrics(plugin_data, docker_id, with_snapshot) + self._collect_container_metrics( + plugin_data, docker_id, with_snapshot + ) # Snapshot if with_snapshot: self._collect_container_snapshot(plugin_data, container) plugins.append(plugin_data) - #logger.debug(to_pretty_json(plugin_data)) + # logger.debug(to_pretty_json(plugin_data)) except Exception: logger.debug("DockerHelper.collect_metrics: ", exc_info=True) return plugins @@ -67,19 +71,29 @@ def _collect_container_snapshot(self, plugin_data, container): networks = container.get("Networks", []) if len(networks) >= 1: - plugin_data["data"]["NetworkMode"] = networks[0].get("NetworkMode", None) + plugin_data["data"]["NetworkMode"] = networks[0].get( + "NetworkMode", None + ) except Exception: logger.debug("_collect_container_snapshot: ", exc_info=True) def _collect_container_metrics(self, plugin_data, docker_id, with_snapshot): container = self.collector.task_stats_metadata.get(docker_id, None) if container is not None: - self._collect_network_metrics(container, plugin_data, docker_id, with_snapshot) + self._collect_network_metrics( + container, plugin_data, docker_id, with_snapshot + ) self._collect_cpu_metrics(container, plugin_data, docker_id, with_snapshot) - self._collect_memory_metrics(container, plugin_data, docker_id, with_snapshot) - self._collect_blkio_metrics(container, plugin_data, docker_id, with_snapshot) + self._collect_memory_metrics( + container, plugin_data, docker_id, with_snapshot + ) + self._collect_blkio_metrics( + container, plugin_data, docker_id, with_snapshot + ) - def _collect_network_metrics(self, container, plugin_data, docker_id, with_snapshot): + def _collect_network_metrics( + self, container, plugin_data, docker_id, with_snapshot + ): try: networks = container.get("networks", None) tx_bytes_total = tx_dropped_total = tx_errors_total = tx_packets_total = 0 @@ -98,23 +112,63 @@ def _collect_network_metrics(self, container, plugin_data, docker_id, with_snaps rx_errors_total += networks[key].get("rx_errors", 0) rx_packets_total += networks[key].get("rx_packets", 0) - self.apply_delta(tx_bytes_total, self.previous[docker_id]["network"]["tx"], - plugin_data["data"]["tx"], "bytes", with_snapshot) - self.apply_delta(tx_dropped_total, self.previous[docker_id]["network"]["tx"], - plugin_data["data"]["tx"], "dropped", with_snapshot) - self.apply_delta(tx_errors_total, self.previous[docker_id]["network"]["tx"], - plugin_data["data"]["tx"], "errors", with_snapshot) - self.apply_delta(tx_packets_total, self.previous[docker_id]["network"]["tx"], - plugin_data["data"]["tx"], "packets", with_snapshot) - - self.apply_delta(rx_bytes_total, self.previous[docker_id]["network"]["rx"], - plugin_data["data"]["rx"], "bytes", with_snapshot) - self.apply_delta(rx_dropped_total, self.previous[docker_id]["network"]["rx"], - plugin_data["data"]["rx"], "dropped", with_snapshot) - self.apply_delta(rx_errors_total, self.previous[docker_id]["network"]["rx"], - plugin_data["data"]["rx"], "errors", with_snapshot) - self.apply_delta(rx_packets_total, self.previous[docker_id]["network"]["rx"], - plugin_data["data"]["rx"], "packets", with_snapshot) + self.apply_delta( + tx_bytes_total, + self.previous[docker_id]["network"]["tx"], + plugin_data["data"]["tx"], + "bytes", + with_snapshot, + ) + self.apply_delta( + tx_dropped_total, + self.previous[docker_id]["network"]["tx"], + plugin_data["data"]["tx"], + "dropped", + with_snapshot, + ) + self.apply_delta( + tx_errors_total, + self.previous[docker_id]["network"]["tx"], + plugin_data["data"]["tx"], + "errors", + with_snapshot, + ) + self.apply_delta( + tx_packets_total, + self.previous[docker_id]["network"]["tx"], + plugin_data["data"]["tx"], + "packets", + with_snapshot, + ) + + self.apply_delta( + rx_bytes_total, + self.previous[docker_id]["network"]["rx"], + plugin_data["data"]["rx"], + "bytes", + with_snapshot, + ) + self.apply_delta( + rx_dropped_total, + self.previous[docker_id]["network"]["rx"], + plugin_data["data"]["rx"], + "dropped", + with_snapshot, + ) + self.apply_delta( + rx_errors_total, + self.previous[docker_id]["network"]["rx"], + plugin_data["data"]["rx"], + "errors", + with_snapshot, + ) + self.apply_delta( + rx_packets_total, + self.previous[docker_id]["network"]["rx"], + plugin_data["data"]["rx"], + "packets", + with_snapshot, + ) except Exception: logger.debug("_collect_network_metrics: ", exc_info=True) @@ -128,28 +182,54 @@ def _collect_cpu_metrics(self, container, plugin_data, docker_id, with_snapshot) online_cpus = cpu_stats.get("online_cpus", 1) system_cpu_usage = cpu_stats.get("system_cpu_usage", 0) - metric_value = (cpu_usage["total_usage"] / system_cpu_usage) * online_cpus - self.apply_delta(round(metric_value, 6), - self.previous[docker_id]["cpu"], - plugin_data["data"]["cpu"], "total_usage", with_snapshot) + metric_value = ( + cpu_usage["total_usage"] / system_cpu_usage + ) * online_cpus + self.apply_delta( + round(metric_value, 6), + self.previous[docker_id]["cpu"], + plugin_data["data"]["cpu"], + "total_usage", + with_snapshot, + ) - metric_value = (cpu_usage["usage_in_usermode"] / system_cpu_usage) * online_cpus - self.apply_delta(round(metric_value, 6), - self.previous[docker_id]["cpu"], - plugin_data["data"]["cpu"], "user_usage", with_snapshot) + metric_value = ( + cpu_usage["usage_in_usermode"] / system_cpu_usage + ) * online_cpus + self.apply_delta( + round(metric_value, 6), + self.previous[docker_id]["cpu"], + plugin_data["data"]["cpu"], + "user_usage", + with_snapshot, + ) - metric_value = (cpu_usage["usage_in_kernelmode"] / system_cpu_usage) * online_cpus - self.apply_delta(round(metric_value, 6), - self.previous[docker_id]["cpu"], - plugin_data["data"]["cpu"], "system_usage", with_snapshot) + metric_value = ( + cpu_usage["usage_in_kernelmode"] / system_cpu_usage + ) * online_cpus + self.apply_delta( + round(metric_value, 6), + self.previous[docker_id]["cpu"], + plugin_data["data"]["cpu"], + "system_usage", + with_snapshot, + ) if throttling_data is not None: - self.apply_delta(throttling_data, - self.previous[docker_id]["cpu"], - plugin_data["data"]["cpu"], ("periods", "throttling_count"), with_snapshot) - self.apply_delta(throttling_data, - self.previous[docker_id]["cpu"], - plugin_data["data"]["cpu"], ("throttled_time", "throttling_time"), with_snapshot) + self.apply_delta( + throttling_data, + self.previous[docker_id]["cpu"], + plugin_data["data"]["cpu"], + ("periods", "throttling_count"), + with_snapshot, + ) + self.apply_delta( + throttling_data, + self.previous[docker_id]["cpu"], + plugin_data["data"]["cpu"], + ("throttled_time", "throttling_time"), + with_snapshot, + ) except Exception: logger.debug("_collect_cpu_metrics: ", exc_info=True) @@ -158,26 +238,71 @@ def _collect_memory_metrics(self, container, plugin_data, docker_id, with_snapsh memory = container.get("memory_stats", {}) memory_stats = memory.get("stats", None) - self.apply_delta(memory, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "usage", with_snapshot) - self.apply_delta(memory, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "max_usage", with_snapshot) - self.apply_delta(memory, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "limit", with_snapshot) + self.apply_delta( + memory, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "usage", + with_snapshot, + ) + self.apply_delta( + memory, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "max_usage", + with_snapshot, + ) + self.apply_delta( + memory, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "limit", + with_snapshot, + ) if memory_stats is not None: - self.apply_delta(memory_stats, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "active_anon", with_snapshot) - self.apply_delta(memory_stats, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "active_file", with_snapshot) - self.apply_delta(memory_stats, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "inactive_anon", with_snapshot) - self.apply_delta(memory_stats, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "inactive_file", with_snapshot) - self.apply_delta(memory_stats, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "total_cache", with_snapshot) - self.apply_delta(memory_stats, self.previous[docker_id]["memory"], - plugin_data["data"]["memory"], "total_rss", with_snapshot) + self.apply_delta( + memory_stats, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "active_anon", + with_snapshot, + ) + self.apply_delta( + memory_stats, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "active_file", + with_snapshot, + ) + self.apply_delta( + memory_stats, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "inactive_anon", + with_snapshot, + ) + self.apply_delta( + memory_stats, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "inactive_file", + with_snapshot, + ) + self.apply_delta( + memory_stats, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "total_cache", + with_snapshot, + ) + self.apply_delta( + memory_stats, + self.previous[docker_id]["memory"], + plugin_data["data"]["memory"], + "total_rss", + with_snapshot, + ) except Exception: logger.debug("_collect_memory_metrics: ", exc_info=True) @@ -189,16 +314,30 @@ def _collect_blkio_metrics(self, container, plugin_data, docker_id, with_snapsho if service_bytes is not None: for entry in service_bytes: if entry["op"] == "Read": - previous_value = self.previous_blkio[docker_id].get("blk_read", 0) + previous_value = self.previous_blkio[docker_id].get( + "blk_read", 0 + ) value_diff = entry["value"] - previous_value - self.apply_delta(value_diff, self.previous[docker_id]["blkio"], - plugin_data["data"]["blkio"], "blk_read", with_snapshot) + self.apply_delta( + value_diff, + self.previous[docker_id]["blkio"], + plugin_data["data"]["blkio"], + "blk_read", + with_snapshot, + ) self.previous_blkio[docker_id]["blk_read"] = entry["value"] elif entry["op"] == "Write": - previous_value = self.previous_blkio[docker_id].get("blk_write", 0) + previous_value = self.previous_blkio[docker_id].get( + "blk_write", 0 + ) value_diff = entry["value"] - previous_value - self.apply_delta(value_diff, self.previous[docker_id]["blkio"], - plugin_data["data"]["blkio"], "blk_write", with_snapshot) + self.apply_delta( + value_diff, + self.previous[docker_id]["blkio"], + plugin_data["data"]["blkio"], + "blk_write", + with_snapshot, + ) self.previous_blkio[docker_id]["blk_write"] = entry["value"] except Exception: logger.debug("_collect_blkio_metrics: ", exc_info=True) diff --git a/src/instana/collector/helpers/fargate/process.py b/src/instana/collector/helpers/fargate/process.py index abadab5f6..2efeefbb4 100644 --- a/src/instana/collector/helpers/fargate/process.py +++ b/src/instana/collector/helpers/fargate/process.py @@ -6,7 +6,7 @@ class FargateProcessHelper(ProcessHelper): - """ Helper class to extend the generic process helper class with the corresponding fargate attributes """ + """Helper class to extend the generic process helper class with the corresponding fargate attributes""" def collect_metrics(self, **kwargs): plugin_data = dict() @@ -14,11 +14,15 @@ def collect_metrics(self, **kwargs): plugin_data = super(FargateProcessHelper, self).collect_metrics(**kwargs) plugin_data["data"]["containerType"] = "docker" if self.collector.root_metadata is not None: - plugin_data["data"]["container"] = self.collector.root_metadata.get("DockerId") + plugin_data["data"]["container"] = self.collector.root_metadata.get( + "DockerId" + ) if kwargs.get("with_snapshot"): if self.collector.task_metadata is not None: - plugin_data["data"]["com.instana.plugin.host.name"] = self.collector.task_metadata.get("TaskArn") + plugin_data["data"]["com.instana.plugin.host.name"] = ( + self.collector.task_metadata.get("TaskArn") + ) except Exception: logger.debug("FargateProcessHelper.collect_metrics: ", exc_info=True) return [plugin_data] diff --git a/src/instana/collector/helpers/fargate/task.py b/src/instana/collector/helpers/fargate/task.py index f24aa0358..1ea67be76 100644 --- a/src/instana/collector/helpers/fargate/task.py +++ b/src/instana/collector/helpers/fargate/task.py @@ -1,14 +1,16 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2020 -""" Module to assist in the data collection about the AWS Fargate task that is running this process """ +"""Module to assist in the data collection about the AWS Fargate task that is running this process""" + from ....log import logger from ..base import BaseHelper from ....util import DictionaryOfStan class TaskHelper(BaseHelper): - """ This class helps in collecting data about the AWS Fargate task that is running """ + """This class helps in collecting data about the AWS Fargate task that is running""" + def collect_metrics(self, **kwargs): """ Collect and return metrics data (and optionally snapshot data) for this task @@ -21,28 +23,54 @@ def collect_metrics(self, **kwargs): plugin_data = dict() try: plugin_data["name"] = "com.instana.plugin.aws.ecs.task" - plugin_data["entityId"] = self.collector.task_metadata.get("TaskARN", None) + plugin_data["entityId"] = self.collector.task_metadata.get( + "TaskARN", None + ) plugin_data["data"] = DictionaryOfStan() - plugin_data["data"]["taskArn"] = self.collector.task_metadata.get("TaskARN", None) - plugin_data["data"]["clusterArn"] = self.collector.task_metadata.get("Cluster", None) - plugin_data["data"]["taskDefinition"] = self.collector.task_metadata.get("Family", None) - plugin_data["data"]["taskDefinitionVersion"] = self.collector.task_metadata.get("Revision", None) - plugin_data["data"]["availabilityZone"] = self.collector.task_metadata.get("AvailabilityZone", None) + plugin_data["data"]["taskArn"] = self.collector.task_metadata.get( + "TaskARN", None + ) + plugin_data["data"]["clusterArn"] = ( + self.collector.task_metadata.get("Cluster", None) + ) + plugin_data["data"]["taskDefinition"] = ( + self.collector.task_metadata.get("Family", None) + ) + plugin_data["data"]["taskDefinitionVersion"] = ( + self.collector.task_metadata.get("Revision", None) + ) + plugin_data["data"]["availabilityZone"] = ( + self.collector.task_metadata.get("AvailabilityZone", None) + ) if kwargs.get("with_snapshot"): - plugin_data["data"]["desiredStatus"] = self.collector.task_metadata.get("DesiredStatus", None) - plugin_data["data"]["knownStatus"] = self.collector.task_metadata.get("KnownStatus", None) - plugin_data["data"]["pullStartedAt"] = self.collector.task_metadata.get("PullStartedAt", None) - plugin_data["data"]["pullStoppedAt"] = self.collector.task_metadata.get("PullStoppeddAt", None) + plugin_data["data"]["desiredStatus"] = ( + self.collector.task_metadata.get("DesiredStatus", None) + ) + plugin_data["data"]["knownStatus"] = ( + self.collector.task_metadata.get("KnownStatus", None) + ) + plugin_data["data"]["pullStartedAt"] = ( + self.collector.task_metadata.get("PullStartedAt", None) + ) + plugin_data["data"]["pullStoppedAt"] = ( + self.collector.task_metadata.get("PullStoppeddAt", None) + ) limits = self.collector.task_metadata.get("Limits", {}) plugin_data["data"]["limits"]["cpu"] = limits.get("CPU", None) - plugin_data["data"]["limits"]["memory"] = limits.get("Memory", None) + plugin_data["data"]["limits"]["memory"] = limits.get( + "Memory", None + ) if self.collector.agent.options.zone is not None: - plugin_data["data"]["instanaZone"] = self.collector.agent.options.zone + plugin_data["data"]["instanaZone"] = ( + self.collector.agent.options.zone + ) if self.collector.agent.options.tags is not None: - plugin_data["data"]["tags"] = self.collector.agent.options.tags + plugin_data["data"]["tags"] = ( + self.collector.agent.options.tags + ) except Exception: logger.debug("collect_task_metrics: ", exc_info=True) finally: diff --git a/src/instana/collector/helpers/google_cloud_run/instance_entity.py b/src/instana/collector/helpers/google_cloud_run/instance_entity.py index 44a68d19d..10b2daf5a 100644 --- a/src/instana/collector/helpers/google_cloud_run/instance_entity.py +++ b/src/instana/collector/helpers/google_cloud_run/instance_entity.py @@ -1,7 +1,8 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2021 -""" Module to assist in the data collection about the google cloud run service revision instance entity """ +"""Module to assist in the data collection about the google cloud run service revision instance entity""" + import os from ....log import logger @@ -10,7 +11,7 @@ class InstanceEntityHelper(BaseHelper): - """ This class helps in collecting data about the google cloud run service revision instance entity """ + """This class helps in collecting data about the google cloud run service revision instance entity""" def collect_metrics(self, **kwargs): """ @@ -19,20 +20,24 @@ def collect_metrics(self, **kwargs): """ plugins = [] plugin_data = dict() - instance_metadata = kwargs.get('instance_metadata', {}) - project_metadata = kwargs.get('project_metadata', {}) + instance_metadata = kwargs.get("instance_metadata", {}) + project_metadata = kwargs.get("project_metadata", {}) try: plugin_data["name"] = "com.instana.plugin.gcp.run.revision.instance" plugin_data["entityId"] = instance_metadata.get("id") plugin_data["data"] = DictionaryOfStan() plugin_data["data"]["runtime"] = "python" - plugin_data["data"]["region"] = instance_metadata.get("region").split("/")[-1] + plugin_data["data"]["region"] = instance_metadata.get("region").split("/")[ + -1 + ] plugin_data["data"]["service"] = self.collector.service plugin_data["data"]["configuration"] = self.collector.configuration plugin_data["data"]["revision"] = self.collector.revision plugin_data["data"]["instanceId"] = plugin_data["entityId"] plugin_data["data"]["port"] = os.getenv("PORT", "") - plugin_data["data"]["numericProjectId"] = project_metadata.get("numericProjectId") + plugin_data["data"]["numericProjectId"] = project_metadata.get( + "numericProjectId" + ) plugin_data["data"]["projectId"] = project_metadata.get("projectId") except Exception: diff --git a/src/instana/collector/helpers/google_cloud_run/process.py b/src/instana/collector/helpers/google_cloud_run/process.py index 443339ef9..537b39a58 100644 --- a/src/instana/collector/helpers/google_cloud_run/process.py +++ b/src/instana/collector/helpers/google_cloud_run/process.py @@ -6,7 +6,7 @@ class GCRProcessHelper(ProcessHelper): - """ Helper class to extend the generic process helper class with the corresponding Google Cloud Run attributes """ + """Helper class to extend the generic process helper class with the corresponding Google Cloud Run attributes""" def collect_metrics(self, **kwargs): plugin_data = dict() @@ -14,8 +14,11 @@ def collect_metrics(self, **kwargs): plugin_data = super(GCRProcessHelper, self).collect_metrics(**kwargs) plugin_data["data"]["containerType"] = "gcpCloudRunInstance" plugin_data["data"]["container"] = self.collector.get_instance_id() - plugin_data["data"]["com.instana.plugin.host.name"] = "gcp:cloud-run:revision:{revision}".format( - revision=self.collector.revision) + plugin_data["data"]["com.instana.plugin.host.name"] = ( + "gcp:cloud-run:revision:{revision}".format( + revision=self.collector.revision + ) + ) except Exception: logger.debug("GCRProcessHelper.collect_metrics: ", exc_info=True) return [plugin_data] diff --git a/src/instana/collector/helpers/runtime.py b/src/instana/collector/helpers/runtime.py index 8aef48e35..70109bee2 100644 --- a/src/instana/collector/helpers/runtime.py +++ b/src/instana/collector/helpers/runtime.py @@ -1,7 +1,8 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2020 -""" Collection helper for the Python runtime """ +"""Collection helper for the Python runtime""" + import gc import importlib.metadata import os @@ -19,10 +20,11 @@ PATH_OF_DEPRECATED_INSTALLATION_VIA_HOST_AGENT = "/tmp/.instana/python" -PATH_OF_AUTOTRACE_WEBHOOK_SITEDIR = '/opt/instana/instrumentation/python/' +PATH_OF_AUTOTRACE_WEBHOOK_SITEDIR = "/opt/instana/instrumentation/python/" + def is_autowrapt_instrumented(): - return 'instana' in os.environ.get('AUTOWRAPT_BOOTSTRAP', ()) + return "instana" in os.environ.get("AUTOWRAPT_BOOTSTRAP", ()) def is_webhook_instrumented(): @@ -316,9 +318,9 @@ def _collect_runtime_snapshot(self, plugin_data): snapshot_payload["iv"] = VERSION if is_autowrapt_instrumented(): - snapshot_payload['m'] = 'Autowrapt' + snapshot_payload["m"] = "Autowrapt" elif is_webhook_instrumented(): - snapshot_payload['m'] = 'AutoTrace' + snapshot_payload["m"] = "AutoTrace" else: snapshot_payload["m"] = "Manual" diff --git a/src/instana/collector/utils.py b/src/instana/collector/utils.py index 7292cca4c..74e3021f7 100644 --- a/src/instana/collector/utils.py +++ b/src/instana/collector/utils.py @@ -23,6 +23,6 @@ def format_span( span.s = format_span_id(span.s) span.p = format_span_id(span.p) if span.p else None if isinstance(span.k, SpanKind): - span.k = span.k.value if not span.k is SpanKind.INTERNAL else 3 + span.k = span.k.value if span.k is not SpanKind.INTERNAL else 3 spans.append(span) return spans diff --git a/src/instana/fsm.py b/src/instana/fsm.py index 1897cf301..77799252a 100644 --- a/src/instana/fsm.py +++ b/src/instana/fsm.py @@ -28,11 +28,11 @@ def __init__(self, **kwds): def to_dict(self): kvs = dict() - kvs['pid'] = self.pid - kvs['name'] = self.name - kvs['args'] = self.args - kvs['fd'] = self.fd - kvs['inode'] = self.inode + kvs["pid"] = self.pid + kvs["name"] = self.name + kvs["args"] = self.args + kvs["fd"] = self.fd + kvs["inode"] = self.inode return kvs @@ -50,19 +50,24 @@ def __init__(self, agent): logger.debug("Initializing host agent state machine") self.agent = agent - self.fsm = Fysom({ - "events": [ - ("lookup", "*", "found"), - ("announce", "found", "announced"), - ("pending", "announced", "wait4init"), - ("ready", "wait4init", "good2go")], - "callbacks": { - # Can add the following to debug - # "onchangestate": self.print_state_change, - "onlookup": self.lookup_agent_host, - "onannounce": self.announce_sensor, - "onpending": self.on_ready, - "ongood2go": self.on_good2go}}) + self.fsm = Fysom( + { + "events": [ + ("lookup", "*", "found"), + ("announce", "found", "announced"), + ("pending", "announced", "wait4init"), + ("ready", "wait4init", "good2go"), + ], + "callbacks": { + # Can add the following to debug + # "onchangestate": self.print_state_change, + "onlookup": self.lookup_agent_host, + "onannounce": self.announce_sensor, + "onpending": self.on_ready, + "ongood2go": self.on_good2go, + }, + } + ) self.timer = threading.Timer(1, self.fsm.lookup) self.timer.daemon = True @@ -71,8 +76,14 @@ def __init__(self, agent): @staticmethod def print_state_change(e): - logger.debug('========= (%i#%s) FSM event: %s, src: %s, dst: %s ==========', - os.getpid(), threading.current_thread().name, e.event, e.src, e.dst) + logger.debug( + "========= (%i#%s) FSM event: %s, src: %s, dst: %s ==========", + os.getpid(), + threading.current_thread().name, + e.event, + e.src, + e.dst, + ) def reset(self): """ @@ -105,39 +116,45 @@ def lookup_agent_host(self, e): return True if self.warnedPeriodic is False: - logger.info("Instana Host Agent couldn't be found. Will retry periodically...") + logger.info( + "Instana Host Agent couldn't be found. Will retry periodically..." + ) self.warnedPeriodic = True - self.schedule_retry(self.lookup_agent_host, e, self.THREAD_NAME + ": agent_lookup") + self.schedule_retry( + self.lookup_agent_host, e, self.THREAD_NAME + ": agent_lookup" + ) return False def announce_sensor(self, e): - logger.debug("Attempting to make an announcement to the agent on %s:%d", - self.agent.options.agent_host, self.agent.options.agent_port) + logger.debug( + "Attempting to make an announcement to the agent on %s:%d", + self.agent.options.agent_host, + self.agent.options.agent_port, + ) pid = os.getpid() try: if os.path.isfile("/proc/self/cmdline"): with open("/proc/self/cmdline") as cmd: cmdinfo = cmd.read() - cmdline = cmdinfo.split('\x00') + cmdline = cmdinfo.split("\x00") else: # Python doesn't provide a reliable method to determine what # the OS process command line may be. Here we are forced to # rely on ps rather than adding a dependency on something like # psutil which requires dev packages, gcc etc... - proc = subprocess.Popen(["ps", "-p", str(pid), "-o", "command"], - stdout=subprocess.PIPE) + proc = subprocess.Popen( + ["ps", "-p", str(pid), "-o", "command"], stdout=subprocess.PIPE + ) (out, _) = proc.communicate() - parts = out.split(b'\n') + parts = out.split(b"\n") cmdline = [parts[1].decode("utf-8")] except Exception: cmdline = sys.argv logger.debug("announce_sensor", exc_info=True) - d = Discovery(pid=self.__get_real_pid(), - name=cmdline[0], - args=cmdline[1:]) + d = Discovery(pid=self.__get_real_pid(), name=cmdline[0], args=cmdline[1:]) # If we're on a system with a procfs if os.path.exists("/proc/"): @@ -146,24 +163,31 @@ def announce_sensor(self, e): # PermissionError: [Errno 13] Permission denied: '/proc/6/fd/8' # Use a try/except as a safety sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.connect((self.agent.options.agent_host, self.agent.options.agent_port)) + sock.connect( + (self.agent.options.agent_host, self.agent.options.agent_port) + ) path = "/proc/%d/fd/%d" % (pid, sock.fileno()) d.fd = sock.fileno() d.inode = os.readlink(path) - except: + except Exception: logger.debug("Error generating file descriptor: ", exc_info=True) payload = self.agent.announce(d) if not payload: logger.debug("Cannot announce sensor. Scheduling retry.") - self.schedule_retry(self.announce_sensor, e, self.THREAD_NAME + ": announce") + self.schedule_retry( + self.announce_sensor, e, self.THREAD_NAME + ": announce" + ) return False - + self.agent.set_from(payload) self.fsm.pending() - logger.debug("Announced pid: %s (true pid: %s). Waiting for Agent Ready...", - str(pid), str(self.agent.announce_data.pid)) + logger.debug( + "Announced pid: %s (true pid: %s). Waiting for Agent Ready...", + str(pid), + str(self.agent.announce_data.pid), + ) return True def schedule_retry(self, fun, e, name): @@ -178,13 +202,20 @@ def on_ready(self, _): ns_pid = str(os.getpid()) true_pid = str(self.agent.announce_data.pid) - logger.info("Instana host agent available. We're in business. Announced PID: %s (true pid: %s)", ns_pid, true_pid) + logger.info( + "Instana host agent available. We're in business. Announced PID: %s (true pid: %s)", + ns_pid, + true_pid, + ) def on_good2go(self, _): ns_pid = str(os.getpid()) true_pid = str(self.agent.announce_data.pid) - self.agent.log_message_to_host_agent("Instana Python Package %s: PID %s (true pid: %s) is now online and reporting" % (VERSION, ns_pid, true_pid)) + self.agent.log_message_to_host_agent( + "Instana Python Package %s: PID %s (true pid: %s) is now online and reporting" + % (VERSION, ns_pid, true_pid) + ) def __get_real_pid(self): """ @@ -201,7 +232,7 @@ def __get_real_pid(self): try: file = open(sched_file) line = file.readline() - g = re.search(r'\((\d+),', line) + g = re.search(r"\((\d+),", line) if len(g.groups()) == 1: pid = int(g.groups()[0]) except Exception: diff --git a/src/instana/helpers.py b/src/instana/helpers.py index d5ddecaf4..bf8c5c3d9 100644 --- a/src/instana/helpers.py +++ b/src/instana/helpers.py @@ -1,12 +1,6 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2018 -import os -from string import Template - -from instana import eum_api_key as global_eum_api_key -from .singletons import tracer -from instana.log import logger # Usage: # @@ -26,7 +20,7 @@ def eum_snippet(trace_id=None, eum_api_key=None, meta=None): @return string """ - return '' + return "" def eum_test_snippet(trace_id=None, eum_api_key=None, meta=None): @@ -40,4 +34,4 @@ def eum_test_snippet(trace_id=None, eum_api_key=None, meta=None): @return string """ - return '' + return "" diff --git a/src/instana/hooks/hook_uwsgi.py b/src/instana/hooks/hook_uwsgi.py index 16c2b26df..b0141920d 100644 --- a/src/instana/hooks/hook_uwsgi.py +++ b/src/instana/hooks/hook_uwsgi.py @@ -12,14 +12,20 @@ try: import uwsgi + logger.debug("uWSGI options: %s", uwsgi.opt) - opt_master = uwsgi.opt.get('master', False) - opt_lazy_apps = uwsgi.opt.get('lazy-apps', False) + opt_master = uwsgi.opt.get("master", False) + opt_lazy_apps = uwsgi.opt.get("lazy-apps", False) - if uwsgi.opt.get('enable-threads', False) is False and uwsgi.opt.get('gevent', False) is False: - logger.warning("Required: Neither uWSGI threads or gevent is enabled. " + - "Please enable by using the uWSGI --enable-threads or --gevent option.") + if ( + uwsgi.opt.get("enable-threads", False) is False + and uwsgi.opt.get("gevent", False) is False + ): + logger.warning( + "Required: Neither uWSGI threads or gevent is enabled. " + + "Please enable by using the uWSGI --enable-threads or --gevent option." + ) if opt_master and opt_lazy_apps is False: # --master is supplied in uWSGI options (otherwise uwsgidecorators package won't be available) @@ -28,13 +34,19 @@ @uwsgidecorators.postfork def uwsgi_handle_fork(): - """ This is our uWSGI hook to detect and act when worker processes are forked off. """ + """This is our uWSGI hook to detect and act when worker processes are forked off.""" logger.debug("Handling uWSGI fork...") agent.handle_fork() logger.debug("Applied uWSGI hooks") else: - logger.debug("uWSGI --master=%s --lazy-apps=%s: postfork hooks not applied", opt_master, opt_lazy_apps) + logger.debug( + "uWSGI --master=%s --lazy-apps=%s: postfork hooks not applied", + opt_master, + opt_lazy_apps, + ) except ImportError: - logger.debug('uwsgi hooks: decorators not available: likely not running under uWSGI') + logger.debug( + "uwsgi hooks: decorators not available: likely not running under uWSGI" + ) pass diff --git a/src/instana/instrumentation/asgi.py b/src/instana/instrumentation/asgi.py index ed0866aeb..5e21e0154 100644 --- a/src/instana/instrumentation/asgi.py +++ b/src/instana/instrumentation/asgi.py @@ -121,7 +121,9 @@ async def send_wrapper(response: Dict[str, Any]) -> Awaitable[None]: if status_code: if 500 <= int(status_code): current_span.mark_as_errored() - current_span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code) + current_span.set_attribute( + SpanAttributes.HTTP_STATUS_CODE, status_code + ) headers = response.get("headers") if headers: diff --git a/src/instana/instrumentation/aws/lambda_inst.py b/src/instana/instrumentation/aws/lambda_inst.py index 9cb37dffc..193f6fd8d 100644 --- a/src/instana/instrumentation/aws/lambda_inst.py +++ b/src/instana/instrumentation/aws/lambda_inst.py @@ -64,7 +64,7 @@ def lambda_handler_with_instana( if agent.collector.started: agent.collector.shutdown() - + return result diff --git a/src/instana/instrumentation/boto3_inst.py b/src/instana/instrumentation/boto3_inst.py index fb7a3233e..d95604925 100644 --- a/src/instana/instrumentation/boto3_inst.py +++ b/src/instana/instrumentation/boto3_inst.py @@ -20,8 +20,6 @@ from botocore.client import BaseClient try: - import boto3 - from boto3.s3 import inject def extract_custom_headers( span: "InstanaSpan", headers: Optional[Dict[str, Any]] = None diff --git a/src/instana/instrumentation/celery.py b/src/instana/instrumentation/celery.py index c69131aaf..661b79a1d 100644 --- a/src/instana/instrumentation/celery.py +++ b/src/instana/instrumentation/celery.py @@ -12,7 +12,6 @@ from opentelemetry import trace, context try: - import celery from celery import registry, signals from urllib import parse diff --git a/src/instana/instrumentation/fastapi_inst.py b/src/instana/instrumentation/fastapi_inst.py index 5edee85c8..b2e9b0186 100644 --- a/src/instana/instrumentation/fastapi_inst.py +++ b/src/instana/instrumentation/fastapi_inst.py @@ -29,7 +29,7 @@ from starlette.requests import Request from starlette.responses import Response - if not ( # pragma: no cover + if not ( # pragma: no cover hasattr(fastapi, "__version__") and ( fastapi.__version__[0] > "0" or int(fastapi.__version__.split(".")[1]) >= 51 @@ -84,7 +84,7 @@ def init_with_instana( logger.debug("Instrumenting FastAPI") # Reload GUnicorn when we are instrumenting an already running application - if "INSTANA_MAGIC" in os.environ and running_in_gunicorn(): # pragma: no cover + if "INSTANA_MAGIC" in os.environ and running_in_gunicorn(): # pragma: no cover os.kill(os.getpid(), signal.SIGHUP) except ImportError: diff --git a/src/instana/instrumentation/flask/__init__.py b/src/instana/instrumentation/flask/__init__.py index 7d85abcd4..4100ede75 100644 --- a/src/instana/instrumentation/flask/__init__.py +++ b/src/instana/instrumentation/flask/__init__.py @@ -10,7 +10,7 @@ # # Blinker support is preferred but we do the best we can when it's not available. # - if hasattr(flask.signals, 'signals_available'): + if hasattr(flask.signals, "signals_available"): from flask.signals import signals_available else: # Beginning from 2.3.0 as stated in the notes @@ -19,11 +19,11 @@ # The signals_available attribute is deprecated. #5056" signals_available = True - from instana.instrumentation.flask import common + from instana.instrumentation.flask import common # noqa: F401 if signals_available is True: import instana.instrumentation.flask.with_blinker else: - import instana.instrumentation.flask.vanilla + import instana.instrumentation.flask.vanilla # noqa: F401 except ImportError: pass diff --git a/src/instana/instrumentation/flask/common.py b/src/instana/instrumentation/flask/common.py index cd966986f..361d2fea0 100644 --- a/src/instana/instrumentation/flask/common.py +++ b/src/instana/instrumentation/flask/common.py @@ -27,7 +27,7 @@ from werkzeug.datastructures import Headers -@wrapt.patch_function_wrapper('flask', 'templating._render') +@wrapt.patch_function_wrapper("flask", "templating._render") def render_with_instana( wrapped: Callable[..., str], instance: object, @@ -58,7 +58,7 @@ def render_with_instana( raise -@wrapt.patch_function_wrapper('flask', 'Flask.handle_user_exception') +@wrapt.patch_function_wrapper("flask", "Flask.handle_user_exception") def handle_user_exception_with_instana( wrapped: Callable[..., Union["HTTPException", "ResponseReturnValue"]], instance: flask.app.Flask, @@ -78,7 +78,7 @@ def handle_user_exception_with_instana( if isinstance(response, tuple): status_code = response[1] else: - if hasattr(response, 'code'): + if hasattr(response, "code"): status_code = response.code else: status_code = response.status_code @@ -88,17 +88,19 @@ def handle_user_exception_with_instana( span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, int(status_code)) - if hasattr(response, 'headers'): + if hasattr(response, "headers"): tracer.inject(span.context, Format.HTTP_HEADERS, response.headers) value = "intid;desc=%s" % span.context.trace_id - if hasattr(response.headers, 'add'): - response.headers.add('Server-Timing', value) - elif type(response.headers) is dict or hasattr(response.headers, "__dict__"): - response.headers['Server-Timing'] = value + if hasattr(response.headers, "add"): + response.headers.add("Server-Timing", value) + elif type(response.headers) is dict or hasattr( + response.headers, "__dict__" + ): + response.headers["Server-Timing"] = value if span and span.is_recording(): span.end() flask.g.span = None - except: + except Exception: logger.debug("handle_user_exception_with_instana:", exc_info=True) return response @@ -112,7 +114,11 @@ def extract_custom_headers( try: for custom_header in agent.options.extra_http_headers: # Headers are available in this format: HTTP_X_CAPTURE_THIS - flask_header = ('HTTP_' + custom_header.upper()).replace('-', '_') if format else custom_header + flask_header = ( + ("HTTP_" + custom_header.upper()).replace("-", "_") + if format + else custom_header + ) if flask_header in headers: span.set_attribute( "http.header.%s" % custom_header, headers[flask_header] diff --git a/src/instana/instrumentation/flask/vanilla.py b/src/instana/instrumentation/flask/vanilla.py index 9e21b0331..c70227d6e 100644 --- a/src/instana/instrumentation/flask/vanilla.py +++ b/src/instana/instrumentation/flask/vanilla.py @@ -16,7 +16,7 @@ from instana.instrumentation.flask.common import extract_custom_headers from instana.propagators.format import Format -path_tpl_re = re.compile('<.*>') +path_tpl_re = re.compile("<.*>") def before_request_with_instana() -> None: @@ -52,7 +52,7 @@ def before_request_with_instana() -> None: path_tpl = flask.request.url_rule.rule.replace("<", "{") path_tpl = path_tpl.replace(">", "}") span.set_attribute("http.path_tpl", path_tpl) - except: + except Exception: logger.debug("Flask before_request", exc_info=True) return None @@ -69,7 +69,6 @@ def after_request_with_instana( span = flask.g.span if span: - if 500 <= response.status_code: span.mark_as_errored() @@ -82,7 +81,7 @@ def after_request_with_instana( response.headers.add( "Server-Timing", "intid;desc=%s" % span.context.trace_id ) - except: + except Exception: logger.debug("Flask after_request", exc_info=True) finally: if span and span.is_recording(): @@ -111,15 +110,17 @@ def teardown_request_with_instana(*argv: Union[Exception, Type[Exception]]) -> N flask.g.token = None -@wrapt.patch_function_wrapper('flask', 'Flask.full_dispatch_request') +@wrapt.patch_function_wrapper("flask", "Flask.full_dispatch_request") def full_dispatch_request_with_instana( wrapped: Callable[..., flask.wrappers.Response], instance: flask.app.Flask, argv: Tuple, kwargs: Dict, ) -> flask.wrappers.Response: - if not hasattr(instance, '_stan_wuz_here'): - logger.debug("Flask(vanilla): Applying flask before/after instrumentation funcs") + if not hasattr(instance, "_stan_wuz_here"): + logger.debug( + "Flask(vanilla): Applying flask before/after instrumentation funcs" + ) setattr(instance, "_stan_wuz_here", True) instance.before_request(before_request_with_instana) instance.after_request(after_request_with_instana) diff --git a/src/instana/instrumentation/flask/with_blinker.py b/src/instana/instrumentation/flask/with_blinker.py index 211f2173c..1b3bc92a2 100644 --- a/src/instana/instrumentation/flask/with_blinker.py +++ b/src/instana/instrumentation/flask/with_blinker.py @@ -55,7 +55,7 @@ def request_started_with_instana(sender: flask.app.Flask, **extra: Any) -> None: path_tpl = flask.request.url_rule.rule.replace("<", "{") path_tpl = path_tpl.replace(">", "}") span.set_attribute("http.path_tpl", path_tpl) - except: + except Exception: logger.debug("Flask request_started_with_instana", exc_info=True) @@ -81,7 +81,7 @@ def request_finished_with_instana( response.headers.add( "Server-Timing", "intid;desc=%s" % span.context.trace_id ) - except: + except Exception: logger.debug("Flask request_finished_with_instana", exc_info=True) finally: if span and span.is_recording(): diff --git a/src/instana/instrumentation/gevent_inst.py b/src/instana/instrumentation/gevent_inst.py index 93ed38001..c083fb842 100644 --- a/src/instana/instrumentation/gevent_inst.py +++ b/src/instana/instrumentation/gevent_inst.py @@ -11,7 +11,7 @@ def instrument_gevent(): - """ Adds context propagation to gevent greenlet spawning """ + """Adds context propagation to gevent greenlet spawning""" try: logger.debug("Instrumenting gevent") @@ -20,28 +20,34 @@ def instrument_gevent(): from opentracing.scope_managers.gevent import _GeventScope def spawn_callback(new_greenlet): - """ Handles context propagation for newly spawning greenlets """ + """Handles context propagation for newly spawning greenlets""" parent_scope = tracer.scope_manager.active if parent_scope is not None: # New greenlet, new clean slate. Clone and make active in this new greenlet # the currently active scope (but don't finish() the span on close - it's a # clone/not the original and we don't want to close it prematurely) # TODO: Change to our own ScopeManagers - parent_scope_clone = _GeventScope(parent_scope.manager, parent_scope.span, finish_on_close=False) - tracer._scope_manager._set_greenlet_scope(parent_scope_clone, new_greenlet) + parent_scope_clone = _GeventScope( + parent_scope.manager, parent_scope.span, finish_on_close=False + ) + tracer._scope_manager._set_greenlet_scope( + parent_scope_clone, new_greenlet + ) logger.debug(" -> Updating tracer to use gevent based context management") tracer._scope_manager = GeventScopeManager() gevent.Greenlet.add_spawn_callback(spawn_callback) - except: + except Exception: logger.debug("instrument_gevent: ", exc_info=True) -if not 'gevent' in sys.modules: +if "gevent" not in sys.modules: logger.debug("Instrumenting gevent: gevent not detected or loaded. Nothing done.") -elif not hasattr(sys.modules['gevent'], 'version_info'): +elif not hasattr(sys.modules["gevent"], "version_info"): logger.debug("gevent module has no 'version_info'. Skipping instrumentation.") -elif sys.modules['gevent'].version_info < (1, 4): - logger.debug("gevent < 1.4 detected. The Instana package supports gevent versions 1.4 and greater.") +elif sys.modules["gevent"].version_info < (1, 4): + logger.debug( + "gevent < 1.4 detected. The Instana package supports gevent versions 1.4 and greater." + ) else: instrument_gevent() diff --git a/src/instana/instrumentation/google/cloud/collectors.py b/src/instana/instrumentation/google/cloud/collectors.py index 7ec446909..0d6a6d33f 100644 --- a/src/instana/instrumentation/google/cloud/collectors.py +++ b/src/instana/instrumentation/google/cloud/collectors.py @@ -15,300 +15,339 @@ # # The API documentation can be found at https://cloud.google.com/storage/docs/json_api _storage_api = { - 'GET': { + "GET": { ##################### # Bucket operations # ##################### - '/b': lambda params, data: { - 'gcs.op': 'buckets.list', - 'gcs.projectId': params.get('project', None) + "/b": lambda params, data: { + "gcs.op": "buckets.list", + "gcs.projectId": params.get("project", None), }, - re.compile('^/b/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'buckets.get', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile("^/b/(?P[^/]+)$"): lambda params, data, match: { + "gcs.op": "buckets.get", + "gcs.bucket": unquote(match.group("bucket")), }, - re.compile('^/b/(?P[^/]+)/iam$'): lambda params, data, match: { - 'gcs.op': 'buckets.getIamPolicy', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile("^/b/(?P[^/]+)/iam$"): lambda params, data, match: { + "gcs.op": "buckets.getIamPolicy", + "gcs.bucket": unquote(match.group("bucket")), }, - re.compile('^/b/(?P[^/]+)/iam/testPermissions$'): lambda params, data, match: { - 'gcs.op': 'buckets.testIamPermissions', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile("^/b/(?P[^/]+)/iam/testPermissions$"): lambda params, + data, + match: { + "gcs.op": "buckets.testIamPermissions", + "gcs.bucket": unquote(match.group("bucket")), }, - ########################## # Object/blob operations # ########################## - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': params.get('alt', 'json') == 'media' and 'objects.get' or 'objects.attrs', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), - }, - re.compile('^/b/(?P[^/]+)/o$'): lambda params, data, match: { - 'gcs.op': 'objects.list', - 'gcs.bucket': unquote(match.group('bucket')) + re.compile("^/b/(?P[^/]+)/o/(?P[^/]+)$"): lambda params, + data, + match: { + "gcs.op": params.get("alt", "json") == "media" + and "objects.get" + or "objects.attrs", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), + }, + re.compile("^/b/(?P[^/]+)/o$"): lambda params, data, match: { + "gcs.op": "objects.list", + "gcs.bucket": unquote(match.group("bucket")), }, - ################################## # Default object ACLs operations # ################################## - re.compile('^/b/(?P[^/]+)/defaultObjectAcl/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'defaultAcls.get', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.entity': unquote(match.group('entity')) - }, - re.compile('^/b/(?P[^/]+)/defaultObjectAcl$'): lambda params, data, match: { - 'gcs.op': 'defaultAcls.list', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile( + "^/b/(?P[^/]+)/defaultObjectAcl/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "defaultAcls.get", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.entity": unquote(match.group("entity")), + }, + re.compile("^/b/(?P[^/]+)/defaultObjectAcl$"): lambda params, + data, + match: { + "gcs.op": "defaultAcls.list", + "gcs.bucket": unquote(match.group("bucket")), }, - ######################### # Object ACL operations # ######################### - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/acl/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objectAcls.get', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), - 'gcs.entity': unquote(match.group('entity')) - }, - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/acl$'): lambda params, data, match: { - 'gcs.op': 'objectAcls.list', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')) + re.compile( + "^/b/(?P[^/]+)/o/(?P[^/]+)/acl/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "objectAcls.get", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), + "gcs.entity": unquote(match.group("entity")), + }, + re.compile("^/b/(?P[^/]+)/o/(?P[^/]+)/acl$"): lambda params, + data, + match: { + "gcs.op": "objectAcls.list", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), }, - ######################## # HMAC keys operations # ######################## - re.compile('^/projects/(?P[^/]+)/hmacKeys$'): lambda params, data, match: { - 'gcs.op': 'hmacKeys.list', - 'gcs.projectId': unquote(match.group('project')) - }, - re.compile('^/projects/(?P[^/]+)/hmacKeys/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'hmacKeys.get', - 'gcs.projectId': unquote(match.group('project')), - 'gcs.accessId': unquote(match.group('accessId')) + re.compile("^/projects/(?P[^/]+)/hmacKeys$"): lambda params, + data, + match: { + "gcs.op": "hmacKeys.list", + "gcs.projectId": unquote(match.group("project")), + }, + re.compile( + "^/projects/(?P[^/]+)/hmacKeys/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "hmacKeys.get", + "gcs.projectId": unquote(match.group("project")), + "gcs.accessId": unquote(match.group("accessId")), }, - ############################## # Service account operations # ############################## - re.compile('^/projects/(?P[^/]+)/serviceAccount$'): lambda params, data, match: { - 'gcs.op': 'serviceAccount.get', - 'gcs.projectId': unquote(match.group('project')) - } + re.compile("^/projects/(?P[^/]+)/serviceAccount$"): lambda params, + data, + match: { + "gcs.op": "serviceAccount.get", + "gcs.projectId": unquote(match.group("project")), + }, }, - 'POST': { + "POST": { ##################### # Bucket operations # ##################### - '/b': lambda params, data: { - 'gcs.op': 'buckets.insert', - 'gcs.projectId': params.get('project', None), - 'gcs.bucket': data.get('name', None), + "/b": lambda params, data: { + "gcs.op": "buckets.insert", + "gcs.projectId": params.get("project", None), + "gcs.bucket": data.get("name", None), }, - re.compile('^/b/(?P[^/]+)/lockRetentionPolicy$'): lambda params, data, match: { - 'gcs.op': 'buckets.lockRetentionPolicy', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile("^/b/(?P[^/]+)/lockRetentionPolicy$"): lambda params, + data, + match: { + "gcs.op": "buckets.lockRetentionPolicy", + "gcs.bucket": unquote(match.group("bucket")), }, - ########################## # Object/blob operations # ########################## - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/compose$'): lambda params, data, match: { - 'gcs.op': 'objects.compose', - 'gcs.destinationBucket': unquote(match.group('bucket')), - 'gcs.destinationObject': unquote(match.group('object')), - 'gcs.sourceObjects': ','.join( - ['%s/%s' % (unquote(match.group('bucket')), o['name']) for o in data.get('sourceObjects', []) if 'name' in o] - ) - }, - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/copyTo/b/(?P[^/]+)/o/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objects.copy', - 'gcs.destinationBucket': unquote(match.group('destBucket')), - 'gcs.destinationObject': unquote(match.group('destObject')), - 'gcs.sourceBucket': unquote(match.group('srcBucket')), - 'gcs.sourceObject': unquote(match.group('srcObject')), - }, - re.compile('^/b/(?P[^/]+)/o$'): lambda params, data, match: { - 'gcs.op': 'objects.insert', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': params.get('name', data.get('name', None)), - }, - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/rewriteTo/b/(?P[^/]+)/o/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objects.rewrite', - 'gcs.destinationBucket': unquote(match.group('destBucket')), - 'gcs.destinationObject': unquote(match.group('destObject')), - 'gcs.sourceBucket': unquote(match.group('srcBucket')), - 'gcs.sourceObject': unquote(match.group('srcObject')), + re.compile("^/b/(?P[^/]+)/o/(?P[^/]+)/compose$"): lambda params, + data, + match: { + "gcs.op": "objects.compose", + "gcs.destinationBucket": unquote(match.group("bucket")), + "gcs.destinationObject": unquote(match.group("object")), + "gcs.sourceObjects": ",".join( + [ + "%s/%s" % (unquote(match.group("bucket")), o["name"]) + for o in data.get("sourceObjects", []) + if "name" in o + ] + ), + }, + re.compile( + "^/b/(?P[^/]+)/o/(?P[^/]+)/copyTo/b/(?P[^/]+)/o/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "objects.copy", + "gcs.destinationBucket": unquote(match.group("destBucket")), + "gcs.destinationObject": unquote(match.group("destObject")), + "gcs.sourceBucket": unquote(match.group("srcBucket")), + "gcs.sourceObject": unquote(match.group("srcObject")), + }, + re.compile("^/b/(?P[^/]+)/o$"): lambda params, data, match: { + "gcs.op": "objects.insert", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": params.get("name", data.get("name", None)), + }, + re.compile( + "^/b/(?P[^/]+)/o/(?P[^/]+)/rewriteTo/b/(?P[^/]+)/o/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "objects.rewrite", + "gcs.destinationBucket": unquote(match.group("destBucket")), + "gcs.destinationObject": unquote(match.group("destObject")), + "gcs.sourceBucket": unquote(match.group("srcBucket")), + "gcs.sourceObject": unquote(match.group("srcObject")), }, - ###################### # Channel operations # ###################### - '/channels/stop': lambda params, data: { - 'gcs.op': 'channels.stop', - 'gcs.entity': data.get('id', None) + "/channels/stop": lambda params, data: { + "gcs.op": "channels.stop", + "gcs.entity": data.get("id", None), }, - ################################## # Default object ACLs operations # ################################## - re.compile('^/b/(?P[^/]+)/defaultObjectAcl$'): lambda params, data, match: { - 'gcs.op': 'defaultAcls.insert', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.entity': data.get('entity', None) + re.compile("^/b/(?P[^/]+)/defaultObjectAcl$"): lambda params, + data, + match: { + "gcs.op": "defaultAcls.insert", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.entity": data.get("entity", None), }, - ######################### # Object ACL operations # ######################### - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/acl$'): lambda params, data, match: { - 'gcs.op': 'objectAcls.insert', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), - 'gcs.entity': data.get('entity', None) + re.compile("^/b/(?P[^/]+)/o/(?P[^/]+)/acl$"): lambda params, + data, + match: { + "gcs.op": "objectAcls.insert", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), + "gcs.entity": data.get("entity", None), }, - ######################## # HMAC keys operations # ######################## - re.compile('^/projects/(?P[^/]+)/hmacKeys$'): lambda params, data, match: { - 'gcs.op': 'hmacKeys.create', - 'gcs.projectId': unquote(match.group('project')) - } + re.compile("^/projects/(?P[^/]+)/hmacKeys$"): lambda params, + data, + match: { + "gcs.op": "hmacKeys.create", + "gcs.projectId": unquote(match.group("project")), + }, }, - 'PATCH': { + "PATCH": { ##################### # Bucket operations # ##################### - re.compile('^/b/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'buckets.patch', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile("^/b/(?P[^/]+)$"): lambda params, data, match: { + "gcs.op": "buckets.patch", + "gcs.bucket": unquote(match.group("bucket")), }, - ########################## # Object/blob operations # ########################## - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objects.patch', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), + re.compile("^/b/(?P[^/]+)/o/(?P[^/]+)$"): lambda params, + data, + match: { + "gcs.op": "objects.patch", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), }, - ################################## # Default object ACLs operations # ################################## - re.compile('^/b/(?P[^/]+)/defaultObjectAcl/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'defaultAcls.patch', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.entity': unquote(match.group('entity')) + re.compile( + "^/b/(?P[^/]+)/defaultObjectAcl/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "defaultAcls.patch", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.entity": unquote(match.group("entity")), }, - ######################### # Object ACL operations # ######################### - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/acl/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objectAcls.patch', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), - 'gcs.entity': unquote(match.group('entity')) - } + re.compile( + "^/b/(?P[^/]+)/o/(?P[^/]+)/acl/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "objectAcls.patch", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), + "gcs.entity": unquote(match.group("entity")), + }, }, - 'PUT': { + "PUT": { ##################### # Bucket operations # ##################### - re.compile('^/b/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'buckets.update', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile("^/b/(?P[^/]+)$"): lambda params, data, match: { + "gcs.op": "buckets.update", + "gcs.bucket": unquote(match.group("bucket")), }, - re.compile('^/b/(?P[^/]+)/iam$'): lambda params, data, match: { - 'gcs.op': 'buckets.setIamPolicy', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile("^/b/(?P[^/]+)/iam$"): lambda params, data, match: { + "gcs.op": "buckets.setIamPolicy", + "gcs.bucket": unquote(match.group("bucket")), }, - ########################## # Object/blob operations # ########################## - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objects.update', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), + re.compile("^/b/(?P[^/]+)/o/(?P[^/]+)$"): lambda params, + data, + match: { + "gcs.op": "objects.update", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), }, - ################################## # Default object ACLs operations # ################################## - re.compile('^/b/(?P[^/]+)/defaultObjectAcl/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'defaultAcls.update', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.entity': unquote(match.group('entity')) + re.compile( + "^/b/(?P[^/]+)/defaultObjectAcl/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "defaultAcls.update", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.entity": unquote(match.group("entity")), }, - ######################### # Object ACL operations # ######################### - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/acl/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objectAcls.update', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), - 'gcs.entity': unquote(match.group('entity')) + re.compile( + "^/b/(?P[^/]+)/o/(?P[^/]+)/acl/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "objectAcls.update", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), + "gcs.entity": unquote(match.group("entity")), }, - ######################## # HMAC keys operations # ######################## - re.compile('^/projects/(?P[^/]+)/hmacKeys/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'hmacKeys.update', - 'gcs.projectId': unquote(match.group('project')), - 'gcs.accessId': unquote(match.group('accessId')) - } + re.compile( + "^/projects/(?P[^/]+)/hmacKeys/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "hmacKeys.update", + "gcs.projectId": unquote(match.group("project")), + "gcs.accessId": unquote(match.group("accessId")), + }, }, - 'DELETE': { + "DELETE": { ##################### # Bucket operations # ##################### - re.compile('^/b/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'buckets.delete', - 'gcs.bucket': unquote(match.group('bucket')), + re.compile("^/b/(?P[^/]+)$"): lambda params, data, match: { + "gcs.op": "buckets.delete", + "gcs.bucket": unquote(match.group("bucket")), }, - ########################## # Object/blob operations # ########################## - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objects.delete', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), + re.compile("^/b/(?P[^/]+)/o/(?P[^/]+)$"): lambda params, + data, + match: { + "gcs.op": "objects.delete", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), }, - ################################## # Default object ACLs operations # ################################## - re.compile('^/b/(?P[^/]+)/defaultObjectAcl/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'defaultAcls.delete', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.entity': unquote(match.group('entity')) + re.compile( + "^/b/(?P[^/]+)/defaultObjectAcl/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "defaultAcls.delete", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.entity": unquote(match.group("entity")), }, - ######################### # Object ACL operations # ######################### - re.compile('^/b/(?P[^/]+)/o/(?P[^/]+)/acl/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'objectAcls.delete', - 'gcs.bucket': unquote(match.group('bucket')), - 'gcs.object': unquote(match.group('object')), - 'gcs.entity': unquote(match.group('entity')) + re.compile( + "^/b/(?P[^/]+)/o/(?P[^/]+)/acl/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "objectAcls.delete", + "gcs.bucket": unquote(match.group("bucket")), + "gcs.object": unquote(match.group("object")), + "gcs.entity": unquote(match.group("entity")), }, - ######################## # HMAC keys operations # ######################## - re.compile('^/projects/(?P[^/]+)/hmacKeys/(?P[^/]+)$'): lambda params, data, match: { - 'gcs.op': 'hmacKeys.delete', - 'gcs.projectId': unquote(match.group('project')), - 'gcs.accessId': unquote(match.group('accessId')) - } - } + re.compile( + "^/projects/(?P[^/]+)/hmacKeys/(?P[^/]+)$" + ): lambda params, data, match: { + "gcs.op": "hmacKeys.delete", + "gcs.projectId": unquote(match.group("project")), + "gcs.accessId": unquote(match.group("accessId")), + }, + }, } diff --git a/src/instana/instrumentation/tornado/client.py b/src/instana/instrumentation/tornado/client.py index e937db68d..835ecc9bc 100644 --- a/src/instana/instrumentation/tornado/client.py +++ b/src/instana/instrumentation/tornado/client.py @@ -15,13 +15,15 @@ from instana.propagators.format import Format from instana.span.span import get_current_span - @wrapt.patch_function_wrapper('tornado.httpclient', 'AsyncHTTPClient.fetch') + @wrapt.patch_function_wrapper("tornado.httpclient", "AsyncHTTPClient.fetch") def fetch_with_instana(wrapped, instance, argv, kwargs): try: parent_span = get_current_span() # If we're not tracing, just return - if (not parent_span.is_recording()) or (parent_span.name == "tornado-client"): + if (not parent_span.is_recording()) or ( + parent_span.name == "tornado-client" + ): return wrapped(*argv, **kwargs) request = argv[0] @@ -32,7 +34,7 @@ def fetch_with_instana(wrapped, instance, argv, kwargs): request = tornado.httpclient.HTTPRequest(url=request, **kwargs) new_kwargs = {} - for param in ('callback', 'raise_error'): + for param in ("callback", "raise_error"): # if not in instead and pop if param in kwargs: new_kwargs[param] = kwargs.pop(param) @@ -44,10 +46,11 @@ def fetch_with_instana(wrapped, instance, argv, kwargs): tracer.inject(span.context, Format.HTTP_HEADERS, request.headers) # Query param scrubbing - parts = request.url.split('?') + parts = request.url.split("?") if len(parts) > 1: - cleaned_qp = strip_secrets_from_query(parts[1], agent.options.secrets_matcher, - agent.options.secrets_list) + cleaned_qp = strip_secrets_from_query( + parts[1], agent.options.secrets_matcher, agent.options.secrets_list + ) span.set_attribute("http.params", cleaned_qp) span.set_attribute(SpanAttributes.HTTP_URL, parts[0]) @@ -63,7 +66,6 @@ def fetch_with_instana(wrapped, instance, argv, kwargs): except Exception: logger.debug("Tornado fetch_with_instana: ", exc_info=True) - def finish_tracing(future, span): try: response = future.result() @@ -76,7 +78,6 @@ def finish_tracing(future, span): if span.is_recording(): span.end() - logger.debug("Instrumenting tornado client") except ImportError: pass diff --git a/src/instana/instrumentation/tornado/server.py b/src/instana/instrumentation/tornado/server.py index 7c9285002..2c9c47648 100644 --- a/src/instana/instrumentation/tornado/server.py +++ b/src/instana/instrumentation/tornado/server.py @@ -20,28 +20,36 @@ def extract_custom_headers(span, headers): try: for custom_header in agent.options.extra_http_headers: if custom_header in headers: - span.set_attribute("http.header.%s" % custom_header, headers[custom_header]) + span.set_attribute( + "http.header.%s" % custom_header, headers[custom_header] + ) except Exception: logger.debug("extract_custom_headers: ", exc_info=True) - - @wrapt.patch_function_wrapper('tornado.web', 'RequestHandler._execute') + @wrapt.patch_function_wrapper("tornado.web", "RequestHandler._execute") def execute_with_instana(wrapped, instance, argv, kwargs): try: span_context = None - if hasattr(instance.request.headers, '__dict__') and '_dict' in instance.request.headers.__dict__: - span_context = tracer.extract(Format.HTTP_HEADERS, - instance.request.headers.__dict__['_dict']) + if ( + hasattr(instance.request.headers, "__dict__") + and "_dict" in instance.request.headers.__dict__ + ): + span_context = tracer.extract( + Format.HTTP_HEADERS, instance.request.headers.__dict__["_dict"] + ) span = tracer.start_span("tornado-server", span_context=span_context) # Query param scrubbing if instance.request.query is not None and len(instance.request.query) > 0: - cleaned_qp = strip_secrets_from_query(instance.request.query, agent.options.secrets_matcher, - agent.options.secrets_list) + cleaned_qp = strip_secrets_from_query( + instance.request.query, + agent.options.secrets_matcher, + agent.options.secrets_list, + ) span.set_attribute("http.params", cleaned_qp) - + url = f"{instance.request.protocol}://{instance.request.host}{instance.request.path}" span.set_attribute(SpanAttributes.HTTP_URL, url) span.set_attribute(SpanAttributes.HTTP_METHOD, instance.request.method) @@ -56,27 +64,29 @@ def execute_with_instana(wrapped, instance, argv, kwargs): # Set the context response headers now because tornado doesn't give us a better option to do so # later for this request. tracer.inject(span.context, Format.HTTP_HEADERS, instance._headers) - instance.set_header(name='Server-Timing', value=f"intid;desc={span.context.trace_id}") + instance.set_header( + name="Server-Timing", value=f"intid;desc={span.context.trace_id}" + ) return wrapped(*argv, **kwargs) except Exception: logger.debug("tornado execute", exc_info=True) - - @wrapt.patch_function_wrapper('tornado.web', 'RequestHandler.set_default_headers') + @wrapt.patch_function_wrapper("tornado.web", "RequestHandler.set_default_headers") def set_default_headers_with_instana(wrapped, instance, argv, kwargs): - if not hasattr(instance.request, '_instana'): + if not hasattr(instance.request, "_instana"): return wrapped(*argv, **kwargs) span = instance.request._instana tracer.inject(span.context, Format.HTTP_HEADERS, instance._headers) - instance.set_header(name='Server-Timing', value=f"intid;desc={span.context.trace_id}") + instance.set_header( + name="Server-Timing", value=f"intid;desc={span.context.trace_id}" + ) - - @wrapt.patch_function_wrapper('tornado.web', 'RequestHandler.on_finish') + @wrapt.patch_function_wrapper("tornado.web", "RequestHandler.on_finish") def on_finish_with_instana(wrapped, instance, argv, kwargs): try: - if not hasattr(instance.request, '_instana'): + if not hasattr(instance.request, "_instana"): return wrapped(*argv, **kwargs) span = instance.request._instana @@ -97,11 +107,10 @@ def on_finish_with_instana(wrapped, instance, argv, kwargs): except Exception: logger.debug("tornado on_finish", exc_info=True) - - @wrapt.patch_function_wrapper('tornado.web', 'RequestHandler.log_exception') + @wrapt.patch_function_wrapper("tornado.web", "RequestHandler.log_exception") def log_exception_with_instana(wrapped, instance, argv, kwargs): try: - if not hasattr(instance.request, '_instana'): + if not hasattr(instance.request, "_instana"): return wrapped(*argv, **kwargs) if not isinstance(argv[1], tornado.web.HTTPError): @@ -112,7 +121,6 @@ def log_exception_with_instana(wrapped, instance, argv, kwargs): except Exception: logger.debug("tornado log_exception", exc_info=True) - logger.debug("Instrumenting tornado server") except ImportError: pass diff --git a/src/instana/instrumentation/wsgi.py b/src/instana/instrumentation/wsgi.py index 40d7e3401..f2ad5f9d1 100644 --- a/src/instana/instrumentation/wsgi.py +++ b/src/instana/instrumentation/wsgi.py @@ -4,6 +4,7 @@ """ Instana WSGI Middleware """ + from typing import Dict, Any, Callable, List, Tuple, Optional from opentelemetry.semconv.trace import SpanAttributes @@ -23,14 +24,23 @@ def __init__(self, app: object) -> None: def __call__(self, environ: Dict[str, Any], start_response: Callable) -> object: env = environ - def new_start_response(status: str, headers: List[Tuple[object, ...]], exc_info: Optional[Exception] = None) -> object: + def new_start_response( + status: str, + headers: List[Tuple[object, ...]], + exc_info: Optional[Exception] = None, + ) -> object: """Modified start response with additional headers.""" tracer.inject(self.span.context, Format.HTTP_HEADERS, headers) headers.append( ("Server-Timing", "intid;desc=%s" % self.span.context.trace_id) ) - headers_str = [(header[0], str(header[1])) if not isinstance(header[1], str) else header for header in headers] + headers_str = [ + (header[0], str(header[1])) + if not isinstance(header[1], str) + else header + for header in headers + ] res = start_response(status, headers_str, exc_info) sc = status.split(" ")[0] diff --git a/src/instana/log.py b/src/instana/log.py index 173437cd7..267ff33e3 100644 --- a/src/instana/log.py +++ b/src/instana/log.py @@ -18,7 +18,9 @@ def get_standard_logger(): standard_logger = logging.getLogger("instana") ch = logging.StreamHandler() - f = logging.Formatter('%(asctime)s: %(process)d %(levelname)s %(name)s: %(message)s') + f = logging.Formatter( + "%(asctime)s: %(process)d %(levelname)s %(name)s: %(message)s" + ) ch.setFormatter(f) standard_logger.addHandler(ch) standard_logger.setLevel(logging.DEBUG) @@ -35,6 +37,7 @@ def get_aws_lambda_logger(): aws_lambda_logger.setLevel(logging.INFO) return aws_lambda_logger + def glogging_available(): """ Determines if the gunicorn.glogging package is available @@ -45,14 +48,15 @@ def glogging_available(): # Is the glogging package available? try: - from gunicorn import glogging + from gunicorn import glogging # noqa: F401 except ImportError: pass else: package_check = True - + return package_check + def running_in_gunicorn(): """ Determines if we are running inside of a gunicorn process. @@ -63,19 +67,19 @@ def running_in_gunicorn(): try: # Is this a gunicorn process? - if hasattr(sys, 'argv'): + if hasattr(sys, "argv"): for arg in sys.argv: - if arg.find('gunicorn') >= 0: + if arg.find("gunicorn") >= 0: process_check = True elif os.path.isfile("/proc/self/cmdline"): with open("/proc/self/cmdline") as cmd: contents = cmd.read() - parts = contents.split('\0') + parts = contents.split("\0") parts.pop() cmdline = " ".join(parts) - if cmdline.find('gunicorn') >= 0: + if cmdline.find("gunicorn") >= 0: process_check = True return process_check diff --git a/src/instana/options.py b/src/instana/options.py index 0f90e62b3..e06c234e2 100644 --- a/src/instana/options.py +++ b/src/instana/options.py @@ -13,6 +13,7 @@ - AWSFargateOptions - Options class for AWS Fargate. Holds settings specific to AWS Fargate. - GCROptions - Options class for Google cloud Run. Holds settings specific to GCR. """ + import os import logging @@ -21,7 +22,7 @@ class BaseOptions(object): - """ Base class for all option classes. Holds items common to all """ + """Base class for all option classes. Holds items common to all""" def __init__(self, **kwds): self.debug = False @@ -35,31 +36,36 @@ def __init__(self, **kwds): self.debug = True if "INSTANA_EXTRA_HTTP_HEADERS" in os.environ: - self.extra_http_headers = str(os.environ["INSTANA_EXTRA_HTTP_HEADERS"]).lower().split(';') + self.extra_http_headers = ( + str(os.environ["INSTANA_EXTRA_HTTP_HEADERS"]).lower().split(";") + ) - if os.environ.get("INSTANA_ALLOW_EXIT_AS_ROOT", None) == '1': + if os.environ.get("INSTANA_ALLOW_EXIT_AS_ROOT", None) == "1": self.allow_exit_as_root = True # Defaults - self.secrets_matcher = 'contains-ignore-case' - self.secrets_list = ['key', 'pass', 'secret'] + self.secrets_matcher = "contains-ignore-case" + self.secrets_list = ["key", "pass", "secret"] # Env var format: :[,] self.secrets = os.environ.get("INSTANA_SECRETS", None) if self.secrets is not None: - parts = self.secrets.split(':') + parts = self.secrets.split(":") if len(parts) == 2: self.secrets_matcher = parts[0] - self.secrets_list = parts[1].split(',') + self.secrets_list = parts[1].split(",") else: - logger.warning("Couldn't parse INSTANA_SECRETS env var: %s", self.secrets) + logger.warning( + "Couldn't parse INSTANA_SECRETS env var: %s", self.secrets + ) self.__dict__.update(kwds) class StandardOptions(BaseOptions): - """ The options class used when running directly on a host/node with an Instana agent """ + """The options class used when running directly on a host/node with an Instana agent""" + AGENT_DEFAULT_HOST = "localhost" AGENT_DEFAULT_PORT = 42699 @@ -74,7 +80,7 @@ def __init__(self, **kwds): class ServerlessOptions(BaseOptions): - """ Base class for serverless environments. Holds settings common to all serverless environments. """ + """Base class for serverless environments. Holds settings common to all serverless environments.""" def __init__(self, **kwds): super(ServerlessOptions, self).__init__() @@ -86,7 +92,7 @@ def __init__(self, **kwds): if self.endpoint_url is not None and self.endpoint_url[-1] == "/": self.endpoint_url = self.endpoint_url[:-1] - if 'INSTANA_DISABLE_CA_CHECK' in os.environ: + if "INSTANA_DISABLE_CA_CHECK" in os.environ: self.ssl_verify = False else: self.ssl_verify = True @@ -95,7 +101,7 @@ def __init__(self, **kwds): if proxy is None: self.endpoint_proxy = {} else: - self.endpoint_proxy = {'https': proxy} + self.endpoint_proxy = {"https": proxy} timeout_in_ms = os.environ.get("INSTANA_TIMEOUT", None) if timeout_in_ms is None: @@ -105,9 +111,14 @@ def __init__(self, **kwds): try: self.timeout = int(timeout_in_ms) / 1000 except ValueError: - logger.warning("Likely invalid INSTANA_TIMEOUT=%s value. Using default.", timeout_in_ms) - logger.warning("INSTANA_TIMEOUT should specify timeout in milliseconds. See " - "https://www.instana.com/docs/reference/environment_variables/#serverless-monitoring") + logger.warning( + "Likely invalid INSTANA_TIMEOUT=%s value. Using default.", + timeout_in_ms, + ) + logger.warning( + "INSTANA_TIMEOUT should specify timeout in milliseconds. See " + "https://www.instana.com/docs/reference/environment_variables/#serverless-monitoring" + ) self.timeout = 0.8 value = os.environ.get("INSTANA_LOG_LEVEL", None) @@ -129,14 +140,14 @@ def __init__(self, **kwds): class AWSLambdaOptions(ServerlessOptions): - """ Options class for AWS Lambda. Holds settings specific to AWS Lambda. """ + """Options class for AWS Lambda. Holds settings specific to AWS Lambda.""" def __init__(self, **kwds): super(AWSLambdaOptions, self).__init__() class AWSFargateOptions(ServerlessOptions): - """ Options class for AWS Fargate. Holds settings specific to AWS Fargate. """ + """Options class for AWS Fargate. Holds settings specific to AWS Fargate.""" def __init__(self, **kwds): super(AWSFargateOptions, self).__init__() @@ -146,9 +157,9 @@ def __init__(self, **kwds): if tag_list is not None: try: self.tags = dict() - tags = tag_list.split(',') + tags = tag_list.split(",") for tag_and_value in tags: - parts = tag_and_value.split('=') + parts = tag_and_value.split("=") length = len(parts) if length == 1: self.tags[parts[0]] = None @@ -159,13 +170,16 @@ def __init__(self, **kwds): self.zone = os.environ.get("INSTANA_ZONE", None) + class EKSFargateOptions(AWSFargateOptions): - """ Options class for EKS Pods on AWS Fargate. Holds settings specific to EKS Pods on AWS Fargate. """ + """Options class for EKS Pods on AWS Fargate. Holds settings specific to EKS Pods on AWS Fargate.""" + def __init__(self, **kwds): super(EKSFargateOptions, self).__init__() + class GCROptions(ServerlessOptions): - """ Options class for Google Cloud Run. Holds settings specific to Google Cloud Run. """ + """Options class for Google Cloud Run. Holds settings specific to Google Cloud Run.""" def __init__(self, **kwds): super(GCROptions, self).__init__() diff --git a/src/instana/propagators/base_propagator.py b/src/instana/propagators/base_propagator.py index f92074f09..82aca0088 100644 --- a/src/instana/propagators/base_propagator.py +++ b/src/instana/propagators/base_propagator.py @@ -31,40 +31,40 @@ class BasePropagator(object): - HEADER_KEY_T = 'X-INSTANA-T' - HEADER_KEY_S = 'X-INSTANA-S' - HEADER_KEY_L = 'X-INSTANA-L' - HEADER_KEY_SYNTHETIC = 'X-INSTANA-SYNTHETIC' + HEADER_KEY_T = "X-INSTANA-T" + HEADER_KEY_S = "X-INSTANA-S" + HEADER_KEY_L = "X-INSTANA-L" + HEADER_KEY_SYNTHETIC = "X-INSTANA-SYNTHETIC" HEADER_KEY_TRACEPARENT = "traceparent" HEADER_KEY_TRACESTATE = "tracestate" - LC_HEADER_KEY_T = 'x-instana-t' - LC_HEADER_KEY_S = 'x-instana-s' - LC_HEADER_KEY_L = 'x-instana-l' - LC_HEADER_KEY_SYNTHETIC = 'x-instana-synthetic' + LC_HEADER_KEY_T = "x-instana-t" + LC_HEADER_KEY_S = "x-instana-s" + LC_HEADER_KEY_L = "x-instana-l" + LC_HEADER_KEY_SYNTHETIC = "x-instana-synthetic" - ALT_LC_HEADER_KEY_T = 'http_x_instana_t' - ALT_LC_HEADER_KEY_S = 'http_x_instana_s' - ALT_LC_HEADER_KEY_L = 'http_x_instana_l' - ALT_LC_HEADER_KEY_SYNTHETIC = 'http_x_instana_synthetic' + ALT_LC_HEADER_KEY_T = "http_x_instana_t" + ALT_LC_HEADER_KEY_S = "http_x_instana_s" + ALT_LC_HEADER_KEY_L = "http_x_instana_l" + ALT_LC_HEADER_KEY_SYNTHETIC = "http_x_instana_synthetic" ALT_HEADER_KEY_TRACEPARENT = "http_traceparent" ALT_HEADER_KEY_TRACESTATE = "http_tracestate" # ByteArray variations - B_HEADER_KEY_T = b'x-instana-t' - B_HEADER_KEY_S = b'x-instana-s' - B_HEADER_KEY_L = b'x-instana-l' - B_HEADER_KEY_SYNTHETIC = b'x-instana-synthetic' - B_HEADER_SERVER_TIMING = b'server-timing' - B_HEADER_KEY_TRACEPARENT = b'traceparent' - B_HEADER_KEY_TRACESTATE = b'tracestate' - - B_ALT_LC_HEADER_KEY_T = b'http_x_instana_t' - B_ALT_LC_HEADER_KEY_S = b'http_x_instana_s' - B_ALT_LC_HEADER_KEY_L = b'http_x_instana_l' - B_ALT_LC_HEADER_KEY_SYNTHETIC = b'http_x_instana_synthetic' - B_ALT_HEADER_KEY_TRACEPARENT = b'http_traceparent' - B_ALT_HEADER_KEY_TRACESTATE = b'http_tracestate' + B_HEADER_KEY_T = b"x-instana-t" + B_HEADER_KEY_S = b"x-instana-s" + B_HEADER_KEY_L = b"x-instana-l" + B_HEADER_KEY_SYNTHETIC = b"x-instana-synthetic" + B_HEADER_SERVER_TIMING = b"server-timing" + B_HEADER_KEY_TRACEPARENT = b"traceparent" + B_HEADER_KEY_TRACESTATE = b"tracestate" + + B_ALT_LC_HEADER_KEY_T = b"http_x_instana_t" + B_ALT_LC_HEADER_KEY_S = b"http_x_instana_s" + B_ALT_LC_HEADER_KEY_L = b"http_x_instana_l" + B_ALT_LC_HEADER_KEY_SYNTHETIC = b"http_x_instana_synthetic" + B_ALT_HEADER_KEY_TRACEPARENT = b"http_traceparent" + B_ALT_HEADER_KEY_TRACESTATE = b"http_tracestate" def __init__(self): self._tp = Traceparent() @@ -87,7 +87,9 @@ def extract_headers_dict(carrier: CarrierT) -> Optional[Dict]: else: dc = dict(carrier) except Exception: - logger.debug(f"base_propagator extract_headers_dict: Couldn't convert - {carrier}") + logger.debug( + f"base_propagator extract_headers_dict: Couldn't convert - {carrier}" + ) return dc @@ -106,7 +108,7 @@ def _get_ctx_level(level: str) -> int: return ctx_level @staticmethod - def _get_correlation_properties(level:str): + def _get_correlation_properties(level: str): """ Get the correlation values if they are present. @@ -115,12 +117,16 @@ def _get_correlation_properties(level:str): """ correlation_type, correlation_id = [None] * 2 try: - correlation_type = level.split(",")[1].split("correlationType=")[1].split(";")[0] + correlation_type = ( + level.split(",")[1].split("correlationType=")[1].split(";")[0] + ) if "correlationId" in level: - correlation_id = level.split(",")[1].split("correlationId=")[1].split(";")[0] + correlation_id = ( + level.split(",")[1].split("correlationId=")[1].split(";")[0] + ) except Exception: logger.debug("extract instana correlation type/id error:", exc_info=True) - + return correlation_type, correlation_id def _get_participating_trace_context(self, span_context: SpanContext): @@ -136,7 +142,9 @@ def _get_participating_trace_context(self, span_context: SpanContext): tp_trace_id = span_context.trace_id traceparent = span_context.traceparent tracestate = span_context.tracestate - traceparent = self._tp.update_traceparent(traceparent, tp_trace_id, span_context.span_id, span_context.level) + traceparent = self._tp.update_traceparent( + traceparent, tp_trace_id, span_context.span_id, span_context.level + ) # In suppression mode do not update the tracestate and # do not add the 'in=' key-value pair to the incoming tracestate @@ -144,19 +152,21 @@ def _get_participating_trace_context(self, span_context: SpanContext): if span_context.suppression: return traceparent, tracestate - tracestate = self._ts.update_tracestate(tracestate, span_context.trace_id, span_context.span_id) + tracestate = self._ts.update_tracestate( + tracestate, span_context.trace_id, span_context.span_id + ) return traceparent, tracestate def __determine_span_context( - self, - trace_id: int, - span_id: int, - level: str, - synthetic: bool, - traceparent, - tracestate, - disable_w3c_trace_context: bool, - ) -> SpanContext: + self, + trace_id: int, + span_id: int, + level: str, + synthetic: bool, + traceparent, + tracestate, + disable_w3c_trace_context: bool, + ) -> SpanContext: """ This method determines the span context depending on a set of conditions being met Detailed description of the conditions can be found in the instana internal technical-documentation, @@ -172,7 +182,9 @@ def __determine_span_context( :return: SpanContext """ correlation = False - disable_traceparent = os.environ.get("INSTANA_DISABLE_W3C_TRACE_CORRELATION", "") + disable_traceparent = os.environ.get( + "INSTANA_DISABLE_W3C_TRACE_CORRELATION", "" + ) instana_ancestor = None if level and "correlationType" in level: @@ -182,7 +194,7 @@ def __determine_span_context( ( ctx_level, ctx_synthetic, - ctx_trace_parent, + ctx_trace_parent, ctx_instana_ancestor, ctx_long_trace_id, ctx_correlation_type, @@ -209,8 +221,15 @@ def __determine_span_context( if len(hex_trace_id) > 16: ctx_long_trace_id = hex_trace_id - elif not disable_w3c_trace_context and traceparent and not trace_id and not span_id: - _, tp_trace_id, tp_parent_id, _ = self._tp.get_traceparent_fields(traceparent) + elif ( + not disable_w3c_trace_context + and traceparent + and not trace_id + and not span_id + ): + _, tp_trace_id, tp_parent_id, _ = self._tp.get_traceparent_fields( + traceparent + ) if tracestate and "in=" in tracestate: instana_ancestor = self._ts.get_instana_ancestor(tracestate) @@ -232,7 +251,9 @@ def __determine_span_context( ctx_synthetic = synthetic if correlation: - ctx_correlation_type, ctx_correlation_id = self._get_correlation_properties(level) + ctx_correlation_type, ctx_correlation_id = self._get_correlation_properties( + level + ) if traceparent: ctx_traceparent = traceparent @@ -253,8 +274,9 @@ def __determine_span_context( tracestate=ctx_tracestate, ) - - def extract_instana_headers(self, dc: Dict[str, Any]) -> Tuple[Optional[int], Optional[int], Optional[str], Optional[bool]]: + def extract_instana_headers( + self, dc: Dict[str, Any] + ) -> Tuple[Optional[int], Optional[int], Optional[str], Optional[bool]]: """ Search carrier for the *HEADER* keys and return the tracing key-values. @@ -265,27 +287,43 @@ def extract_instana_headers(self, dc: Dict[str, Any]) -> Tuple[Optional[int], Op # Headers can exist in the standard X-Instana-T/S format or the alternate HTTP_X_INSTANA_T/S style try: - trace_id = dc.get(self.LC_HEADER_KEY_T) or dc.get(self.ALT_LC_HEADER_KEY_T) or dc.get( - self.B_HEADER_KEY_T) or dc.get(self.B_ALT_LC_HEADER_KEY_T) + trace_id = ( + dc.get(self.LC_HEADER_KEY_T) + or dc.get(self.ALT_LC_HEADER_KEY_T) + or dc.get(self.B_HEADER_KEY_T) + or dc.get(self.B_ALT_LC_HEADER_KEY_T) + ) if trace_id: # trace_id = header_to_long_id(trace_id) trace_id = int(trace_id) - span_id = dc.get(self.LC_HEADER_KEY_S) or dc.get(self.ALT_LC_HEADER_KEY_S) or dc.get( - self.B_HEADER_KEY_S) or dc.get(self.B_ALT_LC_HEADER_KEY_S) + span_id = ( + dc.get(self.LC_HEADER_KEY_S) + or dc.get(self.ALT_LC_HEADER_KEY_S) + or dc.get(self.B_HEADER_KEY_S) + or dc.get(self.B_ALT_LC_HEADER_KEY_S) + ) if span_id: # span_id = header_to_id(span_id) span_id = int(span_id) - level = dc.get(self.LC_HEADER_KEY_L) or dc.get(self.ALT_LC_HEADER_KEY_L) or dc.get( - self.B_HEADER_KEY_L) or dc.get(self.B_ALT_LC_HEADER_KEY_L) + level = ( + dc.get(self.LC_HEADER_KEY_L) + or dc.get(self.ALT_LC_HEADER_KEY_L) + or dc.get(self.B_HEADER_KEY_L) + or dc.get(self.B_ALT_LC_HEADER_KEY_L) + ) if level and isinstance(level, bytes): level = level.decode("utf-8") - synthetic = dc.get(self.LC_HEADER_KEY_SYNTHETIC) or dc.get(self.ALT_LC_HEADER_KEY_SYNTHETIC) or dc.get( - self.B_HEADER_KEY_SYNTHETIC) or dc.get(self.B_ALT_LC_HEADER_KEY_SYNTHETIC) + synthetic = ( + dc.get(self.LC_HEADER_KEY_SYNTHETIC) + or dc.get(self.ALT_LC_HEADER_KEY_SYNTHETIC) + or dc.get(self.B_HEADER_KEY_SYNTHETIC) + or dc.get(self.B_ALT_LC_HEADER_KEY_SYNTHETIC) + ) if synthetic: - synthetic = synthetic in ['1', b'1'] + synthetic = synthetic in ["1", b"1"] except Exception: logger.debug("extract error:", exc_info=True) @@ -302,13 +340,21 @@ def __extract_w3c_trace_context_headers(self, dc): traceparent, tracestate = [None] * 2 try: - traceparent = dc.get(self.HEADER_KEY_TRACEPARENT) or dc.get(self.ALT_HEADER_KEY_TRACEPARENT) or dc.get( - self.B_HEADER_KEY_TRACEPARENT) or dc.get(self.B_ALT_HEADER_KEY_TRACEPARENT) + traceparent = ( + dc.get(self.HEADER_KEY_TRACEPARENT) + or dc.get(self.ALT_HEADER_KEY_TRACEPARENT) + or dc.get(self.B_HEADER_KEY_TRACEPARENT) + or dc.get(self.B_ALT_HEADER_KEY_TRACEPARENT) + ) if traceparent and isinstance(traceparent, bytes): traceparent = traceparent.decode("utf-8") - tracestate = dc.get(self.HEADER_KEY_TRACESTATE) or dc.get(self.ALT_HEADER_KEY_TRACESTATE) or dc.get( - self.B_HEADER_KEY_TRACESTATE) or dc.get(self.B_ALT_HEADER_KEY_TRACESTATE) + tracestate = ( + dc.get(self.HEADER_KEY_TRACESTATE) + or dc.get(self.ALT_HEADER_KEY_TRACESTATE) + or dc.get(self.B_HEADER_KEY_TRACESTATE) + or dc.get(self.B_ALT_HEADER_KEY_TRACESTATE) + ) if tracestate and isinstance(tracestate, bytes): tracestate = tracestate.decode("utf-8") @@ -317,10 +363,12 @@ def __extract_w3c_trace_context_headers(self, dc): return traceparent, tracestate - def extract(self, carrier: CarrierT, disable_w3c_trace_context: bool = False) -> Optional[SpanContext]: + def extract( + self, carrier: CarrierT, disable_w3c_trace_context: bool = False + ) -> Optional[SpanContext]: """ - This method overrides one of the Base classes as with the introduction - of W3C trace context for the HTTP requests more extracting steps and + This method overrides one of the Base classes as with the introduction + of W3C trace context for the HTTP requests more extracting steps and logic was required. :param disable_w3c_trace_context: @@ -334,9 +382,13 @@ def extract(self, carrier: CarrierT, disable_w3c_trace_context: bool = False) -> return None headers = {k.lower(): v for k, v in headers.items()} - trace_id, span_id, level, synthetic = self.extract_instana_headers(dc=headers) + trace_id, span_id, level, synthetic = self.extract_instana_headers( + dc=headers + ) if not disable_w3c_trace_context: - traceparent, tracestate = self.__extract_w3c_trace_context_headers(dc=headers) + traceparent, tracestate = self.__extract_w3c_trace_context_headers( + dc=headers + ) if traceparent: traceparent = self._tp.validate(traceparent) diff --git a/src/instana/propagators/binary_propagator.py b/src/instana/propagators/binary_propagator.py index 89c9002f6..e31f9a879 100644 --- a/src/instana/propagators/binary_propagator.py +++ b/src/instana/propagators/binary_propagator.py @@ -13,12 +13,12 @@ class BinaryPropagator(BasePropagator): """ # ByteArray variations from base class - HEADER_KEY_T = b'x-instana-t' - HEADER_KEY_S = b'x-instana-s' - HEADER_KEY_L = b'x-instana-l' - HEADER_SERVER_TIMING = b'server-timing' - HEADER_KEY_TRACEPARENT = b'traceparent' - HEADER_KEY_TRACESTATE = b'tracestate' + HEADER_KEY_T = b"x-instana-t" + HEADER_KEY_S = b"x-instana-s" + HEADER_KEY_L = b"x-instana-l" + HEADER_SERVER_TIMING = b"server-timing" + HEADER_KEY_TRACEPARENT = b"traceparent" + HEADER_KEY_TRACESTATE = b"tracestate" def __init__(self): super(BinaryPropagator, self).__init__() @@ -33,7 +33,9 @@ def inject(self, span_context, carrier, disable_w3c_trace_context=True): if disable_w3c_trace_context: traceparent, tracestate = [None] * 2 else: - traceparent, tracestate = self._get_participating_trace_context(span_context) + traceparent, tracestate = self._get_participating_trace_context( + span_context + ) try: traceparent = str.encode(traceparent) tracestate = str.encode(tracestate) @@ -58,13 +60,17 @@ def inject(self, span_context, carrier, disable_w3c_trace_context=True): carrier.append((self.HEADER_SERVER_TIMING, server_timing)) elif isinstance(carrier, tuple): if traceparent and tracestate: - carrier = carrier.__add__(((self.HEADER_KEY_TRACEPARENT, traceparent),)) - carrier = carrier.__add__(((self.HEADER_KEY_TRACESTATE, tracestate),)) + carrier = carrier.__add__( + ((self.HEADER_KEY_TRACEPARENT, traceparent),) + ) + carrier = carrier.__add__( + ((self.HEADER_KEY_TRACESTATE, tracestate),) + ) carrier = carrier.__add__(((self.HEADER_KEY_T, trace_id),)) carrier = carrier.__add__(((self.HEADER_KEY_S, span_id),)) carrier = carrier.__add__(((self.HEADER_KEY_L, level),)) carrier = carrier.__add__(((self.HEADER_SERVER_TIMING, server_timing),)) - elif hasattr(carrier, '__setitem__'): + elif hasattr(carrier, "__setitem__"): if traceparent and tracestate: carrier.__setitem__(self.HEADER_KEY_TRACEPARENT, traceparent) carrier.__setitem__(self.HEADER_KEY_TRACESTATE, tracestate) @@ -78,5 +84,3 @@ def inject(self, span_context, carrier, disable_w3c_trace_context=True): return carrier except Exception: logger.debug("inject error:", exc_info=True) - - diff --git a/src/instana/propagators/http_propagator.py b/src/instana/propagators/http_propagator.py index 483f2765b..c6dffc07b 100644 --- a/src/instana/propagators/http_propagator.py +++ b/src/instana/propagators/http_propagator.py @@ -24,7 +24,9 @@ def inject(self, span_context, carrier, disable_w3c_trace_context=False): if dictionary_carrier: # Suppression `level` made in the child context or in the parent context # has priority over any non-suppressed `level` setting - child_level = int(self.extract_instana_headers(dictionary_carrier)[2] or "1") + child_level = int( + self.extract_instana_headers(dictionary_carrier)[2] or "1" + ) span_context.level = min(child_level, span_context.level) serializable_level = str(span_context.level) @@ -32,12 +34,14 @@ def inject(self, span_context, carrier, disable_w3c_trace_context=False): if disable_w3c_trace_context: traceparent, tracestate = [None] * 2 else: - traceparent, tracestate = self._get_participating_trace_context(span_context) + traceparent, tracestate = self._get_participating_trace_context( + span_context + ) def inject_key_value(carrier, key, value): if isinstance(carrier, list): carrier.append((key, value)) - elif isinstance(carrier, dict) or '__setitem__' in dir(carrier): + elif isinstance(carrier, dict) or "__setitem__" in dir(carrier): carrier[key] = value else: raise Exception("Unsupported carrier type", type(carrier)) diff --git a/src/instana/propagators/text_propagator.py b/src/instana/propagators/text_propagator.py index f7ecf04c2..0cbd5b05b 100644 --- a/src/instana/propagators/text_propagator.py +++ b/src/instana/propagators/text_propagator.py @@ -31,7 +31,7 @@ def inject(self, span_context, carrier, disable_w3c_trace_context=True): carrier = carrier.__add__(((self.LC_HEADER_KEY_T, trace_id),)) carrier = carrier.__add__(((self.LC_HEADER_KEY_S, span_id),)) carrier = carrier.__add__(((self.LC_HEADER_KEY_L, "1"),)) - elif hasattr(carrier, '__setitem__'): + elif hasattr(carrier, "__setitem__"): carrier.__setitem__(self.LC_HEADER_KEY_T, trace_id) carrier.__setitem__(self.LC_HEADER_KEY_S, span_id) carrier.__setitem__(self.LC_HEADER_KEY_L, "1") diff --git a/src/instana/singletons.py b/src/instana/singletons.py index 0166bf29b..728893f88 100644 --- a/src/instana/singletons.py +++ b/src/instana/singletons.py @@ -41,23 +41,27 @@ agent = AWSLambdaAgent() elif env_is_aws_fargate: from instana.agent.aws_fargate import AWSFargateAgent + agent = AWSFargateAgent() elif env_is_google_cloud_run: from instana.agent.google_cloud_run import GCRAgent + agent = GCRAgent( service=k_service, configuration=k_configuration, revision=k_revision ) elif env_is_aws_eks_fargate: from instana.agent.aws_eks_fargate import EKSFargateAgent + agent = EKSFargateAgent() else: from instana.agent.host import HostAgent + agent = HostAgent() profiler = Profiler(agent) - + if agent: - span_recorder = StanRecorder(agent) + span_recorder = StanRecorder(agent) def get_agent() -> Type["BaseAgent"]: diff --git a/src/instana/span/registered_span.py b/src/instana/span/registered_span.py index 6164ca861..b67248358 100644 --- a/src/instana/span/registered_span.py +++ b/src/instana/span/registered_span.py @@ -239,8 +239,12 @@ def _populate_exit_span_data(self, span) -> None: self.data["mysql"]["host"] = span.attributes.pop("host", None) self.data["mysql"]["port"] = span.attributes.pop("port", None) self.data["mysql"]["db"] = span.attributes.pop(SpanAttributes.DB_NAME, None) - self.data["mysql"]["user"] = span.attributes.pop(SpanAttributes.DB_USER, None) - self.data["mysql"]["stmt"] = span.attributes.pop(SpanAttributes.DB_STATEMENT, None) + self.data["mysql"]["user"] = span.attributes.pop( + SpanAttributes.DB_USER, None + ) + self.data["mysql"]["stmt"] = span.attributes.pop( + SpanAttributes.DB_STATEMENT, None + ) self.data["mysql"]["error"] = span.attributes.pop("mysql.error", None) elif span.name == "postgres": diff --git a/src/instana/span/sdk_span.py b/src/instana/span/sdk_span.py index 9a4be35b6..c573d1d86 100644 --- a/src/instana/span/sdk_span.py +++ b/src/instana/span/sdk_span.py @@ -22,9 +22,7 @@ def __init__(self, span, source, service_name, **kwargs) -> None: self.data["sdk"]["name"] = span.name self.data["sdk"]["type"] = span_kind[0] - self.data["sdk"]["custom"]["tags"] = self._validate_attributes( - span.attributes - ) + self.data["sdk"]["custom"]["tags"] = self._validate_attributes(span.attributes) if span.events is not None and len(span.events) > 0: events = DictionaryOfStan() @@ -58,5 +56,3 @@ def get_span_kind(self, span) -> Tuple[str, int]: elif span.attributes["span.kind"] in EXIT_KIND: kind = ("exit", 2) return kind - - diff --git a/src/instana/span_context.py b/src/instana/span_context.py index ac14e61d0..f265cad43 100644 --- a/src/instana/span_context.py +++ b/src/instana/span_context.py @@ -44,7 +44,9 @@ def __new__( tracestate=None, # temporary storage of the tracestate header **kwargs, ) -> "SpanContext": - instance = super().__new__(cls, trace_id, span_id, is_remote, trace_flags, trace_state) + instance = super().__new__( + cls, trace_id, span_id, is_remote, trace_flags, trace_state + ) return tuple.__new__( cls, ( diff --git a/src/instana/tracer.py b/src/instana/tracer.py index a5bdf895f..93b4d44ed 100644 --- a/src/instana/tracer.py +++ b/src/instana/tracer.py @@ -118,12 +118,18 @@ def start_span( record_exception: bool = True, set_status_on_exception: bool = True, ) -> InstanaSpan: - parent_context = span_context if span_context else get_current_span().get_span_context() + parent_context = ( + span_context if span_context else get_current_span().get_span_context() + ) if parent_context and not isinstance(parent_context, SpanContext): raise TypeError("parent_context must be an Instana SpanContext or None.") - if parent_context and not parent_context.is_valid and not parent_context.suppression: + if ( + parent_context + and not parent_context.is_valid + and not parent_context.suppression + ): # We probably have an INVALID_SPAN_CONTEXT. parent_context = None diff --git a/src/instana/util/aws.py b/src/instana/util/aws.py index e1e773e62..f06476d04 100644 --- a/src/instana/util/aws.py +++ b/src/instana/util/aws.py @@ -3,6 +3,7 @@ from ..log import logger + def normalize_aws_lambda_arn(context): """ Parse the AWS Lambda context object for a fully qualified AWS Lambda function ARN. @@ -15,12 +16,12 @@ def normalize_aws_lambda_arn(context): """ try: arn = context.invoked_function_arn - parts = arn.split(':') + parts = arn.split(":") count = len(parts) if count == 7: # need to append version - arn = arn + ':' + context.function_version + arn = arn + ":" + context.function_version elif count != 8: logger.debug("Unexpected ARN parse issue: %s", arn) diff --git a/src/instana/util/gunicorn.py b/src/instana/util/gunicorn.py index 48883f32d..ee9883d27 100644 --- a/src/instana/util/gunicorn.py +++ b/src/instana/util/gunicorn.py @@ -5,6 +5,7 @@ import sys from ..log import logger + def running_in_gunicorn(): """ Determines if we are running inside of a gunicorn process. @@ -15,22 +16,22 @@ def running_in_gunicorn(): try: # Is this a gunicorn process? - if hasattr(sys, 'argv'): + if hasattr(sys, "argv"): for arg in sys.argv: - if arg.find('gunicorn') >= 0: + if arg.find("gunicorn") >= 0: process_check = True elif os.path.isfile("/proc/self/cmdline"): with open("/proc/self/cmdline") as cmd: contents = cmd.read() - parts = contents.split('\0') + parts = contents.split("\0") parts.pop() cmdline = " ".join(parts) - if cmdline.find('gunicorn') >= 0: + if cmdline.find("gunicorn") >= 0: process_check = True return process_check except Exception: logger.debug("Instana.log.running_in_gunicorn: ", exc_info=True) - return False \ No newline at end of file + return False diff --git a/src/instana/util/ids.py b/src/instana/util/ids.py index 817920278..e0977fa93 100644 --- a/src/instana/util/ids.py +++ b/src/instana/util/ids.py @@ -39,7 +39,7 @@ def header_to_long_id(header: Union[bytes, str]) -> int: :return: a valid ID to be used internal to the tracer """ if isinstance(header, bytes): - header = header.decode('utf-8') + header = header.decode("utf-8") if not isinstance(header, str): return INVALID_SPAN_ID @@ -64,7 +64,7 @@ def header_to_id(header: Union[bytes, str]) -> int: :return: a valid ID to be used internal to the tracer """ if isinstance(header, bytes): - header = header.decode('utf-8') + header = header.decode("utf-8") if not isinstance(header, str): return INVALID_SPAN_ID diff --git a/src/instana/util/runtime.py b/src/instana/util/runtime.py index 86c75440d..9b7965bc2 100644 --- a/src/instana/util/runtime.py +++ b/src/instana/util/runtime.py @@ -7,6 +7,7 @@ from ..log import logger + def get_py_source(filename): """ Retrieves and returns the source code for any Python @@ -20,7 +21,7 @@ def get_py_source(filename): response = {"error": "Only Python source files are allowed. (*.py)"} else: pysource = "" - with open(filename, 'r') as pyfile: + with open(filename, "r") as pyfile: pysource = pyfile.read() response = {"data": pysource} @@ -36,7 +37,7 @@ def get_py_source(filename): def determine_service_name(): - """ This function makes a best effort to name this application process. """ + """This function makes a best effort to name this application process.""" # One environment variable to rule them all if "INSTANA_SERVICE_NAME" in os.environ: @@ -48,13 +49,13 @@ def determine_service_name(): basename = None try: - if not hasattr(sys, 'argv'): + if not hasattr(sys, "argv"): proc_cmdline = get_proc_cmdline(as_string=False) return os.path.basename(proc_cmdline[0]) # Get first argument that is not an CLI option for candidate in sys.argv: - if len(candidate) > 0 and candidate[0] != '-': + if len(candidate) > 0 and candidate[0] != "-": basename = candidate break @@ -66,7 +67,7 @@ def determine_service_name(): basename = os.path.basename(basename) if basename == "gunicorn": - if 'setproctitle' in sys.modules: + if "setproctitle" in sys.modules: # With the setproctitle package, gunicorn renames their processes # to pretty things - we use those by default # gunicorn: master [djface.wsgi] @@ -77,8 +78,8 @@ def determine_service_name(): elif "FLASK_APP" in os.environ: app_name = os.environ["FLASK_APP"] elif "DJANGO_SETTINGS_MODULE" in os.environ: - app_name = os.environ["DJANGO_SETTINGS_MODULE"].split('.')[0] - elif basename == '': + app_name = os.environ["DJANGO_SETTINGS_MODULE"].split(".")[0] + elif basename == "": if sys.stdout.isatty(): app_name = "Interactive Console" else: @@ -115,6 +116,7 @@ def determine_service_name(): return app_name + def get_proc_cmdline(as_string=False): """ Parse the proc file system for the command line of this process. If not available, then return a default. @@ -134,10 +136,10 @@ def get_proc_cmdline(as_string=False): # /proc/self/command line will have strings with null bytes such as "/usr/bin/python\0-s\0-d\0". This # bit will prep the return value and drop the trailing null byte - parts = name.split('\0') + parts = name.split("\0") parts.pop() if as_string is True: parts = " ".join(parts) - return parts \ No newline at end of file + return parts diff --git a/src/instana/util/secrets.py b/src/instana/util/secrets.py index f5b8c0718..da7f3d389 100644 --- a/src/instana/util/secrets.py +++ b/src/instana/util/secrets.py @@ -25,23 +25,23 @@ def contains_secret(candidate, matcher, kwlist): logger.debug("contains_secret: bad keyword list") return False - if matcher == 'equals-ignore-case': + if matcher == "equals-ignore-case": for keyword in kwlist: if candidate.lower() == keyword.lower(): return True - elif matcher == 'equals': + elif matcher == "equals": for keyword in kwlist: if candidate == keyword: return True - elif matcher == 'contains-ignore-case': + elif matcher == "contains-ignore-case": for keyword in kwlist: if keyword.lower() in candidate: return True - elif matcher == 'contains': + elif matcher == "contains": for keyword in kwlist: if keyword in candidate: return True - elif matcher == 'regex': + elif matcher == "regex": for regexp in kwlist: if re.match(regexp, candidate): return True @@ -72,45 +72,45 @@ def strip_secrets_from_query(qp, matcher, kwlist): try: if qp is None: - return '' + return "" if not isinstance(kwlist, list): logger.debug("strip_secrets_from_query: bad keyword list") return qp # If there are no key=values, then just return - if not '=' in qp: + if "=" not in qp: return qp - if '?' in qp: - path, query = qp.split('?') + if "?" in qp: + path, query = qp.split("?") else: query = qp params = parse.parse_qsl(query, keep_blank_values=True) - redacted = [''] + redacted = [""] - if matcher == 'equals-ignore-case': + if matcher == "equals-ignore-case": for keyword in kwlist: for index, kv in enumerate(params): if kv[0].lower() == keyword.lower(): params[index] = (kv[0], redacted) - elif matcher == 'equals': + elif matcher == "equals": for keyword in kwlist: for index, kv in enumerate(params): if kv[0] == keyword: params[index] = (kv[0], redacted) - elif matcher == 'contains-ignore-case': + elif matcher == "contains-ignore-case": for keyword in kwlist: for index, kv in enumerate(params): if keyword.lower() in kv[0].lower(): params[index] = (kv[0], redacted) - elif matcher == 'contains': + elif matcher == "contains": for keyword in kwlist: for index, kv in enumerate(params): if keyword in kv[0]: params[index] = (kv[0], redacted) - elif matcher == 'regex': + elif matcher == "regex": for regexp in kwlist: for index, kv in enumerate(params): if re.match(regexp, kv[0]): @@ -123,7 +123,7 @@ def strip_secrets_from_query(qp, matcher, kwlist): query = parse.unquote(result) if path: - query = path + '?' + query + query = path + "?" + query return query except Exception: diff --git a/src/instana/util/sql.py b/src/instana/util/sql.py index 8e7ee2f9f..c8d6cbbd9 100644 --- a/src/instana/util/sql.py +++ b/src/instana/util/sql.py @@ -3,6 +3,7 @@ import re + def sql_sanitizer(sql): """ Removes values from valid SQL statements and returns a stripped version. @@ -10,8 +11,8 @@ def sql_sanitizer(sql): :param sql: The SQL statement to be sanitized :return: String - A sanitized SQL statement without values. """ - return regexp_sql_values.sub('?', sql) + return regexp_sql_values.sub("?", sql) # Used by sql_sanitizer -regexp_sql_values = re.compile(r"('[\s\S][^']*'|\d*\.\d+|\d+|NULL)") \ No newline at end of file +regexp_sql_values = re.compile(r"('[\s\S][^']*'|\d*\.\d+|\d+|NULL)") diff --git a/src/instana/w3c_trace_context/traceparent.py b/src/instana/w3c_trace_context/traceparent.py index edfd70665..8840fa75d 100644 --- a/src/instana/w3c_trace_context/traceparent.py +++ b/src/instana/w3c_trace_context/traceparent.py @@ -13,7 +13,8 @@ from instana.util.ids import header_to_id # See https://www.w3.org/TR/trace-context-2/#trace-flags for details on the bitmasks. -SAMPLED_BITMASK = 0b1; +SAMPLED_BITMASK = 0b1 + class Traceparent: SPECIFICATION_VERSION = "00" @@ -39,7 +40,9 @@ def validate(self, traceparent): return None @staticmethod - def get_traceparent_fields(traceparent: str) -> Tuple[Optional[str], Optional[int], Optional[int], Optional[bool]]: + def get_traceparent_fields( + traceparent: str, + ) -> Tuple[Optional[str], Optional[int], Optional[int], Optional[bool]]: """ Parses the validated traceparent header into its fields and returns the fields :param traceparent: the original validated traceparent header diff --git a/src/instana/w3c_trace_context/tracestate.py b/src/instana/w3c_trace_context/tracestate.py index f6eb32cb9..6bfdc7422 100644 --- a/src/instana/w3c_trace_context/tracestate.py +++ b/src/instana/w3c_trace_context/tracestate.py @@ -24,8 +24,10 @@ def get_instana_ancestor(tracestate): try: in_list_member = tracestate.strip().split("in=")[1].split(",")[0] - ia = InstanaAncestor(trace_id=in_list_member.split(";")[0], - parent_id=in_list_member.split(";")[1]) + ia = InstanaAncestor( + trace_id=in_list_member.split(";")[0], + parent_id=in_list_member.split(";")[1], + ) return ia except Exception: @@ -54,14 +56,16 @@ def update_tracestate(self, tracestate, in_trace_id, in_span_id): splitted = tracestate.split("in=") before_in = splitted[0] after_in = splitted[1].split(",")[1:] - tracestate = '{}{}'.format(before_in, ",".join(after_in)) + tracestate = "{}{}".format(before_in, ",".join(after_in)) # tracestate can contain a max of 32 list members, if it contains up to 31 # we can safely add the instana one without the need to truncate anything if len(tracestate.split(",")) <= self.MAX_NUMBER_OF_LIST_MEMBERS - 1: tracestate = "{},{}".format(instana_tracestate, tracestate) else: list_members = tracestate.split(",") - list_members_to_remove = len(list_members) - self.MAX_NUMBER_OF_LIST_MEMBERS + 1 + list_members_to_remove = ( + len(list_members) - self.MAX_NUMBER_OF_LIST_MEMBERS + 1 + ) # Number 1 priority members to be removed are the ones larger than 128 characters for i, m in reversed(list(enumerate(list_members))): if len(m) > self.REMOVE_ENTRIES_LARGER_THAN: @@ -79,6 +83,11 @@ def update_tracestate(self, tracestate, in_trace_id, in_span_id): # adding instana as first list member, total of 32 list members tracestate = "{},{}".format(instana_tracestate, tracestate) except Exception: - logger.debug("Something went wrong while updating tracestate: {}:".format(tracestate), exc_info=True) + logger.debug( + "Something went wrong while updating tracestate: {}:".format( + tracestate + ), + exc_info=True, + ) return tracestate diff --git a/tests/__init__.py b/tests/__init__.py index 39799ddb6..b7a063518 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,7 +3,7 @@ import os -if os.environ.get('GEVENT_STARLETTE_TEST'): +if os.environ.get("GEVENT_STARLETTE_TEST"): from gevent import monkey - monkey.patch_all() + monkey.patch_all() diff --git a/tests/apps/aiohttp_app/__init__.py b/tests/apps/aiohttp_app/__init__.py index 7429a9491..8cd3000e5 100644 --- a/tests/apps/aiohttp_app/__init__.py +++ b/tests/apps/aiohttp_app/__init__.py @@ -8,7 +8,11 @@ APP_THREAD = None -if not any((os.environ.get('GEVENT_STARLETTE_TEST'), - os.environ.get('CASSANDRA_TEST'), - sys.version_info < (3, 5, 3))): +if not any( + ( + os.environ.get("GEVENT_STARLETTE_TEST"), + os.environ.get("CASSANDRA_TEST"), + sys.version_info < (3, 5, 3), + ) +): APP_THREAD = launch_background_thread(server, "AIOHTTP") diff --git a/tests/apps/aiohttp_app/app.py b/tests/apps/aiohttp_app/app.py index 44bdb1a30..3c2d98f55 100755 --- a/tests/apps/aiohttp_app/app.py +++ b/tests/apps/aiohttp_app/app.py @@ -11,11 +11,11 @@ testenv["aiohttp_port"] = 10810 -testenv["aiohttp_server"] = ("http://127.0.0.1:" + str(testenv["aiohttp_port"])) +testenv["aiohttp_server"] = "http://127.0.0.1:" + str(testenv["aiohttp_port"]) def say_hello(request): - return web.Response(text='Hello, world') + return web.Response(text="Hello, world") def two_hundred_four(request): @@ -23,11 +23,15 @@ def two_hundred_four(request): def four_hundred_one(request): - raise web.HTTPUnauthorized(reason="I must simulate errors.", text="Simulated server error.") + raise web.HTTPUnauthorized( + reason="I must simulate errors.", text="Simulated server error." + ) def five_hundred(request): - return web.HTTPInternalServerError(reason="I must simulate errors.", text="Simulated server error.") + return web.HTTPInternalServerError( + reason="I must simulate errors.", text="Simulated server error." + ) def raise_exception(request): @@ -39,15 +43,15 @@ def aiohttp_server(): asyncio.set_event_loop(loop) app = web.Application() - app.add_routes([web.get('/', say_hello)]) - app.add_routes([web.get('/204', two_hundred_four)]) - app.add_routes([web.get('/401', four_hundred_one)]) - app.add_routes([web.get('/500', five_hundred)]) - app.add_routes([web.get('/exception', raise_exception)]) + app.add_routes([web.get("/", say_hello)]) + app.add_routes([web.get("/204", two_hundred_four)]) + app.add_routes([web.get("/401", four_hundred_one)]) + app.add_routes([web.get("/500", five_hundred)]) + app.add_routes([web.get("/exception", raise_exception)]) runner = web.AppRunner(app) loop.run_until_complete(runner.setup()) - site = web.TCPSite(runner, '127.0.0.1', testenv["aiohttp_port"]) + site = web.TCPSite(runner, "127.0.0.1", testenv["aiohttp_port"]) loop.run_until_complete(site.start()) loop.run_forever() diff --git a/tests/apps/aiohttp_app2/__init__.py b/tests/apps/aiohttp_app2/__init__.py index e382343a4..40177540d 100644 --- a/tests/apps/aiohttp_app2/__init__.py +++ b/tests/apps/aiohttp_app2/__init__.py @@ -7,7 +7,11 @@ APP_THREAD = None -if not any((os.environ.get('GEVENT_STARLETTE_TEST'), - os.environ.get('CASSANDRA_TEST'), - sys.version_info < (3, 5, 3))): +if not any( + ( + os.environ.get("GEVENT_STARLETTE_TEST"), + os.environ.get("CASSANDRA_TEST"), + sys.version_info < (3, 5, 3), + ) +): APP_THREAD = launch_background_thread(server, "AIOHTTP") diff --git a/tests/apps/bottle_app/__init__.py b/tests/apps/bottle_app/__init__.py index 44cdabb1f..aa561a97b 100644 --- a/tests/apps/bottle_app/__init__.py +++ b/tests/apps/bottle_app/__init__.py @@ -6,5 +6,5 @@ app_thread = None -if not os.environ.get('CASSANDRA_TEST') and app_thread is None: - app_thread = launch_background_thread(server.serve_forever, "Bottle") \ No newline at end of file +if not os.environ.get("CASSANDRA_TEST") and app_thread is None: + app_thread = launch_background_thread(server.serve_forever, "Bottle") diff --git a/tests/apps/bottle_app/app.py b/tests/apps/bottle_app/app.py index cd56c138c..0d5b68cdb 100644 --- a/tests/apps/bottle_app/app.py +++ b/tests/apps/bottle_app/app.py @@ -15,17 +15,19 @@ logger = logging.getLogger(__name__) testenv["wsgi_port"] = 10812 -testenv["wsgi_server"] = ("http://127.0.0.1:" + str(testenv["wsgi_port"])) +testenv["wsgi_server"] = "http://127.0.0.1:" + str(testenv["wsgi_port"]) app = default_app() + @app.route("/") def hello(): return "

🐍 Hello Stan! 🦄

" + # Wrap the application with the Instana WSGI Middleware app = InstanaWSGIMiddleware(app) -bottle_server = make_server('127.0.0.1', testenv["wsgi_port"], app) +bottle_server = make_server("127.0.0.1", testenv["wsgi_port"], app) if __name__ == "__main__": bottle_server.request_queue_size = 20 diff --git a/tests/apps/fastapi_app/__init__.py b/tests/apps/fastapi_app/__init__.py index dded82bda..56ad83218 100644 --- a/tests/apps/fastapi_app/__init__.py +++ b/tests/apps/fastapi_app/__init__.py @@ -3,7 +3,6 @@ import uvicorn from ...helpers import testenv -from instana.log import logger testenv["fastapi_port"] = 10816 testenv["fastapi_server"] = "http://127.0.0.1:" + str(testenv["fastapi_port"]) diff --git a/tests/apps/fastapi_app/app2.py b/tests/apps/fastapi_app/app2.py index 8f9b7edd4..cffe7c88d 100644 --- a/tests/apps/fastapi_app/app2.py +++ b/tests/apps/fastapi_app/app2.py @@ -1,7 +1,6 @@ # (c) Copyright IBM Corp. 2024 -from fastapi import FastAPI, HTTPException, Response -from fastapi.concurrency import run_in_threadpool +from fastapi import FastAPI from fastapi.middleware import Middleware from fastapi.middleware.trustedhost import TrustedHostMiddleware diff --git a/tests/apps/flask_app/__init__.py b/tests/apps/flask_app/__init__.py index 11c6beab5..1617b8e7f 100644 --- a/tests/apps/flask_app/__init__.py +++ b/tests/apps/flask_app/__init__.py @@ -7,5 +7,5 @@ app_thread = None -if not os.environ.get('CASSANDRA_TEST') and app_thread is None: +if not os.environ.get("CASSANDRA_TEST") and app_thread is None: app_thread = launch_background_thread(server.serve_forever, "Flask") diff --git a/tests/apps/flask_app/app.py b/tests/apps/flask_app/app.py index e49f8fa12..4720859ce 100755 --- a/tests/apps/flask_app/app.py +++ b/tests/apps/flask_app/app.py @@ -7,7 +7,6 @@ import os import logging -from opentelemetry.semconv.trace import SpanAttributes from flask import jsonify, Response from wsgiref.simple_server import make_server @@ -27,13 +26,13 @@ logger = logging.getLogger(__name__) testenv["flask_port"] = 10811 -testenv["flask_server"] = ("http://127.0.0.1:" + str(testenv["flask_port"])) +testenv["flask_server"] = "http://127.0.0.1:" + str(testenv["flask_port"]) app = Flask(__name__) app.debug = False app.use_reloader = False -flask_server = make_server('127.0.0.1', testenv["flask_port"], app.wsgi_app) +flask_server = make_server("127.0.0.1", testenv["flask_port"], app.wsgi_app) class InvalidUsage(Exception): @@ -48,7 +47,7 @@ def __init__(self, message, status_code=None, payload=None): def to_dict(self): rv = dict(self.payload or ()) - rv['message'] = self.message + rv["message"] = self.message return rv @@ -64,7 +63,7 @@ def __init__(self, message, status_code=None, payload=None): def to_dict(self): rv = dict(self.payload or ()) - rv['message'] = self.message + rv["message"] = self.message return rv @@ -75,17 +74,17 @@ def hello(): @app.route("/users//sayhello") def username_hello(username): - return u"

🐍 Hello %s! 🦄

" % username + return "

🐍 Hello %s! 🦄

" % username @app.route("/301") def threehundredone(): - return redirect('/', code=301) + return redirect("/", code=301) @app.route("/302") def threehundredtwo(): - return redirect('/', code=302) + return redirect("/", code=302) @app.route("/400") @@ -115,7 +114,7 @@ def fivehundredfour(): @app.route("/exception") def exception(): - raise Exception('fake error') + raise Exception("fake error") @app.route("/got_request_exception") @@ -130,59 +129,57 @@ def exception_invalid_usage(): @app.route("/render") def render(): - return render_template('flask_render_template.html', name="Peter") + return render_template("flask_render_template.html", name="Peter") @app.route("/render_string") def render_string(): - return render_template_string('hello {{ what }}', what='world') + return render_template_string("hello {{ what }}", what="world") @app.route("/render_error") def render_error(): - return render_template('flask_render_error.html', what='world') + return render_template("flask_render_error.html", what="world") @app.route("/response_headers") def response_headers(): - headers = { - 'X-Capture-This': 'Ok', - 'X-Capture-That': 'Ok too' - } + headers = {"X-Capture-This": "Ok", "X-Capture-That": "Ok too"} return Response("Stan wuz here with headers!", headers=headers) + @app.route("/boto3/sqs") def boto3_sqs(): - os.environ['AWS_ACCESS_KEY_ID'] = 'testing' - os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing' - os.environ['AWS_SECURITY_TOKEN'] = 'testing' - os.environ['AWS_SESSION_TOKEN'] = 'testing' + os.environ["AWS_ACCESS_KEY_ID"] = "testing" + os.environ["AWS_SECRET_ACCESS_KEY"] = "testing" + os.environ["AWS_SECURITY_TOKEN"] = "testing" + os.environ["AWS_SESSION_TOKEN"] = "testing" with mock_aws(): - boto3_client = boto3.client('sqs', region_name='us-east-1') + boto3_client = boto3.client("sqs", region_name="us-east-1") response = boto3_client.create_queue( - QueueName='SQS_QUEUE_NAME', - Attributes={ - 'DelaySeconds': '60', - 'MessageRetentionPeriod': '600' - } + QueueName="SQS_QUEUE_NAME", + Attributes={"DelaySeconds": "60", "MessageRetentionPeriod": "600"}, ) - queue_url = response['QueueUrl'] + queue_url = response["QueueUrl"] response = boto3_client.send_message( - QueueUrl=queue_url, - DelaySeconds=10, - MessageAttributes={ - 'Website': { - 'DataType': 'String', - 'StringValue': 'https://www.instana.com' - }, + QueueUrl=queue_url, + DelaySeconds=10, + MessageAttributes={ + "Website": { + "DataType": "String", + "StringValue": "https://www.instana.com", }, - MessageBody=('Monitor any application, service, or request ' - 'with Instana Application Performance Monitoring') - ) + }, + MessageBody=( + "Monitor any application, service, or request " + "with Instana Application Performance Monitoring" + ), + ) return Response(response) + @app.errorhandler(InvalidUsage) def handle_invalid_usage(error): logger.error("InvalidUsage error handler invoked") @@ -197,6 +194,6 @@ def handle_not_found(e): return "blah: %s" % str(e), 404 -if __name__ == '__main__': +if __name__ == "__main__": flask_server.request_queue_size = 20 flask_server.serve_forever() diff --git a/tests/apps/grpc_server/__init__.py b/tests/apps/grpc_server/__init__.py index 5a222deb1..c6ff4e3a5 100644 --- a/tests/apps/grpc_server/__init__.py +++ b/tests/apps/grpc_server/__init__.py @@ -6,15 +6,19 @@ import time import threading -if not any((os.environ.get('GEVENT_STARLETTE_TEST'), - os.environ.get('CASSANDRA_TEST'), - sys.version_info < (3, 5, 3))): +if not any( + ( + os.environ.get("GEVENT_STARLETTE_TEST"), + os.environ.get("CASSANDRA_TEST"), + sys.version_info < (3, 5, 3), + ) +): # Background RPC application # # Spawn the background RPC app that the tests will throw # requests at. - import tests.apps.grpc_server from .stan_server import StanServicer + stan_servicer = StanServicer() rpc_server_thread = threading.Thread(target=stan_servicer.start_server) rpc_server_thread.daemon = True diff --git a/tests/apps/grpc_server/stan_client.py b/tests/apps/grpc_server/stan_client.py index 450d62eba..7598ddb15 100644 --- a/tests/apps/grpc_server/stan_client.py +++ b/tests/apps/grpc_server/stan_client.py @@ -17,14 +17,14 @@ def generate_questions(): - """ Used in the streaming grpc tests """ + """Used in the streaming grpc tests""" questions = [ stan_pb2.QuestionRequest(question="Are you there?"), stan_pb2.QuestionRequest(question="What time is it?"), stan_pb2.QuestionRequest(question="Where in the world is Waldo?"), stan_pb2.QuestionRequest(question="What did one campfire say to the other?"), stan_pb2.QuestionRequest(question="Is cereal soup?"), - stan_pb2.QuestionRequest(question="What is always coming, but never arrives?") + stan_pb2.QuestionRequest(question="What is always coming, but never arrives?"), ] for q in questions: yield q @@ -36,20 +36,24 @@ def generate_questions(): # The grpc client apparently needs a second to connect and initialize time.sleep(1) -with tracer.start_active_span('http-server') as scope: - scope.span.set_tag('http.url', 'https://localhost:8080/grpc-client') - scope.span.set_tag('http.method', 'GET') - scope.span.set_tag('span.kind', 'entry') - response = server_stub.OneQuestionOneResponse(stan_pb2.QuestionRequest(question="Are you there?")) - -with tracer.start_active_span('http-server') as scope: - scope.span.set_tag('http.url', 'https://localhost:8080/grpc-server-streaming') - scope.span.set_tag('http.method', 'GET') - scope.span.set_tag('span.kind', 'entry') - responses = server_stub.OneQuestionManyResponses(stan_pb2.QuestionRequest(question="Are you there?")) - -with tracer.start_active_span('http-server') as scope: - scope.span.set_tag('http.url', 'https://localhost:8080/grpc-client-streaming') - scope.span.set_tag('http.method', 'GET') - scope.span.set_tag('span.kind', 'entry') +with tracer.start_active_span("http-server") as scope: + scope.span.set_tag("http.url", "https://localhost:8080/grpc-client") + scope.span.set_tag("http.method", "GET") + scope.span.set_tag("span.kind", "entry") + response = server_stub.OneQuestionOneResponse( + stan_pb2.QuestionRequest(question="Are you there?") + ) + +with tracer.start_active_span("http-server") as scope: + scope.span.set_tag("http.url", "https://localhost:8080/grpc-server-streaming") + scope.span.set_tag("http.method", "GET") + scope.span.set_tag("span.kind", "entry") + responses = server_stub.OneQuestionManyResponses( + stan_pb2.QuestionRequest(question="Are you there?") + ) + +with tracer.start_active_span("http-server") as scope: + scope.span.set_tag("http.url", "https://localhost:8080/grpc-client-streaming") + scope.span.set_tag("http.method", "GET") + scope.span.set_tag("span.kind", "entry") response = server_stub.ManyQuestionsOneResponse(generate_questions()) diff --git a/tests/apps/grpc_server/stan_pb2.py b/tests/apps/grpc_server/stan_pb2.py index cd2e63f91..1e5e2512a 100644 --- a/tests/apps/grpc_server/stan_pb2.py +++ b/tests/apps/grpc_server/stan_pb2.py @@ -5,183 +5,219 @@ # source: stan.proto import sys -_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) + from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message from google.protobuf import reflection as _reflection from google.protobuf import symbol_database as _symbol_database -# @@protoc_insertion_point(imports) -_sym_db = _symbol_database.Default() +_b = sys.version_info[0] < 3 and (lambda x: x) or (lambda x: x.encode("latin1")) +# @@protoc_insertion_point(imports) +_sym_db = _symbol_database.Default() DESCRIPTOR = _descriptor.FileDescriptor( - name='stan.proto', - package='stan', - syntax='proto3', - serialized_options=None, - serialized_pb=_b('\n\nstan.proto\x12\x04stan\"#\n\x0fQuestionRequest\x12\x10\n\x08question\x18\x01 \x01(\t\"8\n\x10QuestionResponse\x12\x0e\n\x06\x61nswer\x18\x01 \x01(\t\x12\x14\n\x0cwas_answered\x18\x02 \x01(\x08\x32\xe3\x03\n\x04Stan\x12I\n\x16OneQuestionOneResponse\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse\"\x00\x12M\n\x18ManyQuestionsOneResponse\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse\"\x00(\x01\x12M\n\x18OneQuestionManyResponses\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse\"\x00\x30\x01\x12P\n\x19ManyQuestionsManyReponses\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse\"\x00(\x01\x30\x01\x12N\n\x1bOneQuestionOneErrorResponse\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse\"\x00\x12P\n\x1dOneErroredQuestionOneResponse\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse\"\x00\x62\x06proto3') + name="stan.proto", + package="stan", + syntax="proto3", + serialized_options=None, + serialized_pb=_b( + '\n\nstan.proto\x12\x04stan"#\n\x0fQuestionRequest\x12\x10\n\x08question\x18\x01 \x01(\t"8\n\x10QuestionResponse\x12\x0e\n\x06\x61nswer\x18\x01 \x01(\t\x12\x14\n\x0cwas_answered\x18\x02 \x01(\x08\x32\xe3\x03\n\x04Stan\x12I\n\x16OneQuestionOneResponse\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse"\x00\x12M\n\x18ManyQuestionsOneResponse\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse"\x00(\x01\x12M\n\x18OneQuestionManyResponses\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse"\x00\x30\x01\x12P\n\x19ManyQuestionsManyReponses\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse"\x00(\x01\x30\x01\x12N\n\x1bOneQuestionOneErrorResponse\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse"\x00\x12P\n\x1dOneErroredQuestionOneResponse\x12\x15.stan.QuestionRequest\x1a\x16.stan.QuestionResponse"\x00\x62\x06proto3' + ), ) - - _QUESTIONREQUEST = _descriptor.Descriptor( - name='QuestionRequest', - full_name='stan.QuestionRequest', - filename=None, - file=DESCRIPTOR, - containing_type=None, - fields=[ - _descriptor.FieldDescriptor( - name='question', full_name='stan.QuestionRequest.question', index=0, - number=1, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - serialized_options=None, - is_extendable=False, - syntax='proto3', - extension_ranges=[], - oneofs=[ - ], - serialized_start=20, - serialized_end=55, + name="QuestionRequest", + full_name="stan.QuestionRequest", + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name="question", + full_name="stan.QuestionRequest.question", + index=0, + number=1, + type=9, + cpp_type=9, + label=1, + has_default_value=False, + default_value=_b("").decode("utf-8"), + message_type=None, + enum_type=None, + containing_type=None, + is_extension=False, + extension_scope=None, + serialized_options=None, + file=DESCRIPTOR, + ), + ], + extensions=[], + nested_types=[], + enum_types=[], + serialized_options=None, + is_extendable=False, + syntax="proto3", + extension_ranges=[], + oneofs=[], + serialized_start=20, + serialized_end=55, ) _QUESTIONRESPONSE = _descriptor.Descriptor( - name='QuestionResponse', - full_name='stan.QuestionResponse', - filename=None, - file=DESCRIPTOR, - containing_type=None, - fields=[ - _descriptor.FieldDescriptor( - name='answer', full_name='stan.QuestionResponse.answer', index=0, - number=1, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), - _descriptor.FieldDescriptor( - name='was_answered', full_name='stan.QuestionResponse.was_answered', index=1, - number=2, type=8, cpp_type=7, label=1, - has_default_value=False, default_value=False, - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - serialized_options=None, - is_extendable=False, - syntax='proto3', - extension_ranges=[], - oneofs=[ - ], - serialized_start=57, - serialized_end=113, + name="QuestionResponse", + full_name="stan.QuestionResponse", + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name="answer", + full_name="stan.QuestionResponse.answer", + index=0, + number=1, + type=9, + cpp_type=9, + label=1, + has_default_value=False, + default_value=_b("").decode("utf-8"), + message_type=None, + enum_type=None, + containing_type=None, + is_extension=False, + extension_scope=None, + serialized_options=None, + file=DESCRIPTOR, + ), + _descriptor.FieldDescriptor( + name="was_answered", + full_name="stan.QuestionResponse.was_answered", + index=1, + number=2, + type=8, + cpp_type=7, + label=1, + has_default_value=False, + default_value=False, + message_type=None, + enum_type=None, + containing_type=None, + is_extension=False, + extension_scope=None, + serialized_options=None, + file=DESCRIPTOR, + ), + ], + extensions=[], + nested_types=[], + enum_types=[], + serialized_options=None, + is_extendable=False, + syntax="proto3", + extension_ranges=[], + oneofs=[], + serialized_start=57, + serialized_end=113, ) -DESCRIPTOR.message_types_by_name['QuestionRequest'] = _QUESTIONREQUEST -DESCRIPTOR.message_types_by_name['QuestionResponse'] = _QUESTIONRESPONSE +DESCRIPTOR.message_types_by_name["QuestionRequest"] = _QUESTIONREQUEST +DESCRIPTOR.message_types_by_name["QuestionResponse"] = _QUESTIONRESPONSE _sym_db.RegisterFileDescriptor(DESCRIPTOR) -QuestionRequest = _reflection.GeneratedProtocolMessageType('QuestionRequest', (_message.Message,), dict( - DESCRIPTOR = _QUESTIONREQUEST, - __module__ = 'stan_pb2' - # @@protoc_insertion_point(class_scope:stan.QuestionRequest) - )) +QuestionRequest = _reflection.GeneratedProtocolMessageType( + "QuestionRequest", + (_message.Message,), + dict( + DESCRIPTOR=_QUESTIONREQUEST, + __module__="stan_pb2", + # @@protoc_insertion_point(class_scope:stan.QuestionRequest) + ), +) _sym_db.RegisterMessage(QuestionRequest) -QuestionResponse = _reflection.GeneratedProtocolMessageType('QuestionResponse', (_message.Message,), dict( - DESCRIPTOR = _QUESTIONRESPONSE, - __module__ = 'stan_pb2' - # @@protoc_insertion_point(class_scope:stan.QuestionResponse) - )) +QuestionResponse = _reflection.GeneratedProtocolMessageType( + "QuestionResponse", + (_message.Message,), + dict( + DESCRIPTOR=_QUESTIONRESPONSE, + __module__="stan_pb2", + # @@protoc_insertion_point(class_scope:stan.QuestionResponse) + ), +) _sym_db.RegisterMessage(QuestionResponse) - _STAN = _descriptor.ServiceDescriptor( - name='Stan', - full_name='stan.Stan', - file=DESCRIPTOR, - index=0, - serialized_options=None, - serialized_start=116, - serialized_end=599, - methods=[ - _descriptor.MethodDescriptor( - name='OneQuestionOneResponse', - full_name='stan.Stan.OneQuestionOneResponse', + name="Stan", + full_name="stan.Stan", + file=DESCRIPTOR, index=0, - containing_service=None, - input_type=_QUESTIONREQUEST, - output_type=_QUESTIONRESPONSE, - serialized_options=None, - ), - _descriptor.MethodDescriptor( - name='ManyQuestionsOneResponse', - full_name='stan.Stan.ManyQuestionsOneResponse', - index=1, - containing_service=None, - input_type=_QUESTIONREQUEST, - output_type=_QUESTIONRESPONSE, - serialized_options=None, - ), - _descriptor.MethodDescriptor( - name='OneQuestionManyResponses', - full_name='stan.Stan.OneQuestionManyResponses', - index=2, - containing_service=None, - input_type=_QUESTIONREQUEST, - output_type=_QUESTIONRESPONSE, serialized_options=None, - ), - _descriptor.MethodDescriptor( - name='ManyQuestionsManyReponses', - full_name='stan.Stan.ManyQuestionsManyReponses', - index=3, - containing_service=None, - input_type=_QUESTIONREQUEST, - output_type=_QUESTIONRESPONSE, - serialized_options=None, - ), - _descriptor.MethodDescriptor( - name='OneQuestionOneErrorResponse', - full_name='stan.Stan.OneQuestionOneErrorResponse', - index=4, - containing_service=None, - input_type=_QUESTIONREQUEST, - output_type=_QUESTIONRESPONSE, - serialized_options=None, - ), - _descriptor.MethodDescriptor( - name='OneErroredQuestionOneResponse', - full_name='stan.Stan.OneErroredQuestionOneResponse', - index=5, - containing_service=None, - input_type=_QUESTIONREQUEST, - output_type=_QUESTIONRESPONSE, - serialized_options=None, - ), -]) + serialized_start=116, + serialized_end=599, + methods=[ + _descriptor.MethodDescriptor( + name="OneQuestionOneResponse", + full_name="stan.Stan.OneQuestionOneResponse", + index=0, + containing_service=None, + input_type=_QUESTIONREQUEST, + output_type=_QUESTIONRESPONSE, + serialized_options=None, + ), + _descriptor.MethodDescriptor( + name="ManyQuestionsOneResponse", + full_name="stan.Stan.ManyQuestionsOneResponse", + index=1, + containing_service=None, + input_type=_QUESTIONREQUEST, + output_type=_QUESTIONRESPONSE, + serialized_options=None, + ), + _descriptor.MethodDescriptor( + name="OneQuestionManyResponses", + full_name="stan.Stan.OneQuestionManyResponses", + index=2, + containing_service=None, + input_type=_QUESTIONREQUEST, + output_type=_QUESTIONRESPONSE, + serialized_options=None, + ), + _descriptor.MethodDescriptor( + name="ManyQuestionsManyReponses", + full_name="stan.Stan.ManyQuestionsManyReponses", + index=3, + containing_service=None, + input_type=_QUESTIONREQUEST, + output_type=_QUESTIONRESPONSE, + serialized_options=None, + ), + _descriptor.MethodDescriptor( + name="OneQuestionOneErrorResponse", + full_name="stan.Stan.OneQuestionOneErrorResponse", + index=4, + containing_service=None, + input_type=_QUESTIONREQUEST, + output_type=_QUESTIONRESPONSE, + serialized_options=None, + ), + _descriptor.MethodDescriptor( + name="OneErroredQuestionOneResponse", + full_name="stan.Stan.OneErroredQuestionOneResponse", + index=5, + containing_service=None, + input_type=_QUESTIONREQUEST, + output_type=_QUESTIONRESPONSE, + serialized_options=None, + ), + ], +) _sym_db.RegisterServiceDescriptor(_STAN) -DESCRIPTOR.services_by_name['Stan'] = _STAN +DESCRIPTOR.services_by_name["Stan"] = _STAN # @@protoc_insertion_point(module_scope) diff --git a/tests/apps/grpc_server/stan_pb2_grpc.py b/tests/apps/grpc_server/stan_pb2_grpc.py index 5d0b49a58..4ac428051 100644 --- a/tests/apps/grpc_server/stan_pb2_grpc.py +++ b/tests/apps/grpc_server/stan_pb2_grpc.py @@ -8,127 +8,125 @@ class StanStub(object): - # missing associated documentation comment in .proto file - pass - - def __init__(self, channel): - """Constructor. - - Args: - channel: A grpc.Channel. - """ - self.OneQuestionOneResponse = channel.unary_unary( - '/stan.Stan/OneQuestionOneResponse', - request_serializer=stan__pb2.QuestionRequest.SerializeToString, - response_deserializer=stan__pb2.QuestionResponse.FromString, + # missing associated documentation comment in .proto file + pass + + def __init__(self, channel): + """Constructor. + + Args: + channel: A grpc.Channel. + """ + self.OneQuestionOneResponse = channel.unary_unary( + "/stan.Stan/OneQuestionOneResponse", + request_serializer=stan__pb2.QuestionRequest.SerializeToString, + response_deserializer=stan__pb2.QuestionResponse.FromString, ) - self.ManyQuestionsOneResponse = channel.stream_unary( - '/stan.Stan/ManyQuestionsOneResponse', - request_serializer=stan__pb2.QuestionRequest.SerializeToString, - response_deserializer=stan__pb2.QuestionResponse.FromString, + self.ManyQuestionsOneResponse = channel.stream_unary( + "/stan.Stan/ManyQuestionsOneResponse", + request_serializer=stan__pb2.QuestionRequest.SerializeToString, + response_deserializer=stan__pb2.QuestionResponse.FromString, ) - self.OneQuestionManyResponses = channel.unary_stream( - '/stan.Stan/OneQuestionManyResponses', - request_serializer=stan__pb2.QuestionRequest.SerializeToString, - response_deserializer=stan__pb2.QuestionResponse.FromString, + self.OneQuestionManyResponses = channel.unary_stream( + "/stan.Stan/OneQuestionManyResponses", + request_serializer=stan__pb2.QuestionRequest.SerializeToString, + response_deserializer=stan__pb2.QuestionResponse.FromString, ) - self.ManyQuestionsManyReponses = channel.stream_stream( - '/stan.Stan/ManyQuestionsManyReponses', - request_serializer=stan__pb2.QuestionRequest.SerializeToString, - response_deserializer=stan__pb2.QuestionResponse.FromString, + self.ManyQuestionsManyReponses = channel.stream_stream( + "/stan.Stan/ManyQuestionsManyReponses", + request_serializer=stan__pb2.QuestionRequest.SerializeToString, + response_deserializer=stan__pb2.QuestionResponse.FromString, ) - self.OneQuestionOneErrorResponse = channel.unary_unary( - '/stan.Stan/OneQuestionOneErrorResponse', - request_serializer=stan__pb2.QuestionRequest.SerializeToString, - response_deserializer=stan__pb2.QuestionResponse.FromString, + self.OneQuestionOneErrorResponse = channel.unary_unary( + "/stan.Stan/OneQuestionOneErrorResponse", + request_serializer=stan__pb2.QuestionRequest.SerializeToString, + response_deserializer=stan__pb2.QuestionResponse.FromString, ) - self.OneErroredQuestionOneResponse = channel.unary_unary( - '/stan.Stan/OneErroredQuestionOneResponse', - request_serializer=stan__pb2.QuestionRequest.SerializeToString, - response_deserializer=stan__pb2.QuestionResponse.FromString, + self.OneErroredQuestionOneResponse = channel.unary_unary( + "/stan.Stan/OneErroredQuestionOneResponse", + request_serializer=stan__pb2.QuestionRequest.SerializeToString, + response_deserializer=stan__pb2.QuestionResponse.FromString, ) class StanServicer(object): - # missing associated documentation comment in .proto file - pass - - def OneQuestionOneResponse(self, request, context): - """Unary - """ - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details('Method not implemented!') - raise NotImplementedError('Method not implemented!') - - def ManyQuestionsOneResponse(self, request_iterator, context): - """Streaming - """ - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details('Method not implemented!') - raise NotImplementedError('Method not implemented!') - - def OneQuestionManyResponses(self, request, context): # missing associated documentation comment in .proto file pass - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details('Method not implemented!') - raise NotImplementedError('Method not implemented!') - def ManyQuestionsManyReponses(self, request_iterator, context): - # missing associated documentation comment in .proto file - pass - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details('Method not implemented!') - raise NotImplementedError('Method not implemented!') - - def OneQuestionOneErrorResponse(self, request, context): - """Error Testing - """ - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details('Method not implemented!') - raise NotImplementedError('Method not implemented!') - - def OneErroredQuestionOneResponse(self, request, context): - # missing associated documentation comment in .proto file - pass - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details('Method not implemented!') - raise NotImplementedError('Method not implemented!') + def OneQuestionOneResponse(self, request, context): + """Unary""" + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details("Method not implemented!") + raise NotImplementedError("Method not implemented!") + + def ManyQuestionsOneResponse(self, request_iterator, context): + """Streaming""" + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details("Method not implemented!") + raise NotImplementedError("Method not implemented!") + + def OneQuestionManyResponses(self, request, context): + # missing associated documentation comment in .proto file + pass + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details("Method not implemented!") + raise NotImplementedError("Method not implemented!") + + def ManyQuestionsManyReponses(self, request_iterator, context): + # missing associated documentation comment in .proto file + pass + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details("Method not implemented!") + raise NotImplementedError("Method not implemented!") + + def OneQuestionOneErrorResponse(self, request, context): + """Error Testing""" + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details("Method not implemented!") + raise NotImplementedError("Method not implemented!") + + def OneErroredQuestionOneResponse(self, request, context): + # missing associated documentation comment in .proto file + pass + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details("Method not implemented!") + raise NotImplementedError("Method not implemented!") def add_StanServicer_to_server(servicer, server): - rpc_method_handlers = { - 'OneQuestionOneResponse': grpc.unary_unary_rpc_method_handler( - servicer.OneQuestionOneResponse, - request_deserializer=stan__pb2.QuestionRequest.FromString, - response_serializer=stan__pb2.QuestionResponse.SerializeToString, - ), - 'ManyQuestionsOneResponse': grpc.stream_unary_rpc_method_handler( - servicer.ManyQuestionsOneResponse, - request_deserializer=stan__pb2.QuestionRequest.FromString, - response_serializer=stan__pb2.QuestionResponse.SerializeToString, - ), - 'OneQuestionManyResponses': grpc.unary_stream_rpc_method_handler( - servicer.OneQuestionManyResponses, - request_deserializer=stan__pb2.QuestionRequest.FromString, - response_serializer=stan__pb2.QuestionResponse.SerializeToString, - ), - 'ManyQuestionsManyReponses': grpc.stream_stream_rpc_method_handler( - servicer.ManyQuestionsManyReponses, - request_deserializer=stan__pb2.QuestionRequest.FromString, - response_serializer=stan__pb2.QuestionResponse.SerializeToString, - ), - 'OneQuestionOneErrorResponse': grpc.unary_unary_rpc_method_handler( - servicer.OneQuestionOneErrorResponse, - request_deserializer=stan__pb2.QuestionRequest.FromString, - response_serializer=stan__pb2.QuestionResponse.SerializeToString, - ), - 'OneErroredQuestionOneResponse': grpc.unary_unary_rpc_method_handler( - servicer.OneErroredQuestionOneResponse, - request_deserializer=stan__pb2.QuestionRequest.FromString, - response_serializer=stan__pb2.QuestionResponse.SerializeToString, - ), - } - generic_handler = grpc.method_handlers_generic_handler( - 'stan.Stan', rpc_method_handlers) - server.add_generic_rpc_handlers((generic_handler,)) + rpc_method_handlers = { + "OneQuestionOneResponse": grpc.unary_unary_rpc_method_handler( + servicer.OneQuestionOneResponse, + request_deserializer=stan__pb2.QuestionRequest.FromString, + response_serializer=stan__pb2.QuestionResponse.SerializeToString, + ), + "ManyQuestionsOneResponse": grpc.stream_unary_rpc_method_handler( + servicer.ManyQuestionsOneResponse, + request_deserializer=stan__pb2.QuestionRequest.FromString, + response_serializer=stan__pb2.QuestionResponse.SerializeToString, + ), + "OneQuestionManyResponses": grpc.unary_stream_rpc_method_handler( + servicer.OneQuestionManyResponses, + request_deserializer=stan__pb2.QuestionRequest.FromString, + response_serializer=stan__pb2.QuestionResponse.SerializeToString, + ), + "ManyQuestionsManyReponses": grpc.stream_stream_rpc_method_handler( + servicer.ManyQuestionsManyReponses, + request_deserializer=stan__pb2.QuestionRequest.FromString, + response_serializer=stan__pb2.QuestionResponse.SerializeToString, + ), + "OneQuestionOneErrorResponse": grpc.unary_unary_rpc_method_handler( + servicer.OneQuestionOneErrorResponse, + request_deserializer=stan__pb2.QuestionRequest.FromString, + response_serializer=stan__pb2.QuestionResponse.SerializeToString, + ), + "OneErroredQuestionOneResponse": grpc.unary_unary_rpc_method_handler( + servicer.OneErroredQuestionOneResponse, + request_deserializer=stan__pb2.QuestionRequest.FromString, + response_serializer=stan__pb2.QuestionResponse.SerializeToString, + ), + } + generic_handler = grpc.method_handlers_generic_handler( + "stan.Stan", rpc_method_handlers + ) + server.add_generic_rpc_handlers((generic_handler,)) diff --git a/tests/apps/grpc_server/stan_server.py b/tests/apps/grpc_server/stan_server.py index e69de2a66..d05235f04 100644 --- a/tests/apps/grpc_server/stan_server.py +++ b/tests/apps/grpc_server/stan_server.py @@ -1,7 +1,6 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2019 -import os import sys import grpc import time @@ -24,15 +23,16 @@ class StanServicer(stan_pb2_grpc.StanServicer): """ gRPC server for Stan Service """ + def __init__(self, *args, **kwargs): - self.server_port = testenv['grpc_port'] + self.server_port = testenv["grpc_port"] def OneQuestionOneResponse(self, request, context): # print("😇:I was asked: %s" % request.question) response = """\ Invention, my dear friends, is 93% perspiration, 6% electricity, \ 4% evaporation, and 2% butterscotch ripple. – Willy Wonka""" - result = {'answer': response, 'was_answered': True} + result = {"answer": response, "was_answered": True} return stan_pb2.QuestionResponse(**result) def ManyQuestionsOneResponse(self, request_iterator, context): @@ -40,26 +40,26 @@ def ManyQuestionsOneResponse(self, request_iterator, context): # print("😇:I was asked: %s" % request.question) pass - result = {'answer': 'Ok', 'was_answered': True} + result = {"answer": "Ok", "was_answered": True} return stan_pb2.QuestionResponse(**result) def OneQuestionManyResponses(self, request, context): # print("😇:I was asked: %s" % request.question) for count in range(6): - result = {'answer': 'Ok', 'was_answered': True} + result = {"answer": "Ok", "was_answered": True} yield stan_pb2.QuestionResponse(**result) def ManyQuestionsManyReponses(self, request_iterator, context): for request in request_iterator: # print("😇:I was asked: %s" % request.question) - result = {'answer': 'Ok', 'was_answered': True} + result = {"answer": "Ok", "was_answered": True} yield stan_pb2.QuestionResponse(**result) def OneQuestionOneErrorResponse(self, request, context): - # print("😇:I was asked: %s" % request.question) - raise Exception('Simulated error') - result = {'answer': "ThisError", 'was_answered': True} - return stan_pb2.QuestionResponse(**result) + # print("😇:I was asked: %s" % request.question) + raise Exception("Simulated error") + result = {"answer": "ThisError", "was_answered": True} + return stan_pb2.QuestionResponse(**result) def start_server(self): """ @@ -74,7 +74,7 @@ def start_server(self): stan_pb2_grpc.add_StanServicer_to_server(StanServicer(), rpc_server) # bind the server to the port defined above - rpc_server.add_insecure_port('[::]:{}'.format(self.server_port)) + rpc_server.add_insecure_port("[::]:{}".format(self.server_port)) # start the server rpc_server.start() @@ -84,14 +84,14 @@ def start_server(self): # code is non blocking, and if I don't do this # the program will exit while True: - time.sleep(60*60*60) + time.sleep(60 * 60 * 60) except KeyboardInterrupt: rpc_server.stop(0) - print('Stan as a Service RPC Server Stopped ...') + print("Stan as a Service RPC Server Stopped ...") if __name__ == "__main__": - print ("Booting foreground GRPC application...") + print("Booting foreground GRPC application...") if sys.version_info >= (3, 5, 3): StanServicer().start_server() diff --git a/tests/apps/pubsub_app/pubsub.py b/tests/apps/pubsub_app/pubsub.py index e3d86a152..8352032a2 100644 --- a/tests/apps/pubsub_app/pubsub.py +++ b/tests/apps/pubsub_app/pubsub.py @@ -6,7 +6,6 @@ import logging -import instana from flask import Flask, request from google.cloud import pubsub_v1 @@ -22,9 +21,9 @@ # Use PubSub Emulator exposed at :8432 for local testing and uncomment below # os.environ["PUBSUB_EMULATOR_HOST"] = "localhost:8432" -PROJECT_ID = 'k8s-brewery' -TOPIC_NAME = 'python-test-topic' -SUBSCRIPTION_ID = 'python-test-subscription' +PROJECT_ID = "k8s-brewery" +TOPIC_NAME = "python-test-topic" +SUBSCRIPTION_ID = "python-test-subscription" publisher = pubsub_v1.PublisherClient() subscriber = pubsub_v1.SubscriberClient() @@ -33,17 +32,17 @@ SUBSCRIPTION_PATH = subscriber.subscription_path(PROJECT_ID, SUBSCRIPTION_ID) -@app.route('/') +@app.route("/") def home(): return "Welcome to PubSub testing." -@app.route('/create') +@app.route("/create") def create_topic(): """ Usage: /create?topic= """ - topic = request.args.get('topic') + topic = request.args.get("topic") print(topic, type(topic)) try: @@ -53,17 +52,17 @@ def create_topic(): return "Topic Creation Failed: %s" % e -@app.route('/publish') +@app.route("/publish") def publish(): """ Usage: /publish?message= """ - msg = request.args.get('message').encode('utf-8') - publisher.publish(TOPIC_PATH, msg, origin='instana-test') + msg = request.args.get("message").encode("utf-8") + publisher.publish(TOPIC_PATH, msg, origin="instana-test") return "Published msg: %s" % msg -@app.route('/consume') +@app.route("/consume") def consume(): """ Usage: /consume @@ -72,7 +71,7 @@ def consume(): # Async def callback_handler(message): - print('MESSAGE: ', message, type(message)) + print("MESSAGE: ", message, type(message)) print(message.data) message.ack() @@ -80,11 +79,11 @@ def callback_handler(message): try: res = future.result() - print('CALLBACK: ', res, type(res)) + print("CALLBACK: ", res, type(res)) except KeyboardInterrupt: future.cancel() return "Consumer closed." -if __name__ == '__main__': - app.run(host='127.0.0.1', port='10811') +if __name__ == "__main__": + app.run(host="127.0.0.1", port="10811") diff --git a/tests/apps/sanic_app/server.py b/tests/apps/sanic_app/server.py index a07dafc9e..27513cadb 100644 --- a/tests/apps/sanic_app/server.py +++ b/tests/apps/sanic_app/server.py @@ -1,7 +1,6 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2021 -import instana from sanic import Sanic from sanic.exceptions import SanicException diff --git a/tests/apps/starlette_app/__init__.py b/tests/apps/starlette_app/__init__.py index 6b46de1c3..91228754e 100644 --- a/tests/apps/starlette_app/__init__.py +++ b/tests/apps/starlette_app/__init__.py @@ -7,8 +7,9 @@ testenv["starlette_host"] = "127.0.0.1" testenv["starlette_port"] = 10817 -testenv["starlette_server"] = "http://" + testenv["starlette_host"] + ":" + str(testenv["starlette_port"]) - +testenv["starlette_server"] = ( + "http://" + testenv["starlette_host"] + ":" + str(testenv["starlette_port"]) +) def launch_starlette(): @@ -17,7 +18,7 @@ def launch_starlette(): # Hack together a manual custom headers list; We'll use this in tests agent.options.extra_http_headers = [ - "X-Capture-This", + "X-Capture-This", "X-Capture-That", ] diff --git a/tests/apps/tornado_server/__init__.py b/tests/apps/tornado_server/__init__.py index 20a273613..7c02587d0 100644 --- a/tests/apps/tornado_server/__init__.py +++ b/tests/apps/tornado_server/__init__.py @@ -8,12 +8,17 @@ app_thread = None -if not any((app_thread, os.environ.get('GEVENT_STARLETTE_TEST'), os.environ.get('CASSANDRA_TEST'))): +if not any( + ( + app_thread, + os.environ.get("GEVENT_STARLETTE_TEST"), + os.environ.get("CASSANDRA_TEST"), + ) +): testenv["tornado_port"] = 10813 - testenv["tornado_server"] = ("http://127.0.0.1:" + str(testenv["tornado_port"])) + 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_server/app.py b/tests/apps/tornado_server/app.py index cf71c677e..803dc97d6 100755 --- a/tests/apps/tornado_server/app.py +++ b/tests/apps/tornado_server/app.py @@ -65,15 +65,14 @@ def get(self): class R504Handler(tornado.web.RequestHandler): def get(self): - raise tornado.web.HTTPError(status_code=504, log_message="Simulated Internal Server Errors") + raise tornado.web.HTTPError( + status_code=504, log_message="Simulated Internal Server Errors" + ) class ResponseHeadersHandler(tornado.web.RequestHandler): def get(self): - headers = { - 'X-Capture-This-Too': 'this too', - 'X-Capture-That-Too': 'that too' - } + headers = {"X-Capture-This-Too": "this too", "X-Capture-That-Too": "that too"} for key, value in headers.items(): self.set_header(key, value) self.write("Stan wuz here with headers!") diff --git a/tests/apps/utils.py b/tests/apps/utils.py index 25e64ce2b..2a54a7cbb 100644 --- a/tests/apps/utils.py +++ b/tests/apps/utils.py @@ -6,10 +6,9 @@ def launch_background_thread(app, app_name, fun_args=(), fun_kwargs={}): print("Starting background %s app..." % app_name) - app_thread = threading.Thread(target=app, - name=app_name, - args=fun_args, - kwargs=fun_kwargs) + app_thread = threading.Thread( + target=app, name=app_name, args=fun_args, kwargs=fun_kwargs + ) app_thread.daemon = True app_thread.start() return app_thread diff --git a/tests/autoprofile/samplers/test_allocation_sampler.py b/tests/autoprofile/samplers/test_allocation_sampler.py index efa66bea1..219d7878c 100644 --- a/tests/autoprofile/samplers/test_allocation_sampler.py +++ b/tests/autoprofile/samplers/test_allocation_sampler.py @@ -69,4 +69,3 @@ def record() -> None: profile = sampler.build_profile(2000, 120000).to_dict() assert "test_allocation_sampler.py" in str(profile) - diff --git a/tests/autoprofile/samplers/test_cpu_sampler.py b/tests/autoprofile/samplers/test_cpu_sampler.py index f0581d12e..e398ff094 100644 --- a/tests/autoprofile/samplers/test_cpu_sampler.py +++ b/tests/autoprofile/samplers/test_cpu_sampler.py @@ -24,7 +24,6 @@ def _resources(self) -> Generator[None, None, None]: # teardown self.profiler.destroy() - def test_cpu_profile(self) -> None: if RuntimeInfo.OS_WIN: return @@ -52,4 +51,4 @@ def cpu_work_main_thread() -> None: profile = sampler.build_profile(2000, 120000).to_dict() - assert 'cpu_work_main_thread' in str(profile) + assert "cpu_work_main_thread" in str(profile) diff --git a/tests/clients/boto3/test_boto3_lambda.py b/tests/clients/boto3/test_boto3_lambda.py index 781178044..1637ad398 100644 --- a/tests/clients/boto3/test_boto3_lambda.py +++ b/tests/clients/boto3/test_boto3_lambda.py @@ -43,11 +43,15 @@ def test_lambda_invoke(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -128,11 +132,15 @@ def add_custom_header_before_call(params, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -192,11 +200,15 @@ def add_custom_header_before_sign(request, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -256,11 +268,15 @@ def modify_after_call_args(parsed, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span diff --git a/tests/clients/boto3/test_boto3_s3.py b/tests/clients/boto3/test_boto3_s3.py index 6410a6ea7..ecbe7c91d 100644 --- a/tests/clients/boto3/test_boto3_s3.py +++ b/tests/clients/boto3/test_boto3_s3.py @@ -50,11 +50,15 @@ def test_s3_create_bucket(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -111,11 +115,15 @@ def test_s3_list_buckets(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -155,11 +163,15 @@ def test_s3_upload_file(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -196,11 +208,15 @@ def test_s3_upload_file_obj(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -234,11 +250,15 @@ def test_s3_download_file(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -277,11 +297,15 @@ def test_s3_download_file_obj(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -328,11 +352,15 @@ def add_custom_header_before_call(params, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -388,11 +416,15 @@ def add_custom_header_before_sign(request, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -448,11 +480,15 @@ def modify_after_call_args(parsed, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span diff --git a/tests/clients/boto3/test_boto3_secretsmanager.py b/tests/clients/boto3/test_boto3_secretsmanager.py index e8a715fc6..fd274de26 100644 --- a/tests/clients/boto3/test_boto3_secretsmanager.py +++ b/tests/clients/boto3/test_boto3_secretsmanager.py @@ -51,11 +51,15 @@ def test_get_secret_value(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -155,11 +159,15 @@ def add_custom_header_before_call(params, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -227,11 +235,15 @@ def add_custom_header_before_sign(request, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -301,11 +313,15 @@ def modify_after_call_args(parsed, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span diff --git a/tests/clients/boto3/test_boto3_ses.py b/tests/clients/boto3/test_boto3_ses.py index afea6b0ee..651fec0e1 100644 --- a/tests/clients/boto3/test_boto3_ses.py +++ b/tests/clients/boto3/test_boto3_ses.py @@ -44,11 +44,15 @@ def test_verify_email(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -129,11 +133,15 @@ def add_custom_header_before_call(params, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -192,11 +200,15 @@ def add_custom_header_before_sign(request, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -257,11 +269,15 @@ def modify_after_call_args(parsed, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span diff --git a/tests/clients/boto3/test_boto3_sqs.py b/tests/clients/boto3/test_boto3_sqs.py index e7755b1b4..f7eeebd21 100644 --- a/tests/clients/boto3/test_boto3_sqs.py +++ b/tests/clients/boto3/test_boto3_sqs.py @@ -9,7 +9,6 @@ from moto import mock_aws -import tests.apps.flask_app from instana.singletons import tracer, agent from tests.helpers import get_first_span_by_filter, testenv @@ -70,11 +69,15 @@ def test_send_message(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -174,27 +177,33 @@ def test_app_boto3_sqs(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 5 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "urllib3" + def filter(span): + return span.n == "urllib3" + http_span = get_first_span_by_filter(spans, filter) assert http_span - filter = lambda span: span.n == "wsgi" + def filter(span): + return span.n == "wsgi" + wsgi_span = get_first_span_by_filter(spans, filter) assert wsgi_span - filter = ( - lambda span: span.n == "boto3" and span.data["boto3"]["op"] == "CreateQueue" - ) + def filter(span): + return span.n == "boto3" and span.data["boto3"]["op"] == "CreateQueue" + bcq_span = get_first_span_by_filter(spans, filter) assert bcq_span - filter = ( - lambda span: span.n == "boto3" and span.data["boto3"]["op"] == "SendMessage" - ) + def filter(span): + return span.n == "boto3" and span.data["boto3"]["op"] == "SendMessage" + bsm_span = get_first_span_by_filter(spans, filter) assert bsm_span @@ -258,11 +267,15 @@ def add_custom_header_before_call(params, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -351,11 +364,15 @@ def add_custom_header_before_sign(request, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span @@ -444,11 +461,15 @@ def modify_after_call_args(parsed, **kwargs): spans = self.recorder.queued_spans() assert len(spans) == 2 - filter = lambda span: span.n == "sdk" + def filter(span): + return span.n == "sdk" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "boto3" + def filter(span): + return span.n == "boto3" + boto_span = get_first_span_by_filter(spans, filter) assert boto_span diff --git a/tests/clients/test_couchbase.py b/tests/clients/test_couchbase.py index 9064fb06b..1fba8113e 100644 --- a/tests/clients/test_couchbase.py +++ b/tests/clients/test_couchbase.py @@ -1,7 +1,6 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2020 -import os import time from typing import Generator from unittest.mock import patch diff --git a/tests/clients/test_logging.py b/tests/clients/test_logging.py index 6ec666c53..960e36113 100644 --- a/tests/clients/test_logging.py +++ b/tests/clients/test_logging.py @@ -69,7 +69,7 @@ def test_parameters(self) -> None: try: a = 42 b = 0 - c = a / b + _ = a / b except Exception as e: self.logger.exception("Exception: %s", str(e)) diff --git a/tests/clients/test_pymysql.py b/tests/clients/test_pymysql.py index 8e4793d51..bcc7280bd 100644 --- a/tests/clients/test_pymysql.py +++ b/tests/clients/test_pymysql.py @@ -1,7 +1,6 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2020 -import time import pytest import pymysql diff --git a/tests/clients/test_urllib3.py b/tests/clients/test_urllib3.py index 77b49eceb..eecb338d9 100644 --- a/tests/clients/test_urllib3.py +++ b/tests/clients/test_urllib3.py @@ -82,7 +82,7 @@ def make_request(u=None) -> int: threadpool_size = 15 pool = ThreadPool(processes=threadpool_size) - res = pool.map(make_request, [u for u in range(threadpool_size)]) + _ = pool.map(make_request, [u for u in range(threadpool_size)]) # print(f'requests made within threadpool, instana does not instrument - statuses: {res}') spans = self.recorder.queued_spans() @@ -631,7 +631,9 @@ def test_exception_logging(self): assert test_span.data["sdk"]["name"] == "test" assert urllib3_span.n == "urllib3" assert urllib3_span.data["http"]["status"] == 500 - assert urllib3_span.data["http"]["url"] == testenv["flask_server"] + "/exception" + assert ( + urllib3_span.data["http"]["url"] == testenv["flask_server"] + "/exception" + ) assert urllib3_span.data["http"]["method"] == "GET" assert urllib3_span.stack assert isinstance(urllib3_span.stack, list) diff --git a/tests/frameworks/test_aiohttp_client.py b/tests/frameworks/test_aiohttp_client.py index f1231fa8d..e40158c8b 100644 --- a/tests/frameworks/test_aiohttp_client.py +++ b/tests/frameworks/test_aiohttp_client.py @@ -408,7 +408,10 @@ async def test(): assert aiohttp_span.n == "aiohttp-client" assert aiohttp_span.data["http"]["status"] == 200 - assert aiohttp_span.data["http"]["url"] == testenv["flask_server"] + "/response_headers" + assert ( + aiohttp_span.data["http"]["url"] + == testenv["flask_server"] + "/response_headers" + ) assert aiohttp_span.data["http"]["method"] == "GET" assert aiohttp_span.stack assert isinstance(aiohttp_span.stack, list) @@ -437,7 +440,7 @@ async def test(): response = None try: response = self.loop.run_until_complete(test()) - except: + except Exception: pass spans = self.recorder.queued_spans() diff --git a/tests/frameworks/test_django.py b/tests/frameworks/test_django.py index f79642a61..e0f50e3ae 100644 --- a/tests/frameworks/test_django.py +++ b/tests/frameworks/test_django.py @@ -126,15 +126,21 @@ def test_request_with_error(self) -> None: msg = "Expected 3 spans but got %d" % span_count fail_with_message_and_span_dump(msg, spans) - filter = lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" + def filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, filter) assert test_span - filter = lambda span: span.n == "urllib3" + def filter(span): + return span.n == "urllib3" + urllib3_span = get_first_span_by_filter(spans, filter) assert urllib3_span - filter = lambda span: span.n == "django" + def filter(span): + return span.n == "django" + django_span = get_first_span_by_filter(spans, filter) assert django_span @@ -187,7 +193,9 @@ def test_request_with_not_found(self) -> None: msg = "Expected 3 spans but got %d" % span_count fail_with_message_and_span_dump(msg, spans) - filter = lambda span: span.n == "django" + def filter(span): + return span.n == "django" + django_span = get_first_span_by_filter(spans, filter) assert django_span @@ -209,7 +217,9 @@ def test_request_with_not_found_no_route(self) -> None: msg = "Expected 3 spans but got %d" % span_count fail_with_message_and_span_dump(msg, spans) - filter = lambda span: span.n == "django" + def filter(span): + return span.n == "django" + django_span = get_first_span_by_filter(spans, filter) assert django_span assert django_span.data["http"]["path_tpl"] is None diff --git a/tests/frameworks/test_fastapi.py b/tests/frameworks/test_fastapi.py index 7e82df228..50ca64805 100644 --- a/tests/frameworks/test_fastapi.py +++ b/tests/frameworks/test_fastapi.py @@ -85,7 +85,7 @@ def test_basic_get(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" @@ -136,7 +136,7 @@ def test_400(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" @@ -187,7 +187,7 @@ def test_500(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" @@ -237,7 +237,7 @@ def test_path_templates(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" @@ -287,7 +287,7 @@ def test_secret_scrubbing(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" @@ -338,7 +338,7 @@ def test_synthetic_request(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" @@ -364,7 +364,7 @@ def test_request_header_capture(self) -> None: headers = { "X-INSTANA-T": str(span_context.trace_id), "X-INSTANA-S": str(span_context.span_id), - "X-Capture-This": "this", + "X-Capture-This": "this", "X-Capture-That": "that", } result = self.client.get("/", headers=headers) @@ -393,9 +393,9 @@ def test_request_header_capture(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) - assert result.headers["X-INSTANA-S"] == str(asgi_span.s) + assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" assert not asgi_span.ec @@ -414,7 +414,7 @@ def test_request_header_capture(self) -> None: assert asgi_span.data["http"]["header"]["X-Capture-That"] == "that" def test_response_header_capture(self) -> None: - # The background FastAPI server is pre-configured with custom headers + # The background FastAPI server is pre-configured with custom headers # to capture. with tracer.start_as_current_span("test") as span: @@ -451,7 +451,7 @@ def test_response_header_capture(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" @@ -569,7 +569,7 @@ def test_non_async_threadpool(self) -> None: assert test_span.t == asgi_span.t assert test_span.s == asgi_span.p - + assert result.headers["X-INSTANA-T"] == str(asgi_span.t) assert result.headers["X-INSTANA-S"] == str(asgi_span.s) assert result.headers["Server-Timing"] == f"intid;desc={asgi_span.t}" diff --git a/tests/frameworks/test_fastapi_middleware.py b/tests/frameworks/test_fastapi_middleware.py index 5c915f255..3de9537a0 100644 --- a/tests/frameworks/test_fastapi_middleware.py +++ b/tests/frameworks/test_fastapi_middleware.py @@ -1,7 +1,6 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2020 -import logging from typing import Generator import pytest @@ -22,6 +21,7 @@ def _resource(self) -> Generator[None, None, None]: # setup # We are using the TestClient from FastAPI to make it easier. from tests.apps.fastapi_app.app2 import fastapi_server + self.client = TestClient(fastapi_server) # Clear all spans before a test run. self.recorder = tracer.span_processor diff --git a/tests/frameworks/test_flask.py b/tests/frameworks/test_flask.py index fdbf49192..8c1f8e29b 100644 --- a/tests/frameworks/test_flask.py +++ b/tests/frameworks/test_flask.py @@ -6,7 +6,7 @@ import flask from unittest.mock import patch -if hasattr(flask.signals, 'signals_available'): +if hasattr(flask.signals, "signals_available"): from flask.signals import signals_available else: # Beginning from 2.3.0 as stated in the notes @@ -17,26 +17,24 @@ from opentelemetry.trace import SpanKind -import tests.apps.flask_app from instana.singletons import tracer from instana.span.span import get_current_span from tests.helpers import testenv class TestFlask(unittest.TestCase): - def setUp(self) -> None: - """ Clear all spans before a test run """ + """Clear all spans before a test run""" self.http = urllib3.PoolManager() self.recorder = tracer.span_processor self.recorder.clear_spans() def tearDown(self) -> None: - """ Do nothing for now """ + """Do nothing for now""" return None def test_vanilla_requests(self) -> None: - r = self.http.request('GET', testenv["flask_server"] + '/') + r = self.http.request("GET", testenv["flask_server"] + "/") assert r.status == 200 spans = self.recorder.queued_spans() @@ -192,8 +190,10 @@ def test_get_request_with_query_params(self) -> None: assert wsgi_span.data["http"]["path_tpl"] is None def test_get_request_with_suppression(self) -> None: - headers = {'X-INSTANA-L':'0'} - response = self.http.urlopen('GET', testenv["flask_server"] + '/', headers=headers) + headers = {"X-INSTANA-L": "0"} + response = self.http.urlopen( + "GET", testenv["flask_server"] + "/", headers=headers + ) spans = self.recorder.queued_spans() @@ -212,11 +212,14 @@ def test_get_request_with_suppression(self) -> None: @unittest.skip("Handled when type of trace and span ids are modified to str") def test_get_request_with_suppression_and_w3c(self) -> None: headers = { - 'X-INSTANA-L':'0', - 'traceparent': '00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01', - 'tracestate': 'congo=ucfJifl5GOE,rojo=00f067aa0ba902b7'} + "X-INSTANA-L": "0", + "traceparent": "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01", + "tracestate": "congo=ucfJifl5GOE,rojo=00f067aa0ba902b7", + } - response = self.http.urlopen('GET', testenv["flask_server"] + '/', headers=headers) + response = self.http.urlopen( + "GET", testenv["flask_server"] + "/", headers=headers + ) spans = self.recorder.queued_spans() @@ -233,12 +236,10 @@ def test_get_request_with_suppression_and_w3c(self) -> None: assert spans == [] def test_synthetic_request(self) -> None: - headers = { - 'X-INSTANA-SYNTHETIC': '1' - } + headers = {"X-INSTANA-SYNTHETIC": "1"} with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/', headers=headers) + _ = self.http.request("GET", testenv["flask_server"] + "/", headers=headers) spans = self.recorder.queued_spans() assert len(spans) == 3 @@ -253,7 +254,7 @@ def test_synthetic_request(self) -> None: def test_render_template(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/render') + response = self.http.request("GET", testenv["flask_server"] + "/render") spans = self.recorder.queued_spans() assert len(spans) == 4 @@ -333,7 +334,9 @@ def test_render_template(self) -> None: def test_render_template_string(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/render_string') + response = self.http.request( + "GET", testenv["flask_server"] + "/render_string" + ) spans = self.recorder.queued_spans() assert len(spans) == 4 @@ -416,7 +419,9 @@ def test_render_template_string(self) -> None: def test_301(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/301', redirect=False) + response = self.http.request( + "GET", testenv["flask_server"] + "/301", redirect=False + ) spans = self.recorder.queued_spans() @@ -456,8 +461,8 @@ def test_301(self) -> None: # Error logging assert test_span.ec is None - assert None == urllib3_span.ec - assert None == wsgi_span.ec + assert not urllib3_span.ec + assert not wsgi_span.ec # wsgi assert "wsgi" == wsgi_span.n @@ -485,7 +490,7 @@ def test_301(self) -> None: def test_custom_404(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/custom-404') + response = self.http.request("GET", testenv["flask_server"] + "/custom-404") spans = self.recorder.queued_spans() @@ -525,8 +530,8 @@ def test_custom_404(self) -> None: # Error logging assert test_span.ec is None - assert None == urllib3_span.ec - assert None == wsgi_span.ec + assert not urllib3_span.ec + assert not wsgi_span.ec # wsgi assert "wsgi" == wsgi_span.n @@ -556,7 +561,9 @@ def test_custom_404(self) -> None: def test_404(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/11111111111') + response = self.http.request( + "GET", testenv["flask_server"] + "/11111111111" + ) spans = self.recorder.queued_spans() @@ -596,8 +603,8 @@ def test_404(self) -> None: # Error logging assert test_span.ec is None - assert None == urllib3_span.ec - assert None == wsgi_span.ec + assert not urllib3_span.ec + assert not wsgi_span.ec # wsgi assert "wsgi" == wsgi_span.n @@ -627,7 +634,7 @@ def test_404(self) -> None: def test_500(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/500') + response = self.http.request("GET", testenv["flask_server"] + "/500") spans = self.recorder.queued_spans() @@ -699,7 +706,9 @@ def test_render_error(self) -> None: raise unittest.SkipTest("Exceptions without handlers vary with blinker") with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/render_error') + response = self.http.request( + "GET", testenv["flask_server"] + "/render_error" + ) spans = self.recorder.queued_spans() @@ -767,7 +776,8 @@ def test_render_error(self) -> None: assert "urllib3" == urllib3_span.n assert 500 == urllib3_span.data["http"]["status"] assert ( - testenv["flask_server"] + "/render_error" == urllib3_span.data["http"]["url"] + testenv["flask_server"] + "/render_error" + == urllib3_span.data["http"]["url"] ) assert "GET" == urllib3_span.data["http"]["method"] assert urllib3_span.stack is not None @@ -782,7 +792,7 @@ def test_exception(self) -> None: raise unittest.SkipTest("Exceptions without handlers vary with blinker") with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/exception') + response = self.http.request("GET", testenv["flask_server"] + "/exception") spans = self.recorder.queued_spans() @@ -833,7 +843,9 @@ def test_exception(self) -> None: assert "test" == test_span.data["sdk"]["name"] assert "urllib3" == urllib3_span.n assert 500 == urllib3_span.data["http"]["status"] - assert testenv["flask_server"] + "/exception" == urllib3_span.data["http"]["url"] + assert ( + testenv["flask_server"] + "/exception" == urllib3_span.data["http"]["url"] + ) assert "GET" == urllib3_span.data["http"]["method"] assert urllib3_span.stack is not None assert type(urllib3_span.stack) is list @@ -844,7 +856,9 @@ def test_exception(self) -> None: def test_custom_exception_with_log(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/exception-invalid-usage') + response = self.http.request( + "GET", testenv["flask_server"] + "/exception-invalid-usage" + ) spans = self.recorder.queued_spans() @@ -926,7 +940,9 @@ def test_custom_exception_with_log(self) -> None: def test_path_templates(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/users/Ricky/sayhello') + response = self.http.request( + "GET", testenv["flask_server"] + "/users/Ricky/sayhello" + ) spans = self.recorder.queued_spans() assert len(spans) == 3 @@ -998,11 +1014,14 @@ def test_path_templates(self) -> None: def test_response_header_capture(self) -> None: # Hack together a manual custom headers list from instana.singletons import agent + original_extra_http_headers = agent.options.extra_http_headers - agent.options.extra_http_headers = [u'X-Capture-This', u'X-Capture-That'] + agent.options.extra_http_headers = ["X-Capture-This", "X-Capture-That"] with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["flask_server"] + '/response_headers') + response = self.http.request( + "GET", testenv["flask_server"] + "/response_headers" + ) spans = self.recorder.queued_spans() assert len(spans) == 3 diff --git a/tests/frameworks/test_gevent.py b/tests/frameworks/test_gevent.py index 69a9a6c8c..afb926446 100644 --- a/tests/frameworks/test_gevent.py +++ b/tests/frameworks/test_gevent.py @@ -9,7 +9,6 @@ import urllib3 from opentracing.scope_managers.gevent import GeventScopeManager -import tests.apps.flask_app from instana.span import SDKSpan from instana.singletons import tracer from ..helpers import testenv, get_spans_by_filter @@ -18,20 +17,22 @@ @unittest.skipIf(not os.environ.get("GEVENT_STARLETTE_TEST"), reason="") class TestGEvent(unittest.TestCase): def setUp(self): - self.http = urllib3.HTTPConnectionPool('127.0.0.1', port=testenv["flask_port"], maxsize=20) + self.http = urllib3.HTTPConnectionPool( + "127.0.0.1", port=testenv["flask_port"], maxsize=20 + ) self.recorder = tracer.recorder self.recorder.clear_spans() tracer._scope_manager = GeventScopeManager() def tearDown(self): - """ Do nothing for now """ + """Do nothing for now""" pass def make_http_call(self, n=None): - return self.http.request('GET', testenv["flask_server"] + '/') + return self.http.request("GET", testenv["flask_server"] + "/") def spawn_calls(self): - with tracer.start_active_span('spawn_calls'): + with tracer.start_active_span("spawn_calls"): jobs = [] jobs.append(gevent.spawn(self.make_http_call)) jobs.append(gevent.spawn(self.make_http_call)) @@ -41,12 +42,12 @@ def spawn_calls(self): def spawn_imap_unordered(self): igroup = Group() result = [] - with tracer.start_active_span('test'): + with tracer.start_active_span("test"): for i in igroup.imap_unordered(self.make_http_call, range(3)): result.append(i) def launch_gevent_chain(self): - with tracer.start_active_span('test'): + with tracer.start_active_span("test"): gevent.spawn(self.spawn_calls).join() def test_spawning(self): @@ -58,8 +59,9 @@ def test_spawning(self): self.assertEqual(8, len(spans)) - span_filter = lambda span: span.n == "sdk" \ - and span.data['sdk']['name'] == 'test' and span.p == None + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" and not span.p + test_spans = get_spans_by_filter(spans, span_filter) self.assertIsNotNone(test_spans) self.assertEqual(len(test_spans), 1) @@ -67,8 +69,13 @@ def test_spawning(self): test_span = test_spans[0] self.assertTrue(type(test_spans[0]) is SDKSpan) - span_filter = lambda span: span.n == "sdk" \ - and span.data['sdk']['name'] == 'spawn_calls' and span.p == test_span.s + def span_filter(span): + return ( + span.n == "sdk" + and span.data["sdk"]["name"] == "spawn_calls" + and span.p == test_span.s + ) + spawn_spans = get_spans_by_filter(spans, span_filter) self.assertIsNotNone(spawn_spans) self.assertEqual(len(spawn_spans), 1) @@ -76,7 +83,9 @@ def test_spawning(self): spawn_span = spawn_spans[0] self.assertTrue(type(spawn_spans[0]) is SDKSpan) - span_filter = lambda span: span.n == "urllib3" + def span_filter(span): + return span.n == "urllib3" + urllib3_spans = get_spans_by_filter(spans, span_filter) for urllib3_span in urllib3_spans: @@ -85,7 +94,9 @@ def test_spawning(self): self.assertEqual(urllib3_span.p, spawn_span.s) # find the wsgi span generated from this urllib3 request - span_filter = lambda span: span.n == "wsgi" and span.p == urllib3_span.s + def span_filter(span): + return span.n == "wsgi" and span.p == urllib3_span.s + wsgi_spans = get_spans_by_filter(spans, span_filter) self.assertIsNotNone(wsgi_spans) self.assertEqual(len(wsgi_spans), 1) @@ -98,8 +109,9 @@ def test_imap_unordered(self): spans = self.recorder.queued_spans() self.assertEqual(7, len(spans)) - span_filter = lambda span: span.n == "sdk" \ - and span.data['sdk']['name'] == 'test' and span.p == None + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" and not span.p + test_spans = get_spans_by_filter(spans, span_filter) self.assertIsNotNone(test_spans) self.assertEqual(len(test_spans), 1) @@ -107,7 +119,9 @@ def test_imap_unordered(self): test_span = test_spans[0] self.assertTrue(type(test_spans[0]) is SDKSpan) - span_filter = lambda span: span.n == "urllib3" + def span_filter(span): + return span.n == "urllib3" + urllib3_spans = get_spans_by_filter(spans, span_filter) self.assertEqual(len(urllib3_spans), 3) @@ -117,8 +131,9 @@ def test_imap_unordered(self): self.assertEqual(urllib3_span.p, test_span.s) # find the wsgi span generated from this urllib3 request - span_filter = lambda span: span.n == "wsgi" and span.p == urllib3_span.s + def span_filter(span): + return span.n == "wsgi" and span.p == urllib3_span.s + wsgi_spans = get_spans_by_filter(spans, span_filter) self.assertIsNotNone(wsgi_spans) self.assertEqual(len(wsgi_spans), 1) - diff --git a/tests/frameworks/test_gevent_autotrace.py b/tests/frameworks/test_gevent_autotrace.py index 41bf5f03d..e926576f7 100644 --- a/tests/frameworks/test_gevent_autotrace.py +++ b/tests/frameworks/test_gevent_autotrace.py @@ -4,9 +4,7 @@ import importlib import os import unittest -import socket -import gevent from gevent import monkey from instana import apply_gevent_monkey_patch @@ -15,14 +13,23 @@ class TestGEventAutoTrace(unittest.TestCase): def setUp(self): # Ensure that the test suite is operational even when Django is installed # but not running or configured - os.environ['DJANGO_SETTINGS_MODULE'] = '' - - self.default_patched_modules = ('socket', 'time', 'select', 'os', - 'threading', 'ssl', 'subprocess', 'signal', 'queue',) + os.environ["DJANGO_SETTINGS_MODULE"] = "" + + self.default_patched_modules = ( + "socket", + "time", + "select", + "os", + "threading", + "ssl", + "subprocess", + "signal", + "queue", + ) def tearDown(self): - if os.environ.get('INSTANA_GEVENT_MONKEY_OPTIONS'): - os.environ.pop('INSTANA_GEVENT_MONKEY_OPTIONS') + if os.environ.get("INSTANA_GEVENT_MONKEY_OPTIONS"): + os.environ.pop("INSTANA_GEVENT_MONKEY_OPTIONS") # Clean up after gevent monkey patches, by restore from the saved dict for modname in monkey.saved.keys(): @@ -35,37 +42,48 @@ def tearDown(self): pass monkey.saved = {} - def test_default_patch_all(self): apply_gevent_monkey_patch() for module_name in self.default_patched_modules: - self.assertTrue(monkey.is_module_patched(module_name), - f"{module_name} is not patched") + self.assertTrue( + monkey.is_module_patched(module_name), f"{module_name} is not patched" + ) def test_instana_monkey_options_only_time(self): - os.environ['INSTANA_GEVENT_MONKEY_OPTIONS'] = ( - 'time,no-socket,no-select,no-os,no-select,no-threading,no-os,' - 'no-ssl,no-subprocess,''no-signal,no-queue') + os.environ["INSTANA_GEVENT_MONKEY_OPTIONS"] = ( + "time,no-socket,no-select,no-os,no-select,no-threading,no-os," + "no-ssl,no-subprocess," + "no-signal,no-queue" + ) apply_gevent_monkey_patch() - self.assertTrue(monkey.is_module_patched('time'), "time module is not patched") - not_patched_modules = (m for m in self.default_patched_modules if m not in ('time', 'threading')) + self.assertTrue(monkey.is_module_patched("time"), "time module is not patched") + not_patched_modules = ( + m for m in self.default_patched_modules if m not in ("time", "threading") + ) for module_name in not_patched_modules: - self.assertFalse(monkey.is_module_patched(module_name), - f"{module_name} is patched, when it shouldn't be") - + self.assertFalse( + monkey.is_module_patched(module_name), + f"{module_name} is patched, when it shouldn't be", + ) def test_instana_monkey_options_only_socket(self): - os.environ['INSTANA_GEVENT_MONKEY_OPTIONS'] = ( - '--socket, --no-time, --no-select, --no-os, --no-queue, --no-threading,' - '--no-os, --no-ssl, no-subprocess, --no-signal, --no-select,') + os.environ["INSTANA_GEVENT_MONKEY_OPTIONS"] = ( + "--socket, --no-time, --no-select, --no-os, --no-queue, --no-threading," + "--no-os, --no-ssl, no-subprocess, --no-signal, --no-select," + ) apply_gevent_monkey_patch() - self.assertTrue(monkey.is_module_patched('socket'), "socket module is not patched") - not_patched_modules = (m for m in self.default_patched_modules if m not in ('socket', 'threading')) + self.assertTrue( + monkey.is_module_patched("socket"), "socket module is not patched" + ) + not_patched_modules = ( + m for m in self.default_patched_modules if m not in ("socket", "threading") + ) for module_name in not_patched_modules: - self.assertFalse(monkey.is_module_patched(module_name), - f"{module_name} is patched, when it shouldn't be") - + self.assertFalse( + monkey.is_module_patched(module_name), + f"{module_name} is patched, when it shouldn't be", + ) diff --git a/tests/frameworks/test_grpcio.py b/tests/frameworks/test_grpcio.py index 99081883a..2a4eed97f 100644 --- a/tests/frameworks/test_grpcio.py +++ b/tests/frameworks/test_grpcio.py @@ -10,7 +10,6 @@ from opentelemetry.trace import SpanKind -import tests.apps.grpc_server import tests.apps.grpc_server.stan_pb2 as stan_pb2 import tests.apps.grpc_server.stan_pb2_grpc as stan_pb2_grpc from tests.helpers import testenv, get_first_span_by_name @@ -329,7 +328,7 @@ def test_unary_one_to_one_with_call(self) -> None: assert not get_current_span().is_recording() assert response - assert type(response) == tuple + assert isinstance(response, tuple) assert ( response[0].answer == "Invention, my dear friends, is 93% perspiration, 6% electricity, 4% evaporation, and 2% butterscotch ripple. – Willy Wonka" @@ -448,7 +447,7 @@ def test_streaming_many_to_one_with_call(self) -> None: def test_async_unary(self) -> None: def process_response(future): result = future.result() - assert type(result) == stan_pb2.QuestionResponse + assert isinstance(result, stan_pb2.QuestionResponse) assert result.was_answered assert ( result.answer @@ -515,7 +514,7 @@ def process_response(future): def test_async_stream(self) -> None: def process_response(future): result = future.result() - assert type(result) == stan_pb2.QuestionResponse + assert isinstance(result, stan_pb2.QuestionResponse) assert result.was_answered assert result.answer == "Ok" diff --git a/tests/frameworks/test_pyramid.py b/tests/frameworks/test_pyramid.py index 6aa39ca4a..d860795f8 100644 --- a/tests/frameworks/test_pyramid.py +++ b/tests/frameworks/test_pyramid.py @@ -5,7 +5,7 @@ import urllib3 from typing import Generator -import tests.apps.pyramid.pyramid_app +import tests.apps.pyramid.pyramid_app # noqa: F401 from tests.helpers import testenv from instana.singletons import tracer, agent from instana.span.span import get_current_span @@ -76,7 +76,9 @@ def test_get_request(self) -> None: # wsgi assert pyramid_span.n == "wsgi" - assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str(testenv["pyramid_port"]) + assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str( + testenv["pyramid_port"] + ) assert pyramid_span.data["http"]["url"] == "/" assert pyramid_span.data["http"]["method"] == "GET" assert pyramid_span.data["http"]["status"] == 200 @@ -160,7 +162,9 @@ def test_500(self) -> None: # wsgi assert pyramid_span.n == "wsgi" - assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str(testenv["pyramid_port"]) + assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str( + testenv["pyramid_port"] + ) assert pyramid_span.data["http"]["url"] == "/500" assert pyramid_span.data["http"]["method"] == "GET" assert pyramid_span.data["http"]["status"] == 500 @@ -210,7 +214,9 @@ def test_exception(self) -> None: # wsgi assert pyramid_span.n == "wsgi" - assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str(testenv["pyramid_port"]) + assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str( + testenv["pyramid_port"] + ) assert pyramid_span.data["http"]["url"] == "/exception" assert pyramid_span.data["http"]["method"] == "GET" assert pyramid_span.data["http"]["status"] == 500 @@ -269,7 +275,9 @@ def test_response_header_capture(self) -> None: # wsgi assert pyramid_span.n == "wsgi" - assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str(testenv["pyramid_port"]) + assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str( + testenv["pyramid_port"] + ) assert pyramid_span.data["http"]["url"] == "/response_headers" assert pyramid_span.data["http"]["method"] == "GET" assert pyramid_span.data["http"]["status"] == 200 @@ -340,7 +348,9 @@ def test_request_header_capture(self) -> None: # wsgi assert pyramid_span.n == "wsgi" - assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str(testenv["pyramid_port"]) + assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str( + testenv["pyramid_port"] + ) assert pyramid_span.data["http"]["url"] == "/" assert pyramid_span.data["http"]["method"] == "GET" assert pyramid_span.data["http"]["status"] == 200 @@ -418,7 +428,9 @@ def test_scrub_secret_path_template(self) -> None: # wsgi assert pyramid_span.n == "wsgi" - assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str(testenv["pyramid_port"]) + assert pyramid_span.data["http"]["host"] == "127.0.0.1:" + str( + testenv["pyramid_port"] + ) assert pyramid_span.data["http"]["url"] == "/hello_user/oswald" assert pyramid_span.data["http"]["method"] == "GET" assert pyramid_span.data["http"]["status"] == 200 diff --git a/tests/frameworks/test_sanic.py b/tests/frameworks/test_sanic.py index 275b3e4f5..f2d962d1f 100644 --- a/tests/frameworks/test_sanic.py +++ b/tests/frameworks/test_sanic.py @@ -61,13 +61,15 @@ def test_basic_get(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -107,13 +109,15 @@ def test_404(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -153,13 +157,15 @@ def test_sanic_exception(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 3 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -199,13 +205,15 @@ def test_500_instana_exception(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 3 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -245,13 +253,15 @@ def test_500(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 3 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -291,13 +301,15 @@ def test_path_templates(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -337,13 +349,15 @@ def test_secret_scrubbing(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -384,13 +398,15 @@ def test_synthetic_request(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -435,13 +451,15 @@ def test_request_header_capture(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span @@ -486,13 +504,15 @@ def test_response_header_capture(self) -> None: spans = self.recorder.queued_spans() assert len(spans) == 2 - span_filter = ( - lambda span: span.n == "sdk" and span.data["sdk"]["name"] == "test" - ) + def span_filter(span): + return span.n == "sdk" and span.data["sdk"]["name"] == "test" + test_span = get_first_span_by_filter(spans, span_filter) assert test_span - span_filter = lambda span: span.n == "asgi" + def span_filter(span): + return span.n == "asgi" + asgi_span = get_first_span_by_filter(spans, span_filter) assert asgi_span diff --git a/tests/frameworks/test_starlette_middleware.py b/tests/frameworks/test_starlette_middleware.py index d02039d4d..032d28223 100644 --- a/tests/frameworks/test_starlette_middleware.py +++ b/tests/frameworks/test_starlette_middleware.py @@ -4,7 +4,7 @@ from typing import Generator import pytest -from instana.singletons import agent, tracer +from instana.singletons import tracer from starlette.testclient import TestClient from tests.apps.starlette_app.app2 import starlette_server diff --git a/tests/frameworks/test_tornado_client.py b/tests/frameworks/test_tornado_client.py index 24b8dca36..1365c17b9 100644 --- a/tests/frameworks/test_tornado_client.py +++ b/tests/frameworks/test_tornado_client.py @@ -11,14 +11,14 @@ from instana.singletons import tracer from instana.span.span import get_current_span -import tests.apps.tornado_server +import tests.apps.tornado_server # noqa: F401 from tests.helpers import testenv, get_first_span_by_name, get_first_span_by_filter -class TestTornadoClient: +class TestTornadoClient: @pytest.fixture(autouse=True) def _resource(self) -> Generator[None, None, None]: - """ Clear all spans before a test run """ + """Clear all spans before a test run""" self.recorder = tracer.span_processor self.recorder.clear_spans() @@ -86,14 +86,16 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(server_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId def test_post(self) -> None: async def test(): with tracer.start_as_current_span("test"): - return await self.http_client.fetch(testenv["tornado_server"] + "/", method="POST", body='asdf') + return await self.http_client.fetch( + testenv["tornado_server"] + "/", method="POST", body="asdf" + ) response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) @@ -141,7 +143,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(server_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -163,13 +165,30 @@ async def test(): client301_span = spans[3] test_span = spans[4] - filter = lambda span: span.n == "tornado-server" and span.data["http"]["status"] == 301 + def filter(span): + return span.n == "tornado-server" and span.data["http"]["status"] == 301 + server301_span = get_first_span_by_filter(spans, filter) - filter = lambda span: span.n == "tornado-server" and span.data["http"]["status"] == 200 + + def filter(span): + return span.n == "tornado-server" and span.data["http"]["status"] == 200 + server_span = get_first_span_by_filter(spans, filter) - filter = lambda span: span.n == "tornado-client" and span.data["http"]["url"] == testenv["tornado_server"] + "/" + + def filter(span): + return ( + span.n == "tornado-client" + and span.data["http"]["url"] == testenv["tornado_server"] + "/" + ) + client_span = get_first_span_by_filter(spans, filter) - filter = lambda span: span.n == "tornado-client" and span.data["http"]["url"] == testenv["tornado_server"] + "/301" + + def filter(span): + return ( + span.n == "tornado-client" + and span.data["http"]["url"] == testenv["tornado_server"] + "/301" + ) + client301_span = get_first_span_by_filter(spans, filter) test_span = get_first_span_by_name(spans, "sdk") @@ -226,7 +245,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(server_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -234,7 +253,9 @@ def test_get_405(self) -> None: async def test(): with tracer.start_as_current_span("test"): try: - return await self.http_client.fetch(testenv["tornado_server"] + "/405") + return await self.http_client.fetch( + testenv["tornado_server"] + "/405" + ) except tornado.httpclient.HTTPClientError as e: return e.response @@ -284,7 +305,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(server_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -292,7 +313,9 @@ def test_get_500(self) -> None: async def test(): with tracer.start_as_current_span("test"): try: - return await self.http_client.fetch(testenv["tornado_server"] + "/500") + return await self.http_client.fetch( + testenv["tornado_server"] + "/500" + ) except tornado.httpclient.HTTPClientError as e: return e.response @@ -342,7 +365,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(server_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -350,7 +373,9 @@ def test_get_504(self) -> None: async def test(): with tracer.start_as_current_span("test"): try: - return await self.http_client.fetch(testenv["tornado_server"] + "/504") + return await self.http_client.fetch( + testenv["tornado_server"] + "/504" + ) except tornado.httpclient.HTTPClientError as e: return e.response @@ -400,14 +425,16 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(server_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId def test_get_with_params_to_scrub(self) -> None: async def test(): with tracer.start_as_current_span("test"): - return await self.http_client.fetch(testenv["tornado_server"] + "/?secret=yeah") + return await self.http_client.fetch( + testenv["tornado_server"] + "/?secret=yeah" + ) response = tornado.ioloop.IOLoop.current().run_sync(test) assert isinstance(response, tornado.httpclient.HTTPResponse) @@ -439,13 +466,13 @@ async def test(): assert server_span.n == "tornado-server" assert server_span.data["http"]["status"] == 200 assert testenv["tornado_server"] + "/" == server_span.data["http"]["url"] - assert 'secret=' == server_span.data["http"]["params"] + assert "secret=" == server_span.data["http"]["params"] assert server_span.data["http"]["method"] == "GET" assert client_span.n == "tornado-client" assert client_span.data["http"]["status"] == 200 assert testenv["tornado_server"] + "/" == client_span.data["http"]["url"] - assert 'secret=' == client_span.data["http"]["params"] + assert "secret=" == client_span.data["http"]["params"] assert client_span.data["http"]["method"] == "GET" assert client_span.stack assert type(client_span.stack) is list @@ -456,6 +483,6 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(server_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId diff --git a/tests/frameworks/test_tornado_server.py b/tests/frameworks/test_tornado_server.py index 96f740d6a..ddd93efb6 100644 --- a/tests/frameworks/test_tornado_server.py +++ b/tests/frameworks/test_tornado_server.py @@ -9,7 +9,7 @@ import tornado from tornado.httpclient import AsyncHTTPClient -import tests.apps.tornado_server +import tests.apps.tornado_server # noqa: F401 from instana.singletons import tracer, agent from tests.helpers import testenv, get_first_span_by_name, get_first_span_by_filter @@ -26,14 +26,16 @@ async def fetch(self, session, url, headers=None, params=None): async def post(self, session, url, headers=None): try: - async with session.post(url, headers=headers, data={"hello": "post"}) as response: + async with session.post( + url, headers=headers, data={"hello": "post"} + ) as response: return response except aiohttp.web_exceptions.HTTPException: pass @pytest.fixture(autouse=True) def _resource(self) -> Generator[None, None, None]: - """ Clear all spans before a test run """ + """Clear all spans before a test run""" self.recorder = tracer.span_processor self.recorder.clear_spans() @@ -104,7 +106,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -165,19 +167,19 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId def test_synthetic_request(self) -> None: async def test(): - headers = { - 'X-INSTANA-SYNTHETIC': '1' - } + headers = {"X-INSTANA-SYNTHETIC": "1"} with tracer.start_as_current_span("test"): async with aiohttp.ClientSession() as session: - return await self.fetch(session, testenv["tornado_server"] + "/", headers=headers) + return await self.fetch( + session, testenv["tornado_server"] + "/", headers=headers + ) tornado.ioloop.IOLoop.current().run_sync(test) @@ -203,9 +205,14 @@ async def test(): spans = self.recorder.queued_spans() assert len(spans) == 4 - filter = lambda span: span.n == "tornado-server" and span.data["http"]["status"] == 301 + def filter(span): + return span.n == "tornado-server" and span.data["http"]["status"] == 301 + tornado_301_span = get_first_span_by_filter(spans, filter) - filter = lambda span: span.n == "tornado-server" and span.data["http"]["status"] == 200 + + def filter(span): + return span.n == "tornado-server" and span.data["http"]["status"] == 200 + tornado_span = get_first_span_by_filter(spans, filter) aiohttp_span = get_first_span_by_name(spans, "aiohttp-client") test_span = get_first_span_by_name(spans, "sdk") @@ -240,7 +247,9 @@ async def test(): assert not tornado_span.ec assert tornado_301_span.data["http"]["status"] == 301 - assert testenv["tornado_server"] + "/301" == tornado_301_span.data["http"]["url"] + assert ( + testenv["tornado_server"] + "/301" == tornado_301_span.data["http"]["url"] + ) assert not tornado_span.data["http"]["params"] assert tornado_301_span.data["http"]["method"] == "GET" assert not tornado_301_span.stack @@ -262,7 +271,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -324,7 +333,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -377,7 +386,7 @@ async def test(): assert aiohttp_span.data["http"]["status"] == 500 assert testenv["tornado_server"] + "/500" == aiohttp_span.data["http"]["url"] assert aiohttp_span.data["http"]["method"] == "GET" - assert 'Internal Server Error' == aiohttp_span.data["http"]["error"] + assert "Internal Server Error" == aiohttp_span.data["http"]["error"] assert aiohttp_span.stack assert isinstance(aiohttp_span.stack, list) assert len(aiohttp_span.stack) > 1 @@ -387,7 +396,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -440,7 +449,7 @@ async def test(): assert aiohttp_span.data["http"]["status"] == 504 assert testenv["tornado_server"] + "/504" == aiohttp_span.data["http"]["url"] assert aiohttp_span.data["http"]["method"] == "GET" - assert 'Gateway Timeout' == aiohttp_span.data["http"]["error"] + assert "Gateway Timeout" == aiohttp_span.data["http"]["error"] assert aiohttp_span.stack assert isinstance(aiohttp_span.stack, list) assert len(aiohttp_span.stack) > 1 @@ -450,7 +459,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -458,7 +467,9 @@ def test_get_with_params_to_scrub(self) -> None: async def test(): with tracer.start_as_current_span("test"): async with aiohttp.ClientSession() as session: - return await self.fetch(session, testenv["tornado_server"], params={"secret": "yeah"}) + return await self.fetch( + session, testenv["tornado_server"], params={"secret": "yeah"} + ) response = tornado.ioloop.IOLoop.current().run_sync(test) @@ -512,7 +523,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -521,14 +532,22 @@ async def test(): with tracer.start_as_current_span("test"): async with aiohttp.ClientSession() as session: # Hack together a manual custom request headers list - agent.options.extra_http_headers = ["X-Capture-This", "X-Capture-That"] + agent.options.extra_http_headers = [ + "X-Capture-This", + "X-Capture-That", + ] request_headers = { "X-Capture-This": "this", - "X-Capture-That": "that" + "X-Capture-That": "that", } - return await self.fetch(session, testenv["tornado_server"], headers=request_headers, params={"secret": "iloveyou"}) + return await self.fetch( + session, + testenv["tornado_server"], + headers=request_headers, + params={"secret": "iloveyou"}, + ) response = tornado.ioloop.IOLoop.current().run_sync(test) @@ -582,7 +601,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId @@ -596,9 +615,16 @@ async def test(): with tracer.start_as_current_span("test"): async with aiohttp.ClientSession() as session: # Hack together a manual custom response headers list - agent.options.extra_http_headers = ["X-Capture-This-Too", "X-Capture-That-Too"] + agent.options.extra_http_headers = [ + "X-Capture-This-Too", + "X-Capture-That-Too", + ] - return await self.fetch(session, testenv["tornado_server"] + "/response_headers", params={"secret": "itsasecret"}) + return await self.fetch( + session, + testenv["tornado_server"] + "/response_headers", + params={"secret": "itsasecret"}, + ) response = tornado.ioloop.IOLoop.current().run_sync(test) @@ -634,13 +660,19 @@ async def test(): assert not tornado_span.ec assert tornado_span.data["http"]["status"] == 200 - assert testenv["tornado_server"] + "/response_headers" == tornado_span.data["http"]["url"] + assert ( + testenv["tornado_server"] + "/response_headers" + == tornado_span.data["http"]["url"] + ) assert tornado_span.data["http"]["params"] == "secret=" assert tornado_span.data["http"]["method"] == "GET" assert not tornado_span.stack assert aiohttp_span.data["http"]["status"] == 200 - assert testenv["tornado_server"] + "/response_headers" == aiohttp_span.data["http"]["url"] + assert ( + testenv["tornado_server"] + "/response_headers" + == aiohttp_span.data["http"]["url"] + ) assert aiohttp_span.data["http"]["method"] == "GET" assert aiohttp_span.data["http"]["params"] == "secret=" assert aiohttp_span.stack @@ -652,7 +684,7 @@ async def test(): assert "X-INSTANA-S" in response.headers assert response.headers["X-INSTANA-S"] == str(tornado_span.s) assert "X-INSTANA-L" in response.headers - assert response.headers["X-INSTANA-L"] == '1' + assert response.headers["X-INSTANA-L"] == "1" assert "Server-Timing" in response.headers assert response.headers["Server-Timing"] == "intid;desc=%s" % traceId diff --git a/tests/frameworks/test_wsgi.py b/tests/frameworks/test_wsgi.py index 7e9f34847..1ba3c265a 100644 --- a/tests/frameworks/test_wsgi.py +++ b/tests/frameworks/test_wsgi.py @@ -6,7 +6,7 @@ import pytest from typing import Generator -from tests.apps import bottle_app +from tests.apps import bottle_app # noqa: F401 from tests.helpers import testenv from instana.singletons import agent, tracer from instana.span.span import get_current_span @@ -15,14 +15,14 @@ class TestWSGI: @pytest.fixture(autouse=True) def _resource(self) -> Generator[None, None, None]: - """ Clear all spans before a test run """ + """Clear all spans before a test run""" self.http = urllib3.PoolManager() self.recorder = tracer.span_processor self.recorder.clear_spans() time.sleep(0.1) def test_vanilla_requests(self) -> None: - response = self.http.request('GET', testenv["wsgi_server"] + '/') + response = self.http.request("GET", testenv["wsgi_server"] + "/") spans = self.recorder.queued_spans() assert 1 == len(spans) @@ -31,7 +31,7 @@ def test_vanilla_requests(self) -> None: def test_get_request(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["wsgi_server"] + '/') + response = self.http.request("GET", testenv["wsgi_server"] + "/") spans = self.recorder.queued_spans() @@ -45,20 +45,20 @@ def test_get_request(self) -> None: assert response assert 200 == response.status - assert 'X-INSTANA-T' in response.headers - assert int(response.headers['X-INSTANA-T'], 16) + assert "X-INSTANA-T" in response.headers + assert int(response.headers["X-INSTANA-T"], 16) assert response.headers["X-INSTANA-T"] == str(wsgi_span.t) - assert 'X-INSTANA-S' in response.headers - assert int(response.headers['X-INSTANA-S'], 16) + assert "X-INSTANA-S" in response.headers + assert int(response.headers["X-INSTANA-S"], 16) assert response.headers["X-INSTANA-S"] == str(wsgi_span.s) - assert 'X-INSTANA-L' in response.headers - assert response.headers['X-INSTANA-L'] == '1' + assert "X-INSTANA-L" in response.headers + assert response.headers["X-INSTANA-L"] == "1" - assert 'Server-Timing' in response.headers + assert "Server-Timing" in response.headers server_timing_value = "intid;desc=%s" % wsgi_span.t - assert response.headers['Server-Timing'] == server_timing_value + assert response.headers["Server-Timing"] == server_timing_value # Same traceId assert test_span.t == urllib3_span.t @@ -79,19 +79,19 @@ def test_get_request(self) -> None: # wsgi assert "wsgi" == wsgi_span.n - assert '127.0.0.1:' + str(testenv["wsgi_port"]) == wsgi_span.data["http"]["host"] - assert '/' == wsgi_span.data["http"]["path"] - assert 'GET' == wsgi_span.data["http"]["method"] + assert ( + "127.0.0.1:" + str(testenv["wsgi_port"]) == wsgi_span.data["http"]["host"] + ) + assert "/" == wsgi_span.data["http"]["path"] + assert "GET" == wsgi_span.data["http"]["method"] assert "200" == wsgi_span.data["http"]["status"] assert wsgi_span.data["http"]["error"] is None assert wsgi_span.stack is None def test_synthetic_request(self) -> None: - headers = { - 'X-INSTANA-SYNTHETIC': '1' - } + headers = {"X-INSTANA-SYNTHETIC": "1"} with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["wsgi_server"] + '/', headers=headers) + _ = self.http.request("GET", testenv["wsgi_server"] + "/", headers=headers) spans = self.recorder.queued_spans() @@ -106,17 +106,18 @@ def test_synthetic_request(self) -> None: assert urllib3_span.sy is None assert test_span.sy is None - def test_custom_header_capture(self) -> None: # Hack together a manual custom headers list - agent.options.extra_http_headers = [u'X-Capture-This', u'X-Capture-That'] + agent.options.extra_http_headers = ["X-Capture-This", "X-Capture-That"] request_headers = {} - request_headers['X-Capture-This'] = 'this' - request_headers['X-Capture-That'] = 'that' + request_headers["X-Capture-This"] = "this" + request_headers["X-Capture-That"] = "that" with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["wsgi_server"] + '/', headers=request_headers) + response = self.http.request( + "GET", testenv["wsgi_server"] + "/", headers=request_headers + ) spans = self.recorder.queued_spans() @@ -130,20 +131,20 @@ def test_custom_header_capture(self) -> None: assert response assert 200 == response.status - assert 'X-INSTANA-T' in response.headers - assert int(response.headers['X-INSTANA-T'], 16) + assert "X-INSTANA-T" in response.headers + assert int(response.headers["X-INSTANA-T"], 16) assert response.headers["X-INSTANA-T"] == str(wsgi_span.t) - assert 'X-INSTANA-S' in response.headers - assert int(response.headers['X-INSTANA-S'], 16) + assert "X-INSTANA-S" in response.headers + assert int(response.headers["X-INSTANA-S"], 16) assert response.headers["X-INSTANA-S"] == str(wsgi_span.s) - assert 'X-INSTANA-L' in response.headers - assert response.headers['X-INSTANA-L'] == '1' + assert "X-INSTANA-L" in response.headers + assert response.headers["X-INSTANA-L"] == "1" - assert 'Server-Timing' in response.headers + assert "Server-Timing" in response.headers server_timing_value = "intid;desc=%s" % wsgi_span.t - assert response.headers['Server-Timing'] == server_timing_value + assert response.headers["Server-Timing"] == server_timing_value # Same traceId assert test_span.t == urllib3_span.t @@ -160,9 +161,11 @@ def test_custom_header_capture(self) -> None: # wsgi assert "wsgi" == wsgi_span.n - assert '127.0.0.1:' + str(testenv["wsgi_port"]) == wsgi_span.data["http"]["host"] - assert '/' == wsgi_span.data["http"]["path"] - assert 'GET' == wsgi_span.data["http"]["method"] + assert ( + "127.0.0.1:" + str(testenv["wsgi_port"]) == wsgi_span.data["http"]["host"] + ) + assert "/" == wsgi_span.data["http"]["path"] + assert "GET" == wsgi_span.data["http"]["method"] assert "200" == wsgi_span.data["http"]["status"] assert wsgi_span.data["http"]["error"] is None assert wsgi_span.stack is None @@ -174,7 +177,9 @@ def test_custom_header_capture(self) -> None: def test_secret_scrubbing(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["wsgi_server"] + '/?secret=shhh') + response = self.http.request( + "GET", testenv["wsgi_server"] + "/?secret=shhh" + ) spans = self.recorder.queued_spans() @@ -188,20 +193,20 @@ def test_secret_scrubbing(self) -> None: assert response assert 200 == response.status - assert 'X-INSTANA-T' in response.headers - assert int(response.headers['X-INSTANA-T'], 16) + assert "X-INSTANA-T" in response.headers + assert int(response.headers["X-INSTANA-T"], 16) assert response.headers["X-INSTANA-T"] == str(wsgi_span.t) - assert 'X-INSTANA-S' in response.headers - assert int(response.headers['X-INSTANA-S'], 16) + assert "X-INSTANA-S" in response.headers + assert int(response.headers["X-INSTANA-S"], 16) assert response.headers["X-INSTANA-S"] == str(wsgi_span.s) - assert 'X-INSTANA-L' in response.headers - assert response.headers['X-INSTANA-L'] == '1' + assert "X-INSTANA-L" in response.headers + assert response.headers["X-INSTANA-L"] == "1" - assert 'Server-Timing' in response.headers + assert "Server-Timing" in response.headers server_timing_value = "intid;desc=%s" % wsgi_span.t - assert response.headers['Server-Timing'] == server_timing_value + assert response.headers["Server-Timing"] == server_timing_value # Same traceId assert test_span.t == urllib3_span.t @@ -218,20 +223,24 @@ def test_secret_scrubbing(self) -> None: # wsgi assert "wsgi" == wsgi_span.n - assert '127.0.0.1:' + str(testenv["wsgi_port"]) == wsgi_span.data["http"]["host"] - assert '/' == wsgi_span.data["http"]["path"] - assert 'secret=' == wsgi_span.data["http"]["params"] - assert 'GET' == wsgi_span.data["http"]["method"] + assert ( + "127.0.0.1:" + str(testenv["wsgi_port"]) == wsgi_span.data["http"]["host"] + ) + assert "/" == wsgi_span.data["http"]["path"] + assert "secret=" == wsgi_span.data["http"]["params"] + assert "GET" == wsgi_span.data["http"]["method"] assert "200" == wsgi_span.data["http"]["status"] assert wsgi_span.data["http"]["error"] is None assert wsgi_span.stack is None def test_with_incoming_context(self) -> None: request_headers = dict() - request_headers['X-INSTANA-T'] = '0000000000000001' - request_headers['X-INSTANA-S'] = '0000000000000001' + request_headers["X-INSTANA-T"] = "0000000000000001" + request_headers["X-INSTANA-S"] = "0000000000000001" - response = self.http.request('GET', testenv["wsgi_server"] + '/', headers=request_headers) + response = self.http.request( + "GET", testenv["wsgi_server"] + "/", headers=request_headers + ) assert response assert 200 == response.status @@ -246,27 +255,29 @@ def test_with_incoming_context(self) -> None: assert wsgi_span.t == 1 assert wsgi_span.p == 1 - assert 'X-INSTANA-T' in response.headers - assert int(response.headers['X-INSTANA-T'], 16) + assert "X-INSTANA-T" in response.headers + assert int(response.headers["X-INSTANA-T"], 16) assert response.headers["X-INSTANA-T"] == str(wsgi_span.t) - assert 'X-INSTANA-S' in response.headers - assert int(response.headers['X-INSTANA-S'], 16) + assert "X-INSTANA-S" in response.headers + assert int(response.headers["X-INSTANA-S"], 16) assert response.headers["X-INSTANA-S"] == str(wsgi_span.s) - assert 'X-INSTANA-L' in response.headers - assert response.headers['X-INSTANA-L'] == '1' + assert "X-INSTANA-L" in response.headers + assert response.headers["X-INSTANA-L"] == "1" - assert 'Server-Timing' in response.headers + assert "Server-Timing" in response.headers server_timing_value = "intid;desc=%s" % wsgi_span.t - assert response.headers['Server-Timing'] == server_timing_value + assert response.headers["Server-Timing"] == server_timing_value def test_with_incoming_mixed_case_context(self) -> None: request_headers = dict() - request_headers['X-InSTANa-T'] = '0000000000000001' - request_headers['X-instana-S'] = '0000000000000001' + request_headers["X-InSTANa-T"] = "0000000000000001" + request_headers["X-instana-S"] = "0000000000000001" - response = self.http.request('GET', testenv["wsgi_server"] + '/', headers=request_headers) + response = self.http.request( + "GET", testenv["wsgi_server"] + "/", headers=request_headers + ) assert response assert 200 == response.status @@ -281,24 +292,24 @@ def test_with_incoming_mixed_case_context(self) -> None: assert wsgi_span.t == 1 assert wsgi_span.p == 1 - assert 'X-INSTANA-T' in response.headers - assert int(response.headers['X-INSTANA-T'], 16) + assert "X-INSTANA-T" in response.headers + assert int(response.headers["X-INSTANA-T"], 16) assert response.headers["X-INSTANA-T"] == str(wsgi_span.t) - assert 'X-INSTANA-S' in response.headers - assert int(response.headers['X-INSTANA-S'], 16) + assert "X-INSTANA-S" in response.headers + assert int(response.headers["X-INSTANA-S"], 16) assert response.headers["X-INSTANA-S"] == str(wsgi_span.s) - assert 'X-INSTANA-L' in response.headers - assert response.headers['X-INSTANA-L'] == '1' + assert "X-INSTANA-L" in response.headers + assert response.headers["X-INSTANA-L"] == "1" - assert 'Server-Timing' in response.headers + assert "Server-Timing" in response.headers server_timing_value = "intid;desc=%s" % wsgi_span.t - assert response.headers['Server-Timing'] == server_timing_value + assert response.headers["Server-Timing"] == server_timing_value def test_response_headers(self) -> None: with tracer.start_as_current_span("test"): - response = self.http.request('GET', testenv["wsgi_server"] + '/') + response = self.http.request("GET", testenv["wsgi_server"] + "/") spans = self.recorder.queued_spans() @@ -306,23 +317,21 @@ def test_response_headers(self) -> None: assert get_current_span().is_recording() is False wsgi_span = spans[0] - urllib3_span = spans[1] - test_span = spans[2] assert response assert 200 == response.status - assert 'X-INSTANA-T' in response.headers - assert int(response.headers['X-INSTANA-T'], 16) + assert "X-INSTANA-T" in response.headers + assert int(response.headers["X-INSTANA-T"], 16) assert response.headers["X-INSTANA-T"] == str(wsgi_span.t) - assert 'X-INSTANA-S' in response.headers - assert int(response.headers['X-INSTANA-S'], 16) + assert "X-INSTANA-S" in response.headers + assert int(response.headers["X-INSTANA-S"], 16) assert response.headers["X-INSTANA-S"] == str(wsgi_span.s) - assert 'X-INSTANA-L' in response.headers - assert response.headers['X-INSTANA-L'] == '1' + assert "X-INSTANA-L" in response.headers + assert response.headers["X-INSTANA-L"] == "1" - assert 'Server-Timing' in response.headers + assert "Server-Timing" in response.headers server_timing_value = "intid;desc=%s" % wsgi_span.t - assert response.headers['Server-Timing'] == server_timing_value + assert response.headers["Server-Timing"] == server_timing_value diff --git a/tests/platforms/test_gcr_collector.py b/tests/platforms/test_gcr_collector.py index 39c7e8860..f37f47b8d 100644 --- a/tests/platforms/test_gcr_collector.py +++ b/tests/platforms/test_gcr_collector.py @@ -14,7 +14,7 @@ class TestGCRCollector(unittest.TestCase): - def __init__(self, methodName='runTest'): + def __init__(self, methodName="runTest"): super(TestGCRCollector, self).__init__(methodName) self.agent = None self.span_recorder = None @@ -35,7 +35,7 @@ def setUp(self): os.environ.pop("INSTANA_TAGS") def tearDown(self): - """ Reset all environment variables of consequence """ + """Reset all environment variables of consequence""" if "PORT" in os.environ: os.environ.pop("PORT") if "INSTANA_EXTRA_HTTP_HEADERS" in os.environ: @@ -53,43 +53,51 @@ def tearDown(self): set_tracer(self.original_tracer) def create_agent_and_setup_tracer(self): - self.agent = GCRAgent(service="service", configuration="configuration", revision="revision") + self.agent = GCRAgent( + service="service", configuration="configuration", revision="revision" + ) self.span_recorder = StanRecorder(self.agent) self.tracer = InstanaTracer(recorder=self.span_recorder) set_agent(self.agent) set_tracer(self.tracer) # Manually set the Instance and Project Metadata API results on the collector - with open(self.pwd + '/../data/gcr/instance_metadata.json', 'r') as json_file: + with open(self.pwd + "/../data/gcr/instance_metadata.json", "r") as json_file: self.agent.collector.instance_metadata = json.load(json_file) - with open(self.pwd + '/../data/gcr/project_metadata.json', 'r') as json_file: + with open(self.pwd + "/../data/gcr/project_metadata.json", "r") as json_file: self.agent.collector.project_metadata = json.load(json_file) @requests_mock.Mocker() def test_prepare_payload_basics(self, m): self.create_agent_and_setup_tracer() - m.get("http://metadata.google.internal/computeMetadata/v1/project/?recursive=true", - headers={"Metadata-Flavor": "Google"}, json=self.agent.collector.project_metadata) - - m.get("http://metadata.google.internal/computeMetadata/v1/instance/?recursive=true", - headers={"Metadata-Flavor": "Google"}, json=self.agent.collector.instance_metadata) + m.get( + "http://metadata.google.internal/computeMetadata/v1/project/?recursive=true", + headers={"Metadata-Flavor": "Google"}, + json=self.agent.collector.project_metadata, + ) + + m.get( + "http://metadata.google.internal/computeMetadata/v1/instance/?recursive=true", + headers={"Metadata-Flavor": "Google"}, + json=self.agent.collector.instance_metadata, + ) payload = self.agent.collector.prepare_payload() - assert (payload) - - assert (len(payload.keys()) == 2) - assert ('spans' in payload) - assert (isinstance(payload['spans'], list)) - assert (len(payload['spans']) == 0) - assert ('metrics' in payload) - assert (len(payload['metrics'].keys()) == 1) - assert ('plugins' in payload['metrics']) - assert (isinstance(payload['metrics']['plugins'], list)) - assert (len(payload['metrics']['plugins']) == 2) - - plugins = payload['metrics']['plugins'] + assert payload + + assert len(payload.keys()) == 2 + assert "spans" in payload + assert isinstance(payload["spans"], list) + assert len(payload["spans"]) == 0 + assert "metrics" in payload + assert len(payload["metrics"].keys()) == 1 + assert "plugins" in payload["metrics"] + assert isinstance(payload["metrics"]["plugins"], list) + assert len(payload["metrics"]["plugins"]) == 2 + + plugins = payload["metrics"]["plugins"] for plugin in plugins: # print("%s - %s" % (plugin["name"], plugin["entityId"])) - assert ('name' in plugin) - assert ('entityId' in plugin) - assert ('data' in plugin) + assert "name" in plugin + assert "entityId" in plugin + assert "data" in plugin diff --git a/tests/platforms/test_google_cloud_run.py b/tests/platforms/test_google_cloud_run.py index 8b086a708..5fd2a0faa 100644 --- a/tests/platforms/test_google_cloud_run.py +++ b/tests/platforms/test_google_cloud_run.py @@ -13,7 +13,7 @@ class TestGCR(unittest.TestCase): - def __init__(self, methodName='runTest'): + def __init__(self, methodName="runTest"): super(TestGCR, self).__init__(methodName) self.agent = None self.span_recorder = None @@ -31,7 +31,7 @@ def setUp(self): os.environ["INSTANA_AGENT_KEY"] = "Fake_Key" def tearDown(self): - """ Reset all environment variables of consequence """ + """Reset all environment variables of consequence""" if "K_SERVICE" in os.environ: os.environ.pop("K_SERVICE") if "K_CONFIGURATION" in os.environ: @@ -61,7 +61,9 @@ def tearDown(self): set_tracer(self.original_tracer) def create_agent_and_setup_tracer(self): - self.agent = GCRAgent(service="service", configuration="configuration", revision="revision") + self.agent = GCRAgent( + service="service", configuration="configuration", revision="revision" + ) self.span_recorder = StanRecorder(self.agent) self.tracer = InstanaTracer(recorder=self.span_recorder) set_agent(self.agent) @@ -69,7 +71,7 @@ def create_agent_and_setup_tracer(self): def test_has_options(self): self.create_agent_and_setup_tracer() - self.assertTrue(hasattr(self.agent, 'options')) + self.assertTrue(hasattr(self.agent, "options")) self.assertTrue(isinstance(self.agent.options, GCROptions)) def test_invalid_options(self): @@ -81,37 +83,41 @@ def test_invalid_options(self): if "INSTANA_AGENT_KEY" in os.environ: os.environ.pop("INSTANA_AGENT_KEY") - agent = GCRAgent(service="service", configuration="configuration", revision="revision") + agent = GCRAgent( + service="service", configuration="configuration", revision="revision" + ) self.assertFalse(agent.can_send()) self.assertIsNone(agent.collector) def test_default_secrets(self): self.create_agent_and_setup_tracer() self.assertIsNone(self.agent.options.secrets) - self.assertTrue(hasattr(self.agent.options, 'secrets_matcher')) - self.assertEqual(self.agent.options.secrets_matcher, 'contains-ignore-case') - self.assertTrue(hasattr(self.agent.options, 'secrets_list')) - self.assertEqual(self.agent.options.secrets_list, ['key', 'pass', 'secret']) + self.assertTrue(hasattr(self.agent.options, "secrets_matcher")) + self.assertEqual(self.agent.options.secrets_matcher, "contains-ignore-case") + self.assertTrue(hasattr(self.agent.options, "secrets_list")) + self.assertEqual(self.agent.options.secrets_list, ["key", "pass", "secret"]) def test_custom_secrets(self): os.environ["INSTANA_SECRETS"] = "equals:love,war,games" self.create_agent_and_setup_tracer() - self.assertTrue(hasattr(self.agent.options, 'secrets_matcher')) - self.assertEqual(self.agent.options.secrets_matcher, 'equals') - self.assertTrue(hasattr(self.agent.options, 'secrets_list')) - self.assertEqual(self.agent.options.secrets_list, ['love', 'war', 'games']) + self.assertTrue(hasattr(self.agent.options, "secrets_matcher")) + self.assertEqual(self.agent.options.secrets_matcher, "equals") + self.assertTrue(hasattr(self.agent.options, "secrets_list")) + self.assertEqual(self.agent.options.secrets_list, ["love", "war", "games"]) def test_has_extra_http_headers(self): self.create_agent_and_setup_tracer() - self.assertTrue(hasattr(self.agent, 'options')) - self.assertTrue(hasattr(self.agent.options, 'extra_http_headers')) + self.assertTrue(hasattr(self.agent, "options")) + self.assertTrue(hasattr(self.agent.options, "extra_http_headers")) def test_agent_extra_http_headers(self): - os.environ['INSTANA_EXTRA_HTTP_HEADERS'] = "X-Test-Header;X-Another-Header;X-And-Another-Header" + os.environ["INSTANA_EXTRA_HTTP_HEADERS"] = ( + "X-Test-Header;X-Another-Header;X-And-Another-Header" + ) self.create_agent_and_setup_tracer() self.assertIsNotNone(self.agent.options.extra_http_headers) - should_headers = ['x-test-header', 'x-another-header', 'x-and-another-header'] + should_headers = ["x-test-header", "x-another-header", "x-and-another-header"] self.assertEqual(should_headers, self.agent.options.extra_http_headers) def test_agent_default_log_level(self): @@ -119,11 +125,11 @@ def test_agent_default_log_level(self): assert self.agent.options.log_level == logging.WARNING def test_agent_custom_log_level(self): - os.environ['INSTANA_LOG_LEVEL'] = "eRror" + os.environ["INSTANA_LOG_LEVEL"] = "eRror" self.create_agent_and_setup_tracer() assert self.agent.options.log_level == logging.ERROR def test_custom_proxy(self): os.environ["INSTANA_ENDPOINT_PROXY"] = "http://myproxy.123" self.create_agent_and_setup_tracer() - assert self.agent.options.endpoint_proxy == {'https': "http://myproxy.123"} + assert self.agent.options.endpoint_proxy == {"https": "http://myproxy.123"} diff --git a/tests/platforms/test_host.py b/tests/platforms/test_host.py index fcfc80a9a..2ff02afc2 100644 --- a/tests/platforms/test_host.py +++ b/tests/platforms/test_host.py @@ -18,7 +18,7 @@ class TestHost(unittest.TestCase): - def __init__(self, methodName='runTest'): + def __init__(self, methodName="runTest"): super(TestHost, self).__init__(methodName) self.agent = None self.span_recorder = None @@ -30,13 +30,18 @@ def setUp(self): pass def tearDown(self): - """ Reset all environment variables of consequence """ + """Reset all environment variables of consequence""" variable_names = ( - "AWS_EXECUTION_ENV", "INSTANA_EXTRA_HTTP_HEADERS", - "INSTANA_ENDPOINT_URL", "INSTANA_ENDPOINT_PROXY", - "INSTANA_AGENT_KEY", "INSTANA_LOG_LEVEL", - "INSTANA_SERVICE_NAME", "INSTANA_SECRETS", "INSTANA_TAGS", - ) + "AWS_EXECUTION_ENV", + "INSTANA_EXTRA_HTTP_HEADERS", + "INSTANA_ENDPOINT_URL", + "INSTANA_ENDPOINT_PROXY", + "INSTANA_AGENT_KEY", + "INSTANA_LOG_LEVEL", + "INSTANA_SERVICE_NAME", + "INSTANA_SECRETS", + "INSTANA_TAGS", + ) for variable_name in variable_names: if variable_name in os.environ: @@ -52,19 +57,19 @@ def create_agent_and_setup_tracer(self): def test_secrets(self): self.create_agent_and_setup_tracer() - self.assertTrue(hasattr(self.agent.options, 'secrets_matcher')) - self.assertEqual(self.agent.options.secrets_matcher, 'contains-ignore-case') - self.assertTrue(hasattr(self.agent.options, 'secrets_list')) - self.assertEqual(self.agent.options.secrets_list, ['key', 'pass', 'secret']) + self.assertTrue(hasattr(self.agent.options, "secrets_matcher")) + self.assertEqual(self.agent.options.secrets_matcher, "contains-ignore-case") + self.assertTrue(hasattr(self.agent.options, "secrets_list")) + self.assertEqual(self.agent.options.secrets_list, ["key", "pass", "secret"]) def test_options_have_extra_http_headers(self): self.create_agent_and_setup_tracer() - self.assertTrue(hasattr(self.agent, 'options')) - self.assertTrue(hasattr(self.agent.options, 'extra_http_headers')) + self.assertTrue(hasattr(self.agent, "options")) + self.assertTrue(hasattr(self.agent.options, "extra_http_headers")) def test_has_options(self): self.create_agent_and_setup_tracer() - self.assertTrue(hasattr(self.agent, 'options')) + self.assertTrue(hasattr(self.agent, "options")) self.assertTrue(isinstance(self.agent.options, StandardOptions)) def test_agent_default_log_level(self): @@ -72,164 +77,142 @@ def test_agent_default_log_level(self): self.assertEqual(self.agent.options.log_level, logging.WARNING) def test_agent_instana_debug(self): - os.environ['INSTANA_DEBUG'] = "asdf" + os.environ["INSTANA_DEBUG"] = "asdf" self.create_agent_and_setup_tracer() self.assertEqual(self.agent.options.log_level, logging.DEBUG) def test_agent_instana_service_name(self): - os.environ['INSTANA_SERVICE_NAME'] = "greycake" + os.environ["INSTANA_SERVICE_NAME"] = "greycake" self.create_agent_and_setup_tracer() self.assertEqual(self.agent.options.service_name, "greycake") @patch.object(requests.Session, "put") def test_announce_is_successful(self, mock_requests_session_put): test_pid = 4242 - test_process_name = 'test_process' - test_process_args = ['-v', '-d'] - test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a' + test_process_name = "test_process" + test_process_args = ["-v", "-d"] + test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a" mock_response = MagicMock() mock_response.status_code = 200 mock_response.content = ( - '{' - f' "pid": {test_pid}, ' - f' "agentUuid": "{test_agent_uuid}"' - '}') + "{" f' "pid": {test_pid}, ' f' "agentUuid": "{test_agent_uuid}"' "}" + ) # This mocks the call to self.agent.client.put mock_requests_session_put.return_value = mock_response self.create_agent_and_setup_tracer() - d = Discovery(pid=test_pid, - name=test_process_name, args=test_process_args) + d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) payload = self.agent.announce(d) - self.assertIn('pid', payload) - self.assertEqual(test_pid, payload['pid']) - - self.assertIn('agentUuid', payload) - self.assertEqual(test_agent_uuid, payload['agentUuid']) + self.assertIn("pid", payload) + self.assertEqual(test_pid, payload["pid"]) + self.assertIn("agentUuid", payload) + self.assertEqual(test_agent_uuid, payload["agentUuid"]) @patch.object(requests.Session, "put") def test_announce_fails_with_non_200(self, mock_requests_session_put): test_pid = 4242 - test_process_name = 'test_process' - test_process_args = ['-v', '-d'] - test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a' + test_process_name = "test_process" + test_process_args = ["-v", "-d"] mock_response = MagicMock() mock_response.status_code = 404 - mock_response.content = '' + mock_response.content = "" mock_requests_session_put.return_value = mock_response self.create_agent_and_setup_tracer() - d = Discovery(pid=test_pid, - name=test_process_name, args=test_process_args) - with self.assertLogs(logger, level='DEBUG') as log: + d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) + with self.assertLogs(logger, level="DEBUG") as log: payload = self.agent.announce(d) self.assertIsNone(payload) self.assertEqual(len(log.output), 1) self.assertEqual(len(log.records), 1) - self.assertIn('response status code', log.output[0]) - self.assertIn('is NOT 200', log.output[0]) - + self.assertIn("response status code", log.output[0]) + self.assertIn("is NOT 200", log.output[0]) @patch.object(requests.Session, "put") def test_announce_fails_with_non_json(self, mock_requests_session_put): test_pid = 4242 - test_process_name = 'test_process' - test_process_args = ['-v', '-d'] - test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a' + test_process_name = "test_process" + test_process_args = ["-v", "-d"] mock_response = MagicMock() mock_response.status_code = 200 - mock_response.content = '' + mock_response.content = "" mock_requests_session_put.return_value = mock_response self.create_agent_and_setup_tracer() - d = Discovery(pid=test_pid, - name=test_process_name, args=test_process_args) - with self.assertLogs(logger, level='DEBUG') as log: + d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) + with self.assertLogs(logger, level="DEBUG") as log: payload = self.agent.announce(d) self.assertIsNone(payload) self.assertEqual(len(log.output), 1) self.assertEqual(len(log.records), 1) - self.assertIn('response is not JSON', log.output[0]) + self.assertIn("response is not JSON", log.output[0]) @patch.object(requests.Session, "put") def test_announce_fails_with_empty_list_json(self, mock_requests_session_put): test_pid = 4242 - test_process_name = 'test_process' - test_process_args = ['-v', '-d'] - test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a' + test_process_name = "test_process" + test_process_args = ["-v", "-d"] mock_response = MagicMock() mock_response.status_code = 200 - mock_response.content = '[]' + mock_response.content = "[]" mock_requests_session_put.return_value = mock_response self.create_agent_and_setup_tracer() - d = Discovery(pid=test_pid, - name=test_process_name, args=test_process_args) - with self.assertLogs(logger, level='DEBUG') as log: + d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) + with self.assertLogs(logger, level="DEBUG") as log: payload = self.agent.announce(d) self.assertIsNone(payload) self.assertEqual(len(log.output), 1) self.assertEqual(len(log.records), 1) - self.assertIn('payload has no fields', log.output[0]) - + self.assertIn("payload has no fields", log.output[0]) @patch.object(requests.Session, "put") def test_announce_fails_with_missing_pid(self, mock_requests_session_put): test_pid = 4242 - test_process_name = 'test_process' - test_process_args = ['-v', '-d'] - test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a' + test_process_name = "test_process" + test_process_args = ["-v", "-d"] + test_agent_uuid = "83bf1e09-ab16-4203-abf5-34ee0977023a" mock_response = MagicMock() mock_response.status_code = 200 - mock_response.content = ( - '{' - f' "agentUuid": "{test_agent_uuid}"' - '}') + mock_response.content = "{" f' "agentUuid": "{test_agent_uuid}"' "}" mock_requests_session_put.return_value = mock_response self.create_agent_and_setup_tracer() - d = Discovery(pid=test_pid, - name=test_process_name, args=test_process_args) - with self.assertLogs(logger, level='DEBUG') as log: + d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) + with self.assertLogs(logger, level="DEBUG") as log: payload = self.agent.announce(d) self.assertIsNone(payload) self.assertEqual(len(log.output), 1) self.assertEqual(len(log.records), 1) - self.assertIn('response payload has no pid', log.output[0]) - + self.assertIn("response payload has no pid", log.output[0]) @patch.object(requests.Session, "put") def test_announce_fails_with_missing_uuid(self, mock_requests_session_put): test_pid = 4242 - test_process_name = 'test_process' - test_process_args = ['-v', '-d'] - test_agent_uuid = '83bf1e09-ab16-4203-abf5-34ee0977023a' + test_process_name = "test_process" + test_process_args = ["-v", "-d"] mock_response = MagicMock() mock_response.status_code = 200 - mock_response.content = ( - '{' - f' "pid": {test_pid} ' - '}') + mock_response.content = "{" f' "pid": {test_pid} ' "}" mock_requests_session_put.return_value = mock_response self.create_agent_and_setup_tracer() - d = Discovery(pid=test_pid, - name=test_process_name, args=test_process_args) - with self.assertLogs(logger, level='DEBUG') as log: + d = Discovery(pid=test_pid, name=test_process_name, args=test_process_args) + with self.assertLogs(logger, level="DEBUG") as log: payload = self.agent.announce(d) self.assertIsNone(payload) self.assertEqual(len(log.output), 1) self.assertEqual(len(log.records), 1) - self.assertIn('response payload has no agentUuid', log.output[0]) + self.assertIn("response payload has no agentUuid", log.output[0]) @pytest.mark.original @patch.object(requests.Session, "get") @@ -242,8 +225,8 @@ def test_agent_connection_attempt(self, mock_requests_session_get): host = self.agent.options.agent_host port = self.agent.options.agent_port msg = f"Instana host agent found on {host}:{port}" - - with self.assertLogs(logger, level='DEBUG') as log: + + with self.assertLogs(logger, level="DEBUG") as log: result = self.agent.is_agent_listening(host, port) self.assertTrue(result) @@ -259,11 +242,13 @@ def test_agent_connection_attempt_fails_with_404(self, mock_requests_session_get self.create_agent_and_setup_tracer() host = self.agent.options.agent_host port = self.agent.options.agent_port - msg = "The attempt to connect to the Instana host agent on " \ - f"{host}:{port} has failed with an unexpected status code. " \ - f"Expected HTTP 200 but received: {mock_response.status_code}" + msg = ( + "The attempt to connect to the Instana host agent on " + f"{host}:{port} has failed with an unexpected status code. " + f"Expected HTTP 200 but received: {mock_response.status_code}" + ) - with self.assertLogs(logger, level='DEBUG') as log: + with self.assertLogs(logger, level="DEBUG") as log: result = self.agent.is_agent_listening(host, port) self.assertFalse(result) diff --git a/tests/platforms/test_host_collector.py b/tests/platforms/test_host_collector.py index 48b6fd3d4..c795ad083 100644 --- a/tests/platforms/test_host_collector.py +++ b/tests/platforms/test_host_collector.py @@ -25,7 +25,7 @@ def __init__(self, methodName="runTest"): self.original_tracer = get_tracer() def setUp(self): - self.webhook_sitedir_path = PATH_OF_AUTOTRACE_WEBHOOK_SITEDIR + '3.8.0' + self.webhook_sitedir_path = PATH_OF_AUTOTRACE_WEBHOOK_SITEDIR + "3.8.0" def tearDown(self): """Reset all environment variables of consequence""" diff --git a/tests/test_configurator.py b/tests/test_configurator.py index a95ee13d7..1637373c9 100644 --- a/tests/test_configurator.py +++ b/tests/test_configurator.py @@ -14,4 +14,4 @@ def tearDown(self): pass def test_has_default_config(self): - self.assertEqual(config['asyncio_task_context_propagation']['enabled'], False) \ No newline at end of file + self.assertEqual(config["asyncio_task_context_propagation"]["enabled"], False) diff --git a/tests/test_utils.py b/tests/test_utils.py index 2be9a2287..3d01e3739 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,7 @@ # (c) Copyright IBM Corp. 2021 # (c) Copyright Instana Inc. 2020 + class _TraceContextMixin: def assertTraceContextPropagated(self, parent_span, child_span): assert parent_span.t == child_span.t diff --git a/tests/util/test_secrets.py b/tests/util/test_secrets.py index 04346e856..2a2ee9df8 100644 --- a/tests/util/test_secrets.py +++ b/tests/util/test_secrets.py @@ -14,8 +14,8 @@ def tearDown(self): pass def test_equals_ignore_case(self): - matcher = 'equals-ignore-case' - kwlist = ['two'] + matcher = "equals-ignore-case" + kwlist = ["two"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -24,8 +24,8 @@ def test_equals_ignore_case(self): self.assertEqual(stripped, "one=1&Two=&THREE=&4='+'&five='okyeah'") def test_equals(self): - matcher = 'equals' - kwlist = ['Two'] + matcher = "equals" + kwlist = ["Two"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -34,8 +34,8 @@ def test_equals(self): self.assertEqual(stripped, "one=1&Two=&THREE=&4='+'&five='okyeah'") def test_equals_no_match(self): - matcher = 'equals' - kwlist = ['two'] + matcher = "equals" + kwlist = ["two"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -44,8 +44,8 @@ def test_equals_no_match(self): self.assertEqual(stripped, "one=1&Two=two&THREE=&4='+'&five='okyeah'") def test_contains_ignore_case(self): - matcher = 'contains-ignore-case' - kwlist = ['FI'] + matcher = "contains-ignore-case" + kwlist = ["FI"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -54,8 +54,8 @@ def test_contains_ignore_case(self): self.assertEqual(stripped, "one=1&Two=two&THREE=&4='+'&five=") def test_contains_ignore_case_no_match(self): - matcher = 'contains-ignore-case' - kwlist = ['XXXXXX'] + matcher = "contains-ignore-case" + kwlist = ["XXXXXX"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -64,8 +64,8 @@ def test_contains_ignore_case_no_match(self): self.assertEqual(stripped, "one=1&Two=two&THREE=&4='+'&five='okyeah'") def test_contains(self): - matcher = 'contains' - kwlist = ['fi'] + matcher = "contains" + kwlist = ["fi"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -74,8 +74,8 @@ def test_contains(self): self.assertEqual(stripped, "one=1&Two=two&THREE=&4='+'&five=") def test_contains_no_match(self): - matcher = 'contains' - kwlist = ['XXXXXX'] + matcher = "contains" + kwlist = ["XXXXXX"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -84,7 +84,7 @@ def test_contains_no_match(self): self.assertEqual(stripped, "one=1&Two=two&THREE=&4='+'&five='okyeah'") def test_regex(self): - matcher = 'regex' + matcher = "regex" kwlist = [r"\d"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -94,7 +94,7 @@ def test_regex(self): self.assertEqual(stripped, "one=1&Two=two&THREE=&4=&five='okyeah'") def test_regex_no_match(self): - matcher = 'regex' + matcher = "regex" kwlist = [r"\d\d\d"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -104,38 +104,45 @@ def test_regex_no_match(self): self.assertEqual(stripped, "one=1&Two=two&THREE=&4='+'&five='okyeah'") def test_equals_with_path_component(self): - matcher = 'equals' - kwlist = ['Two'] + matcher = "equals" + kwlist = ["Two"] query_params = "/signup?one=1&Two=two&THREE=&4='+'&five='okyeah'" stripped = strip_secrets_from_query(query_params, matcher, kwlist) - self.assertEqual(stripped, "/signup?one=1&Two=&THREE=&4='+'&five='okyeah'") + self.assertEqual( + stripped, "/signup?one=1&Two=&THREE=&4='+'&five='okyeah'" + ) def test_equals_with_full_url(self): - matcher = 'equals' - kwlist = ['Two'] + matcher = "equals" + kwlist = ["Two"] - query_params = "http://www.x.org/signup?one=1&Two=two&THREE=&4='+'&five='okyeah'" + query_params = ( + "http://www.x.org/signup?one=1&Two=two&THREE=&4='+'&five='okyeah'" + ) stripped = strip_secrets_from_query(query_params, matcher, kwlist) - self.assertEqual(stripped, "http://www.x.org/signup?one=1&Two=&THREE=&4='+'&five='okyeah'") + self.assertEqual( + stripped, + "http://www.x.org/signup?one=1&Two=&THREE=&4='+'&five='okyeah'", + ) def test_equals_with_none(self): - matcher = 'equals' - kwlist = ['Two'] + matcher = "equals" + kwlist = ["Two"] query_params = None stripped = strip_secrets_from_query(query_params, matcher, kwlist) - self.assertEqual('', stripped) + self.assertEqual("", stripped) def test_bad_matcher(self): - matcher = 'BADCAFE' - kwlist = ['Two'] + matcher = "BADCAFE" + kwlist = ["Two"] query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" @@ -144,7 +151,7 @@ def test_bad_matcher(self): self.assertEqual(stripped, "one=1&Two=two&THREE=&4='+'&five='okyeah'") def test_bad_kwlist(self): - matcher = 'equals' + matcher = "equals" kwlist = None query_params = "one=1&Two=two&THREE=&4='+'&five='okyeah'" diff --git a/tests/util/test_util.py b/tests/util/test_util.py index 3aa2db6a6..374badf65 100644 --- a/tests/util/test_util.py +++ b/tests/util/test_util.py @@ -15,7 +15,9 @@ def test_validate_url(self): self.assertTrue(validate_url("http://127.0.0.1")) self.assertTrue(validate_url("https://10.0.12.221/")) self.assertTrue(validate_url("http://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/")) - self.assertTrue(validate_url("https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/")) + self.assertTrue( + validate_url("https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/") + ) self.assertFalse(validate_url("boligrafo")) self.assertFalse(validate_url("http:boligrafo")) self.assertFalse(validate_url(None)) diff --git a/tests/w3c_trace_context/test_traceparent.py b/tests/w3c_trace_context/test_traceparent.py index 3eb83a4e7..88ff00f04 100644 --- a/tests/w3c_trace_context/test_traceparent.py +++ b/tests/w3c_trace_context/test_traceparent.py @@ -17,7 +17,9 @@ def test_validate_valid(self): def test_validate_newer_version(self): # Although the incoming traceparent header sports a newer version number, we should still be able to parse the # parts that we understand (and consider it valid). - traceparent = "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd" + traceparent = ( + "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd" + ) self.assertEqual(traceparent, self.tp.validate(traceparent)) def test_validate_unknown_flags(self): @@ -38,14 +40,18 @@ def test_validate_traceparent_None(self): def test_get_traceparent_fields(self): traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01" - version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent) + version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields( + traceparent + ) self.assertEqual(trace_id, 11803532876627986230) self.assertEqual(parent_id, 67667974448284343) self.assertTrue(sampled_flag) def test_get_traceparent_fields_unsampled(self): traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00" - version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent) + version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields( + traceparent + ) self.assertEqual(trace_id, 11803532876627986230) self.assertEqual(parent_id, 67667974448284343) self.assertFalse(sampled_flag) @@ -53,29 +59,39 @@ def test_get_traceparent_fields_unsampled(self): def test_get_traceparent_fields_newer_version(self): # Although the incoming traceparent header sports a newer version number, we should still be able to parse the # parts that we understand (and consider it valid). - traceparent = "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd" - version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent) + traceparent = ( + "fe-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01-12345-abcd" + ) + version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields( + traceparent + ) self.assertEqual(trace_id, 11803532876627986230) self.assertEqual(parent_id, 67667974448284343) self.assertTrue(sampled_flag) def test_get_traceparent_fields_unknown_flags(self): traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-ff" - version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent) + version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields( + traceparent + ) self.assertEqual(trace_id, 11803532876627986230) self.assertEqual(parent_id, 67667974448284343) self.assertTrue(sampled_flag) def test_get_traceparent_fields_None_input(self): traceparent = None - version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent) + version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields( + traceparent + ) self.assertIsNone(trace_id) self.assertIsNone(parent_id) self.assertFalse(sampled_flag) def test_get_traceparent_fields_string_input_no_dash(self): traceparent = "invalid" - version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields(traceparent) + version, trace_id, parent_id, sampled_flag = self.tp.get_traceparent_fields( + traceparent + ) self.assertIsNone(trace_id) self.assertIsNone(parent_id) self.assertFalse(sampled_flag) @@ -87,7 +103,10 @@ def test_update_traceparent(self): in_span_id = "1234567890abcdef" level = 1 expected_traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-1234567890abcdef-01" - self.assertEqual(expected_traceparent, self.tp.update_traceparent(traceparent, in_trace_id, in_span_id, level)) + self.assertEqual( + expected_traceparent, + self.tp.update_traceparent(traceparent, in_trace_id, in_span_id, level), + ) @pytest.mark.skip("Handled when type of trace and span ids are modified to str") def test_update_traceparent_None(self): @@ -96,4 +115,7 @@ def test_update_traceparent_None(self): in_span_id = "7890abcdef" level = 0 expected_traceparent = "00-00000000000000001234d0e0e4736234-0000007890abcdef-00" - self.assertEqual(expected_traceparent, self.tp.update_traceparent(traceparent, in_trace_id, in_span_id, level)) + self.assertEqual( + expected_traceparent, + self.tp.update_traceparent(traceparent, in_trace_id, in_span_id, level), + ) diff --git a/tests/w3c_trace_context/test_tracestate.py b/tests/w3c_trace_context/test_tracestate.py index 8bc0ce222..648756c67 100644 --- a/tests/w3c_trace_context/test_tracestate.py +++ b/tests/w3c_trace_context/test_tracestate.py @@ -28,44 +28,65 @@ def test_update_tracestate(self): in_trace_id = "1234d0e0e4736234" in_span_id = "1234567890abcdef" expected_tracestate = "in=1234d0e0e4736234;1234567890abcdef,congo=t61rcWkgMzE" - self.assertEqual(expected_tracestate, self.ts.update_tracestate(tracestate, in_trace_id, in_span_id)) + self.assertEqual( + expected_tracestate, + self.ts.update_tracestate(tracestate, in_trace_id, in_span_id), + ) def test_update_tracestate_None(self): tracestate = None in_trace_id = "1234d0e0e4736234" in_span_id = "1234567890abcdef" expected_tracestate = "in=1234d0e0e4736234;1234567890abcdef" - self.assertEqual(expected_tracestate, self.ts.update_tracestate(tracestate, in_trace_id, in_span_id)) + self.assertEqual( + expected_tracestate, + self.ts.update_tracestate(tracestate, in_trace_id, in_span_id), + ) def test_update_tracestate_more_than_32_members_already(self): - tracestate = "congo=t61rcWkgMzE,robo=1221213jdfjkdsfjsd,alpha=5889fnjkllllllll," \ - "beta=aslsdklkljfdshasfaskkfnnnsdsd,gamadeltaepsilonpirpsigma=125646845613675451535445155126666fgsdfdsfjsdfhsdfsdsdsaddfasfdfdsfdsfsd;qwertyuiopasdfghjklzxcvbnm1234567890," \ - "b=121,c=23344,d=asd,e=ldkfj,f=1212121,g=sadahsda,h=jjhdada,i=eerjrjrr,j=sadsasd,k=44444,l=dadadad," \ - "m=rrrr,n=3424jdg,p=ffss,q=12,r=3,s=5,t=u5,u=43,v=gj,w=wew,x=23123,y=sdf,z=kasdl,aa=dsdas,ab=res," \ - "ac=trwa,ad=kll,ae=pds" + tracestate = ( + "congo=t61rcWkgMzE,robo=1221213jdfjkdsfjsd,alpha=5889fnjkllllllll," + "beta=aslsdklkljfdshasfaskkfnnnsdsd,gamadeltaepsilonpirpsigma=125646845613675451535445155126666fgsdfdsfjsdfhsdfsdsdsaddfasfdfdsfdsfsd;qwertyuiopasdfghjklzxcvbnm1234567890," + "b=121,c=23344,d=asd,e=ldkfj,f=1212121,g=sadahsda,h=jjhdada,i=eerjrjrr,j=sadsasd,k=44444,l=dadadad," + "m=rrrr,n=3424jdg,p=ffss,q=12,r=3,s=5,t=u5,u=43,v=gj,w=wew,x=23123,y=sdf,z=kasdl,aa=dsdas,ab=res," + "ac=trwa,ad=kll,ae=pds" + ) in_trace_id = "1234d0e0e4736234" in_span_id = "1234567890abcdef" - expected_tracestate = "in=1234d0e0e4736234;1234567890abcdef,congo=t61rcWkgMzE,robo=1221213jdfjkdsfjsd," \ - "alpha=5889fnjkllllllll,beta=aslsdklkljfdshasfaskkfnnnsdsd,b=121,c=23344,d=asd,e=ldkfj," \ - "f=1212121,g=sadahsda,h=jjhdada,i=eerjrjrr,j=sadsasd,k=44444,l=dadadad,m=rrrr,n=3424jdg," \ - "p=ffss,q=12,r=3,s=5,t=u5,u=43,v=gj,w=wew,x=23123,y=sdf,z=kasdl,aa=dsdas,ab=res,ac=trwa" - actual_tracestate = self.ts.update_tracestate(tracestate, in_trace_id, in_span_id) + expected_tracestate = ( + "in=1234d0e0e4736234;1234567890abcdef,congo=t61rcWkgMzE,robo=1221213jdfjkdsfjsd," + "alpha=5889fnjkllllllll,beta=aslsdklkljfdshasfaskkfnnnsdsd,b=121,c=23344,d=asd,e=ldkfj," + "f=1212121,g=sadahsda,h=jjhdada,i=eerjrjrr,j=sadsasd,k=44444,l=dadadad,m=rrrr,n=3424jdg," + "p=ffss,q=12,r=3,s=5,t=u5,u=43,v=gj,w=wew,x=23123,y=sdf,z=kasdl,aa=dsdas,ab=res,ac=trwa" + ) + actual_tracestate = self.ts.update_tracestate( + tracestate, in_trace_id, in_span_id + ) self.assertEqual(len(tracestate.split(",")), 34) # input had 34 list members - self.assertEqual(len(actual_tracestate.split(",")), 32) # output has 32 list members, 3 removed and 1 added + self.assertEqual( + len(actual_tracestate.split(",")), 32 + ) # output has 32 list members, 3 removed and 1 added self.assertEqual(expected_tracestate, actual_tracestate) - self.assertNotIn("gamadeltaepsilonpirpsigma", - actual_tracestate) # member longer than 128 characters gets removed + self.assertNotIn( + "gamadeltaepsilonpirpsigma", actual_tracestate + ) # member longer than 128 characters gets removed def test_update_tracestate_empty_string(self): tracestate = "" in_trace_id = "1234d0e0e4736234" in_span_id = "1234567890abcdef" expected_tracestate = "in=1234d0e0e4736234;1234567890abcdef" - self.assertEqual(expected_tracestate, self.ts.update_tracestate(tracestate, in_trace_id, in_span_id)) + self.assertEqual( + expected_tracestate, + self.ts.update_tracestate(tracestate, in_trace_id, in_span_id), + ) def test_update_tracestate_exception(self): tracestate = [] in_trace_id = "1234d0e0e4736234" in_span_id = "1234567890abcdef" expected_tracestate = [] - self.assertEqual(expected_tracestate, self.ts.update_tracestate(tracestate, in_trace_id, in_span_id)) \ No newline at end of file + self.assertEqual( + expected_tracestate, + self.ts.update_tracestate(tracestate, in_trace_id, in_span_id), + ) diff --git a/tests_aws/01_lambda/test_lambda.py b/tests_aws/01_lambda/test_lambda.py index a0e176249..1e0ad05ed 100644 --- a/tests_aws/01_lambda/test_lambda.py +++ b/tests_aws/01_lambda/test_lambda.py @@ -23,6 +23,7 @@ if TYPE_CHECKING: from instana.span.span import InstanaSpan + # Mock Context object class MockContext(dict): def __init__(self, **kwargs: Dict[str, Any]) -> None: @@ -80,7 +81,7 @@ def _resource(self) -> Generator[None, None, None]: self.agent: AWSLambdaAgent = get_agent() yield # tearDown - # Reset collector config + # Reset collector config self.agent.collector.snapshot_data_sent = False # Reset all environment variables of consequence if "AWS_EXECUTION_ENV" in os.environ: @@ -183,7 +184,7 @@ def test_custom_service_name(self, trace_id: int, span_id: int) -> None: os.environ["INSTANA_AGENT_KEY"] = "Fake_Key" # We need reset the AWSLambdaOptions with new INSTANA_SERVICE_NAME self.agent.options = AWSLambdaOptions() - + with open( self.pwd + "/../data/lambda/api_gateway_event.json", "r" ) as json_file: @@ -778,7 +779,9 @@ def test_arn_parsing(self) -> None: def test_agent_default_log_level(self) -> None: assert self.agent.options.log_level == logging.WARNING - def __validate_result_and_payload_for_gateway_v2_trace(self, result: Dict[str, Any], payload: defaultdict) -> "InstanaSpan": + def __validate_result_and_payload_for_gateway_v2_trace( + self, result: Dict[str, Any], payload: defaultdict + ) -> "InstanaSpan": assert isinstance(result, dict) assert "headers" in result assert "Server-Timing" in result["headers"] @@ -833,4 +836,4 @@ def __validate_result_and_payload_for_gateway_v2_trace(self, result: Dict[str, A assert span.data["http"]["path_tpl"] == "/my/{resource}" assert span.data["http"]["params"] == "secret=key&q=term" - return span \ No newline at end of file + return span diff --git a/tests_aws/02_fargate/conftest.py b/tests_aws/02_fargate/conftest.py index d249421a7..4edea235f 100644 --- a/tests_aws/02_fargate/conftest.py +++ b/tests_aws/02_fargate/conftest.py @@ -5,6 +5,7 @@ from instana.collector.aws_fargate import AWSFargateCollector + # Mocking AWSFargateCollector.get_ecs_metadata() @pytest.fixture(autouse=True) def get_ecs_metadata(monkeypatch, request) -> None: @@ -16,6 +17,10 @@ def _always_true(_: object) -> bool: if "original" in request.keywords: # If using the `@pytest.mark.original` marker before the test function, # uses the original AWSFargateCollector.get_ecs_metadata() - monkeypatch.setattr(AWSFargateCollector, "get_ecs_metadata", AWSFargateCollector.get_ecs_metadata) + monkeypatch.setattr( + AWSFargateCollector, + "get_ecs_metadata", + AWSFargateCollector.get_ecs_metadata, + ) else: monkeypatch.setattr(AWSFargateCollector, "get_ecs_metadata", _always_true) diff --git a/tests_aws/02_fargate/test_fargate.py b/tests_aws/02_fargate/test_fargate.py index 551b09686..dce29859f 100644 --- a/tests_aws/02_fargate/test_fargate.py +++ b/tests_aws/02_fargate/test_fargate.py @@ -9,7 +9,6 @@ from instana.agent.aws_fargate import AWSFargateAgent from instana.options import AWSFargateOptions -from instana.singletons import get_agent class TestFargate: diff --git a/tests_aws/02_fargate/test_fargate_collector.py b/tests_aws/02_fargate/test_fargate_collector.py index 673b7c784..e0e46a873 100644 --- a/tests_aws/02_fargate/test_fargate_collector.py +++ b/tests_aws/02_fargate/test_fargate_collector.py @@ -8,7 +8,6 @@ import pytest from instana.agent.aws_fargate import AWSFargateAgent -from instana.singletons import get_agent def get_docker_plugin(plugins): @@ -82,7 +81,7 @@ def _resource(self) -> Generator[None, None, None]: os.environ.pop("INSTANA_ZONE") if "INSTANA_TAGS" in os.environ: os.environ.pop("INSTANA_TAGS") - + self.agent.collector.snapshot_data_last_sent = 0 _unset_ecs_metadata(self.agent) diff --git a/tests_aws/03_eks/test_eksfargate.py b/tests_aws/03_eks/test_eksfargate.py index 6f7984d9d..59b23cee3 100644 --- a/tests_aws/03_eks/test_eksfargate.py +++ b/tests_aws/03_eks/test_eksfargate.py @@ -8,7 +8,6 @@ from instana.agent.aws_eks_fargate import EKSFargateAgent from instana.options import EKSFargateOptions -from instana.singletons import get_agent class TestEKSFargate: diff --git a/tests_aws/03_eks/test_eksfargate_collector.py b/tests_aws/03_eks/test_eksfargate_collector.py index 32f8f93ef..0c1f64718 100644 --- a/tests_aws/03_eks/test_eksfargate_collector.py +++ b/tests_aws/03_eks/test_eksfargate_collector.py @@ -6,7 +6,6 @@ import pytest from instana.agent.aws_eks_fargate import EKSFargateAgent -from instana.singletons import get_agent class TestEKSFargateCollector: