Skip to content

Commit

Permalink
Merge branch 'main' into transition/urllib3
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Jul 23, 2024
2 parents c926a6b + 38e4ea4 commit 1a1bf81
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/instrumentations_1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
- "resource-detector-azure"
- "resource-detector-container"
- "util-http"
- "fastapi-slim"
os: [ubuntu-20.04]
exclude:
- python-version: pypy3
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-httpx`, `opentelemetry-instrumentation-aiohttp-client`,
`opentelemetry-instrumentation-requests` Populate `{method}` as `HTTP` on `_OTHER` methods
([#2726](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2726))
- `opentelemetry-instrumentation-fastapi` Add dependency support for fastapi-slim
([#2702](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2702))

### Fixed

- Handle `redis.exceptions.WatchError` as a non-error event in redis instrumentation
([#2668](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2668))
- `opentelemetry-instrumentation-httpx` Ensure httpx.get or httpx.request like methods are instrumented
Expand Down
2 changes: 1 addition & 1 deletion instrumentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
| [opentelemetry-instrumentation-django](./opentelemetry-instrumentation-django) | django >= 1.10 | Yes | experimental
| [opentelemetry-instrumentation-elasticsearch](./opentelemetry-instrumentation-elasticsearch) | elasticsearch >= 6.0 | No | experimental
| [opentelemetry-instrumentation-falcon](./opentelemetry-instrumentation-falcon) | falcon >= 1.4.1, < 4.0.0 | Yes | experimental
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.58 | Yes | migration
| [opentelemetry-instrumentation-fastapi](./opentelemetry-instrumentation-fastapi) | fastapi ~= 0.58,fastapi-slim ~= 0.111.0 | Yes | migration
| [opentelemetry-instrumentation-flask](./opentelemetry-instrumentation-flask) | flask >= 1.0 | Yes | migration
| [opentelemetry-instrumentation-grpc](./opentelemetry-instrumentation-grpc) | grpcio ~= 1.27 | No | experimental
| [opentelemetry-instrumentation-httpx](./opentelemetry-instrumentation-httpx) | httpx >= 0.18.0 | No | migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ dependencies = [

[project.optional-dependencies]
instruments = [
"fastapi ~= 0.58",
"fastapi ~= 0.58",
"fastapi-slim ~= 0.111.0",
]

[project.entry-points.opentelemetry_instrumentor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
---
"""
import logging
from importlib.util import find_spec
from typing import Collection

import fastapi
Expand All @@ -189,7 +190,11 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
ClientResponseHook,
ServerRequestHook,
)
from opentelemetry.instrumentation.fastapi.package import _instruments
from opentelemetry.instrumentation.fastapi.package import (
_fastapi,
_fastapi_slim,
_instruments,
)
from opentelemetry.instrumentation.fastapi.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.metrics import get_meter
Expand Down Expand Up @@ -280,6 +285,11 @@ def uninstrument_app(app: fastapi.FastAPI):
app._is_instrumented_by_opentelemetry = False

def instrumentation_dependencies(self) -> Collection[str]:
if find_spec("fastapi") is not None:
return (_fastapi,)
if find_spec("fastapi_slim") is not None:
return (_fastapi_slim,)
# If neither is installed, return both as potential dependencies
return _instruments

def _instrument(self, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
# limitations under the License.


_instruments = ("fastapi ~= 0.58",)
_fastapi = "fastapi ~= 0.58"
_fastapi_slim = "fastapi-slim ~= 0.111.0"

_instruments = (_fastapi, _fastapi_slim)

_supports_metrics = True

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
annotated-types==0.6.0
anyio==4.3.0
asgiref==3.7.2
certifi==2024.7.4
charset-normalizer==3.3.2
Deprecated==1.2.14
exceptiongroup==1.2.0
fastapi-slim==0.111.0
h11==0.14.0
httpcore==1.0.4
httpx==0.27.0
idna==3.7
importlib-metadata==6.11.0
iniconfig==2.0.0
packaging==24.0
pluggy==1.5.0
py-cpuinfo==9.0.0
pydantic==2.6.2
pydantic_core==2.16.3
pytest==7.4.4
requests==2.32.3
sniffio==1.3.0
starlette==0.37.2
tomli==2.0.1
typing_extensions==4.9.0
urllib3==2.2.2
wrapt==1.16.0
zipp==3.19.2
-e opentelemetry-instrumentation
-e instrumentation/opentelemetry-instrumentation-asgi
-e util/opentelemetry-util-http
-e instrumentation/opentelemetry-instrumentation-fastapi
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
LoggingInstrumentor,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import ProxyTracer, get_tracer
from opentelemetry.trace import NoOpTracerProvider, ProxyTracer, get_tracer


class FakeTracerProvider:
Expand Down Expand Up @@ -207,3 +207,18 @@ def test_uninstrumented(self):
self.assertFalse(hasattr(record, "otelTraceID"))
self.assertFalse(hasattr(record, "otelServiceName"))
self.assertFalse(hasattr(record, "otelTraceSampled"))

def test_no_op_tracer_provider(self):
LoggingInstrumentor().uninstrument()
LoggingInstrumentor().instrument(tracer_provider=NoOpTracerProvider())

with self.caplog.at_level(level=logging.INFO):
logger = logging.getLogger("test logger")
logger.info("hello")

self.assertEqual(len(self.caplog.records), 1)
record = self.caplog.records[0]
self.assertEqual(record.otelSpanID, "0")
self.assertEqual(record.otelTraceID, "0")
self.assertEqual(record.otelServiceName, "")
self.assertEqual(record.otelTraceSampled, False)
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@
"library": "fastapi ~= 0.58",
"instrumentation": "opentelemetry-instrumentation-fastapi==0.47b0.dev",
},
{
"library": "fastapi-slim ~= 0.111.0",
"instrumentation": "opentelemetry-instrumentation-fastapi==0.47b0.dev",
},
{
"library": "flask >= 1.0",
"instrumentation": "opentelemetry-instrumentation-flask==0.47b0.dev",
Expand Down
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ envlist =

; opentelemetry-instrumentation-fastapi
py3{8,9,10,11,12}-test-instrumentation-fastapi
py3{8,9,10,11,12}-test-instrumentation-fastapi-slim
pypy3-test-instrumentation-fastapi
pypy3-test-instrumentation-fastapi-slim
lint-instrumentation-fastapi

; opentelemetry-instrumentation-flask
Expand Down Expand Up @@ -544,6 +546,11 @@ commands_pre =
fastapi: pip install opentelemetry-sdk@{env:CORE_REPO}\#egg=opentelemetry-sdk&subdirectory=opentelemetry-sdk
fastapi: pip install opentelemetry-test-utils@{env:CORE_REPO}\#egg=opentelemetry-test-utils&subdirectory=tests/opentelemetry-test-utils
fastapi: pip install -r {toxinidir}/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements.txt
fastapi-slim: pip install opentelemetry-api@{env:CORE_REPO}\#egg=opentelemetry-api&subdirectory=opentelemetry-api
fastapi-slim: pip install opentelemetry-semantic-conventions@{env:CORE_REPO}\#egg=opentelemetry-semantic-conventions&subdirectory=opentelemetry-semantic-conventions
fastapi-slim: pip install opentelemetry-sdk@{env:CORE_REPO}\#egg=opentelemetry-sdk&subdirectory=opentelemetry-sdk
fastapi-slim: pip install opentelemetry-test-utils@{env:CORE_REPO}\#egg=opentelemetry-test-utils&subdirectory=tests/opentelemetry-test-utils
fastapi-slim: pip install -r {toxinidir}/instrumentation/opentelemetry-instrumentation-fastapi/test-requirements-slim.txt

mysql: pip install opentelemetry-api@{env:CORE_REPO}\#egg=opentelemetry-api&subdirectory=opentelemetry-api
mysql: pip install opentelemetry-semantic-conventions@{env:CORE_REPO}\#egg=opentelemetry-semantic-conventions&subdirectory=opentelemetry-semantic-conventions
Expand Down

0 comments on commit 1a1bf81

Please sign in to comment.