Skip to content

feat(elasticsearch): Add Elasticsearch integration#5559

Closed
dmkoch wants to merge 1 commit intogetsentry:masterfrom
dmkoch:dkoch/add-elasticsearch-integration
Closed

feat(elasticsearch): Add Elasticsearch integration#5559
dmkoch wants to merge 1 commit intogetsentry:masterfrom
dmkoch:dkoch/add-elasticsearch-integration

Conversation

@dmkoch
Copy link

@dmkoch dmkoch commented Feb 26, 2026

Summary

  • Adds a new ElasticsearchIntegration for the elasticsearch Python client library
  • Instruments search, index, delete, and update operations with spans and breadcrumbs
  • Supports both elasticsearch 7.x (bundled transport) and 8.x/9.x (elastic-transport) APIs
  • Adds CI test configuration with Elasticsearch service container

Test plan

  • CI passes for the new elasticsearch test suite in test-integrations-dbs.yml
  • Verify tox environments are correctly generated for elasticsearch
  • Check that the integration auto-enables when the elasticsearch package is installed

🤖 Generated with Claude Code

Add a new integration for the `elasticsearch` Python client that
instruments search, index, delete, and update operations with spans
and breadcrumbs. Supports both elasticsearch 7.x and 8.x/9.x APIs.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@dmkoch dmkoch requested a review from a team as a code owner February 26, 2026 17:01
@dmkoch dmkoch closed this Feb 26, 2026
operation, index = _parse_url(path)
description = "{} {}".format(method, path)

span = sentry_sdk.start_span(
Copy link

Choose a reason for hiding this comment

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

Bug: The new Elasticsearch integration creates a span but doesn't use it as a context manager or manually call span.__enter__(), so the span is not attached to the current scope.
Severity: MEDIUM

Suggested Fix

The span should be managed using a with statement to ensure its lifecycle is handled correctly. Wrap the operation that the span is measuring in a with start_span(...) as span: block. This will automatically handle entering and exiting the span's context, ensuring it is properly attached to the transaction.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: sentry_sdk/integrations/elasticsearch.py#L131

Potential issue: The `elasticsearch` integration creates a new span using
`sentry_sdk.start_span(...)` but fails to properly manage its lifecycle. It does not use
a `with` statement or manually call `span.__enter__()`. This prevents the span from
being pushed onto the current scope's span stack. As a result, the span is not correctly
associated with the active transaction and may not be recorded or sent to Sentry,
leading to incomplete tracing data for Elasticsearch operations. Other integrations like
`boto3` and `pymongo` correctly handle this by explicitly calling `span.__enter__()` and
`span.__exit__()`.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

except Exception:
pass

return None, None
Copy link

Choose a reason for hiding this comment

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

Unused major_version parameter in _get_connection_info

Low Severity

The major_version parameter in _get_connection_info is accepted but never referenced in the function body. The function tries both the v7 approach (obj.hosts[0]) and the v8/v9 approach (obj.transport.node_pool.all()) sequentially via try/except regardless of the version. This unused parameter is misleading — it suggests version-specific logic that doesn't exist, and could confuse future maintainers into thinking version-dependent handling was intended but accidentally omitted.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant