Skip to content

Commit 68f70e6

Browse files
author
Andrew Slotin
authored
Send separate 'host' and 'port' tags for MySQL/PostgreSQL tags (#214)
1 parent d6dc27f commit 68f70e6

File tree

6 files changed

+46
-25
lines changed

6 files changed

+46
-25
lines changed

instana/instrumentation/pep0249.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ def _collect_kvs(self, span, sql):
2727
span.set_tag(ext.DATABASE_INSTANCE, self._connect_params[1]['database'])
2828

2929
span.set_tag(ext.DATABASE_STATEMENT, sql_sanitizer(sql))
30-
# span.set_tag(ext.DATABASE_TYPE, 'mysql')
3130
span.set_tag(ext.DATABASE_USER, self._connect_params[1]['user'])
32-
span.set_tag('host', "%s:%s" %
33-
(self._connect_params[1]['host'],
34-
self._connect_params[1]['port']))
31+
span.set_tag('host', self._connect_params[1]['host'])
32+
span.set_tag('port', self._connect_params[1]['port'])
3533
except Exception as e:
3634
logger.debug(e)
3735
finally:

instana/recorder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ def build_registered_span(self, span):
218218

219219
elif span.operation_name == "mysql":
220220
data.mysql = MySQLData(host=span.tags.pop('host', None),
221+
port=span.tags.pop('port', None),
221222
db=span.tags.pop(ext.DATABASE_INSTANCE, None),
222223
user=span.tags.pop(ext.DATABASE_USER, None),
223224
stmt=span.tags.pop(ext.DATABASE_STATEMENT, None))
@@ -227,6 +228,7 @@ def build_registered_span(self, span):
227228

228229
elif span.operation_name == "postgres":
229230
data.pg = PostgresData(host=span.tags.pop('host', None),
231+
port=span.tags.pop('port', None),
230232
db=span.tags.pop(ext.DATABASE_INSTANCE, None),
231233
user=span.tags.pop(ext.DATABASE_USER, None),
232234
stmt=span.tags.pop(ext.DATABASE_STATEMENT, None),

tests/test_mysql-python.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def test_basic_query(self):
100100
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
101101
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
102102
assert_equals(db_span.data.mysql.stmt, 'SELECT * from users')
103-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
103+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
104+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
104105

105106
def test_basic_insert(self):
106107
result = None
@@ -128,7 +129,8 @@ def test_basic_insert(self):
128129
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
129130
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
130131
assert_equals(db_span.data.mysql.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
131-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
132+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
133+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
132134

133135
def test_executemany(self):
134136
result = None
@@ -156,7 +158,8 @@ def test_executemany(self):
156158
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
157159
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
158160
assert_equals(db_span.data.mysql.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
159-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
161+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
162+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
160163

161164
def test_call_proc(self):
162165
result = None
@@ -182,7 +185,8 @@ def test_call_proc(self):
182185
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
183186
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
184187
assert_equals(db_span.data.mysql.stmt, 'test_proc')
185-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
188+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
189+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
186190

187191
def test_error_capture(self):
188192
result = None
@@ -217,4 +221,5 @@ def test_error_capture(self):
217221
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
218222
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
219223
assert_equals(db_span.data.mysql.stmt, 'SELECT * from blah')
220-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
224+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
225+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])

tests/test_mysqlclient.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def test_basic_query(self):
100100
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
101101
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
102102
assert_equals(db_span.data.mysql.stmt, 'SELECT * from users')
103-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
103+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
104+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
104105

105106
def test_basic_insert(self):
106107
result = None
@@ -128,7 +129,8 @@ def test_basic_insert(self):
128129
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
129130
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
130131
assert_equals(db_span.data.mysql.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
131-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
132+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
133+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
132134

133135
def test_executemany(self):
134136
result = None
@@ -156,7 +158,8 @@ def test_executemany(self):
156158
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
157159
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
158160
assert_equals(db_span.data.mysql.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
159-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
161+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
162+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
160163

161164
def test_call_proc(self):
162165
result = None
@@ -182,7 +185,8 @@ def test_call_proc(self):
182185
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
183186
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
184187
assert_equals(db_span.data.mysql.stmt, 'test_proc')
185-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
188+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
189+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
186190

187191
def test_error_capture(self):
188192
result = None
@@ -217,4 +221,5 @@ def test_error_capture(self):
217221
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
218222
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
219223
assert_equals(db_span.data.mysql.stmt, 'SELECT * from blah')
220-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
224+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
225+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])

tests/test_psycopg2.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def test_basic_query(self):
9999
assert_equals(db_span.data.pg.db, testenv['postgresql_db'])
100100
assert_equals(db_span.data.pg.user, testenv['postgresql_user'])
101101
assert_equals(db_span.data.pg.stmt, 'SELECT * from users')
102-
assert_equals(db_span.data.pg.host, "%s:5432" % testenv['postgresql_host'])
102+
assert_equals(db_span.data.pg.host, testenv['postgresql_host'])
103+
assert_equals(db_span.data.pg.port, testenv['postgresql_port'])
103104

104105
def test_basic_insert(self):
105106
with tracer.start_active_span('test'):
@@ -122,7 +123,8 @@ def test_basic_insert(self):
122123
assert_equals(db_span.data.pg.db, testenv['postgresql_db'])
123124
assert_equals(db_span.data.pg.user, testenv['postgresql_user'])
124125
assert_equals(db_span.data.pg.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
125-
assert_equals(db_span.data.pg.host, "%s:5432" % testenv['postgresql_host'])
126+
assert_equals(db_span.data.pg.host, testenv['postgresql_host'])
127+
assert_equals(db_span.data.pg.port, testenv['postgresql_port'])
126128

127129
def test_executemany(self):
128130
result = None
@@ -148,7 +150,8 @@ def test_executemany(self):
148150
assert_equals(db_span.data.pg.db, testenv['postgresql_db'])
149151
assert_equals(db_span.data.pg.user, testenv['postgresql_user'])
150152
assert_equals(db_span.data.pg.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
151-
assert_equals(db_span.data.pg.host, "%s:5432" % testenv['postgresql_host'])
153+
assert_equals(db_span.data.pg.host, testenv['postgresql_host'])
154+
assert_equals(db_span.data.pg.port, testenv['postgresql_port'])
152155

153156
def test_call_proc(self):
154157
result = None
@@ -174,7 +177,8 @@ def test_call_proc(self):
174177
assert_equals(db_span.data.pg.db, testenv['postgresql_db'])
175178
assert_equals(db_span.data.pg.user, testenv['postgresql_user'])
176179
assert_equals(db_span.data.pg.stmt, 'test_proc')
177-
assert_equals(db_span.data.pg.host, "%s:5432" % testenv['postgresql_host'])
180+
assert_equals(db_span.data.pg.host, testenv['postgresql_host'])
181+
assert_equals(db_span.data.pg.port, testenv['postgresql_port'])
178182

179183
def test_error_capture(self):
180184
result = None
@@ -209,7 +213,8 @@ def test_error_capture(self):
209213
assert_equals(db_span.data.pg.db, testenv['postgresql_db'])
210214
assert_equals(db_span.data.pg.user, testenv['postgresql_user'])
211215
assert_equals(db_span.data.pg.stmt, 'SELECT * from blah')
212-
assert_equals(db_span.data.pg.host, "%s:5432" % testenv['postgresql_host'])
216+
assert_equals(db_span.data.pg.host, testenv['postgresql_host'])
217+
assert_equals(db_span.data.pg.port, testenv['postgresql_port'])
213218

214219
# Added to validate unicode support and register_type.
215220
def test_unicode(self):

tests/test_pymysql.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ def test_basic_query(self):
9696
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
9797
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
9898
assert_equals(db_span.data.mysql.stmt, 'SELECT * from users')
99-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
99+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
100+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
100101

101102
def test_query_with_params(self):
102103
result = None
@@ -123,7 +124,8 @@ def test_query_with_params(self):
123124
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
124125
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
125126
assert_equals(db_span.data.mysql.stmt, 'SELECT * from users where id=?')
126-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
127+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
128+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
127129

128130
def test_basic_insert(self):
129131
result = None
@@ -151,7 +153,8 @@ def test_basic_insert(self):
151153
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
152154
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
153155
assert_equals(db_span.data.mysql.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
154-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
156+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
157+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
155158

156159
def test_executemany(self):
157160
result = None
@@ -179,7 +182,8 @@ def test_executemany(self):
179182
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
180183
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
181184
assert_equals(db_span.data.mysql.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
182-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
185+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
186+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
183187

184188
def test_call_proc(self):
185189
result = None
@@ -205,7 +209,8 @@ def test_call_proc(self):
205209
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
206210
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
207211
assert_equals(db_span.data.mysql.stmt, 'test_proc')
208-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
212+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
213+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])
209214

210215
def test_error_capture(self):
211216
result = None
@@ -246,4 +251,5 @@ def test_error_capture(self):
246251
assert_equals(db_span.data.mysql.db, testenv['mysql_db'])
247252
assert_equals(db_span.data.mysql.user, testenv['mysql_user'])
248253
assert_equals(db_span.data.mysql.stmt, 'SELECT * from blah')
249-
assert_equals(db_span.data.mysql.host, "%s:3306" % testenv['mysql_host'])
254+
assert_equals(db_span.data.mysql.host, testenv['mysql_host'])
255+
assert_equals(db_span.data.mysql.port, testenv['mysql_port'])

0 commit comments

Comments
 (0)