Skip to content

Commit bd937a0

Browse files
lrafeeinewrelic-python-agent-team
authored andcommitted
[MegaLinter] Apply linters fixes
1 parent 7781c71 commit bd937a0

File tree

7 files changed

+99
-228
lines changed

7 files changed

+99
-228
lines changed

newrelic/api/opentelemetry.py

Lines changed: 35 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,19 @@
1616
from contextlib import contextmanager
1717

1818
from opentelemetry import trace as otel_api_trace
19+
1920
from newrelic.api.application import application_instance, register_application
20-
from newrelic.api.web_transaction import WebTransaction
2121
from newrelic.api.background_task import BackgroundTask
22-
from newrelic.api.message_transaction import MessageTransaction
23-
from newrelic.api.function_trace import FunctionTrace
2422
from newrelic.api.datastore_trace import DatastoreTrace
25-
from newrelic.api.message_trace import MessageTrace
2623
from newrelic.api.external_trace import ExternalTrace
24+
from newrelic.api.function_trace import FunctionTrace
25+
from newrelic.api.message_trace import MessageTrace
26+
from newrelic.api.message_transaction import MessageTransaction
2727
from newrelic.api.time_trace import current_trace, notice_error
28-
from newrelic.api.transaction import (
29-
current_transaction,
30-
record_custom_metric,
31-
record_dimensional_metric,
32-
Sentinel,
33-
)
28+
from newrelic.api.transaction import Sentinel, current_transaction
29+
from newrelic.api.web_transaction import WebTransaction
3430
from newrelic.core.otlp_utils import create_resource
3531

36-
3732
# Attributes that help distinguish span types are
3833
# sometimes added after span creation and sometimes
3934
# they are added during span creation. During the case
@@ -95,11 +90,13 @@ def __init__(
9590
self.otel_parent = parent
9691
self.attributes = attributes or {}
9792
self.kind = kind
98-
self.nr_transaction = nr_transaction or current_transaction() # This attribute is purely to prevent garbage collection
93+
self.nr_transaction = (
94+
nr_transaction or current_transaction()
95+
) # This attribute is purely to prevent garbage collection
9996
self.nr_trace = None
10097
self.instrumenting_module = instrumenting_module
10198

102-
# We should never reach this point. Leave this in
99+
# We should never reach this point. Leave this in
103100
# for debug purposes but eventually take this out.
104101
if not self.nr_transaction:
105102
raise ValueError("HOW DID WE GET HERE??")
@@ -108,10 +105,7 @@ def __init__(
108105
current_nr_trace = current_trace()
109106
if (
110107
not self.otel_parent
111-
or (
112-
self.otel_parent
113-
and self.otel_parent.span_id == int(current_nr_trace.guid, 16)
114-
)
108+
or (self.otel_parent and self.otel_parent.span_id == int(current_nr_trace.guid, 16))
115109
or (self.otel_parent and isinstance(current_nr_trace, Sentinel))
116110
):
117111
# Expected to come here if one of three scenarios have occured:
@@ -129,11 +123,7 @@ def __init__(
129123
raise ValueError("Unexpected span parent scenario encountered")
130124

131125
if nr_trace_type == FunctionTrace:
132-
trace_kwargs = {
133-
"name": self.name,
134-
"params": self.attributes,
135-
"parent": self.nr_parent,
136-
}
126+
trace_kwargs = {"name": self.name, "params": self.attributes, "parent": self.nr_parent}
137127
self.nr_trace = nr_trace_type(**trace_kwargs)
138128
elif nr_trace_type == DatastoreTrace:
139129
trace_kwargs = {
@@ -165,41 +155,30 @@ def __init__(
165155
else:
166156
# TODO: Still need to implement GraphQLOperationTrace
167157
# and GraphQLResolverTrace
168-
trace_kwargs = {
169-
"name": self.name,
170-
"params": self.attributes,
171-
"parent": self.nr_parent,
172-
}
158+
trace_kwargs = {"name": self.name, "params": self.attributes, "parent": self.nr_parent}
173159
self.nr_trace = nr_trace_type(**trace_kwargs)
174160

175161
self.nr_trace.__enter__()
176162

177163
def _is_sampled(self):
178164
# Uses NR to determine if the trace is sampled
179-
165+
180166
# transaction.sampled can be None, True, False.
181167
# If None, this has not been computed by NR which
182168
# can also mean the following:
183169
# 1. There was not a context passed in that explicitly has sampling disabled.
184170
# This flag would be found in the traceparent or traceparent and tracespan headers.
185171
# 2. Transaction was not created where DT headers are accepted during __init__
186172
# Therefore, we will treat a value of `None` as `True` for now.
187-
173+
188174
if self.otel_parent:
189175
return bool(self.otel_parent.trace_flags)
190176
else:
191-
return bool(
192-
self.nr_transaction
193-
and (
194-
self.nr_transaction.sampled
195-
or
196-
(self.nr_transaction._sampled is None)
197-
)
198-
)
177+
return bool(self.nr_transaction and (self.nr_transaction.sampled or (self.nr_transaction._sampled is None)))
199178

200179
def _is_remote(self):
201180
# Remote span denotes if propagated from a remote parent
202-
if (self.otel_parent and self.otel_parent.is_remote):
181+
if self.otel_parent and self.otel_parent.is_remote:
203182
return True
204183
return False
205184

@@ -248,28 +227,21 @@ def update_name(self, name):
248227

249228
def is_recording(self):
250229
return self._is_sampled() and not (
251-
hasattr(self, "nr_trace")
252-
and hasattr(self.nr_trace, "end_time")
253-
and self.nr_trace.end_time
230+
hasattr(self, "nr_trace") and hasattr(self.nr_trace, "end_time") and self.nr_trace.end_time
254231
)
255232

256233
def set_status(self, status, description=None):
257234
# TODO: not implemented yet
258235
pass
259236

260-
def record_exception(
261-
self, exception, attributes=None, timestamp=None, escaped=False
262-
):
237+
def record_exception(self, exception, attributes=None, timestamp=None, escaped=False):
263238
if not hasattr(self, "nr_trace"):
264239
if exception:
265240
notice_error((type(exception), exception, exception.__traceback__))
266241
else:
267242
notice_error(sys.exc_info(), attributes=attributes)
268243
else:
269-
self.nr_trace.notice_error(
270-
(type(exception), exception, exception.__traceback__),
271-
attributes=attributes,
272-
)
244+
self.nr_trace.notice_error((type(exception), exception, exception.__traceback__), attributes=attributes)
273245

274246
def end(self, end_time=None, *args, **kwargs):
275247
# We will ignore the end_time parameter and use NR's end_time
@@ -291,7 +263,7 @@ def end(self, end_time=None, *args, **kwargs):
291263
# Add attributes as Trace parameters
292264
self._set_attributes_in_nr(self.attributes)
293265

294-
# For each kind of NR Trace, we will need to add
266+
# For each kind of NR Trace, we will need to add
295267
# specific attributes since they were likely not
296268
# available at the time of the trace's creation.
297269
if self.instrumenting_module in ("Redis", "Mongodb"):
@@ -315,18 +287,10 @@ class Tracer(otel_api_trace.Tracer):
315287
def __init__(self, resource=None, instrumentation_library=None, *args, **kwargs):
316288
self.resource = resource
317289
self.instrumentation_library = instrumentation_library.split(".")[-1].capitalize()
318-
self.nr_application = application_instance(
319-
activate=False
320-
) or register_application("OtelTracer")
321-
self.global_settings = (
322-
self.nr_application and self.nr_application.global_settings
323-
)
290+
self.nr_application = application_instance(activate=False) or register_application("OtelTracer")
291+
self.global_settings = self.nr_application and self.nr_application.global_settings
324292

325-
if (
326-
self.nr_application
327-
and self.global_settings.enabled
328-
and self.nr_application.enabled
329-
):
293+
if self.nr_application and self.global_settings.enabled and self.nr_application.enabled:
330294
self._settings = self.nr_application.settings
331295
if not self._settings:
332296
self.nr_application.activate()
@@ -378,17 +342,13 @@ def start_span(
378342
request_path=request_path,
379343
headers=headers,
380344
)
381-
elif kind in (
382-
otel_api_trace.SpanKind.PRODUCER,
383-
otel_api_trace.SpanKind.INTERNAL,
384-
):
345+
elif kind in (otel_api_trace.SpanKind.PRODUCER, otel_api_trace.SpanKind.INTERNAL):
385346
transaction = BackgroundTask(self.nr_application, name=name)
386347
elif kind == otel_api_trace.SpanKind.CONSUMER:
387348
# NOTE: NR uses MessageTransaction for Pika, RabbitMQ, Kafka
388349
if (
389350
self.instrumentation_library in INSTRUMENTING_MODULE_TYPE
390-
and INSTRUMENTING_MODULE_TYPE[self.instrumentation_library]
391-
== "message"
351+
and INSTRUMENTING_MODULE_TYPE[self.instrumentation_library] == "message"
392352
):
393353
transaction = MessageTransaction(
394354
library=self.instrumentation_library,
@@ -399,12 +359,8 @@ def start_span(
399359
headers=headers,
400360
)
401361
else:
402-
transaction = BackgroundTask(
403-
self.nr_application,
404-
name=name,
405-
group="Celery",
406-
)
407-
362+
transaction = BackgroundTask(self.nr_application, name=name, group="Celery")
363+
408364
transaction.__enter__()
409365

410366
# If not parent_span_context or not parent_span_context.is_remote
@@ -434,7 +390,7 @@ def start_span(
434390
request_method=request_method,
435391
request_path=request_path,
436392
headers=headers,
437-
)
393+
)
438394
transaction.__enter__()
439395
elif kind == otel_api_trace.SpanKind.INTERNAL:
440396
if transaction:
@@ -445,12 +401,8 @@ def start_span(
445401
if transaction:
446402
if (
447403
(self.instrumentation_library in INSTRUMENTING_MODULE_TYPE)
448-
and (
449-
INSTRUMENTING_MODULE_TYPE[self.instrumentation_library]
450-
== "db"
451-
)
452-
or (attributes and ("db.system" in attributes))
453-
):
404+
and (INSTRUMENTING_MODULE_TYPE[self.instrumentation_library] == "db")
405+
) or (attributes and ("db.system" in attributes)):
454406
nr_trace_type = DatastoreTrace
455407
else:
456408
nr_trace_type = ExternalTrace
@@ -463,8 +415,7 @@ def start_span(
463415
# NOTE: NR uses MessageTransaction for Pika, RabbitMQ, Kafka
464416
if (
465417
self.instrumentation_library in INSTRUMENTING_MODULE_TYPE
466-
and INSTRUMENTING_MODULE_TYPE[self.instrumentation_library]
467-
== "message"
418+
and INSTRUMENTING_MODULE_TYPE[self.instrumentation_library] == "message"
468419
):
469420
transaction = MessageTransaction(
470421
library=self.instrumentation_library,
@@ -475,11 +426,7 @@ def start_span(
475426
headers=headers,
476427
)
477428
else:
478-
transaction = BackgroundTask(
479-
self.nr_application,
480-
name=name,
481-
group="Celery",
482-
)
429+
transaction = BackgroundTask(self.nr_application, name=name, group="Celery")
483430
transaction.__enter__()
484431
elif kind == otel_api_trace.SpanKind.PRODUCER:
485432
if transaction:
@@ -503,7 +450,6 @@ def start_span(
503450

504451
return span
505452

506-
507453
@contextmanager
508454
def start_as_current_span(
509455
self,
@@ -525,9 +471,7 @@ def start_as_current_span(
525471
set_status_on_exception=set_status_on_exception,
526472
)
527473

528-
with otel_api_trace.use_span(
529-
span, end_on_exit=end_on_exit, record_exception=record_exception
530-
) as current_span:
474+
with otel_api_trace.use_span(span, end_on_exit=end_on_exit, record_exception=record_exception) as current_span:
531475
yield current_span
532476

533477

@@ -544,8 +488,4 @@ def get_tracer(
544488
*args,
545489
**kwargs,
546490
):
547-
return Tracer(
548-
resource=self._resource, instrumentation_library=instrumenting_module_name
549-
)
550-
551-
491+
return Tracer(resource=self._resource, instrumentation_library=instrumenting_module_name)

newrelic/config.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4362,15 +4362,11 @@ def _process_module_builtin_defaults():
43624362

43634363
# Hybrid Agent Hooks
43644364
_process_module_definition(
4365-
"opentelemetry.trace",
4366-
"newrelic.hooks.hybridagent_opentelemetry",
4367-
"instrument_trace_api",
4365+
"opentelemetry.trace", "newrelic.hooks.hybridagent_opentelemetry", "instrument_trace_api"
43684366
)
4369-
4367+
43704368
_process_module_definition(
4371-
"opentelemetry.instrumentation.utils",
4372-
"newrelic.hooks.hybridagent_opentelemetry",
4373-
"instrument_utils",
4369+
"opentelemetry.instrumentation.utils", "newrelic.hooks.hybridagent_opentelemetry", "instrument_utils"
43744370
)
43754371

43764372

newrelic/core/config.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ class EventHarvestConfigHarvestLimitSettings(Settings):
519519
class OtelBridgeSettings(Settings):
520520
pass
521521

522+
522523
_settings = TopLevelSettings()
523524
_settings.agent_limits = AgentLimitsSettings()
524525
_settings.application_logging = ApplicationLoggingSettings()
@@ -1227,9 +1228,7 @@ def default_otlp_host(host):
12271228
_settings.azure_operator.enabled = _environ_as_bool("NEW_RELIC_AZURE_OPERATOR_ENABLED", default=False)
12281229
_settings.package_reporting.enabled = _environ_as_bool("NEW_RELIC_PACKAGE_REPORTING_ENABLED", default=True)
12291230
_settings.ml_insights_events.enabled = _environ_as_bool("NEW_RELIC_ML_INSIGHTS_EVENTS_ENABLED", default=False)
1230-
_settings.otel_bridge.enabled = _environ_as_bool(
1231-
"NEW_RELIC_OTEL_BRIDGE_ENABLED", default=False
1232-
)
1231+
_settings.otel_bridge.enabled = _environ_as_bool("NEW_RELIC_OTEL_BRIDGE_ENABLED", default=False)
12331232

12341233

12351234
def global_settings():

0 commit comments

Comments
 (0)