Skip to content

Commit

Permalink
Update SDK docs and Add example with OTEL collector logging (debug) e…
Browse files Browse the repository at this point in the history
…xporter (#2050)
  • Loading branch information
srikanthccv authored and Alex Boten committed Oct 29, 2021
1 parent f6462e9 commit 3a92dce
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 2 deletions.
75 changes: 75 additions & 0 deletions docs/examples/logs/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
OpenTelemetry Logs SDK
======================

Start the Collector locally to see data being exported. Write the following file:

.. code-block:: yaml
# otel-collector-config.yaml
receivers:
otlp:
protocols:
grpc:
exporters:
logging:
processors:
batch:
Then start the Docker container:

.. code-block:: sh
docker run \
-p 4317:4317 \
-v $(pwd)/otel-collector-config.yaml:/etc/otel/config.yaml \
otel/opentelemetry-collector-contrib:latest
.. code-block:: sh
$ python example.py
The resulting logs will appear in the output from the collector and look similar to this:

.. code-block:: sh
ResourceLog #0
Resource labels:
-> telemetry.sdk.language: STRING(python)
-> telemetry.sdk.name: STRING(opentelemetry)
-> telemetry.sdk.version: STRING(1.5.0.dev0)
-> service.name: STRING(unknown_service)
InstrumentationLibraryLogs #0
InstrumentationLibrary __main__ 0.1
LogRecord #0
Timestamp: 2021-08-18 08:26:53.837349888 +0000 UTC
Severity: ERROR
ShortName:
Body: Exception while exporting logs.
ResourceLog #1
Resource labels:
-> telemetry.sdk.language: STRING(python)
-> telemetry.sdk.name: STRING(opentelemetry)
-> telemetry.sdk.version: STRING(1.5.0.dev0)
-> service.name: STRING(unknown_service)
InstrumentationLibraryLogs #0
InstrumentationLibrary __main__ 0.1
LogRecord #0
Timestamp: 2021-08-18 08:26:53.842546944 +0000 UTC
Severity: ERROR
ShortName:
Body: The five boxing wizards jump quickly.
ResourceLog #2
Resource labels:
-> telemetry.sdk.language: STRING(python)
-> telemetry.sdk.name: STRING(opentelemetry)
-> telemetry.sdk.version: STRING(1.5.0.dev0)
-> service.name: STRING(unknown_service)
InstrumentationLibraryLogs #0
InstrumentationLibrary __main__ 0.1
LogRecord #0
Timestamp: 2021-08-18 08:26:53.843979008 +0000 UTC
Severity: ERROR
ShortName:
Body: Hyderabad, we have a major problem.
46 changes: 46 additions & 0 deletions docs/examples/logs/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import logging

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.export import SimpleLogProcessor
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleSpanProcessor,
)

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(ConsoleSpanExporter())
)

log_emitter_provider = get_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")
handler = OTLPHandler(level=logging.NOTSET, log_emitter=log_emitter)

# Attach OTLP handler to root logger
logging.getLogger("root").addHandler(handler)

# Log directly
logging.info("Jackdaws love my big sphinx of quartz.")

# Create different namespaced loggers
logger1 = logging.getLogger("myapp.area1")
logger2 = logging.getLogger("myapp.area2")

logger1.debug("Quick zephyrs blow, vexing daft Jim.")
logger1.info("How quickly daft jumping zebras vex.")
logger2.warning("Jail zesty vixen who grabbed pay from quack.")
logger2.error("The five boxing wizards jump quickly.")


# Trace context correlation
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("foo"):
# Do something
logger2.error("Hyderabad, we have a major problem.")

log_emitter_provider.shutdown()
10 changes: 10 additions & 0 deletions docs/examples/logs/otel-collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
receivers:
otlp:
protocols:
grpc:

exporters:
logging:

processors:
batch:
7 changes: 7 additions & 0 deletions docs/sdk/logs.export.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.sdk.logs.export
=============================

.. automodule:: opentelemetry.sdk.logs.export
:members:
:undoc-members:
:show-inheritance:
15 changes: 15 additions & 0 deletions docs/sdk/logs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
opentelemetry.sdk.logs package
===============================

Submodules
----------

.. toctree::

logs.export
logs.severity

.. automodule:: opentelemetry.sdk.logs
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/sdk/logs.severity.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
opentelemetry.sdk.logs.severity
===============================

.. automodule:: opentelemetry.sdk.logs.severity
:members:
:undoc-members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/sdk/sdk.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ OpenTelemetry Python SDK

resources
trace
logs
error_handler
environment_variables
3 changes: 1 addition & 2 deletions opentelemetry-sdk/src/opentelemetry/sdk/logs/severity.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class SeverityNumber(enum.Enum):
See the `Log Data Model`_ spec for more info and how to map the
severity from source format to OTLP Model.
.. _Log Data Model:
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber
.. _Log Data Model: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber
"""

UNSPECIFIED = 0
Expand Down

0 comments on commit 3a92dce

Please sign in to comment.