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

Logs: LogRecord not documented, usage attempt fails with an exception #3552

Closed
tigrannajaryan opened this issue Nov 22, 2023 · 5 comments
Closed
Labels
bug Something isn't working logging

Comments

@tigrannajaryan
Copy link
Member

Describe your environment
Mac OS X
Python 3.9.6

Steps to reproduce
Try to use LogRecord described here: https://opentelemetry-python.readthedocs.io/en/latest/api/_logs.html#opentelemetry._logs.LogRecord

The docs don't say how to construct the LogRecord. I tried this:

logger_provider = LoggerProvider(
    resource=Resource.create(
        {
            "service.name": "shoppingcart",
            "service.instance.id": "instance-12",
        }
    ),
)
set_logger_provider(logger_provider)

_logger = _logs.get_logger("bridge-logger")
logRecord = _logs.LogRecord()
_logger.emit(logRecord)

What is the expected behavior?
An empty log record outputed.

What is the actual behavior?

Exception while exporting logs.
Traceback (most recent call last):
  File "/Users/tnajaryan/work/experiments/otel-python/lib/python3.9/site-packages/opentelemetry/sdk/_logs/_internal/export/__init__.py", line 312, in _export_batch
    self._exporter.export(self._log_records[:idx])  # type: ignore
  File "/Users/tnajaryan/work/experiments/otel-python/lib/python3.9/site-packages/opentelemetry/exporter/otlp/proto/grpc/_log_exporter/__init__.py", line 108, in export
    return self._export(batch)
  File "/Users/tnajaryan/work/experiments/otel-python/lib/python3.9/site-packages/opentelemetry/exporter/otlp/proto/grpc/exporter.py", line 263, in _export
    request=self._translate_data(data),
  File "/Users/tnajaryan/work/experiments/otel-python/lib/python3.9/site-packages/opentelemetry/exporter/otlp/proto/grpc/_log_exporter/__init__.py", line 105, in _translate_data
    return encode_logs(data)
  File "/Users/tnajaryan/work/experiments/otel-python/lib/python3.9/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/tnajaryan/work/experiments/otel-python/lib/python3.9/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py", line 59, in _encode_resource_logs
    sdk_resource = sdk_log.log_record.resource
AttributeError: 'LogRecord' object has no attribute 'resource'

I can't see a way to supply the 'resource', the LogRecord() constructor does not accept one. Besides, I should not need to, it should use the one given to LoggerProvider.

Additional context
Part of review open-telemetry/community#1751

@tigrannajaryan tigrannajaryan added the bug Something isn't working label Nov 22, 2023
@lzchen lzchen added the logging label Nov 22, 2023
@lzchen
Copy link
Contributor

lzchen commented Jun 27, 2024

Seems like you are using the LogRecord as defined by the api rather than the sdk LogRecord that the OpenTelemetry exporters expect when using the sdk LoggerProvider.

@emdneto
Copy link
Member

emdneto commented Jun 28, 2024

@lzchen if I remember correctly, right now we also support passing the resource object through LogRecord as well

@lzchen
Copy link
Contributor

lzchen commented Jul 1, 2024

@emdneto

Could you be more specific about what you are referring to? resource is an SDK concept so using the api logRecord = _logs.LogRecord() is going to fail.

@emdneto
Copy link
Member

emdneto commented Jul 1, 2024

@lzchen Yes. I was referring to the SDK, forgot to complete the sentence.

@lzchen
Copy link
Contributor

lzchen commented Jul 1, 2024

Given that LogRecords (both api and sdk) are not meant to be instantiated directly (which is why we don't refer to them in our docs) as well as using the api LogRecord in conjunction with the SDK LoggerProvider is a misuse of the sdk, I believe this issue can be closed.

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

No branches or pull requests

3 participants