-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
OpenTelemetry instrumentation #1790
Merged
Merged
Conversation
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
… service class name
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #1790 +/- ##
==========================================
+ Coverage 72.92% 73.59% +0.67%
==========================================
Files 43 52 +9
Lines 6193 7646 +1453
==========================================
+ Hits 4516 5627 +1111
- Misses 1677 2019 +342
☔ View full report in Codecov by Sentry. |
kjagiello
reviewed
Aug 22, 2023
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
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.
Using OpenTelemetry instrumentation
Install
tomodachi
using theopentelemetry
extras to enable instrumentation for OpenTelemetry. In addition, install with theopentelemetry-exporter-prometheus
extras to use Prometheus exporter metrics.When added as a Poetry dependency the
opentelemetry
extras can be enabled by addingtomodachi = {extras = ["opentelemetry"]}
to thepyproject.toml
file, and when added to arequiements.txt
file theopentelemetry
extras can be enabled by addingtomodachi[opentelemetry]
to the file.Auto instrumentation:
tomodachi --opentelemetry-instrument
Passing the
--opentelemetry-instrument
argument totomodachi run
will automatically instrument the service with the appropriate exporters and configuration according to the setOTEL_*
environment variables.If
tomodachi
is installed in the environment, usingtomodachi --opentelemetry-instrument service.py
is mostly equivalent to runningopentelemetry-instrument tomodachi run service.py
and will load distros, configurators and instrumentors automatically in the same way as theopentelemetry-instrument
CLI would do.The environment variable
TOMODACHI_OPENTELEMETRY_INSTRUMENT
if set will also enable auto instrumentation in the same way.Auto instrumentation using the
opentelemetry-instrument
CLIAuto instrumentation using
opentelemetry
can then be activated by starting services usingopentelemetry-instrument [otel-options] tomodachi run [options] <service.py ...>
.Manual instrumentation
Auto instrumentation using either
tomodachi --opentelemetry-instrument
, setting theTOMODACHI_OPENTELEMETRY_INSTRUMENT=1
env value or using theopentelemetry-instrument
CLI are the recommended ways of instrumenting services, as they will automatically instrument the service (and libs with instrumentors installed) with the appropriate exporters and configuration.However, instrumentation can also be enabled by importing the
TomodachiInstrumentor
instrumentation class and calling its'instrument
function.Starting such a service with the appropriate
OTEL_*
environment variables would properly instrument traces, logs and metrics for the service without the need to use theopentelemetry-instrument
CLI.Service name dynamically set if missing
OTEL_SERVICE_NAME
valueIf the
OTEL_SERVICE_NAME
environment variable value (or--service_name
argument toopentelemetry-instrument
) is not set, the resource'service.name
will instead be set to thename
attribute of the service class. In case the service class uses the default generic names (service
orapp
), the resource'service.name
will instead be set to the default as specified in https://github.com/open-telemetry/semantic-conventions/tree/main/docs/resource#service.Note that instrumentation for logging will currently primarily use the
OTEL_SERVICE_NAME
, and if it's missing then use the name from the first instrumented service class.Exclude lists to exclude certain URLs from traces and metrics
To exclude certain URLs from tracking, set the environment variable
OTEL_PYTHON_TOMODACHI_EXCLUDED_URLS
(orOTEL_PYTHON_EXCLUDED_URLS
to cover all instrumentations) to a string of comma delimited regexes that match the URLs.Regexes from the
OTEL_PYTHON_AIOHTTP_EXCLUDED_URLS
environment variable will also be excluded.For example,
will exclude requests such as
https://site/client/123/info
andhttps://site/xyz/healthcheck
.You can also pass comma delimited regexes directly to the
instrument
method:Prometheus meter provider (experimental)
The
tomodachi.opentelemetry
module also provides a Prometheus meter provider that can be used to export metrics to Prometheus. Runopentelemetry-instrument
with the--meter_provider tomodachi_prometheus
argument (or setOTEL_PYTHON_METER_PROVIDER=tomodachi_prometheus
environment value) to enable the Prometheus meter provider.Environment variables to configure Prometheus meter provider
OTEL_PYTHON_TOMODACHI_PROMETHEUS_ADDRESS
specifies the host address the Prometheus export server should listen on. (default:"localhost"
)OTEL_PYTHON_TOMODACHI_PROMETHEUS_PORT
specifies the port the Prometheus export server should listen on. (default:9464
)OTEL_PYTHON_TOMODACHI_PROMETHEUS_INCLUDE_SCOPE_INFO
specifies whether to include scope information asotel_scope_info
value. (default:true
)OTEL_PYTHON_TOMODACHI_PROMETHEUS_INCLUDE_TARGET_INFO
specifies whether to include resource attributes astarget_info
value. (default:true
)OTEL_PYTHON_TOMODACHI_PROMETHEUS_EXEMPLARS_ENABLED
specifies whether exemplars (experimental) should be collected and used in Prometheus export. (default:false
)OTEL_PYTHON_TOMODACHI_PROMETHEUS_NAMESPACE_PREFIX
specifies the namespace prefix for Prometheus metrics. A final underscore is automatically added if prefix is used. (default:""
)Dependency requirement for Prometheus meter provider
The
tomodachi_prometheus
meter provider requires that theopentelemetry-exporter-prometheus
andprometheus_client
packages package are installed.Use
tomodachi
extrasopentelemetry-exporter-prometheus
to automatically include a compatible version of the exporter.OpenMetrics output from Prometheus with exemplars enabled
With exemplars enabled, make sure to call the Prometheus client with the accept header
application/openmetrics-text
to ensure exemplars are included in the response.curl http://localhost:9464/metrics -H "Accept: application/openmetrics-text"
Note that if the accept header
application/openmetrics-text
is missing from the request, exemplars will be excluded from the response.Example: starting a service with instrumentation
This example will start and instrument a service with OTLP exported traces sent to the endpoint
otelcol:4317
and metrics that can be scraped by Prometheus from port9464
. All metrics except fortarget_info
andotel_scope_inf
will be prefixed with"tomodachi_"
. Additionally exemplars will be added to the Prometheus collected metrics that includes sample exemplars with trace_id and span_id labels.