Skip to content

Commit 59fca1f

Browse files
committed
fix(asyncclick): disable asyncclick for long running processes like servers
1 parent 3b97af6 commit 59fca1f

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Fixed
1515

16+
- `opentelemetry-instrumentation-asyncclick`: fix issue where servers using asyncclick would not get a separate span per-request
1617
- `opentelemetry-instrumentation-click`: fix issue where starting uvicorn via `python -m` would cause the click instrumentation to give all requests the same trace id
1718
- `opentelemetry-instrumentation-botocore`: migrate off the deprecated events API to use the logs API
1819
([#3624](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3624))

instrumentation/opentelemetry-instrumentation-asyncclick/src/opentelemetry/instrumentation/asyncclick/__init__.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,7 @@ async def hello():
4949
import sys
5050
from functools import partial
5151
from logging import getLogger
52-
from typing import (
53-
TYPE_CHECKING,
54-
Any,
55-
Awaitable,
56-
Callable,
57-
Collection,
58-
TypeVar,
59-
)
52+
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Collection, TypeVar
6053

6154
import asyncclick
6255
from typing_extensions import ParamSpec, Unpack
@@ -68,9 +61,7 @@ async def hello():
6861
from opentelemetry.instrumentation.asyncclick.package import _instruments
6962
from opentelemetry.instrumentation.asyncclick.version import __version__
7063
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
71-
from opentelemetry.instrumentation.utils import (
72-
unwrap,
73-
)
64+
from opentelemetry.instrumentation.utils import unwrap
7465
from opentelemetry.semconv._incubating.attributes.process_attributes import (
7566
PROCESS_COMMAND_ARGS,
7667
PROCESS_EXECUTABLE_NAME,
@@ -112,6 +103,14 @@ async def _command_invoke_wrapper(
112103

113104
ctx = args[0]
114105

106+
# we don't want to create a root span for long running processes like servers
107+
# otherwise all requests would have the same trace id
108+
if (
109+
"opentelemetry.instrumentation.asgi" in sys.modules
110+
or "opentelemetry.instrumentation.wsgi" in sys.modules
111+
):
112+
return await wrapped(*args, **kwargs)
113+
115114
span_name = ctx.info_name
116115
span_attributes = {
117116
PROCESS_COMMAND_ARGS: sys.argv,

instrumentation/opentelemetry-instrumentation-asyncclick/tests/test_asyncclick.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import asyncio
1818
import os
19+
import sys
1920
from typing import Any
2021
from unittest import IsolatedAsyncioTestCase, mock
2122

@@ -158,6 +159,36 @@ async def command_raises() -> None:
158159
},
159160
)
160161

162+
@mock.patch("sys.argv", ["command.py"])
163+
def test_disabled_when_asgi_instrumentation_loaded(self):
164+
@asyncclick.command()
165+
async def command():
166+
pass
167+
168+
with mock.patch.dict(
169+
sys.modules,
170+
{**sys.modules, "opentelemetry.instrumentation.asgi": mock.Mock()},
171+
):
172+
result = run_asyncclick_command_test(command)
173+
self.assertEqual(result.exit_code, 0)
174+
175+
self.assertFalse(self.memory_exporter.get_finished_spans())
176+
177+
@mock.patch("sys.argv", ["command.py"])
178+
def test_disabled_when_wsgi_instrumentation_loaded(self):
179+
@asyncclick.command()
180+
async def command():
181+
pass
182+
183+
with mock.patch.dict(
184+
sys.modules,
185+
{**sys.modules, "opentelemetry.instrumentation.wsgi": mock.Mock()},
186+
):
187+
result = run_asyncclick_command_test(command)
188+
self.assertEqual(result.exit_code, 0)
189+
190+
self.assertFalse(self.memory_exporter.get_finished_spans())
191+
161192
def test_uninstrument(self):
162193
AsyncClickInstrumentor().uninstrument()
163194

0 commit comments

Comments
 (0)