Skip to content

Commit 966f128

Browse files
committed
pymongo: clean up command initialization
because we pretty much always have the db.
1 parent 9d9c1c6 commit 966f128

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

ddtrace/contrib/pymongo/parse.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ class Command(object):
4343

4444
__slots__ = ['name', 'coll', 'db', 'tags', 'metrics', 'query']
4545

46-
def __init__(self, name, coll):
46+
def __init__(self, name, db, coll):
4747
self.name = name
4848
self.coll = coll
49-
self.db = None
49+
self.db = db
5050
self.tags = {}
5151
self.metrics = {}
5252
self.query = None
@@ -55,8 +55,9 @@ def __repr__(self):
5555
return (
5656
"Command("
5757
"name=%s,"
58+
"db=%s,"
5859
"coll=%s)"
59-
) % (self.name, self.coll)
60+
) % (self.name, self.db, self.coll)
6061

6162

6263
def parse_msg(msg_bytes):
@@ -103,14 +104,12 @@ def parse_msg(msg_bytes):
103104
# inserts will be affected.
104105
codec = CodecOptions(SON)
105106
spec = next(bson.decode_iter(msg_bytes[offset:], codec_options=codec))
106-
cmd = parse_spec(spec)
107+
cmd = parse_spec(spec, db)
107108
else:
108109
# let's still note that a command happened.
109-
cmd = Command("command", "untraced_message_too_large")
110+
cmd = Command("command", db, "untraced_message_too_large")
110111

111112
# If the command didn't contain namespace info, set it here.
112-
if not cmd.db:
113-
cmd.db = db
114113
if not cmd.coll:
115114
cmd.coll = coll
116115

@@ -131,12 +130,11 @@ def parse_query(query):
131130

132131
# FIXME[matt] mongo < 3.1 _Query doesn't not have a name field,
133132
# so hardcode to query.
134-
cmd = Command("query", coll)
133+
cmd = Command("query", db, coll)
135134
cmd.query = query.spec
136-
cmd.db = db
137135
return cmd
138136

139-
def parse_spec(spec):
137+
def parse_spec(spec, db=None):
140138
""" Return a Command that has parsed the relevant detail for the given
141139
pymongo SON spec.
142140
"""
@@ -146,7 +144,7 @@ def parse_spec(spec):
146144
if not items:
147145
return None
148146
name, coll = items[0]
149-
cmd = Command(name, coll)
147+
cmd = Command(name, db, coll)
150148

151149
if 'ordered' in spec: # in insert and update
152150
cmd.tags['mongodb.ordered'] = spec['ordered']

ddtrace/contrib/pymongo/trace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, tracer, service, sock):
3939
def command(self, dbname, spec, *args, **kwargs):
4040
cmd = None
4141
try:
42-
cmd = parse_spec(spec)
42+
cmd = parse_spec(spec, dbname)
4343
except Exception:
4444
log.exception("error parsing spec. skipping trace")
4545

0 commit comments

Comments
 (0)