-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed33957
commit 459e085
Showing
5 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
|
||
from newrelic.api.application import application_instance | ||
from newrelic.api.background_task import background_task | ||
from newrelic.api.transaction import record_dimensional_metric, record_dimensional_metrics | ||
from newrelic.common.metric_utils import create_metric_identity | ||
|
||
from testing_support.fixtures import reset_core_stats_engine | ||
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics | ||
from testing_support.validators.validate_dimensional_metrics_outside_transaction import validate_dimensional_metrics_outside_transaction | ||
|
||
|
||
_test_tags_examples = [ | ||
(None, None), | ||
({}, None), | ||
([], None), | ||
({"str": "a"}, frozenset({("str", "a")})), | ||
({"int": 1}, frozenset({("int", 1)})), | ||
({"float": 1.0}, frozenset({("float", 1.0)})), | ||
({"bool": True}, frozenset({("bool", True)})), | ||
({"list": [1]}, frozenset({("list", "[1]")})), | ||
({"dict": {"subtag": 1}}, frozenset({("dict", "{'subtag': 1}")})), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("tags,expected", _test_tags_examples) | ||
def test_create_metric_identity(tags, expected): | ||
name = "Metric" | ||
output_name, output_tags = create_metric_identity(name, tags=tags) | ||
assert output_name == name, "Name does not match." | ||
assert output_tags == expected, "Output tags do not match." | ||
|
||
|
||
@pytest.mark.parametrize("tags,expected", _test_tags_examples) | ||
def test_record_dimensional_metric_inside_transaction(tags, expected): | ||
@validate_transaction_metrics("test_record_dimensional_metric_inside_transaction", background_task=True, dimensional_metrics=[ | ||
("Metric", expected, 1), | ||
]) | ||
@background_task(name="test_record_dimensional_metric_inside_transaction") | ||
def _test(): | ||
record_dimensional_metric("Metric", 1, tags=tags) | ||
|
||
_test() | ||
|
||
|
||
@pytest.mark.parametrize("tags,expected", _test_tags_examples) | ||
@reset_core_stats_engine() | ||
def test_record_dimensional_metric_outside_transaction(tags, expected): | ||
@validate_dimensional_metrics_outside_transaction([("Metric", expected, 1)]) | ||
def _test(): | ||
app = application_instance() | ||
record_dimensional_metric("Metric", 1, tags=tags, application=app) | ||
|
||
_test() | ||
|
||
|
||
@pytest.mark.parametrize("tags,expected", _test_tags_examples) | ||
def test_record_dimensional_metrics_inside_transaction(tags, expected): | ||
@validate_transaction_metrics("test_record_dimensional_metrics_inside_transaction", background_task=True, dimensional_metrics=[("Metric/1", expected, 1), ("Metric/2", expected, 1)]) | ||
@background_task(name="test_record_dimensional_metrics_inside_transaction") | ||
def _test(): | ||
record_dimensional_metrics([("Metric/1", 1, tags), ("Metric/2", 1, tags)]) | ||
|
||
_test() | ||
|
||
|
||
@pytest.mark.parametrize("tags,expected", _test_tags_examples) | ||
@reset_core_stats_engine() | ||
def test_record_dimensional_metrics_outside_transaction(tags, expected): | ||
@validate_dimensional_metrics_outside_transaction([("Metric/1", expected, 1), ("Metric/2", expected, 1)]) | ||
def _test(): | ||
app = application_instance() | ||
record_dimensional_metrics([("Metric/1", 1, tags), ("Metric/2", 1, tags)], application=app) | ||
|
||
_test() | ||
|
||
|
||
def test_dimensional_metrics_different_tags(): | ||
@validate_transaction_metrics("test_dimensional_metrics_different_tags", background_task=True, dimensional_metrics=[ | ||
("Metric", frozenset({("tag", 1)}), 1), | ||
("Metric", frozenset({("tag", 2)}), 2), | ||
]) | ||
@background_task(name="test_dimensional_metrics_different_tags") | ||
def _test(): | ||
record_dimensional_metrics([ | ||
("Metric", 1, {"tag": 1}), | ||
("Metric", 1, {"tag": 2}), | ||
]) | ||
record_dimensional_metric("Metric", 1, {"tag": 2}) | ||
|
||
_test() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
tests/testing_support/validators/validate_dimensional_metrics_outside_transaction.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Copyright 2010 New Relic, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import copy | ||
|
||
from testing_support.fixtures import catch_background_exceptions | ||
from newrelic.common.object_wrapper import transient_function_wrapper, function_wrapper | ||
|
||
|
||
def validate_dimensional_metrics_outside_transaction(dimensional_metrics=None): | ||
dimensional_metrics = dimensional_metrics or [] | ||
|
||
@function_wrapper | ||
def _validate_wrapper(wrapped, instance, args, kwargs): | ||
|
||
record_dimensional_metric_called = [] | ||
recorded_metrics = [None] | ||
|
||
@transient_function_wrapper("newrelic.core.stats_engine", "StatsEngine.record_dimensional_metric") | ||
@catch_background_exceptions | ||
def _validate_dimensional_metrics_outside_transaction(wrapped, instance, args, kwargs): | ||
record_dimensional_metric_called.append(True) | ||
try: | ||
result = wrapped(*args, **kwargs) | ||
except: | ||
raise | ||
else: | ||
metrics = instance.dimensional_stats_table | ||
# Record a copy of the metric value so that the values aren't | ||
# merged in the future | ||
_metrics = {} | ||
for k, v in metrics.items(): | ||
_metrics[k] = copy.copy(v) | ||
recorded_metrics[0] = _metrics | ||
|
||
return result | ||
|
||
def _validate(metrics, name, tags, count): | ||
key = (name, tags) | ||
metric = metrics.get(key) | ||
|
||
def _metrics_table(): | ||
out = [""] | ||
out.append("Expected: {0}: {1}".format(key, count)) | ||
for metric_key, metric_value in metrics.items(): | ||
out.append("{0}: {1}".format(metric_key, metric_value[0])) | ||
return "\n".join(out) | ||
|
||
def _metric_details(): | ||
return "metric=%r, count=%r" % (key, metric.call_count) | ||
|
||
if count is not None: | ||
assert metric is not None, _metrics_table() | ||
if count == "present": | ||
assert metric.call_count > 0, _metric_details() | ||
else: | ||
assert metric.call_count == count, _metric_details() | ||
|
||
assert metric.total_call_time >= 0, (key, metric) | ||
assert metric.total_exclusive_call_time >= 0, (key, metric) | ||
assert metric.min_call_time >= 0, (key, metric) | ||
assert metric.sum_of_squares >= 0, (key, metric) | ||
|
||
else: | ||
assert metric is None, _metrics_table() | ||
|
||
_new_wrapper = _validate_dimensional_metrics_outside_transaction(wrapped) | ||
val = _new_wrapper(*args, **kwargs) | ||
assert record_dimensional_metric_called | ||
metrics = recorded_metrics[0] | ||
|
||
record_dimensional_metric_called[:] = [] | ||
recorded_metrics[:] = [] | ||
|
||
for dimensional_metric, dimensional_tags, count in dimensional_metrics: | ||
if isinstance(dimensional_tags, dict): | ||
dimensional_tags = frozenset(dimensional_tags.items()) | ||
_validate(metrics, dimensional_metric, dimensional_tags, count) | ||
|
||
return val | ||
|
||
return _validate_wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters