-
Notifications
You must be signed in to change notification settings - Fork 7
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
5fda8ec
commit d2dee23
Showing
6 changed files
with
211 additions
and
68 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
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,63 @@ | ||
"""Tests for caller tracking.""" | ||
from functools import wraps | ||
from prometheus_client.exposition import generate_latest | ||
|
||
from .decorator import autometrics | ||
from .utils import get_caller_function, get_function_name, get_module_name | ||
|
||
|
||
def test_caller_detection(): | ||
"""This is a test to see if the caller is properly detected.""" | ||
|
||
def dummy_decorator(func): | ||
@wraps(func) | ||
def dummy_wrapper(*args, **kwargs): | ||
return func(*args, **kwargs) | ||
|
||
return dummy_wrapper | ||
|
||
def another_decorator(func): | ||
@wraps(func) | ||
def another_wrapper(*args, **kwargs): | ||
return func(*args, **kwargs) | ||
|
||
return another_wrapper | ||
|
||
@dummy_decorator | ||
@autometrics | ||
@another_decorator | ||
def foo(): | ||
print(get_caller_function(func_name="foo", module_name=__name__)) | ||
pass | ||
|
||
foo() | ||
|
||
func_name = get_function_name(foo) | ||
|
||
blob = generate_latest() | ||
assert blob is not None | ||
data = blob.decode("utf-8") | ||
|
||
expected = """function_calls_count_total{caller="test_caller_detection",function="test_caller_detection.<locals>.foo",module="autometrics.test_caller",objective_name="",objective_percentile="",result="ok"} 1.0""" | ||
assert "wrapper" not in data | ||
assert expected in data | ||
|
||
|
||
def test_caller_detection_external(): | ||
"""Same as above, but for external wrapper.""" | ||
|
||
from .test_caller_external import bar | ||
|
||
bar() | ||
|
||
caller = get_caller_function(depth=1) | ||
func_name = get_function_name(bar) | ||
module_name = get_module_name(bar) | ||
|
||
blob = generate_latest() | ||
assert blob is not None | ||
data = blob.decode("utf-8") | ||
|
||
expected = f"""function_calls_count_total{{caller="{caller}",function="{func_name}",module="{module_name}",objective_name="",objective_percentile="",result="ok"}} 1.0""" | ||
assert "external_wrapper" not in data | ||
assert expected in data |
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,27 @@ | ||
"""Supplementary for caller tests.""" | ||
from functools import wraps | ||
|
||
from .decorator import autometrics | ||
|
||
|
||
def dummy_external_decorator(func): | ||
@wraps(func) | ||
def dummy_external_wrapper(*args, **kwargs): | ||
return func(*args, **kwargs) | ||
|
||
return dummy_external_wrapper | ||
|
||
|
||
def another_external_decorator(func): | ||
@wraps(func) | ||
def another_external_wrapper(*args, **kwargs): | ||
return func(*args, **kwargs) | ||
|
||
return another_external_wrapper | ||
|
||
|
||
@dummy_external_decorator | ||
@autometrics | ||
@another_external_decorator | ||
def bar(): | ||
pass |
Oops, something went wrong.