opentelemetry-python
opentelemetry-python copied to clipboard
Add param for indent size to LogRecord.to_json()
Description
This change will allow callers to the LogRecord.to_json() func to specify an indent
. Previously the func used 4
as the indent
. The consequences of that hard coding meant that log records were being emitted on > 1 line.
This PR will allow callers to send None
to the func, which allows the LogRecord to be emitted on a single line. This has the benefit of allowing downstream consumers to read the log as single line and index its keys for queries.
The example below shows how this can be leveraged by sending the indent
level as a param to the LogRecord.to_json() func as part of a formatter to the exporter.
This addresses an issue previously documented here: Single log line
logs.py
import time
from os import linesep
from opentelemetry.sdk._logs import (
LogEmitterProvider,
LogRecord,
get_log_emitter_provider,
set_log_emitter_provider,
)
from opentelemetry.sdk._logs.export import ConsoleLogExporter, SimpleLogProcessor
from opentelemetry.sdk.resources import Resource
def configure_log_emitter_provider(indent):
provider = LogEmitterProvider(resource=Resource.create())
set_log_emitter_provider(provider)
formatter = lambda record: LogRecord.to_json(record, indent=indent) + linesep
exporter = ConsoleLogExporter(formatter=formatter)
provider.add_log_processor(SimpleLogProcessor(exporter))
if __name__ == "__main__":
configure_log_emitter_provider(indent=None)
log_emitter = get_log_emitter_provider().get_log_emitter(
"logs.py",
"0.0.1",
)
log_record = LogRecord(
timestamp=time.time_ns(),
body="I am a log line. The one and only.",
)
log_emitter.emit(log_record)
{"body": "I am a log line. The one and only.", "severity_number": "None", "severity_text": null, "attributes": null, "timestamp": "2022-08-10T13:26:04.048569Z", "trace_id": "", "span_id": "", "trace_flags": null, "resource": ""}
Type of change
Please delete options that are not relevant.
- [x] Bug fix (non-breaking change which fixes an issue)
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
- [x] Ran opentelemetry-api test suite
- [x] Ran opentelemetry-sdk test suite
Does This PR Require a Contrib Repo Change?
- [x] No.
Checklist:
- [x] Followed the style guidelines of this project
- [x] Changelogs have been updated
The committers listed above are authorized under a signed CLA.
- :white_check_mark: login: dougramirez / name: Doug Ramirez (f7aa32e7f4bdb42d3b0692cbe8c217c1dcf47224, 3561bb3f946f098485234f0a403d91df439c6581)
I just want to call out couple of things based on your usage. The Console{Trace/Metric/Log}Exporter
s primarily intended for the debugging. And Simple{Span/Log}Processor
s are blocking and are not recommended for production usage. This console exporter is not part of the sdk specification so it might be dropped in future. If you want to have log/trace co-relation you might just want to use https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/logging/logging.html
I just want to call out couple of things based on your usage. The
Console{Trace/Metric/Log}Exporter
s primarily intended for the debugging. AndSimple{Span/Log}Processor
s are blocking and are not recommended for production usage. This console exporter is not part of the sdk specification so it might be dropped in future. If you want to have log/trace co-relation you might just want to use https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/logging/logging.html
Thank you for the call out! If the console exporter gets dropped, is it safe to assume that LogRecord
and the to_json()
func will exist in the future as the Logs Data Model is Stable?
Maybe, but we do not guarantee anything about logs SDK.
@dougramirez please fix the conflicts in this PR