Skip to content

Commit 2b5870f

Browse files
committed
chore: use get_tracer() for all tracer access
Signed-off-by: Cagri Yonca <[email protected]>
1 parent 9369fdd commit 2b5870f

File tree

95 files changed

+1473
-1124
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1473
-1124
lines changed

example/autoprofile/app.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
import sys
88
import os
99

10-
sys.path.append('../..')
11-
os.environ['INSTANA_DEBUG'] = 'yes'
12-
os.environ['INSTANA_AUTOPROFILE'] = 'yes'
13-
import instana
10+
sys.path.append("../..")
11+
os.environ["INSTANA_DEBUG"] = "yes"
12+
os.environ["INSTANA_AUTOPROFILE"] = "yes"
13+
import instana # noqa: F401
14+
1415

1516
# Simulate CPU intensive work
1617
def simulate_cpu():
@@ -27,14 +28,15 @@ def simulate_mem_leak():
2728
for j in range(0, 1800):
2829
mem2 = []
2930
for i in range(0, 1000):
30-
obj1 = {'v': random.randint(0, 1000000)}
31+
obj1 = {"v": random.randint(0, 1000000)}
3132
mem1.append(obj1)
3233

33-
obj2 = {'v': random.randint(0, 1000000)}
34+
obj2 = {"v": random.randint(0, 1000000)}
3435
mem2.append(obj2)
3536

3637
time.sleep(1)
3738

39+
3840
threading.Thread(target=simulate_mem_leak).start()
3941

4042

@@ -48,13 +50,14 @@ def lock_wait():
4850

4951
while True:
5052
lock.acquire()
51-
53+
5254
threading.Thread(target=lock_wait).start()
5355

5456
time.sleep(1)
5557
lock.release()
5658
time.sleep(1)
5759

60+
5861
threading.Thread(target=simulate_lock).start()
5962

6063

example/carry_context.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
import uvloop
1212
import aiohttp
1313

14-
from instana.singletons import tracer, async_tracer
14+
from instana.singletons import get_tracer
1515

1616
uvloop.install()
1717

18+
tracer = get_tracer()
19+
20+
1821
async def launch_async_calls(parent_span):
1922
"""
2023
Method to launch a series (1 currently) of asynchronous http calls
@@ -24,24 +27,24 @@ async def launch_async_calls(parent_span):
2427

2528
# Now that we are inside of the event loop, first thing to do is to initialize
2629
# the tracing context using <parent_span> _and_ the asynchronous tracer <async_tracer>
27-
with async_tracer.start_active_span('launch_async_calls', child_of=parent_span):
30+
with async_tracer.start_active_span("launch_async_calls", child_of=parent_span): # noqa: F821
2831
async with aiohttp.ClientSession() as session:
2932
session.get("http://127.0.0.1/api/v2/endpoint/1")
3033
session.get("http://127.0.0.1/api/v2/endpoint/2")
3134
session.get("http://127.0.0.1/api/v2/endpoint/3")
3235

36+
3337
#
3438
# Synchronous application code such as from inside a Django or Flask handler
3539
#
3640

3741
# Start an ENTRY span in our synchronous execution scope
3842
with tracer.start_active_span("launch_uvloop") as sync_scope:
39-
sync_scope.span.set_tag('span.kind', 'entry')
43+
sync_scope.span.set_tag("span.kind", "entry")
4044

4145
# You can also retrieve the currently active span with:
4246
# tracer.active_span
4347

4448
# Launch our requests asynchronously
4549
# Enter the event loop and pass in the parent tracing context (sync_scope) manually
4650
asyncio.run(launch_async_calls(sync_scope.span))
47-

src/instana/agent/aws_eks_fargate.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
The Instana agent (for AWS EKS Fargate) that manages
55
monitoring state and reporting that data.
66
"""
7-
import os
8-
import time
7+
98
from instana.options import EKSFargateOptions
109
from instana.collector.aws_eks_fargate import EKSFargateCollector
1110
from instana.collector.helpers.eks.process import get_pod_name
@@ -16,7 +15,8 @@
1615

1716

1817
class EKSFargateAgent(BaseAgent):
19-
""" In-process agent for AWS Fargate """
18+
"""In-process agent for AWS Fargate"""
19+
2020
def __init__(self):
2121
super(EKSFargateAgent, self).__init__()
2222

@@ -29,15 +29,20 @@ def __init__(self):
2929
# Update log level (if INSTANA_LOG_LEVEL was set)
3030
self.update_log_level()
3131

32-
logger.info("Stan is on the EKS Pod on AWS Fargate scene. Starting Instana instrumentation version: %s", VERSION)
32+
logger.info(
33+
"Stan is on the EKS Pod on AWS Fargate scene. Starting Instana instrumentation version: %s",
34+
VERSION,
35+
)
3336

3437
if self._validate_options():
3538
self._can_send = True
3639
self.collector = EKSFargateCollector(self)
3740
self.collector.start()
3841
else:
39-
logger.warning("Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. "
40-
"We will not be able to monitor this Pod.")
42+
logger.warning(
43+
"Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. "
44+
"We will not be able to monitor this Pod."
45+
)
4146

4247
def can_send(self):
4348
"""
@@ -52,7 +57,7 @@ def get_from_structure(self):
5257
@return: dict()
5358
"""
5459

55-
return {'hl': True, 'cp': 'k8s', 'e': self.podname}
60+
return {"hl": True, "cp": "k8s", "e": self.podname}
5661

5762
def report_data_payload(self, payload):
5863
"""
@@ -67,15 +72,20 @@ def report_data_payload(self, payload):
6772
self.report_headers["X-Instana-Host"] = self.podname
6873
self.report_headers["X-Instana-Key"] = self.options.agent_key
6974

70-
response = self.client.post(self.__data_bundle_url(),
71-
data=to_json(payload),
72-
headers=self.report_headers,
73-
timeout=self.options.timeout,
74-
verify=self.options.ssl_verify,
75-
proxies=self.options.endpoint_proxy)
75+
response = self.client.post(
76+
self.__data_bundle_url(),
77+
data=to_json(payload),
78+
headers=self.report_headers,
79+
timeout=self.options.timeout,
80+
verify=self.options.ssl_verify,
81+
proxies=self.options.endpoint_proxy,
82+
)
7683

7784
if not 200 <= response.status_code < 300:
78-
logger.info("report_data_payload: Instana responded with status code %s", response.status_code)
85+
logger.info(
86+
"report_data_payload: Instana responded with status code %s",
87+
response.status_code,
88+
)
7989
except Exception as exc:
8090
logger.debug("report_data_payload: connection error (%s)", type(exc))
8191
return response
@@ -84,7 +94,9 @@ def _validate_options(self):
8494
"""
8595
Validate that the options used by this Agent are valid. e.g. can we report data?
8696
"""
87-
return self.options.endpoint_url is not None and self.options.agent_key is not None
97+
return (
98+
self.options.endpoint_url is not None and self.options.agent_key is not None
99+
)
88100

89101
def __data_bundle_url(self):
90102
"""

src/instana/agent/aws_fargate.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
The Instana agent (for AWS Fargate) that manages
66
monitoring state and reporting that data.
77
"""
8-
import time
8+
99
from instana.options import AWSFargateOptions
1010
from instana.collector.aws_fargate import AWSFargateCollector
1111
from ..log import logger
@@ -15,7 +15,8 @@
1515

1616

1717
class AWSFargateAgent(BaseAgent):
18-
""" In-process agent for AWS Fargate """
18+
"""In-process agent for AWS Fargate"""
19+
1920
def __init__(self):
2021
super(AWSFargateAgent, self).__init__()
2122

@@ -27,15 +28,20 @@ def __init__(self):
2728
# Update log level (if INSTANA_LOG_LEVEL was set)
2829
self.update_log_level()
2930

30-
logger.info("Stan is on the AWS Fargate scene. Starting Instana instrumentation version: %s", VERSION)
31+
logger.info(
32+
"Stan is on the AWS Fargate scene. Starting Instana instrumentation version: %s",
33+
VERSION,
34+
)
3135

3236
if self._validate_options():
3337
self._can_send = True
3438
self.collector = AWSFargateCollector(self)
3539
self.collector.start()
3640
else:
37-
logger.warning("Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. "
38-
"We will not be able monitor this AWS Fargate cluster.")
41+
logger.warning(
42+
"Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. "
43+
"We will not be able monitor this AWS Fargate cluster."
44+
)
3945

4046
def can_send(self):
4147
"""
@@ -49,7 +55,7 @@ def get_from_structure(self):
4955
Retrieves the From data that is reported alongside monitoring data.
5056
@return: dict()
5157
"""
52-
return {'hl': True, 'cp': 'aws', 'e': self.collector.get_fq_arn()}
58+
return {"hl": True, "cp": "aws", "e": self.collector.get_fq_arn()}
5359

5460
def report_data_payload(self, payload):
5561
"""
@@ -64,15 +70,20 @@ def report_data_payload(self, payload):
6470
self.report_headers["X-Instana-Host"] = self.collector.get_fq_arn()
6571
self.report_headers["X-Instana-Key"] = self.options.agent_key
6672

67-
response = self.client.post(self.__data_bundle_url(),
68-
data=to_json(payload),
69-
headers=self.report_headers,
70-
timeout=self.options.timeout,
71-
verify=self.options.ssl_verify,
72-
proxies=self.options.endpoint_proxy)
73+
response = self.client.post(
74+
self.__data_bundle_url(),
75+
data=to_json(payload),
76+
headers=self.report_headers,
77+
timeout=self.options.timeout,
78+
verify=self.options.ssl_verify,
79+
proxies=self.options.endpoint_proxy,
80+
)
7381

7482
if not 200 <= response.status_code < 300:
75-
logger.info("report_data_payload: Instana responded with status code %s", response.status_code)
83+
logger.info(
84+
"report_data_payload: Instana responded with status code %s",
85+
response.status_code,
86+
)
7687
except Exception as exc:
7788
logger.debug("report_data_payload: connection error (%s)", type(exc))
7889
return response
@@ -81,7 +92,9 @@ def _validate_options(self):
8192
"""
8293
Validate that the options used by this Agent are valid. e.g. can we report data?
8394
"""
84-
return self.options.endpoint_url is not None and self.options.agent_key is not None
95+
return (
96+
self.options.endpoint_url is not None and self.options.agent_key is not None
97+
)
8598

8699
def __data_bundle_url(self):
87100
"""

src/instana/agent/google_cloud_run.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
The Instana agent (for GCR) that manages
66
monitoring state and reporting that data.
77
"""
8-
import time
8+
99
from instana.options import GCROptions
1010
from instana.collector.google_cloud_run import GCRCollector
1111
from instana.log import logger
@@ -15,7 +15,7 @@
1515

1616

1717
class GCRAgent(BaseAgent):
18-
""" In-process agent for Google Cloud Run """
18+
"""In-process agent for Google Cloud Run"""
1919

2020
def __init__(self, service, configuration, revision):
2121
super(GCRAgent, self).__init__()
@@ -28,15 +28,20 @@ def __init__(self, service, configuration, revision):
2828
# Update log level (if INSTANA_LOG_LEVEL was set)
2929
self.update_log_level()
3030

31-
logger.info("Stan is on the AWS Fargate scene. Starting Instana instrumentation version: %s", VERSION)
31+
logger.info(
32+
"Stan is on the AWS Fargate scene. Starting Instana instrumentation version: %s",
33+
VERSION,
34+
)
3235

3336
if self._validate_options():
3437
self._can_send = True
3538
self.collector = GCRCollector(self, service, configuration, revision)
3639
self.collector.start()
3740
else:
38-
logger.warning("Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. "
39-
"We will not be able monitor this GCR cluster.")
41+
logger.warning(
42+
"Required INSTANA_AGENT_KEY and/or INSTANA_ENDPOINT_URL environment variables not set. "
43+
"We will not be able monitor this GCR cluster."
44+
)
4045

4146
def can_send(self):
4247
"""
@@ -50,7 +55,7 @@ def get_from_structure(self):
5055
Retrieves the From data that is reported alongside monitoring data.
5156
@return: dict()
5257
"""
53-
return {'hl': True, 'cp': 'gcp', 'e': self.collector.get_instance_id()}
58+
return {"hl": True, "cp": "gcp", "e": self.collector.get_instance_id()}
5459

5560
def report_data_payload(self, payload):
5661
"""
@@ -63,19 +68,25 @@ def report_data_payload(self, payload):
6368
self.report_headers = {
6469
"Content-Type": "application/json",
6570
"X-Instana-Host": "gcp:cloud-run:revision:{revision}".format(
66-
revision=self.collector.revision),
67-
"X-Instana-Key": self.options.agent_key
71+
revision=self.collector.revision
72+
),
73+
"X-Instana-Key": self.options.agent_key,
6874
}
6975

70-
response = self.client.post(self.__data_bundle_url(),
71-
data=to_json(payload),
72-
headers=self.report_headers,
73-
timeout=self.options.timeout,
74-
verify=self.options.ssl_verify,
75-
proxies=self.options.endpoint_proxy)
76+
response = self.client.post(
77+
self.__data_bundle_url(),
78+
data=to_json(payload),
79+
headers=self.report_headers,
80+
timeout=self.options.timeout,
81+
verify=self.options.ssl_verify,
82+
proxies=self.options.endpoint_proxy,
83+
)
7684

7785
if response.status_code >= 400:
78-
logger.info("report_data_payload: Instana responded with status code %s", response.status_code)
86+
logger.info(
87+
"report_data_payload: Instana responded with status code %s",
88+
response.status_code,
89+
)
7990
except Exception as exc:
8091
logger.debug("report_data_payload: connection error (%s)", type(exc))
8192
return response
@@ -84,7 +95,9 @@ def _validate_options(self):
8495
"""
8596
Validate that the options used by this Agent are valid. e.g. can we report data?
8697
"""
87-
return self.options.endpoint_url is not None and self.options.agent_key is not None
98+
return (
99+
self.options.endpoint_url is not None and self.options.agent_key is not None
100+
)
88101

89102
def __data_bundle_url(self):
90103
"""

src/instana/collector/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from opentelemetry.trace import SpanKind
77

88
from instana.util.ids import hex_id
9+
910
if TYPE_CHECKING:
1011
from instana.span.base_span import BaseSpan
1112

@@ -25,6 +26,6 @@ def format_span(
2526
span.p = format_span_id(span.p) if span.p else None
2627
span.lt = hex_id(span.lt) if hasattr(span, "lt") else None
2728
if isinstance(span.k, SpanKind):
28-
span.k = span.k.value if not span.k is SpanKind.INTERNAL else 3
29+
span.k = span.k.value if span.k is not SpanKind.INTERNAL else 3
2930
spans.append(span)
3031
return spans

0 commit comments

Comments
 (0)