From 8c78f0cee40c9c55e0a82e79cd7299d130aa30d6 Mon Sep 17 00:00:00 2001 From: "Shane R. Spencer" <305301+whardier@users.noreply.github.com> Date: Sun, 17 May 2020 03:48:28 +0000 Subject: [PATCH 1/2] Use bson.json_util to flatten out events received when description information is present (replica sets.. possibly sharding information). Ensure that command is stringy when attempting to concatenate strings. Suggest use fstrings if older pythons can be depreciated --- .../src/opentelemetry/ext/pymongo/__init__.py | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/ext/opentelemetry-ext-pymongo/src/opentelemetry/ext/pymongo/__init__.py b/ext/opentelemetry-ext-pymongo/src/opentelemetry/ext/pymongo/__init__.py index ee61f6d4f9..23eac274f5 100644 --- a/ext/opentelemetry-ext-pymongo/src/opentelemetry/ext/pymongo/__init__.py +++ b/ext/opentelemetry-ext-pymongo/src/opentelemetry/ext/pymongo/__init__.py @@ -40,6 +40,10 @@ --- """ +import json + +import bson.json_util + from pymongo import monitoring from opentelemetry import trace @@ -66,8 +70,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) @@ -104,7 +108,11 @@ 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): @@ -115,7 +123,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)), + ) + ) span.end() def _pop_span(self, event): From c659f40e32c63c80d8b9a1eb39504ba7db965521 Mon Sep 17 00:00:00 2001 From: "Shane R. Spencer" <305301+whardier@users.noreply.github.com> Date: Mon, 18 May 2020 01:22:49 +0000 Subject: [PATCH 2/2] Use bson.json_util to flatten out events received when description information is present (replica sets.. possibly sharding information). Ensure that command is stringy when attempting to concatenate strings. Suggest use fstrings if older pythons can be depreciated --- .../src/opentelemetry/ext/pymongo/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ext/opentelemetry-ext-pymongo/src/opentelemetry/ext/pymongo/__init__.py b/ext/opentelemetry-ext-pymongo/src/opentelemetry/ext/pymongo/__init__.py index 23eac274f5..6fa67eec87 100644 --- a/ext/opentelemetry-ext-pymongo/src/opentelemetry/ext/pymongo/__init__.py +++ b/ext/opentelemetry-ext-pymongo/src/opentelemetry/ext/pymongo/__init__.py @@ -43,14 +43,12 @@ import json import bson.json_util - -from pymongo import monitoring - 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"] @@ -110,7 +108,8 @@ def succeeded(self, event: monitoring.CommandSucceededEvent): span.set_attribute("db.mongo.duration_micros", event.duration_micros) span.set_status( Status( - StatusCanonicalCode.OK, json.loads(bson.json_util.dumps(event.reply)) + StatusCanonicalCode.OK, + json.loads(bson.json_util.dumps(event.reply)), ) ) span.end()