Skip to content

Commit

Permalink
Merge pull request #162 from spraakbanken/161-formattinglogginghandle…
Browse files Browse the repository at this point in the history
…r-has-no-attr-_logger

fix: use LoggingHandler from opentelemetry directly
  • Loading branch information
kod-kristoff authored Nov 13, 2024
2 parents 0466584 + 4e3934b commit 700fdbb
Show file tree
Hide file tree
Showing 3 changed files with 490 additions and 414 deletions.
16 changes: 4 additions & 12 deletions src/sblex/telemetry/otlp_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@
logger = logging.getLogger(__name__)


# The default Otel SDK completely ignores formatters when outputting the message being logged.
# We overcome this by creating our own LoggingHandler class which respects formatters.
class FormattedLoggingHandler(LoggingHandler):
def emit(self, record: logging.LogRecord) -> None:
msg = self.format(record)
record.msg = msg
record.args = None
self._logger.emit(self._translate(record))


def init_otel_logging(settings: OTelSettings) -> None:
msg = "Using log level:"
match settings.otel_python_log_level or "INFO":
Expand Down Expand Up @@ -84,12 +74,14 @@ def init_otel_logging(settings: OTelSettings) -> None:
otel_log_exporter = OTLPLogExporterGRPC(endpoint=endpoint, headers=headers)
logger_provider.add_log_record_processor(BatchLogRecordProcessor(otel_log_exporter))

otel_log_handler = FormattedLoggingHandler(logger_provider=logger_provider, level=log_level)
otel_log_handler = LoggingHandler(logger_provider=logger_provider, level=log_level)

# This has to be called first before logger.getLogger().addHandler()
# so that it can call logging.basicConfig first to set the logging format
# based on the environment variable OTEL_PYTHON_LOG_FORMAT
LoggingInstrumentor(log_level=log_level).instrument()
LoggingInstrumentor(
log_level=log_level,
).instrument()
logFormatter = logging.Formatter(settings.otel_python_log_format)
otel_log_handler.setFormatter(logFormatter)
logging.getLogger().addHandler(otel_log_handler)
Expand Down
212 changes: 138 additions & 74 deletions tests/requirements-testing.lock
Original file line number Diff line number Diff line change
@@ -1,104 +1,168 @@
# This file is @generated by PDM.
# Please do not edit it manually.

# This file was autogenerated by uv via the following command:
# uv pip compile pyproject.toml --output-file tests/requirements-testing.lock
annotated-types==0.7.0
# via pydantic
anyio==4.4.0
asgi-lifespan==2.1.0
# via
# httpx
# starlette
asgi-matomo==0.6.0
# via sblex-server (pyproject.toml)
asgiref==3.8.1
bracex==2.4
# via
# asgi-matomo
# opentelemetry-instrumentation-asgi
brotli==1.1.0
# via brotli-asgi
brotli-asgi==1.4.0
bump-my-version==0.23.0
# via sblex-server (pyproject.toml)
certifi==2024.6.2
# via
# httpcore
# httpx
# requests
charset-normalizer==3.3.2
# via requests
click==8.1.7
colorama==0.4.6; sys_platform == "win32" or platform_system == "Windows"
coverage==7.5.3
# via uvicorn
deprecated==1.2.14
deptracpy @ git+https://github.com/patrickkusebauch/deptracpy@d1f699dace9db56ef3ed710c4998ae1909caa08a
deptry==0.16.1
dnspython==2.6.1
docstring-parser==0.16
email-validator==2.1.1
environs==11.0.0
exceptiongroup==1.2.1; python_version < "3.11"
fastapi==0.111.0
fastapi-cli==0.0.4
# via
# opentelemetry-api
# opentelemetry-exporter-otlp-proto-grpc
# opentelemetry-exporter-otlp-proto-http
# opentelemetry-semantic-conventions
fastapi==0.115.5
# via sblex-server (pyproject.toml)
googleapis-common-protos==1.63.1
# via
# opentelemetry-exporter-otlp-proto-grpc
# opentelemetry-exporter-otlp-proto-http
grpcio==1.64.1
gunicorn==22.0.0
# via opentelemetry-exporter-otlp-proto-grpc
h11==0.14.0
# via
# httpcore
# uvicorn
httpcore==1.0.5
httptools==0.6.1
httpx==0.27.0
# via httpx
httpx==0.27.2
# via
# sblex-server (pyproject.toml)
# asgi-matomo
idna==3.7
# via
# anyio
# httpx
# requests
ijson==3.3.0
# via json-arrays
importlib-metadata==7.1.0
iniconfig==2.0.0
# via opentelemetry-api
jinja2==3.1.4
# via sblex-server (pyproject.toml)
json-arrays==0.15.0
libcst==0.4.9
markdown-it-py==2.2.0
# via sblex-server (pyproject.toml)
markupsafe==2.1.5
marshmallow==3.21.3
mdurl==0.1.2
mypy==1.10.0
mypy-extensions==1.0.0
opentelemetry-api==1.25.0
opentelemetry-exporter-otlp==1.25.0
opentelemetry-exporter-otlp-proto-common==1.25.0
opentelemetry-exporter-otlp-proto-grpc==1.25.0
opentelemetry-exporter-otlp-proto-http==1.25.0
opentelemetry-instrumentation==0.46b0
opentelemetry-instrumentation-asgi==0.46b0
opentelemetry-instrumentation-fastapi==0.46b0
opentelemetry-instrumentation-httpx==0.46b0
opentelemetry-instrumentation-logging==0.46b0
opentelemetry-proto==1.25.0
opentelemetry-sdk==1.25.0
opentelemetry-semantic-conventions==0.46b0
opentelemetry-util-http==0.46b0
orjson==3.10.5
# via jinja2
opentelemetry-api==1.28.1
# via
# opentelemetry-exporter-otlp-proto-grpc
# opentelemetry-exporter-otlp-proto-http
# opentelemetry-instrumentation
# opentelemetry-instrumentation-asgi
# opentelemetry-instrumentation-fastapi
# opentelemetry-instrumentation-httpx
# opentelemetry-instrumentation-logging
# opentelemetry-sdk
# opentelemetry-semantic-conventions
opentelemetry-exporter-otlp==1.28.1
# via sblex-server (pyproject.toml)
opentelemetry-exporter-otlp-proto-common==1.28.1
# via
# opentelemetry-exporter-otlp-proto-grpc
# opentelemetry-exporter-otlp-proto-http
opentelemetry-exporter-otlp-proto-grpc==1.28.1
# via opentelemetry-exporter-otlp
opentelemetry-exporter-otlp-proto-http==1.28.1
# via opentelemetry-exporter-otlp
opentelemetry-instrumentation==0.49b1
# via
# opentelemetry-instrumentation-asgi
# opentelemetry-instrumentation-fastapi
# opentelemetry-instrumentation-httpx
# opentelemetry-instrumentation-logging
opentelemetry-instrumentation-asgi==0.49b1
# via opentelemetry-instrumentation-fastapi
opentelemetry-instrumentation-fastapi==0.49b1
# via sblex-server (pyproject.toml)
opentelemetry-instrumentation-httpx==0.49b1
# via sblex-server (pyproject.toml)
opentelemetry-instrumentation-logging==0.49b1
# via sblex-server (pyproject.toml)
opentelemetry-proto==1.28.1
# via
# opentelemetry-exporter-otlp-proto-common
# opentelemetry-exporter-otlp-proto-grpc
# opentelemetry-exporter-otlp-proto-http
opentelemetry-sdk==1.28.1
# via
# sblex-server (pyproject.toml)
# opentelemetry-exporter-otlp-proto-grpc
# opentelemetry-exporter-otlp-proto-http
opentelemetry-semantic-conventions==0.49b1
# via
# opentelemetry-instrumentation
# opentelemetry-instrumentation-asgi
# opentelemetry-instrumentation-fastapi
# opentelemetry-instrumentation-httpx
# opentelemetry-sdk
opentelemetry-util-http==0.49b1
# via
# opentelemetry-instrumentation-asgi
# opentelemetry-instrumentation-fastapi
# opentelemetry-instrumentation-httpx
orjson==3.10.11
# via sblex-server (pyproject.toml)
packaging==24.1
pluggy==1.5.0
prompt-toolkit==3.0.36
protobuf==4.25.3
# via opentelemetry-instrumentation
protobuf==5.28.3
# via
# googleapis-common-protos
# opentelemetry-proto
pydantic==2.7.4
# via
# fastapi
# pydantic-settings
pydantic-core==2.18.4
pydantic-settings==2.3.3
pygments==2.18.0
pytest==8.2.2
pytest-asyncio==0.23.7
pytest-cov==5.0.0
# via pydantic
pydantic-settings==2.6.1
# via sblex-server (pyproject.toml)
python-dotenv==1.0.1
python-json-logger==2.0.7
python-multipart==0.0.9
pyyaml==6.0.1
questionary==2.0.1
# via pydantic-settings
requests==2.32.3
returns==0.19.0
rich==13.3.4
rich-click==1.8.3
ruff==0.4.8
setuptools==70.0.0
shellingham==1.5.4
# via opentelemetry-exporter-otlp-proto-http
sniffio==1.3.1
starlette==0.37.2
syrupy==4.6.1
tomli==2.0.1; python_version < "3.11"
tomlkit==0.12.5
typed-argument-parser==1.8.0
typer==0.12.3
# via
# anyio
# httpx
starlette==0.41.2
# via
# brotli-asgi
# fastapi
typing-extensions==4.12.2
typing-inspect==0.9.0
ujson==5.10.0
# via
# fastapi
# opentelemetry-sdk
# pydantic
# pydantic-core
urllib3==2.2.1
uvicorn==0.30.1
uvloop==0.19.0; (sys_platform != "cygwin" and sys_platform != "win32") and platform_python_implementation != "PyPy"
watchfiles==0.22.0
wcmatch==8.5.2
wcwidth==0.2.13
websockets==12.0
# via requests
uvicorn==0.32.0
# via sblex-server (pyproject.toml)
wrapt==1.16.0
# via
# deprecated
# opentelemetry-instrumentation
# opentelemetry-instrumentation-httpx
zipp==3.19.2
# via importlib-metadata
Loading

0 comments on commit 700fdbb

Please sign in to comment.