From ca2a8440f75bad0e1deb0b79224426f4acbe6d3e Mon Sep 17 00:00:00 2001 From: Tigran Najaryan <4194920+tigrannajaryan@users.noreply.github.com> Date: Sun, 17 Oct 2021 13:10:19 -0400 Subject: [PATCH] Demonstrate how to set the Resource for LogEmitterProvider (#2209) * Demonstrate how to set the Resource for LogEmitterProvider Added a Resource to the logs example to make it more complete. Previously it was using the built-in Resource. Now it adds the service.name and service.instance.id attributes. The resulting emitted log records look like this: ``` Resource labels: -> telemetry.sdk.language: STRING(python) -> telemetry.sdk.name: STRING(opentelemetry) -> telemetry.sdk.version: STRING(1.5.0) -> service.name: STRING(shoppingcart) -> service.instance.id: STRING(instance-12) InstrumentationLibraryLogs #0 InstrumentationLibrary __main__ 0.1 LogRecord #0 Timestamp: 2021-10-14 18:33:43.425820928 +0000 UTC Severity: ERROR ShortName: Body: Hyderabad, we have a major problem. Trace ID: ce1577e4a703f42d569e72593ad71888 Span ID: f8908ac4258ceff6 Flags: 1 ``` * Fix linting --- docs/examples/logs/example.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/docs/examples/logs/example.py b/docs/examples/logs/example.py index 5cf4ed838c5..f172c02aa16 100644 --- a/docs/examples/logs/example.py +++ b/docs/examples/logs/example.py @@ -2,8 +2,13 @@ from opentelemetry import trace from opentelemetry.exporter.otlp.proto.grpc.log_exporter import OTLPLogExporter -from opentelemetry.sdk.logs import OTLPHandler, get_log_emitter_provider +from opentelemetry.sdk.logs import ( + LogEmitterProvider, + OTLPHandler, + set_log_emitter_provider, +) from opentelemetry.sdk.logs.export import SimpleLogProcessor +from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import ( ConsoleSpanExporter, @@ -15,7 +20,16 @@ SimpleSpanProcessor(ConsoleSpanExporter()) ) -log_emitter_provider = get_log_emitter_provider() +log_emitter_provider = LogEmitterProvider( + resource=Resource.create( + { + "service.name": "shoppingcart", + "service.instance.id": "instance-12", + } + ), +) +set_log_emitter_provider(log_emitter_provider) + exporter = OTLPLogExporter(insecure=True) log_emitter_provider.add_log_processor(SimpleLogProcessor(exporter)) log_emitter = log_emitter_provider.get_log_emitter(__name__, "0.1")