36
36
import socket
37
37
38
38
from cassandra .cluster import Cluster
39
+ from cassandra .protocol import ConfigurationException
39
40
from cassandra .query import SimpleStatement
40
41
41
42
from elasticapm .conf .constants import TRANSACTION
@@ -57,6 +58,11 @@ def cassandra_cluster():
57
58
@pytest .fixture ()
58
59
def cassandra_session (cassandra_cluster ):
59
60
session = cassandra_cluster .connect ()
61
+ try :
62
+ # make sure the keyspace doesn't exist
63
+ session .execute ("DROP KEYSPACE testkeyspace;" )
64
+ except ConfigurationException :
65
+ pass
60
66
session .execute (
61
67
"""
62
68
CREATE KEYSPACE testkeyspace
@@ -89,6 +95,38 @@ def test_cassandra_connect(instrument, elasticapm_client, cassandra_cluster):
89
95
}
90
96
91
97
98
+ def test_cassandra_connect_keyspace (instrument , elasticapm_client , cassandra_cluster ):
99
+ session = cassandra_cluster .connect ()
100
+ try :
101
+ session .execute (
102
+ """
103
+ CREATE KEYSPACE testkeyspace
104
+ WITH REPLICATION = { 'class' : 'SimpleStrategy' ,'replication_factor' : 1 }
105
+ """
106
+ )
107
+ elasticapm_client .begin_transaction ("transaction.test" )
108
+ sess = cassandra_cluster .connect ("testkeyspace" )
109
+ elasticapm_client .end_transaction ("test" )
110
+ finally :
111
+ session .execute ("DROP KEYSPACE testkeyspace;" )
112
+
113
+ transactions = elasticapm_client .events [TRANSACTION ]
114
+ span = elasticapm_client .spans_for_transaction (transactions [0 ])[0 ]
115
+
116
+ assert span ["type" ] == "db"
117
+ assert span ["subtype" ] == "cassandra"
118
+ assert span ["action" ] == "connect"
119
+ assert span ["duration" ] > 0
120
+ assert span ["name" ] == "Cluster.connect"
121
+ assert span ["context" ]["destination" ] == {
122
+ "address" : socket .gethostbyname (os .environ .get ("CASSANDRA_HOST" , "localhost" )),
123
+ "port" : 9042 ,
124
+ "service" : {"name" : "" , "resource" : "cassandra/testkeyspace" , "type" : "" },
125
+ }
126
+ assert span ["context" ]["service" ]["target" ]["type" ] == "cassandra"
127
+ assert span ["context" ]["service" ]["target" ]["name" ] == "testkeyspace"
128
+
129
+
92
130
def test_select_query_string (instrument , cassandra_session , elasticapm_client ):
93
131
elasticapm_client .begin_transaction ("transaction.test" )
94
132
cassandra_session .execute ("SELECT name from users" )
@@ -99,12 +137,14 @@ def test_select_query_string(instrument, cassandra_session, elasticapm_client):
99
137
assert span ["subtype" ] == "cassandra"
100
138
assert span ["action" ] == "query"
101
139
assert span ["name" ] == "SELECT FROM users"
102
- assert span ["context" ]["db" ] == {"statement" : "SELECT name from users" , "type" : "sql" }
140
+ assert span ["context" ]["db" ] == {"statement" : "SELECT name from users" , "type" : "sql" , "instance" : "testkeyspace" }
103
141
assert span ["context" ]["destination" ] == {
104
142
"address" : socket .gethostbyname (os .environ .get ("CASSANDRA_HOST" , "localhost" )),
105
143
"port" : 9042 ,
106
- "service" : {"name" : "" , "resource" : "cassandra" , "type" : "" },
144
+ "service" : {"name" : "" , "resource" : "cassandra/testkeyspace " , "type" : "" },
107
145
}
146
+ assert span ["context" ]["service" ]["target" ]["type" ] == "cassandra"
147
+ assert span ["context" ]["service" ]["target" ]["name" ] == "testkeyspace"
108
148
109
149
110
150
def test_select_simple_statement (instrument , cassandra_session , elasticapm_client ):
@@ -118,7 +158,7 @@ def test_select_simple_statement(instrument, cassandra_session, elasticapm_clien
118
158
assert span ["subtype" ] == "cassandra"
119
159
assert span ["action" ] == "query"
120
160
assert span ["name" ] == "SELECT FROM users"
121
- assert span ["context" ]["db" ] == {"statement" : "SELECT name from users" , "type" : "sql" }
161
+ assert span ["context" ]["db" ] == {"statement" : "SELECT name from users" , "type" : "sql" , "instance" : "testkeyspace" }
122
162
123
163
124
164
def test_select_prepared_statement (instrument , cassandra_session , elasticapm_client ):
@@ -132,7 +172,7 @@ def test_select_prepared_statement(instrument, cassandra_session, elasticapm_cli
132
172
assert span ["subtype" ] == "cassandra"
133
173
assert span ["action" ] == "query"
134
174
assert span ["name" ] == "SELECT FROM users"
135
- assert span ["context" ]["db" ] == {"statement" : "SELECT name from users" , "type" : "sql" }
175
+ assert span ["context" ]["db" ] == {"statement" : "SELECT name from users" , "type" : "sql" , "instance" : "testkeyspace" }
136
176
137
177
138
178
def test_signature_create_keyspace ():
0 commit comments