Skip to content

Commit d410645

Browse files
Bogdan Degtyariovrsomla1
authored andcommitted
OTel - Add span closure for statements
Change-Id: I3e0d6f36e2f4644466f0f32861420c74984f82b5
1 parent 453a210 commit d410645

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

jdbc/driver/mysql_statement.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ MySQL_Statement::cancel()
190190
/* {{{ MySQL_Statement::execute() -I- */
191191
bool
192192
MySQL_Statement::execute(const sql::SQLString& sql)
193+
try
193194
{
194195
CPP_ENTER("MySQL_Statement::execute");
195196
CPP_INFO_FMT("this=%p", this);
@@ -201,15 +202,31 @@ MySQL_Statement::execute(const sql::SQLString& sql)
201202
throw sql::InvalidInstanceException("Connection has been closed");
202203
}
203204
bool ret = proxy_p->field_count() > 0;
205+
if (!ret)
206+
{
207+
// If no results the span can be ended.
208+
telemetry.span_end(this);
209+
}
204210
last_update_count = ret? UL64(~0):proxy_p->affected_rows();
205211
return ret;
206212
}
213+
catch (sql::SQLException &e)
214+
{
215+
telemetry.set_error(this, e.what());
216+
throw e;
217+
}
218+
catch (...)
219+
{
220+
telemetry.set_error(this, "Unknown error in MySQL_Statement::execute");
221+
throw;
222+
}
207223
/* }}} */
208224

209225

210226
/* {{{ MySQL_Statement::executeQuery() -I- */
211227
sql::ResultSet *
212228
MySQL_Statement::executeQuery(const sql::SQLString& sql)
229+
try
213230
{
214231
CPP_ENTER("MySQL_Statement::executeQuery");
215232
CPP_INFO_FMT("this=%p", this);
@@ -226,8 +243,23 @@ MySQL_Statement::executeQuery(const sql::SQLString& sql)
226243
logger
227244
);
228245
CPP_INFO_FMT("rset=%p", tmp);
246+
if (tmp->isClosed() || tmp->rowsCount() == 0)
247+
{
248+
// Close span if the result set is closed or empty
249+
telemetry.span_end(this);
250+
}
229251
return tmp;
230252
}
253+
catch (sql::SQLException &e)
254+
{
255+
telemetry.set_error(this, e.what());
256+
throw e;
257+
}
258+
catch (...)
259+
{
260+
telemetry.set_error(this, "Unknown error in MySQL_Statement::executeQuery");
261+
throw;
262+
}
231263
/* }}} */
232264

233265

@@ -243,6 +275,7 @@ dirty_drop_rs(std::shared_ptr< NativeAPI::NativeConnectionWrapper > proxy)
243275
/* {{{ MySQL_Statement::executeUpdate() -I- */
244276
int
245277
MySQL_Statement::executeUpdate(const sql::SQLString& sql)
278+
try
246279
{
247280
CPP_ENTER("MySQL_Statement::executeUpdate");
248281
CPP_INFO_FMT("this=%p", this);
@@ -272,6 +305,7 @@ MySQL_Statement::executeUpdate(const sql::SQLString& sql)
272305
if (got_rs){
273306
throw sql::InvalidArgumentException("Statement returning result set");
274307
} else {
308+
telemetry.span_end(this);
275309
return static_cast<int>(last_update_count);
276310
}
277311
}
@@ -289,8 +323,20 @@ MySQL_Statement::executeUpdate(const sql::SQLString& sql)
289323
} while (1);
290324

291325
/* Should not actually get here*/
326+
telemetry.span_end(this);
292327
return 0;
293328
}
329+
catch (sql::SQLException &e)
330+
{
331+
telemetry.set_error(this, e.what());
332+
throw e;
333+
}
334+
catch (...)
335+
{
336+
telemetry.set_error(this, "Unknown error in MySQL_Statement::executeUpdate");
337+
throw;
338+
}
339+
294340
/* }}} */
295341

296342

@@ -322,6 +368,7 @@ MySQL_Statement::getFetchSize()
322368
/* {{{ MySQL_Statement::getResultSet() -I- */
323369
sql::ResultSet *
324370
MySQL_Statement::getResultSet()
371+
try
325372
{
326373
CPP_ENTER("MySQL_Statement::getResultSet");
327374
CPP_INFO_FMT("this=%p", this);
@@ -365,12 +412,15 @@ MySQL_Statement::getResultSet()
365412
}
366413
else
367414
{
415+
// End span for NULL resultset
416+
telemetry.span_end(this);
368417
return NULL;
369418
}
370419
}
371420

372421
if (!result) {
373422
/* if there was an update then this method should return NULL and not throw */
423+
telemetry.span_end(this);
374424
return NULL;
375425
}
376426

@@ -379,6 +429,17 @@ MySQL_Statement::getResultSet()
379429
CPP_INFO_FMT("res=%p", ret);
380430
return ret;
381431
}
432+
catch (sql::SQLException &e)
433+
{
434+
telemetry.set_error(this, e.what());
435+
throw e;
436+
}
437+
catch (...)
438+
{
439+
telemetry.set_error(this, "Unknown error in MySQL_Statement::getResultSet");
440+
throw;
441+
}
442+
382443
/* }}} */
383444

384445

@@ -460,6 +521,7 @@ MySQL_Statement::getMaxRows()
460521
/* {{{ MySQL_Statement::getMoreResults() -I- */
461522
bool
462523
MySQL_Statement::getMoreResults()
524+
try
463525
{
464526
CPP_ENTER("MySQL_Statement::getMoreResults");
465527
CPP_INFO_FMT("this=%p", this);
@@ -486,8 +548,21 @@ MySQL_Statement::getMoreResults()
486548
throw sql::SQLException("Impossible! more_results() said true, next_result says no more results");
487549
}
488550
}
551+
// If no more results close the statement span
552+
telemetry.span_end(this);
489553
return false;
490554
}
555+
catch (sql::SQLException &e)
556+
{
557+
telemetry.set_error(this, e.what());
558+
throw e;
559+
}
560+
catch (...)
561+
{
562+
telemetry.set_error(this, "Unknown error in MySQL_Statement::getMoreResults");
563+
throw;
564+
}
565+
491566
/* }}} */
492567

493568

jdbc/driver/mysql_telemetry.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ namespace telemetry
117117
return span;
118118
}
119119

120-
}
121-
}
122-
}
120+
} // telemetry
121+
} // mysql
122+
} // sql

jdbc/driver/mysql_telemetry.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ namespace mysql
108108
#ifndef TELEMETRY
109109

110110
static void span_start(Obj*) {}
111+
static void span_end(Obj*) {}
111112
static void set_error(Obj*, std::string) {}
112113

113114
#else
@@ -122,6 +123,18 @@ namespace mysql
122123
span = Base::mk_span(obj);
123124
}
124125

126+
127+
void span_end(Obj *obj)
128+
{
129+
if (!span)
130+
return;
131+
span->End();
132+
// Destroy span just in case
133+
Span_ptr sink;
134+
span.swap(sink);
135+
}
136+
137+
125138
void set_error(Obj *obj, std::string msg)
126139
{
127140
if (Base::disabled(obj))

0 commit comments

Comments
 (0)