Skip to content

Commit 36a3dc8

Browse files
authored
Merge branch 'main' into add-jinja-testing
2 parents da44575 + e663c36 commit 36a3dc8

File tree

5 files changed

+257
-153
lines changed

5 files changed

+257
-153
lines changed

newrelic/api/transaction.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,11 @@ def path(self):
159159

160160

161161
class Transaction(object):
162-
163162
STATE_PENDING = 0
164163
STATE_RUNNING = 1
165164
STATE_STOPPED = 2
166165

167166
def __init__(self, application, enabled=None, source=None):
168-
169167
self._application = application
170168

171169
self._source = source
@@ -343,7 +341,6 @@ def __del__(self):
343341
self.__exit__(None, None, None)
344342

345343
def __enter__(self):
346-
347344
assert self._state == self.STATE_PENDING
348345

349346
# Bail out if the transaction is not enabled.
@@ -403,7 +400,6 @@ def __enter__(self):
403400
return self
404401

405402
def __exit__(self, exc, value, tb):
406-
407403
# Bail out if the transaction is not enabled.
408404

409405
if not self.enabled:
@@ -636,7 +632,6 @@ def __exit__(self, exc, value, tb):
636632
# new samples can cause an error.
637633

638634
if not self.ignore_transaction:
639-
640635
self._application.record_transaction(node)
641636

642637
@property
@@ -929,9 +924,7 @@ def filter_request_parameters(self, params):
929924
@property
930925
def request_parameters(self):
931926
if (self.capture_params is None) or self.capture_params:
932-
933927
if self._request_params:
934-
935928
r_attrs = {}
936929

937930
for k, v in self._request_params.items():
@@ -1095,7 +1088,6 @@ def _generate_distributed_trace_headers(self, data=None):
10951088
try:
10961089
data = data or self._create_distributed_trace_data()
10971090
if data:
1098-
10991091
traceparent = W3CTraceParent(data).text()
11001092
yield ("traceparent", traceparent)
11011093

@@ -1192,11 +1184,10 @@ def _accept_distributed_trace_payload(self, payload, transport_type="HTTP"):
11921184
except:
11931185
return False
11941186

1195-
if "pr" in data:
1196-
try:
1197-
data["pr"] = float(data["pr"])
1198-
except:
1199-
data["pr"] = None
1187+
try:
1188+
data["pr"] = float(data["pr"])
1189+
except Exception:
1190+
data["pr"] = None
12001191

12011192
self._accept_distributed_trace_data(data, transport_type)
12021193
self._record_supportability("Supportability/DistributedTrace/AcceptPayload/Success")
@@ -1382,7 +1373,6 @@ def _generate_response_headers(self, read_length=None):
13821373
# process web external calls.
13831374

13841375
if self.client_cross_process_id is not None:
1385-
13861376
# Need to work out queueing time and duration up to this
13871377
# point for inclusion in metrics and response header. If the
13881378
# recording of the transaction had been prematurely stopped
@@ -1426,11 +1416,17 @@ def _generate_response_headers(self, read_length=None):
14261416

14271417
return nr_headers
14281418

1429-
def get_response_metadata(self):
1419+
# This function is CAT related and has been deprecated.
1420+
# Eventually, this will be removed. Until then, coverage
1421+
# does not need to factor this function into its analysis.
1422+
def get_response_metadata(self): # pragma: no cover
14301423
nr_headers = dict(self._generate_response_headers())
14311424
return convert_to_cat_metadata_value(nr_headers)
14321425

1433-
def process_request_metadata(self, cat_linking_value):
1426+
# This function is CAT related and has been deprecated.
1427+
# Eventually, this will be removed. Until then, coverage
1428+
# does not need to factor this function into its analysis.
1429+
def process_request_metadata(self, cat_linking_value): # pragma: no cover
14341430
try:
14351431
payload = base64_decode(cat_linking_value)
14361432
except:
@@ -1447,7 +1443,6 @@ def process_request_metadata(self, cat_linking_value):
14471443
return self._process_incoming_cat_headers(encoded_cross_process_id, encoded_txn_header)
14481444

14491445
def set_transaction_name(self, name, group=None, priority=None):
1450-
14511446
# Always perform this operation even if the transaction
14521447
# is not active at the time as will be called from
14531448
# constructor. If path has been frozen do not allow
@@ -1517,7 +1512,9 @@ def record_log_event(self, message, level=None, timestamp=None, priority=None):
15171512

15181513
self._log_events.add(event, priority=priority)
15191514

1520-
def record_exception(self, exc=None, value=None, tb=None, params=None, ignore_errors=None):
1515+
# This function has been deprecated (and will be removed eventually)
1516+
# and therefore does not need to be included in coverage analysis
1517+
def record_exception(self, exc=None, value=None, tb=None, params=None, ignore_errors=None): # pragma: no cover
15211518
# Deprecation Warning
15221519
warnings.warn(
15231520
("The record_exception function is deprecated. Please use the new api named notice_error instead."),
@@ -1684,15 +1681,19 @@ def add_custom_attributes(self, items):
16841681

16851682
return result
16861683

1687-
def add_custom_parameter(self, name, value):
1684+
# This function has been deprecated (and will be removed eventually)
1685+
# and therefore does not need to be included in coverage analysis
1686+
def add_custom_parameter(self, name, value): # pragma: no cover
16881687
# Deprecation warning
16891688
warnings.warn(
16901689
("The add_custom_parameter API has been deprecated. " "Please use the add_custom_attribute API."),
16911690
DeprecationWarning,
16921691
)
16931692
return self.add_custom_attribute(name, value)
16941693

1695-
def add_custom_parameters(self, items):
1694+
# This function has been deprecated (and will be removed eventually)
1695+
# and therefore does not need to be included in coverage analysis
1696+
def add_custom_parameters(self, items): # pragma: no cover
16961697
# Deprecation warning
16971698
warnings.warn(
16981699
("The add_custom_parameters API has been deprecated. " "Please use the add_custom_attributes API."),
@@ -1796,19 +1797,23 @@ def add_custom_attributes(items):
17961797
return False
17971798

17981799

1799-
def add_custom_parameter(key, value):
1800+
# This function has been deprecated (and will be removed eventually)
1801+
# and therefore does not need to be included in coverage analysis
1802+
def add_custom_parameter(key, value): # pragma: no cover
18001803
# Deprecation warning
18011804
warnings.warn(
1802-
("The add_custom_parameter API has been deprecated. " "Please use the add_custom_attribute API."),
1805+
("The add_custom_parameter API has been deprecated. Please use the add_custom_attribute API."),
18031806
DeprecationWarning,
18041807
)
18051808
return add_custom_attribute(key, value)
18061809

18071810

1808-
def add_custom_parameters(items):
1811+
# This function has been deprecated (and will be removed eventually)
1812+
# and therefore does not need to be included in coverage analysis
1813+
def add_custom_parameters(items): # pragma: no cover
18091814
# Deprecation warning
18101815
warnings.warn(
1811-
("The add_custom_parameters API has been deprecated. " "Please use the add_custom_attributes API."),
1816+
("The add_custom_parameters API has been deprecated. Please use the add_custom_attributes API."),
18121817
DeprecationWarning,
18131818
)
18141819
return add_custom_attributes(items)

tests/agent_features/test_apdex_metrics.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,41 @@
1313
# limitations under the License.
1414

1515
import webtest
16-
17-
from testing_support.validators.validate_apdex_metrics import (
18-
validate_apdex_metrics)
1916
from testing_support.sample_applications import simple_app
17+
from testing_support.validators.validate_apdex_metrics import validate_apdex_metrics
2018

19+
from newrelic.api.transaction import current_transaction, suppress_apdex_metric
20+
from newrelic.api.wsgi_application import wsgi_application
2121

2222
normal_application = webtest.TestApp(simple_app)
2323

24-
2524
# NOTE: This test validates that the server-side apdex_t is set to 0.5
2625
# If the server-side configuration changes, this test will start to fail.
2726

2827

2928
@validate_apdex_metrics(
30-
name='',
31-
group='Uri',
29+
name="",
30+
group="Uri",
3231
apdex_t_min=0.5,
3332
apdex_t_max=0.5,
3433
)
3534
def test_apdex():
36-
normal_application.get('/')
35+
normal_application.get("/")
36+
37+
38+
# This has to be a Web Transaction.
39+
# The apdex measurement only applies to Web Transactions
40+
def test_apdex_suppression():
41+
@wsgi_application()
42+
def simple_apdex_supression_app(environ, start_response):
43+
suppress_apdex_metric()
44+
45+
start_response(status="200 OK", response_headers=[])
46+
transaction = current_transaction()
47+
48+
assert transaction.suppress_apdex
49+
assert transaction.apdex == 0
50+
return []
51+
52+
apdex_suppression_app = webtest.TestApp(simple_apdex_supression_app)
53+
apdex_suppression_app.get("/")
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from testing_support.fixtures import reset_core_stats_engine
16+
from testing_support.validators.validate_custom_metrics_outside_transaction import (
17+
validate_custom_metrics_outside_transaction,
18+
)
19+
20+
from newrelic.api.application import application_instance as application
21+
from newrelic.api.background_task import background_task
22+
from newrelic.api.transaction import (
23+
current_transaction,
24+
record_custom_metric,
25+
record_custom_metrics,
26+
)
27+
28+
29+
# Testing record_custom_metric
30+
@reset_core_stats_engine()
31+
@background_task()
32+
def test_custom_metric_inside_transaction():
33+
transaction = current_transaction()
34+
record_custom_metric("CustomMetric/InsideTransaction/Count", 1)
35+
for metric in transaction._custom_metrics.metrics():
36+
assert metric == ("CustomMetric/InsideTransaction/Count", [1, 1, 1, 1, 1, 1])
37+
38+
39+
@reset_core_stats_engine()
40+
@validate_custom_metrics_outside_transaction([("CustomMetric/OutsideTransaction/Count", 1)])
41+
@background_task()
42+
def test_custom_metric_outside_transaction_with_app():
43+
app = application()
44+
record_custom_metric("CustomMetric/OutsideTransaction/Count", 1, application=app)
45+
46+
47+
# Testing record_custom_metricS
48+
@reset_core_stats_engine()
49+
@background_task()
50+
def test_custom_metrics_inside_transaction():
51+
transaction = current_transaction()
52+
record_custom_metrics([("CustomMetrics/InsideTransaction/Count", 1)])
53+
for metric in transaction._custom_metrics.metrics():
54+
assert metric == ("CustomMetrics/InsideTransaction/Count", [1, 1, 1, 1, 1, 1])
55+
56+
57+
@reset_core_stats_engine()
58+
@validate_custom_metrics_outside_transaction([("CustomMetrics/OutsideTransaction/Count", 1)])
59+
@background_task()
60+
def test_custom_metrics_outside_transaction_with_app():
61+
app = application()
62+
record_custom_metrics([("CustomMetrics/OutsideTransaction/Count", 1)], application=app)

tests/agent_features/test_distributed_tracing.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
from newrelic.api.background_task import BackgroundTask, background_task
3333
from newrelic.api.time_trace import current_trace
3434
from newrelic.api.transaction import (
35+
accept_distributed_trace_headers,
36+
accept_distributed_trace_payload,
37+
create_distributed_trace_payload,
3538
current_span_id,
3639
current_trace_id,
3740
current_transaction,
@@ -185,10 +188,10 @@ def _test():
185188
payload["d"]["pa"] = "5e5733a911cfbc73"
186189

187190
if accept_payload:
188-
result = txn.accept_distributed_trace_payload(payload)
191+
result = accept_distributed_trace_payload(payload)
189192
assert result
190193
else:
191-
txn._create_distributed_trace_payload()
194+
create_distributed_trace_payload()
192195

193196
try:
194197
raise ValueError("cookies")
@@ -319,7 +322,6 @@ def _test():
319322
)
320323
@override_application_settings(_override_settings)
321324
def test_distributed_tracing_backwards_compatibility(traceparent, tracestate, newrelic, metrics):
322-
323325
headers = []
324326
if traceparent:
325327
headers.append(("traceparent", TRACEPARENT))
@@ -333,8 +335,7 @@ def test_distributed_tracing_backwards_compatibility(traceparent, tracestate, ne
333335
)
334336
@background_task(name="test_distributed_tracing_backwards_compatibility")
335337
def _test():
336-
transaction = current_transaction()
337-
transaction.accept_distributed_trace_headers(headers)
338+
accept_distributed_trace_headers(headers)
338339

339340
_test()
340341

0 commit comments

Comments
 (0)