Skip to content

Commit 9f2ffc5

Browse files
committed
WL#15808: Fix logic that decides when "net.peer.port" attribute should be set.
1 parent 7334af1 commit 9f2ffc5

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

jdbc/driver/mysql_connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,7 @@ void MySQL_Connection::init(ConnectOptionsMap & properties)
14971497
connected = true;
14981498

14991499
// Connected. We can set the connection telemetry.
1500-
intern->telemetry.set_attribs(this, *el);
1500+
intern->telemetry.set_attribs(this, *el, properties);
15011501
currentUser = userName;
15021502
break;
15031503
}

jdbc/driver/mysql_telemetry.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,17 @@ namespace telemetry
8585

8686

8787
void
88-
Telemetry_base<MySQL_Connection>::set_attribs(MySQL_Connection *obj, MySQL_Uri::Host_data &data)
88+
Telemetry_base<MySQL_Connection>::set_attribs(
89+
MySQL_Connection* con,
90+
MySQL_Uri::Host_data& endpoint,
91+
sql::ConnectOptionsMap& options
92+
)
8993
{
90-
if (disabled(obj) || !span)
94+
if (disabled(con) || !span)
9195
return;
9296

9397
std::string transport;
94-
switch(data.Protocol())
98+
switch(endpoint.Protocol())
9599
{
96100
case NativeAPI::PROTOCOL_TCP:
97101
transport = "ip_tcp";
@@ -106,10 +110,19 @@ namespace telemetry
106110
}
107111

108112
span->SetAttribute("net.transport", transport);
109-
span->SetAttribute("net.peer.name", data.Host().c_str());
110-
if (data.hasPort())
113+
span->SetAttribute("net.peer.name", endpoint.Host().c_str());
114+
115+
/*
116+
Note: `endpoint.hasPort()` alone is not good because it tells whether
117+
in a multi-host sceanrio a non-default port was specified for the chosen
118+
endpoint. We want to send the port attribute also when
119+
no endpint-specific port was given but user set the "global" port value
120+
which is used for all hosts (which is a more typical scenario).
121+
*/
122+
123+
if (options.count(OPT_PORT) || endpoint.hasPort())
111124
{
112-
span->SetAttribute("net.peer.port", data.Port());
125+
span->SetAttribute("net.peer.port", endpoint.Port());
113126
}
114127
}
115128

jdbc/driver/mysql_telemetry.h

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,63 +58,70 @@ namespace mysql
5858
be optimized out by the compiler).
5959
*/
6060

61-
#ifndef TELEMETRY
62-
63-
template<class>
64-
struct Telemetry_base
65-
{
66-
void set_mode(opentelemetry_mode) {}
67-
};
68-
69-
template<>
70-
struct Telemetry_base<MySQL_Connection>
71-
{
72-
void set_mode(opentelemetry_mode) {}
73-
void set_attribs(MySQL_Connection*, MySQL_Uri::Host_data&) {}
74-
};
75-
76-
#else
61+
#ifdef TELEMETRY
7762

7863
namespace nostd = opentelemetry::nostd;
7964
namespace trace = opentelemetry::trace;
8065

8166
using Span_ptr = nostd::shared_ptr<trace::Span>;
8267

68+
#endif
69+
70+
8371
template<class Obj>
8472
struct Telemetry_base
8573
{
74+
#ifdef TELEMETRY
8675
bool disabled(Obj*) const;
8776
Span_ptr span;
8877

8978
protected:
9079
Span_ptr mk_span(Obj*, const char *);
80+
#endif
9181
};
9282

83+
9384
template<>
9485
struct Telemetry_base<MySQL_Connection>
9586
{
9687
using Obj = MySQL_Connection;
97-
Span_ptr span;
9888

99-
bool disabled(Obj *) const
89+
void set_mode(opentelemetry_mode m)
90+
#ifndef TELEMETRY
91+
{}
92+
#else
10093
{
101-
return OTEL_DISABLED == mode;
94+
mode = m;
10295
}
96+
#endif
97+
98+
void set_attribs(
99+
MySQL_Connection* con,
100+
MySQL_Uri::Host_data& endpoint,
101+
sql::ConnectOptionsMap& options
102+
)
103+
#ifndef TELEMETRY
104+
{}
105+
#else
106+
; // Note: Defined in .cpp file
107+
#endif
103108

109+
#ifdef TELEMETRY
110+
111+
Span_ptr span;
104112
enum opentelemetry_mode mode = OTEL_PREFERRED;
105-
void set_mode(opentelemetry_mode m)
113+
114+
bool disabled(Obj *) const
106115
{
107-
mode = m;
116+
return OTEL_DISABLED == mode;
108117
}
109118

110-
void set_attribs(Obj*, MySQL_Uri::Host_data&);
111-
112119
protected:
113120

114121
Span_ptr mk_span(Obj*, const char *);
122+
#endif
115123
};
116124

117-
#endif
118125

119126
template<class Obj>
120127
struct Telemetry

0 commit comments

Comments
 (0)