Skip to content
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

ext/pymongo: Add instrumentor #612

Merged
merged 11 commits into from
Apr 27, 2020
Merged

ext/pymongo: Add instrumentor #612

merged 11 commits into from
Apr 27, 2020

Conversation

mauriciovasquezbernal
Copy link
Member

@mauriciovasquezbernal mauriciovasquezbernal commented Apr 24, 2020

This PR implements the Instrumentor interface for the Pymongo integration. It also implements a way to disable instrumentation.

I compared with the DataDog donated implementation available at https://github.com/open-telemetry/opentelemetry-python-contrib/tree/master/reference/ddtrace/contrib/pymongo, the already existing implementation is good enough and we do not need to take any from that in this case.

I'm opening a new PR to supersedes #598 to avoid reviewing and approving my own changes there.

How to try:

# install
pip install -e ext/opentelemetry-ext-pymymongo/
# set up test db
opentelemetry-ext-docker-tests/tests
docker-compose up

pymongo_example.py:

# Configuration code
from opentelemetry import trace

from opentelemetry.sdk.trace.export import (
    SimpleExportSpanProcessor,
    ConsoleSpanExporter,
)

trace.get_tracer_provider().add_span_processor(
    SimpleExportSpanProcessor(ConsoleSpanExporter())
)

# Real example starts here
import os

from pymongo import MongoClient

MONGODB_HOST = os.getenv("MONGODB_HOST ", "localhost")
MONGODB_PORT = int(os.getenv("MONGODB_PORT ", "27017"))
MONGODB_DB_NAME = os.getenv("MONGODB_DB_NAME ", "opentelemetry-tests")
MONGODB_COLLECTION_NAME = "test"

client = MongoClient(
    MONGODB_HOST, MONGODB_PORT, serverSelectionTimeoutMS=2000
)
db = client[MONGODB_DB_NAME]
collection = db[MONGODB_COLLECTION_NAME]

collection.insert_one({"name": "testName", "value": "testValue"})
OPENTELEMETRY_PYTHON_TRACER_PROVIDER=sdk_tracer_provider opentelemetry-auto-instrumentation pymongo_example.py

If everything went fine you see some spans on the terminal.

Alex Boten and others added 3 commits April 23, 2020 07:26
The current Pymongo integration uses the monitoring.register() [1] to hook
the different internal calls of Pymongo. This integration doesn't allow to
unregister a monitor. This commit workaround that limitation by adding an
enable flag to the CommandTracer class and adds a logic to disable the
integration.  This solution is not perfect becasue there will be some overhead
even when the instrumentation is disabled, but that's what we can do with
the current approach.

[1] https://api.mongodb.com/python/current/api/pymongo/monitoring.html#pymongo.monitoring.register
Pop span instead of getting and then removing it.
@mauriciovasquezbernal mauriciovasquezbernal requested a review from a team as a code owner April 24, 2020 14:54
@mauriciovasquezbernal mauriciovasquezbernal changed the title ext/pymogo: Add instrumentor ext/pymongo: Add instrumentor Apr 24, 2020
@mauriciovasquezbernal mauriciovasquezbernal added ext instrumentation Related to the instrumentation of third party libraries or frameworks labels Apr 24, 2020
Copy link
Member

@hectorhdzg hectorhdzg left a comment

Choose a reason for hiding this comment

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

LGTM only added some small suggestions

Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks.

ext/opentelemetry-ext-pymongo/CHANGELOG.md Show resolved Hide resolved

def _remove_span(self, event):
self._span_dict.pop(_get_span_dict_key(event))
if not self.enable:
Copy link
Member

Choose a reason for hiding this comment

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

I notice this snippet is almost identical to the above code. Don't know if it's worth refactoring into a utility method though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean the following snippet?

        if not self.is_enabled:
            return

?

Copy link
Member

Choose a reason for hiding this comment

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

nvm. I maybe be going crazy, but I thought a diff verifying significantly more standard attributes like db type.

@toumorokoshi toumorokoshi merged commit 8c360a1 into open-telemetry:master Apr 27, 2020
srikanthccv pushed a commit to srikanthccv/opentelemetry-python that referenced this pull request Nov 1, 2020
closes open-telemetry#612

Signed-off-by: Olivier Albertini <olivier.albertini@montreal.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
instrumentation Related to the instrumentation of third party libraries or frameworks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants