Skip to content

Commit 453a210

Browse files
committed
OTel: Do not include on non-Linux platforms.
Also refactor to use telemetry::Telemetry<X> object that is always present but does nothing if telemetry is not supported.
1 parent 67e4e98 commit 453a210

12 files changed

+162
-172
lines changed

cdk/cmake/dependency.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function(add_ext NAME HEADER)
129129
)
130130

131131

132-
show_config_options()
132+
#show_config_options()
133133

134134
# Handle non-path value of WITH_X option
135135

jdbc/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,17 @@ endif()
148148

149149

150150
find_dependency(MySQL)
151-
find_dependency(Otel)
151+
152+
add_library(otel_api INTERFACE)
153+
# Note: For now make it phony target on platforms other than Linux
154+
if(NOT (WIN32 OR APPLE OR CMAKE_SYSTEM_NAME MATCHES "SunOS"))
155+
message(STATUS "Adding OTel support")
156+
set(TELEMETRY ON)
157+
target_include_directories(otel_api INTERFACE
158+
"${PROJECT_SOURCE_DIR}/extra/otel/opentelemetry-cpp-1.8.3/api/include"
159+
)
160+
target_compile_definitions(otel_api INTERFACE TELEMETRY)
161+
endif()
152162

153163

154164
#-----------------

jdbc/cmake/DepFindOtel.cmake

Lines changed: 0 additions & 68 deletions
This file was deleted.

jdbc/driver/CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ SET(MYSQLCPPCONN_SOURCES
109109
mysql_resultset.cpp
110110
mysql_resultset_metadata.cpp
111111
mysql_statement.cpp
112-
mysql_telemetry.cpp
113112
mysql_util.cpp
114113
mysql_warning.cpp
115114
mysql_uri.cpp
@@ -121,6 +120,10 @@ SET(MYSQLCPPCONN_SOURCES
121120
nativeapi/mysql_native_resultset_wrapper.cpp
122121
)
123122

123+
if(TELEMETRY)
124+
list(APPEND MYSQLCPPCONN_SOURCES mysql_telemetry.cpp)
125+
endif()
126+
124127

125128
IF(WIN32)
126129
# adding headers to sources, so they will be included to VS projects
@@ -144,7 +147,6 @@ IF(WIN32)
144147
mysql_resultset.h
145148
mysql_resultset_metadata.h
146149
mysql_statement.h
147-
mysql_telemetry.h
148150
mysql_util.h
149151
mysql_warning.h
150152
mysql_uri.h
@@ -180,6 +182,10 @@ IF(WIN32)
180182
nativeapi/library_loader.h)
181183
ENDIF(NOT MYSQLCLIENT_STATIC_BINDING)
182184

185+
if(TELEMETRY)
186+
list(APPEND MYSQLCPPCONN_SOURCES mysql_telemetry.h)
187+
endif()
188+
183189
# Used to create source filter in projects in VS
184190
SOURCE_GROUP(NativeApi nativeapi/.+)
185191
SOURCE_GROUP(API ../cppconn/.+)
@@ -207,7 +213,7 @@ else()
207213
ENDIF(NOT WIN32)
208214
endif()
209215

210-
target_link_libraries(jdbc PRIVATE ext::opentelemetry_api)
216+
target_link_libraries(jdbc PRIVATE otel_api)
211217

212218
if(NOT BUILD_SHARED_LIBS)
213219
target_compile_definitions(jdbc PRIVATE -DCPPCONN_LIB_BUILD)

jdbc/driver/mysql_connection.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
538538

539539
sql::ConnectOptionsMap::const_iterator it;
540540

541+
#ifdef TELEMETRY
541542
// TODO: Use these helpers to reduce code repetition.
542543

543544
auto get_option_i = [&properties, &p_i](std::string name, bool check = true)
@@ -601,6 +602,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
601602
};
602603
}
603604
};
605+
#endif
604606
#endif
605607

606608
/* Port from options must be set as default for all hosts where port
@@ -694,6 +696,17 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
694696
not work, we try bool value.
695697
*/
696698

699+
#ifndef TELEMETRY
700+
701+
if (properties.count(OPT_OPENTELEMETRY))
702+
{
703+
throw sql::SQLException{
704+
"Option OPT_OPENTELEMETRY not yet supported on this platform."
705+
};
706+
}
707+
708+
#else
709+
697710
try {
698711
if (get_option_i(OPT_OPENTELEMETRY))
699712
{
@@ -709,7 +722,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
709722
};
710723
};
711724

712-
intern->telemetryMode = (enum_opentelemetry_mode)*p_i;
725+
intern->telemetry.set_mode((enum_opentelemetry_mode)*p_i);
713726
}
714727
}
715728
catch(const sql::InvalidArgumentException&)
@@ -721,7 +734,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
721734
throw sql::InvalidArgumentException{
722735
"OPT_OPENTELEMETRY can only be set to FALSE"
723736
};
724-
intern->telemetryMode = OTEL_DISABLED;
737+
intern->telemetry.set_mode(OTEL_DISABLED);
725738
}
726739
catch(const sql::InvalidArgumentException&)
727740
{
@@ -732,6 +745,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
732745
}
733746
}
734747

748+
#endif
735749

736750
#define PROCESS_CONN_OPTION(option_type, options_map) \
737751
process_connection_option< option_type >(it, options_map, sizeof(options_map)/sizeof(String2IntMap), proxy)
@@ -1304,10 +1318,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
13041318

13051319
CPP_INFO_FMT("OPT_DNS_SRV=%d", opt_dns_srv);
13061320

1307-
if (!telemetry_disabled())
1308-
{
1309-
intern->trace_span = telemetry::mk_span(this);
1310-
}
1321+
intern->telemetry.span_start(this);
13111322

13121323
try
13131324
{
@@ -1564,7 +1575,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
15641575
}
15651576
catch(sql::SQLException &e)
15661577
{
1567-
telemetry::set_error(intern->trace_span, e.what());
1578+
intern->telemetry.set_error(this, e.what());
15681579
throw;
15691580
}
15701581
}
@@ -1782,12 +1793,6 @@ MySQL_Connection::getTransactionIsolation()
17821793
/* }}} */
17831794

17841795

1785-
bool MySQL_Connection::telemetry_disabled() const
1786-
{
1787-
return !intern || (OTEL_DISABLED == intern->telemetryMode);
1788-
}
1789-
1790-
17911796
/* {{{ MySQL_Connection::getWarnings() -I- */
17921797
const SQLWarning *
17931798
MySQL_Connection::getWarnings()

jdbc/driver/mysql_connection.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,6 @@ class CPPCONN_PUBLIC_FUNC MySQL_Connection : public sql::Connection
206206
#pragma warning(pop)
207207
#endif
208208

209-
bool telemetry_disabled() const;
210-
211209
/* Prevent use of these */
212210
MySQL_Connection(const MySQL_Connection &);
213211
void operator=(MySQL_Connection &);

jdbc/driver/mysql_connection_data.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ struct MySQL_ConnectionData
8989

9090
std::unique_ptr<MySQL_ConnectionMetaData> meta;
9191

92-
enum_opentelemetry_mode telemetryMode = OTEL_PREFERRED;
93-
telemetry::Span_ptr trace_span;
92+
telemetry::Telemetry<MySQL_Connection> telemetry;
9493
};
9594

9695
} /* namespace mysql */

jdbc/driver/mysql_statement.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <algorithm>
3535
#include <sstream>
3636
#include <cstring>
37+
#include <cassert>
3738

3839

3940
/*
@@ -105,10 +106,7 @@ MySQL_Statement::do_query(const ::sql::SQLString &q)
105106
throw sql::InvalidInstanceException("Connection has been closed");
106107
}
107108

108-
if (!connection->telemetry_disabled())
109-
{
110-
trace_span = telemetry::mk_span(this);
111-
}
109+
telemetry.span_start(this);
112110

113111
try
114112
{
@@ -125,7 +123,7 @@ MySQL_Statement::do_query(const ::sql::SQLString &q)
125123
}
126124
catch(sql::SQLException &e)
127125
{
128-
telemetry::set_error(trace_span, e.what());
126+
telemetry.set_error(this, e.what());
129127
throw;
130128
}
131129

@@ -134,11 +132,11 @@ MySQL_Statement::do_query(const ::sql::SQLString &q)
134132
}
135133
/* }}} */
136134

137-
138-
telemetry::Span_ptr
139-
MySQL_Statement::get_conn_span()
135+
telemetry::Telemetry<MySQL_Connection>&
136+
MySQL_Statement::conn_telemetry()
140137
{
141-
return connection->intern->trace_span;
138+
assert(connection && connection->intern);
139+
return connection->intern->telemetry;
142140
}
143141

144142

jdbc/driver/mysql_statement.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ class NativeConnectionWrapper;
5959

6060
class MySQL_Statement : public sql::Statement
6161
{
62-
friend telemetry::Span_ptr telemetry::mk_span(MySQL_Statement*);
63-
6462
protected:
6563
std::unique_ptr<MySQL_Warning> warnings;
6664
MySQL_Connection *connection;
@@ -80,12 +78,15 @@ class MySQL_Statement : public sql::Statement
8078

8179
unsigned int warningsCount;
8280

83-
telemetry::Span_ptr trace_span;
84-
8581
virtual std::shared_ptr<NativeAPI::NativeResultsetWrapper> get_resultset();
8682
virtual void checkClosed();
8783

88-
telemetry::Span_ptr get_conn_span();
84+
telemetry::Telemetry<MySQL_Statement> telemetry;
85+
86+
// Get connection's telemetry object.
87+
telemetry::Telemetry<MySQL_Connection>& conn_telemetry();
88+
89+
friend telemetry::Telemetry_base<MySQL_Statement>;
8990

9091
public:
9192
MySQL_Statement(MySQL_Connection *conn,

0 commit comments

Comments
 (0)