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

Update SDK docs and Add example with OTEL collector logging (debug) exporter #2050

Merged
merged 6 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ __pycache__
venv*/
.venv*/
opentelemetry-python-contrib/
include
# in case of symlink
opentelemetry-python-contrib

Expand Down
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be set to loglevel: debug if we want the log output shown below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below example script has logs with different severities including error which is always shown I guess. I tested this manually before committing. I didn't have to set this log level.


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())
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
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