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

Fix pymongo event object and string commands #703

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@
---
"""

from pymongo import monitoring
import json

import bson.json_util
from opentelemetry import trace
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.ext.pymongo.version import __version__
from opentelemetry.trace import SpanKind, get_tracer
from opentelemetry.trace.status import Status, StatusCanonicalCode
from pymongo import monitoring

DATABASE_TYPE = "mongodb"
COMMAND_ATTRIBUTES = ["filter", "sort", "skip", "limit", "pipeline"]
Expand All @@ -66,8 +68,8 @@ def started(self, event: monitoring.CommandStartedEvent):
name = DATABASE_TYPE + "." + event.command_name
statement = event.command_name
if command:
name += "." + command
statement += " " + command
name += "." + str(command)
statement += " " + str(command)

try:
span = self._tracer.start_span(name, kind=SpanKind.CLIENT)
Expand Down Expand Up @@ -104,7 +106,12 @@ def succeeded(self, event: monitoring.CommandSucceededEvent):
if span is None:
return
span.set_attribute("db.mongo.duration_micros", event.duration_micros)
span.set_status(Status(StatusCanonicalCode.OK, event.reply))
span.set_status(
Status(
StatusCanonicalCode.OK,
json.loads(bson.json_util.dumps(event.reply)),
)
)
span.end()

def failed(self, event: monitoring.CommandFailedEvent):
Expand All @@ -115,7 +122,12 @@ def failed(self, event: monitoring.CommandFailedEvent):
if span is None:
return
span.set_attribute("db.mongo.duration_micros", event.duration_micros)
span.set_status(Status(StatusCanonicalCode.UNKNOWN, event.failure))
span.set_status(
Status(
StatusCanonicalCode.UNKNOWN,
json.loads(bson.json_util.dumps(event.failure)),
Copy link
Member

Choose a reason for hiding this comment

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

will this stringify all expected types? One thing I see is that event.failure is a more structured document. the Status description must be a string, or it will be discarded.

Instead of json.loads, should this be a str() cast?

)
)
span.end()

def _pop_span(self, event):
Expand Down