Skip to content

Commit

Permalink
chore(pymongo): service name override in pymongo (#10917)
Browse files Browse the repository at this point in the history
This PR makes the following changes:
- Setting `config.pymongo['service']` did not override service name, due
to previously having a `Pin` overriding. This changes the `Pin` on the
MongoClient` to `None`
- We should use `ext_service` instead of `int_service` for pymongo 

## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist
- [x] Reviewer has checked that all the criteria below are met 
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
  • Loading branch information
quinna-h authored Oct 7, 2024
1 parent f446733 commit 6536952
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
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

0 comments on commit 6536952

Please sign in to comment.