Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging Instrumentation gives Exception: Invalid type when an exception is passed directly to logging handler #2768

Closed
meldsza opened this issue Aug 1, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@meldsza
Copy link

meldsza commented Aug 1, 2024

Describe your environment

OS: Mac and RHEL 9
Python version: python 3.12
Package version: 0.47b0

What happened?

The Opentelemetry Logging instrumentation gives an exception when logging an exception directly.

Steps to Reproduce

Provided below is a minimal example to reproduce this issue

import logging
import sys

from opentelemetry import trace

tracer = trace.get_tracer_provider().get_tracer(__name__)


# Trace context correlation
with tracer.start_as_current_span("foo"):
    # Do something
    current_span = trace.get_current_span()
    current_span.add_event("This is a span event")
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))
    logging.getLogger().error(ValueError("Test"))

Run this using opentelemetry-instrument --traces_exporter none --logs_exporter otlp python example.py

Expected Result

No exception is raised and the log is transported successfully to the otel collector.

Actual Result

An exception is raised and the log is not transported to the collector

otel-logs-py3.12meldsza@MacBook-Pro otel-logs % opentelemetry-instrument --traces_exporter none --logs_exporter otlp python example.py
Test
Exception while exporting logs.
Traceback (most recent call last):
  File "/Users/meldsza/Desktop/work/repos/otel-logs/.venv/lib/python3.12/site-packages/opentelemetry/sdk/_logs/_internal/export/__init__.py", line 311, in _export_batch
    self._exporter.export(self._log_records[:idx])  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/meldsza/Desktop/work/repos/otel-logs/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/http/_log_exporter/__init__.py", line 138, in export
    serialized_data = encode_logs(batch).SerializeToString()
                      ^^^^^^^^^^^^^^^^^^
  File "/Users/meldsza/Desktop/work/repos/otel-logs/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py", line 38, in encode_logs
    return ExportLogsServiceRequest(resource_logs=_encode_resource_logs(batch))
                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/meldsza/Desktop/work/repos/otel-logs/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py", line 72, in _encode_resource_logs
    pb2_log = _encode_log(sdk_log)
              ^^^^^^^^^^^^^^^^^^^^
  File "/Users/meldsza/Desktop/work/repos/otel-logs/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py", line 58, in _encode_log
    body=_encode_value(log_data.log_record.body),
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/meldsza/Desktop/work/repos/otel-logs/.venv/lib/python3.12/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py", line 88, in _encode_value
    raise Exception(f"Invalid type {type(value)} of value {value}")
Exception: Invalid type <class 'ValueError'> of value Test

Additional context

No response

Would you like to implement a fix?

None

@meldsza meldsza added the bug Something isn't working label Aug 1, 2024
@meldsza
Copy link
Author

meldsza commented Aug 1, 2024

Closing this as I realised this is not part of contrib but the main opentelemetry python repo

@meldsza meldsza closed this as completed Aug 1, 2024
@meldsza
Copy link
Author

meldsza commented Aug 1, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant