-
Notifications
You must be signed in to change notification settings - Fork 657
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fc66a47
Add docs, example with OTEL collector debug exporter
srikanthccv 87c8e73
Fix lint
srikanthccv 0a2f594
Update rst formatting
srikanthccv e827e13
Merge branch 'logs' into add-docs-example
srikanthccv 6cc39c7
Merge branch 'logs' into add-docs-example
srikanthccv 5de9d51
Merge branch 'logs' into add-docs-example
srikanthccv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
|
||
exporters: | ||
logging: | ||
|
||
processors: | ||
batch: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,6 @@ OpenTelemetry Python SDK | |
|
||
resources | ||
trace | ||
logs | ||
error_handler | ||
environment_variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.