Skip to content

Commit

Permalink
removes start event from telemetry (#77)
Browse files Browse the repository at this point in the history
* removes start event from telemetry

* fix docs

* showing traceback inline

* fix doc
  • Loading branch information
edublancas authored Oct 25, 2023
1 parent c85768f commit 63f8cc7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 404 deletions.
2 changes: 2 additions & 0 deletions doc/_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ sphinx:
extra_extensions:
- 'sphinx.ext.autodoc'
- 'sphinx.ext.napoleon'
config:
nb_execution_show_tb: True
9 changes: 5 additions & 4 deletions doc/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ For more details, see the [API Reference](api/telemetry).

+++

To unit test decorated functions, call the function and check `__wrapped__._telemetry_started` attribute. If it exists, it means the function has been decorated with `@log_call()`, you can use it to verify what's logged:
To unit test decorated functions, call the function and check `__wrapped__._telemetry_success` attribute. If it exists, it means the function has been decorated with `@log_call()`, you can use it to verify what's logged:

```{code-cell} ipython3
@telemetry.log_call(log_args=True, ignore_args=("y",))
Expand All @@ -89,16 +89,17 @@ _ = divide(2, 4)
```{code-cell} ipython3
from unittest.mock import ANY
assert divide.__wrapped__._telemetry_started == {
"action": "ploomber-core-divide-started",
assert divide.__wrapped__._telemetry_success == {
"action": "ploomber-core-divide-success",
"total_runtime": ANY,
"metadata": {
"argv": ANY,
"args": {"x": 2},
},
}
```

`__wrapped__._telemetry_started` will keep the latest logged data, so you must call it at least one; otherwise, it'll be `None`.
`__wrapped__._telemetry_success` will keep the latest logged data, so you must call it at least one; otherwise, it'll be `None`.

+++

Expand Down
6 changes: 0 additions & 6 deletions src/ploomber_core/telemetry/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,6 @@ def _log_call(func):
name = action or getattr(func, "__name__", "funcion-without-name")
action_ = (f"{action_}-{name}").replace("_", "-")

func._telemetry_started = None
func._telemetry_success = None
func._telemetry_error = None

Expand All @@ -610,7 +609,6 @@ def _log_call(func):
@wraps(func)
def wrapper(*args, **kwargs):
# reset attributes before calling
func._telemetry_started = None
func._telemetry_success = None
func._telemetry_error = None

Expand All @@ -626,10 +624,6 @@ def wrapper(*args, **kwargs):
if log_args:
metadata_started["args"] = args_parsed

started = dict(action=f"{action_}-started", metadata=metadata_started)
func._telemetry_started = started
self.log_api(**started)

start = datetime.datetime.now()

try:
Expand Down
90 changes: 0 additions & 90 deletions tests/telemetry/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,6 @@ def my_function():

mock_telemetry.assert_has_calls(
[
call(
action="some-package-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-some-action-success",
total_runtime="1",
Expand All @@ -489,10 +485,6 @@ def my_function():

mock_telemetry.assert_has_calls(
[
call(
action="some-package-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-some-action-error",
total_runtime="1",
Expand All @@ -518,10 +510,6 @@ def my_function():

mock_telemetry.assert_has_calls(
[
call(
action="some-package-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-some-action-error",
total_runtime="1",
Expand All @@ -548,10 +536,6 @@ def my_function(payload):

mock_telemetry.assert_has_calls(
[
call(
action="some-package-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-some-action-error",
total_runtime="1",
Expand All @@ -577,10 +561,6 @@ def my_function(payload):

mock_telemetry.assert_has_calls(
[
call(
action="some-package-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-some-action-success",
total_runtime="1",
Expand All @@ -606,10 +586,6 @@ def my_function(self, payload, x, y):
test_class.my_function(1, 2)
mock_telemetry.assert_has_calls(
[
call(
action="some-package-TestClass-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-TestClass-some-action-success",
total_runtime="1",
Expand All @@ -635,10 +611,6 @@ def my_function(self, payload):

mock_telemetry.assert_has_calls(
[
call(
action="some-package-TestClass-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-TestClass-some-action-success",
total_runtime="1",
Expand All @@ -664,10 +636,6 @@ def my_function(self, payload, apple=2, banana=4):
assert result == "Give me 5 apples and 10 bananas please"
mock_telemetry.assert_has_calls(
[
call(
action="some-package-TestClass-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-TestClass-some-action-success",
total_runtime="1",
Expand Down Expand Up @@ -698,10 +666,6 @@ def my_function(self, payload, users, *, separator):

mock_telemetry.assert_has_calls(
[
call(
action="some-package-TestClass-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-TestClass-some-action-success",
total_runtime="1",
Expand Down Expand Up @@ -730,10 +694,6 @@ def my_function(self, payload, arg1, *, arg2, arg3):

mock_telemetry.assert_has_calls(
[
call(
action="some-package-TestClass-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-TestClass-some-action-success",
total_runtime="1",
Expand Down Expand Up @@ -762,10 +722,6 @@ def my_function(self, payload, **info):

mock_telemetry.assert_has_calls(
[
call(
action="some-package-TestClass-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-TestClass-some-action-success",
total_runtime="1",
Expand All @@ -792,10 +748,6 @@ def my_function(self, payload, *todo_list):

mock_telemetry.assert_has_calls(
[
call(
action="some-package-TestClass-some-action-started",
metadata=dict(argv=["bin", "arg"]),
),
call(
action="some-package-TestClass-some-action-success",
total_runtime="1",
Expand Down Expand Up @@ -905,27 +857,6 @@ def my_function():
)

assert mock.call_args_list == [
call(
distinct_id="fake-uuid",
event="some-package-some-action-started",
properties={
"event_id": ANY,
"user_id": "fake-uuid",
"action": "some-package-some-action-started",
"client_time": ANY,
"metadata": {"argv": ["bin", "arg2", "arg2"]},
"total_runtime": None,
"python_version": py_version,
"version": "1.2.2",
"package_name": "some-package",
"docker_container": ANY,
"cloud": ANY,
"email": None,
"os": ANY,
"environment": ANY,
"telemetry_version": ANY,
},
),
call(
distinct_id="fake-uuid",
event="some-package-some-action-success",
Expand Down Expand Up @@ -984,17 +915,11 @@ def my_function():
"action": "some-package-my-function",
}

assert my_function.__wrapped__._telemetry_started is None
assert my_function.__wrapped__._telemetry_success is None
assert my_function.__wrapped__._telemetry_error is None

my_function()

assert my_function.__wrapped__._telemetry_started == {
"action": "some-package-my-function-started",
"metadata": {"argv": ANY},
}

assert my_function.__wrapped__._telemetry_success == {
"action": "some-package-my-function-success",
"total_runtime": ANY,
Expand All @@ -1019,7 +944,6 @@ def my_function():
"action": "some-package-my-function",
}

assert my_function.__wrapped__._telemetry_started is None
assert my_function.__wrapped__._telemetry_success is None
assert my_function.__wrapped__._telemetry_error is None

Expand All @@ -1028,11 +952,6 @@ def my_function():
except ValueError:
pass

assert my_function.__wrapped__._telemetry_started == {
"action": "some-package-my-function-started",
"metadata": {"argv": ANY},
}

assert my_function.__wrapped__._telemetry_success is None

assert my_function.__wrapped__._telemetry_error == {
Expand Down Expand Up @@ -1061,20 +980,11 @@ def my_function(x, y, z):
"group": None,
}

assert my_function.__wrapped__._telemetry_started is None
assert my_function.__wrapped__._telemetry_success is None
assert my_function.__wrapped__._telemetry_error is None

my_function(1, 2, 3)

assert my_function.__wrapped__._telemetry_started == {
"action": "some-package-my-function-started",
"metadata": {
"argv": ANY,
"args": {"x": 1, "y": 2},
},
}

assert my_function.__wrapped__._telemetry_success == {
"action": "some-package-my-function-success",
"total_runtime": ANY,
Expand Down
Loading

0 comments on commit 63f8cc7

Please sign in to comment.