Skip to content

Commit

Permalink
db.collection added to span events (#571)
Browse files Browse the repository at this point in the history
* db.collection attributed added

Co-authored-by: Timothy Pansino <TimPansino@users.noreply.github.com>

* Fix db.collection gate

* Fix database_node gate

Co-authored-by: Timothy Pansino <TimPansino@users.noreply.github.com>
  • Loading branch information
Nayuken and TimPansino authored Jun 30, 2022
1 parent 77c5909 commit 5502577
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions newrelic/core/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
'aws.lambda.arn',
'aws.lambda.coldStart',
'aws.lambda.eventSource.arn',
"db.collection",
'db.instance',
'db.operation',
'db.statement',
Expand Down
4 changes: 4 additions & 0 deletions newrelic/core/database_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,8 @@ def span_event(self, *args, **kwargs):
'db.statement', sql, max_length=2000, ending='...')

self.agent_attributes['db.statement'] = sql

if self.target:
self.agent_attributes["db.collection"] = self.target

return super(DatabaseNode, self).span_event(*args, **kwargs)
10 changes: 7 additions & 3 deletions newrelic/core/datastore_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from newrelic.core.metric import TimeMetric

_DatastoreNode = namedtuple('_DatastoreNode',
['product', 'target', 'operation', 'children', 'start_time',
'end_time', 'duration', 'exclusive', 'host', 'port_path_or_id',
'database_name', 'guid', 'agent_attributes', 'user_attributes'])
['product', 'target', 'operation', 'children', 'start_time',
'end_time', 'duration', 'exclusive', 'host', 'port_path_or_id',
'database_name', 'guid', 'agent_attributes', 'user_attributes'])


class DatastoreNode(_DatastoreNode, DatastoreNodeMixin):
Expand Down Expand Up @@ -139,4 +139,8 @@ def trace_node(self, stats, root, connections):
def span_event(self, *args, **kwargs):
if self.operation:
self.agent_attributes["db.operation"] = self.operation

if self.target:
self.agent_attributes["db.collection"] = self.target

return super(DatastoreNode, self).span_event(*args, **kwargs)
24 changes: 24 additions & 0 deletions tests/agent_features/test_span_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ def test_database_db_statement_exclude():
pass


@pytest.mark.parametrize('trace_type,args,attrs', (
(DatastoreTrace, ('db_product', 'db_target', 'db_operation'), {"db.collection": "db_target", "db.operation": "db_operation"}),
(DatabaseTrace, ("select 1 from db_table",), {"db.collection": "db_table", "db.statement": "select ? from db_table"}),
))
def test_datastore_database_trace_attrs(trace_type, args, attrs):
@validate_span_events(
count=1,
exact_agents=attrs,
)
@override_application_settings({
'distributed_tracing.enabled': True,
'span_events.enabled': True,
})
@background_task(name='test_database_db_statement_exclude')
def test():
transaction = current_transaction()
transaction._sampled = True

with trace_type(*args):
pass

test()


@pytest.mark.parametrize('exclude_url', (True, False))
def test_external_spans(exclude_url):
override_settings = {
Expand Down

0 comments on commit 5502577

Please sign in to comment.