Skip to content

Commit

Permalink
Document and enable OpenTelemetry instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Jun 24, 2024
1 parent c4dda0d commit 4adcf84
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
Binary file added docs/guide/images/otel-waterfall-retry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/guide/images/otel-waterfall-with-http.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/guide/images/otel-waterfall-without-http.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions docs/guide/integrations.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
You can find integration options and information on this page.


[discrete]
[[opentelemetry-intro]]
=== OpenTelemetry instrumentation

The Python Elasticsearch client supports native OpenTelemetry instrumentation following the https://opentelemetry.io/docs/specs/semconv/database/elasticsearch/[OpenTelemetry Semantic Conventions for Elasticsearch].
Refer to the <<opentelemetry>> page for details.

[discrete]
[[transport]]
=== Transport
Expand Down Expand Up @@ -53,3 +60,6 @@ es.options(
------------------------------------

Type hints also allow tools like your IDE to check types and provide better auto-complete functionality.


include::open-telemetry.asciidoc[]
75 changes: 75 additions & 0 deletions docs/guide/open-telemetry.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[[opentelemetry]]
=== Using OpenTelemetry

You can use https://opentelemetry.io/[OpenTelemetry] to monitor the performance and behavior of your {es} requests through the Elasticsearch Python client.
The Python client comes with built-in OpenTelemetry instrumentation that emits https://www.elastic.co/guide/en/apm/guide/current/apm-distributed-tracing.html[distributed tracing spans] by default.
With that, applications using https://www.elastic.co/blog/manual-instrumentation-of-python-applications-opentelemetry[manual OpenTelemetry instrumentation] or https://www.elastic.co/blog/auto-instrumentation-of-python-applications-opentelemetry[automatic OpenTelemetry instrumentation] are enriched with additional spans that contain insightful information about the execution of the {es} requests.

The native instrumentation in the Python client follows the https://opentelemetry.io/docs/specs/semconv/database/elasticsearch/[OpenTelemetry Semantic Conventions for {es}]. In particular, the instrumentation in the client covers the logical layer of {es} requests. A single span per request is created that is processed by the service through the Python client. The following image shows a trace that records the handling of two different {es} requests: an `info` request and a `search` request.

[role="screenshot"]
image::images/otel-waterfall-without-http.png[alt="Distributed trace with Elasticsearch spans",align="center"]

Usually, OpenTelemetry auto-instrumentation modules come with instrumentation support for HTTP-level communication. In this case, in addition to the logical {es} client requests, spans will be captured for the physical HTTP requests emitted by the client. The following image shows a trace with both, {es} spans (in blue) and the corresponding HTTP-level spans (in red) after having installed the ``opentelemetry-instrumentation-urllib3`` package:

[role="screenshot"]
image::images/otel-waterfall-with-http.png[alt="Distributed trace with Elasticsearch spans",align="center"]

Advanced Python client behavior such as nodes round-robin and request retries are revealed through the combination of logical {es} spans and the physical HTTP spans. The following example shows a `search` request in a scenario with two nodes:

[role="screenshot"]
image::images/otel-waterfall-retry.png[alt="Distributed trace with Elasticsearch spans",align="center"]

The first node is unavailable and results in an HTTP error, while the retry to the second node succeeds. Both HTTP requests are subsumed by the logical {es} request span (in blue).

[discrete]
==== Setup the OpenTelemetry instrumentation

When using the https://opentelemetry.io/docs/languages/python/instrumentation/[manual Python OpenTelemetry instrumentation] or the https://opentelemetry.io/docs/languages/python/automatic/[OpenTelemetry Python agent], the Python client's OpenTelemetry instrumentation is enabled by default and uses the global OpenTelemetry SDK with the global tracer provider.
If you're getting started with OpenTelemetry instrumentation, the following blog posts have step-by-step instructions to ingest and explore tracing data with the Elastic stack:

* https://www.elastic.co/blog/manual-instrumentation-of-python-applications-opentelemetry[Manual instrumentation with OpenTelemetry for Python applications]
* https://www.elastic.co/blog/auto-instrumentation-of-python-applications-opentelemetry[Automatic instrumentation with OpenTelemetry for Python applications]

[discrete]
=== Comparison with community instrumentation

The https://opentelemetry-python-contrib.readthedocs.io/en/latest/instrumentation/elasticsearch/elasticsearch.html[commmunity OpenTelemetry Elasticsearch instrumentation] also instruments the client and sends OpenTelemetry traces, but was developed before the OpenTelemetry Semantic Conventions for {es}, so the traces attributes are inconsistent with other OpenTelemetry Elasticsearch client instrumentations. To avoid tracing the same requests twice, make sure to use only one instrumentation, either by uninstalling the opentelemetry-instrumentation-elasticsearch Python package or by <<opentelemetry-config-enable,disabling the native instrumentation>>.

[discrete]
==== Configuring the OpenTelemetry instrumentation

You can configure this OpenTelemetry instrumentation through environment variables.
The following configuration options are available.

[discrete]
[[opentelemetry-config-enable]]
===== Enable / Disable the OpenTelemetry instrumentation

With this configuration option you can enable (default) or disable the built-in OpenTelemetry instrumentation.

**Default:** `true`

|============
| Environment Variable | `OTEL_PYTHON_INSTRUMENTATION_ELASTICSEARCH_ENABLED`
|============

[discrete]
===== Capture search request bodies

Per default, the built-in OpenTelemetry instrumentation does not capture request bodies due to data privacy considerations. You can use this option to enable capturing of search queries from the request bodies of {es} search requests in case you wish to gather this information regardless. The options are to capture the raw search query or not capture it at all.

**Default:** `omit`

**Valid Options:** `omit`, `raw`

|============
| Environment Variable | `OTEL_PYTHON_INSTRUMENTATION_ELASTICSEARCH_CAPTURE_SEARCH_QUERY`
|============

[discrete]
==== Overhead

The OpenTelemetry instrumentation (as any other monitoring approach) may come with a slight overhead on CPU, memory, and/or latency. The overhead may only occur when the instrumentation is enabled (default) and an OpenTelemetry SDK is active in the target application. When the instrumentation is disabled or no OpenTelemetry SDK is active within the target application, monitoring overhead is not expected when using the client.

Even in cases where the instrumentation is enabled and is actively used (by an OpenTelemetry SDK), the overhead is minimal and negligible in the vast majority of cases. In edge cases where there is a noticeable overhead, the <<opentelemetry-config-enable,instrumentation can be explicitly disabled>> to eliminate any potential impact on performance.
2 changes: 1 addition & 1 deletion elasticsearch_serverless/_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(
body_strategy: 'Literal["omit", "raw"]' | None = None,
):
if enabled is None:
enabled = os.environ.get(ENABLED_ENV_VAR, "false") != "false"
enabled = os.environ.get(ENABLED_ENV_VAR, "true") == "true"
self.tracer = tracer or _tracer
self.enabled = enabled and self.tracer is not None

Expand Down
2 changes: 1 addition & 1 deletion test_elasticsearch_serverless/test_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def setup_tracing():

def test_enabled():
otel = OpenTelemetry()
assert otel.enabled == (os.environ.get(ENABLED_ENV_VAR, "false") != "false")
assert otel.enabled == (os.environ.get(ENABLED_ENV_VAR, "true") == "true")


def test_minimal_span():
Expand Down

0 comments on commit 4adcf84

Please sign in to comment.