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

chore(pymongo): service name override in pymongo #10917

Merged
merged 15 commits into from
Oct 7, 2024
8 changes: 5 additions & 3 deletions ddtrace/contrib/internal/pymongo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

# project
import ddtrace
from ddtrace import Pin
from ddtrace import config
from ddtrace.constants import _ANALYTICS_SAMPLE_RATE_KEY
from ddtrace.constants import SPAN_KIND
from ddtrace.constants import SPAN_MEASURED_KEY
from ddtrace.contrib import trace_utils
from ddtrace.ext import SpanKind
from ddtrace.ext import SpanTypes
from ddtrace.ext import db
Expand Down Expand Up @@ -67,7 +69,7 @@ def __getddpin__(client):
client.__getddpin__ = functools.partial(__getddpin__, client)

# Set a pin on the traced mongo client
ddtrace.Pin(service=_DEFAULT_SERVICE).onto(client)
Pin(service=None).onto(client)


# The function is exposed in the public API, but it is not used in the codebase.
Expand Down Expand Up @@ -128,7 +130,7 @@ def _datadog_trace_operation(operation, wrapped):
span = pin.tracer.trace(
schematize_database_operation("pymongo.cmd", database_provider="mongodb"),
span_type=SpanTypes.MONGODB,
service=pin.service,
service=trace_utils.ext_service(pin, config.pymongo),
)

span.set_tag_str(COMPONENT, config.pymongo.integration_name)
Expand Down Expand Up @@ -251,7 +253,7 @@ def _trace_cmd(cmd, socket_instance, address):
s = pin.tracer.trace(
schematize_database_operation("pymongo.cmd", database_provider="mongodb"),
span_type=SpanTypes.MONGODB,
service=pin.service,
service=trace_utils.ext_service(pin, config.pymongo),
)

s.set_tag_str(COMPONENT, config.pymongo.integration_name)
Expand Down
9 changes: 4 additions & 5 deletions ddtrace/contrib/internal/pymongo/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from ddtrace.internal.wrapping import unwrap as _u
from ddtrace.internal.wrapping import wrap as _w

from ....internal.schema import schematize_service_name

# keep TracedMongoClient import to maintain backwards compatibility
from .client import TracedMongoClient # noqa: F401
from .client import _trace_mongo_client_init
Expand Down Expand Up @@ -46,10 +48,7 @@
_CHECKOUT_FN_NAME = "get_socket" if pymongo.version_tuple < (4, 5) else "checkout"


config._add(
"pymongo",
dict(_default_service="pymongo"),
)
config._add("pymongo", dict(_default_service=schematize_service_name("pymongo")))


def get_version():
Expand Down Expand Up @@ -119,7 +118,7 @@ def traced_get_socket(func, args, kwargs):

with pin.tracer.trace(
"pymongo.%s" % _CHECKOUT_FN_NAME,
service=trace_utils.int_service(pin, config.pymongo),
service=trace_utils.ext_service(pin, config.pymongo),
span_type=SpanTypes.MONGODB,
) as span:
span.set_tag_str(COMPONENT, config.pymongo.integration_name)
Expand Down
35 changes: 35 additions & 0 deletions tests/contrib/pymongo/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,27 @@ def test_user_specified_service_v0(self):
assert len(spans) == 2
assert spans[1].service != "mysvc"

@TracerTestCase.run_in_subprocess(env_overrides=dict())
def test_user_specified_service_default_override(self):
"""
Override service name using config
"""
from ddtrace import config
from ddtrace import patch

cfg = config.pymongo
cfg["service"] = "new-mongo"
patch(pymongo=True)
assert cfg.service == "new-mongo", f"service name is {cfg.service}"
tracer = DummyTracer()
client = pymongo.MongoClient(port=MONGO_CONFIG["port"])
Pin.get_from(client).clone(tracer=tracer).onto(client)

client["testdb"].drop_collection("whatever")
spans = tracer.pop()
assert spans
assert spans[0].service == "new-mongo", f"service name is {spans[0].service}"

@TracerTestCase.run_in_subprocess(env_overrides=dict(DD_SERVICE="mysvc", DD_TRACE_SPAN_ATTRIBUTE_SCHEMA="v1"))
def test_user_specified_service_v1(self):
"""
Expand All @@ -563,6 +584,20 @@ def test_user_specified_service_v1(self):
assert len(spans) == 2
assert spans[1].service == "mysvc"

@TracerTestCase.run_in_subprocess(env_overrides=dict())
def test_unspecified_service_v0(self):
"""
v0: When a user does not specify a service for the app
The pymongo integration should use pymongo.
"""
tracer = DummyTracer()
client = pymongo.MongoClient(port=MONGO_CONFIG["port"])
Pin.get_from(client).clone(tracer=tracer).onto(client)
client["testdb"].drop_collection("whatever")
spans = tracer.pop()
assert len(spans) == 2
assert spans[1].service == "pymongo"

@TracerTestCase.run_in_subprocess(
env_overrides=dict(DD_PYMONGO_SERVICE="mypymongo", DD_TRACE_SPAN_ATTRIBUTE_SCHEMA="v0")
)
Expand Down
Loading