Skip to content

Commit 5502577

Browse files
NayukenTimPansino
andauthored
db.collection added to span events (#571)
* db.collection attributed added Co-authored-by: Timothy Pansino <[email protected]> * Fix db.collection gate * Fix database_node gate Co-authored-by: Timothy Pansino <[email protected]>
1 parent 77c5909 commit 5502577

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

newrelic/core/attribute.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
'aws.lambda.arn',
6565
'aws.lambda.coldStart',
6666
'aws.lambda.eventSource.arn',
67+
"db.collection",
6768
'db.instance',
6869
'db.operation',
6970
'db.statement',

newrelic/core/database_node.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,8 @@ def span_event(self, *args, **kwargs):
259259
'db.statement', sql, max_length=2000, ending='...')
260260

261261
self.agent_attributes['db.statement'] = sql
262+
263+
if self.target:
264+
self.agent_attributes["db.collection"] = self.target
265+
262266
return super(DatabaseNode, self).span_event(*args, **kwargs)

newrelic/core/datastore_node.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
from newrelic.core.metric import TimeMetric
2222

2323
_DatastoreNode = namedtuple('_DatastoreNode',
24-
['product', 'target', 'operation', 'children', 'start_time',
25-
'end_time', 'duration', 'exclusive', 'host', 'port_path_or_id',
26-
'database_name', 'guid', 'agent_attributes', 'user_attributes'])
24+
['product', 'target', 'operation', 'children', 'start_time',
25+
'end_time', 'duration', 'exclusive', 'host', 'port_path_or_id',
26+
'database_name', 'guid', 'agent_attributes', 'user_attributes'])
2727

2828

2929
class DatastoreNode(_DatastoreNode, DatastoreNodeMixin):
@@ -139,4 +139,8 @@ def trace_node(self, stats, root, connections):
139139
def span_event(self, *args, **kwargs):
140140
if self.operation:
141141
self.agent_attributes["db.operation"] = self.operation
142+
143+
if self.target:
144+
self.agent_attributes["db.collection"] = self.target
145+
142146
return super(DatastoreNode, self).span_event(*args, **kwargs)

tests/agent_features/test_span_events.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,30 @@ def test_database_db_statement_exclude():
203203
pass
204204

205205

206+
@pytest.mark.parametrize('trace_type,args,attrs', (
207+
(DatastoreTrace, ('db_product', 'db_target', 'db_operation'), {"db.collection": "db_target", "db.operation": "db_operation"}),
208+
(DatabaseTrace, ("select 1 from db_table",), {"db.collection": "db_table", "db.statement": "select ? from db_table"}),
209+
))
210+
def test_datastore_database_trace_attrs(trace_type, args, attrs):
211+
@validate_span_events(
212+
count=1,
213+
exact_agents=attrs,
214+
)
215+
@override_application_settings({
216+
'distributed_tracing.enabled': True,
217+
'span_events.enabled': True,
218+
})
219+
@background_task(name='test_database_db_statement_exclude')
220+
def test():
221+
transaction = current_transaction()
222+
transaction._sampled = True
223+
224+
with trace_type(*args):
225+
pass
226+
227+
test()
228+
229+
206230
@pytest.mark.parametrize('exclude_url', (True, False))
207231
def test_external_spans(exclude_url):
208232
override_settings = {

0 commit comments

Comments
 (0)