Skip to content

Commit

Permalink
db.system is a mandatory OTEL span attribute (#1373)
Browse files Browse the repository at this point in the history
See #64

The specification requires `db.system` span attribute to be present, with a default value of `other_sql`.

The database connection implemented have been changed to report a specific system value (static or computed from database metadata).

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont committed Nov 9, 2023
1 parent d15dd64 commit 98bc029
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
*/
package io.vertx.db2client.impl;

import java.util.Map;
import java.util.function.Predicate;

import io.netty.channel.ChannelPipeline;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
Expand All @@ -33,13 +30,12 @@
import io.vertx.sqlclient.impl.Connection;
import io.vertx.sqlclient.impl.QueryResultHandler;
import io.vertx.sqlclient.impl.SocketConnectionBase;
import io.vertx.sqlclient.impl.command.CommandBase;
import io.vertx.sqlclient.impl.command.CommandResponse;
import io.vertx.sqlclient.impl.command.QueryCommandBase;
import io.vertx.sqlclient.impl.command.SimpleQueryCommand;
import io.vertx.sqlclient.impl.command.TxCommand;
import io.vertx.sqlclient.impl.command.*;
import io.vertx.sqlclient.spi.DatabaseMetadata;

import java.util.Map;
import java.util.function.Predicate;

public class DB2SocketConnection extends SocketConnectionBase {

private final DB2ConnectOptions connectOptions;
Expand Down Expand Up @@ -108,6 +104,11 @@ public void handleClose(Throwable t) {
context().runOnContext(closeHandler);
}

@Override
public String system() {
return "db2";
}

@Override
public DatabaseMetadata getDatabaseMetaData() {
return connMetadata.getDbMetadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ protected Pool createPool(Vertx vertx) {
protected String statement(String... parts) {
return String.join("?", parts);
}

@Override
protected boolean isValidDbSystem(String dbSystem) {
return "db2".equals(dbSystem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ protected void handleMessage(Object msg) {
}
}

@Override
public String system() {
return "mssql";
}

@Override
public DatabaseMetadata getDatabaseMetaData() {
return databaseMetadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ protected String statement(String... parts) {
}
return sb.toString();
}

@Override
protected boolean isValidDbSystem(String dbSystem) {
return "mssql".equals(dbSystem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,33 @@ public class MySQLDatabaseMetadata implements DatabaseMetadata {

private static final Logger LOGGER = LoggerFactory.getLogger(MySQLDatabaseMetadata.class);

private enum System {
MYSQL("mysql", "MySQL"),
MARIA_DB("mariadb", "MariaDB"),
;

final String value;
final String productName;

System(String value, String productName) {
this.value = value;
this.productName = productName;
}
}

private final System system;
private final String fullVersion;
private final String productName;
private final int majorVersion;
private final int minorVersion;
private final int microVersion;

private MySQLDatabaseMetadata(String fullVersion,
String productName,
private MySQLDatabaseMetadata(System system,
String fullVersion,
int majorVersion,
int minorVersion,
int microVersion) {
this.system = system;
this.fullVersion = fullVersion;
this.productName = productName;
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
this.microVersion = microVersion;
Expand All @@ -32,11 +46,10 @@ public static MySQLDatabaseMetadata parse(String serverVersion) {
int microVersion = 0;

int len = serverVersion.length();
boolean isMariaDb = serverVersion.contains("MariaDB");
String productName = isMariaDb ? "MariaDB" : "MySQL";
System system = serverVersion.contains("MariaDB") ? System.MARIA_DB : System.MYSQL;

String fullServerVersion = serverVersion;
if (isMariaDb) {
if (system == System.MARIA_DB) {
// MariaDB server version < 11.x.x is by default prefixed by "5.5.5-"
serverVersion = serverVersion.replace("5.5.5-", "");
}
Expand Down Expand Up @@ -67,12 +80,16 @@ public static MySQLDatabaseMetadata parse(String serverVersion) {
LOGGER.warn("Incorrect parsing server version tokens", ex);
}

return new MySQLDatabaseMetadata(fullServerVersion, productName, majorVersion, minorVersion, microVersion);
return new MySQLDatabaseMetadata(system, fullServerVersion, majorVersion, minorVersion, microVersion);
}

public String system() {
return system.value;
}

@Override
public String productName() {
return productName;
return system.productName;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public void upgradeToSsl(Handler<AsyncResult<Void>> completionHandler) {
socket.upgradeToSsl(completionHandler);
}

@Override
public String system() {
return metaData.system();
}

@Override
public DatabaseMetadata getDatabaseMetaData() {
return metaData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ protected Pool createPool(Vertx vertx) {
protected String statement(String... parts) {
return String.join("?", parts);
}

@Override
protected boolean isValidDbSystem(String dbSystem) {
return "mysql".equals(dbSystem) || "mariadb".equals(dbSystem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
package io.vertx.oracleclient.impl;

import io.vertx.core.*;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.impl.NoStackTraceThrowable;
import io.vertx.core.impl.future.PromiseInternal;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.oracleclient.OracleConnectOptions;
import io.vertx.oracleclient.impl.commands.*;
Expand Down Expand Up @@ -72,6 +72,11 @@ public TracingPolicy tracingPolicy() {
return options.getTracingPolicy();
}

@Override
public String system() {
return "oracle";
}

@Override
public String database() {
return options.getDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public void testTraceBatchQuery(TestContext ctx) {
@Override
public void testTracePooledBatchQuery(TestContext ctx) {
}

@Override
protected boolean isValidDbSystem(String dbSystem) {
return "oracle".equals(dbSystem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@
import io.vertx.sqlclient.impl.Notification;
import io.vertx.sqlclient.impl.QueryResultHandler;
import io.vertx.sqlclient.impl.SocketConnectionBase;
import io.vertx.sqlclient.impl.command.CommandBase;
import io.vertx.sqlclient.impl.command.InitCommand;
import io.vertx.sqlclient.impl.command.QueryCommandBase;
import io.vertx.sqlclient.impl.command.SimpleQueryCommand;
import io.vertx.sqlclient.impl.command.TxCommand;
import io.vertx.sqlclient.impl.command.*;
import io.vertx.sqlclient.spi.DatabaseMetadata;

import java.util.Map;
Expand Down Expand Up @@ -187,4 +183,9 @@ public boolean isIndeterminatePreparedStatementError(Throwable error) {
}
return false;
}

@Override
public String system() {
return "postgresql";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ protected String statement(String... parts) {
}
return sb.toString();
}

@Override
protected boolean isValidDbSystem(String dbSystem) {
return "postgresql".equals(dbSystem);
}
}
1 change: 1 addition & 0 deletions vertx-sql-client/src/main/asciidoc/tracing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The client reports the following _client_ spans:

- `Query` operation name
- tags
- `db.system`: the database management system product
- `db.user`: the database username
- `db.instance`: the database instance
- `db.statement`: the SQL query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ default boolean isIndeterminatePreparedStatementError(Throwable error) {

SocketAddress server();

default String system() {
return "other_sql";
}

String database();

String user();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ public TracingPolicy tracingPolicy() {
return conn.tracingPolicy();
}

@Override
public String system() {
return conn.system();
}

@Override
public String database() {
return conn.database();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ enum RequestTags {
SPAN_KIND("span.kind", q -> "client"),

// DB
// See https://github.com/opentracing/specification/blob/master/semantic_conventions.md
// See https://github.com/open-telemetry/opentelemetry-specification/blob/v1.18.0/specification/trace/semantic_conventions/database.md#connection-level-attributes

DB_USER("db.user", q -> q.tracer.user),
DB_INSTANCE("db.instance", q -> q.tracer.database),
DB_STATEMENT("db.statement", QueryRequest::sql),
DB_TYPE("db.type", q -> "sql");
DB_TYPE("db.type", q -> "sql"),
DB_NAME("db.system", q -> q.tracer.system),
;

final String name;
final Function<QueryRequest, String> fn;
Expand Down Expand Up @@ -71,6 +73,7 @@ public String value(QueryRequest obj, int index) {
private final TracingPolicy tracingPolicy;
private final String address;
private final String user;
private final String system;
private final String database;
private Object payload;
private Object metric;
Expand All @@ -82,6 +85,7 @@ public QueryReporter(VertxTracer tracer, ClientMetrics metrics, ContextInternal
this.tracingPolicy = conn.tracingPolicy();
this.address = conn.server().hostAddress() + ":" + conn.server().port();
this.user = conn.user();
this.system = conn.system();
this.database = conn.database();
this.cmd = queryCmd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.spi.VertxTracerFactory;
import io.vertx.core.spi.tracing.SpanKind;
import io.vertx.core.spi.tracing.TagExtractor;
import io.vertx.core.spi.tracing.VertxTracer;
import io.vertx.core.tracing.TracingOptions;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.sqlclient.*;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.Tuple;
import io.vertx.sqlclient.impl.tracing.QueryRequest;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -127,6 +128,9 @@ public <R> Object sendRequest(Context context, SpanKind kind, TracingPolicy trac
ctx.assertEquals("client", tags.get("span.kind"));
ctx.assertEquals("sql", tags.get("db.type"));
ctx.assertEquals(expectedSql, tags.get("db.statement"));
String dbSystem = tags.get("db.system");
ctx.assertNotNull(dbSystem);
ctx.assertTrue(isValidDbSystem(dbSystem));
requestContext.set(context);
completed.countDown();
return expectedPayload;
Expand Down Expand Up @@ -157,6 +161,8 @@ public <R> void receiveResponse(Context context, R response, Object payload, Thr
});
}

protected abstract boolean isValidDbSystem(String dbSystem);

@Test
public void testTracingFailure(TestContext ctx) {
Async completed = ctx.async();
Expand Down

0 comments on commit 98bc029

Please sign in to comment.