-
Notifications
You must be signed in to change notification settings - Fork 2
Add option to disable sending telemetry to an Otel Collector #647
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e171410
Add OTEL_SDK_DISABLED environment variable to disable sending telemetry
p-j-smith a935b6d
Manually instrument traces in `pixl_cli`, `orthanc_raw`, and `orthanc…
p-j-smith c66c4d2
Use OTEL_SDK_DISABLED to check whether telemtry collection should be …
p-j-smith 4a288bf
Set OTEL_PYTHON_LOG_AUTO_INSTRUMENTATION to false
p-j-smith dd04239
Reuse existing logging provider if it exists
p-j-smith 8b3802a
Remove unused environment variable
p-j-smith c832a1a
Merge branch 'main' into paul/646-disable-otel
p-j-smith 7731884
Set OTEL_SDK_DISABLED to true for unit tests
p-j-smith 6262b02
Disable the otel sdk in the core unit tests too
p-j-smith b66196e
Add tests to check provider is reused or created correctly
p-j-smith 1253d85
Set OTEL_SDK_DISABLED to true for cli unit tests
p-j-smith 62d565d
Add a core.telemetry module for configuring otel
p-j-smith 3d8fb96
Import configure_logging and configure_tracing from the new telemetry…
p-j-smith cfa44e3
Add missing docstring
p-j-smith a3c6b8a
Add comments to clarify why we need to reuse an existing otel provider
p-j-smith 2405278
Make linters happy
p-j-smith 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
| # Copyright (c) University College London Hospitals NHS Foundation Trust | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """Configure OpenTelemetry for PIXL services.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import atexit | ||
| import os | ||
| import sys | ||
|
|
||
| from decouple import config | ||
| from loguru import logger | ||
| from opentelemetry import trace | ||
| from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter | ||
| from opentelemetry.sdk.resources import Resource | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
|
||
| from core.logging import OTelSink | ||
|
|
||
| __all__ = [ | ||
| "configure_logging", | ||
| "configure_tracing", | ||
| "telemetry_is_enabled", | ||
| ] | ||
|
|
||
|
|
||
| def telemetry_is_enabled() -> bool: | ||
| """ | ||
| Check whether telemetry should be enabled. | ||
|
|
||
| It should be disabled if OTEL_SDK_DISABLED is true or if OTEL_EXPORTER_OTLP_ENDPOINT is not set. | ||
| """ | ||
| disabled = config("OTEL_SDK_DISABLED", cast=bool) | ||
| if disabled: | ||
| logger.warning("OTEL_SDK_DISABLED is set, skipping OTel configuration") | ||
| return False | ||
|
|
||
| endpoint = config("OTEL_EXPORTER_OTLP_ENDPOINT") | ||
| if not endpoint: | ||
| logger.warning( | ||
| "OTEL_EXPORTER_OTLP_ENDPOINT is not set. Telemetry will not be sent to the collector." | ||
| ) | ||
| # Disable the SDK to avoid errors from the OTel SDK | ||
| os.environ["OTEL_SDK_DISABLED"] = "true" | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def configure_logging(level: str) -> None: | ||
| """ | ||
| Configure loguru for a PIXL service. | ||
|
|
||
| Always logs to stderr, which will be viewable in the Docker logs. When | ||
| OTEL_SDK_DISABLED is false and OTEL_EXPORTER_OTLP_ENDPOINT is set, also | ||
| sends logs to the OTel collector. | ||
| """ | ||
| logger.remove() | ||
| logger.add(sys.stderr, level=level.upper()) | ||
|
|
||
| if not telemetry_is_enabled(): | ||
| return | ||
|
|
||
| sink = OTelSink() | ||
| logger.add(sink, level=level.upper()) | ||
|
|
||
|
|
||
| def configure_tracing() -> None: | ||
| """ | ||
| Set up an OTLP span exporter when OTEL_SDK_DISABLED is false | ||
| and OTEL_EXPORTER_OTLP_ENDPOINT is set in the environment. | ||
| """ | ||
| if not telemetry_is_enabled(): | ||
| return | ||
|
|
||
| # If we have auto-instrumented the service, there's no way to tell the OTel SDK not to | ||
| # create the provider. So we have to reuse it here to avoid warnings in the logs. | ||
| # The provider created by the OTel SDK is equivalent to the one we create below. | ||
| existing_provider = trace.get_tracer_provider() | ||
| if isinstance(existing_provider, TracerProvider): | ||
| return | ||
|
|
||
| exporter = OTLPSpanExporter() | ||
| processor = BatchSpanProcessor(exporter) | ||
| provider = TracerProvider(resource=Resource.create()) | ||
| provider.add_span_processor(processor) | ||
| trace.set_tracer_provider(provider) | ||
| atexit.register(provider.shutdown) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.