Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrument driver with OpenTelemetry tracing #147

Open
wants to merge 10 commits into
base: scylla-3.x
Choose a base branch
from
12 changes: 12 additions & 0 deletions clirr-ignores.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
Modified by ScyllaDB
-->
<differences>
<difference>
<differenceType>7012</differenceType> <!-- method added to interface -->
<className>com/datastax/driver/core/PreparedStatement</className>
<method>java.lang.String getOperationType()</method>
<justification>New method to get the type of operation performed by this PreparedStatement</justification>
</difference>
<difference>
<differenceType>7012</differenceType> <!-- method added to interface -->
<className>com/datastax/driver/core/Session</className>
<method>com.datastax.driver.core.tracing.TracingInfoFactory getTracingInfoFactory()</method>
<justification>New method to get the TracingInfo data factory associated with this Session</justification>
</difference>
<difference>
<differenceType>7004</differenceType> <!-- the number of arguments has changed -->
<className>com/datastax/driver/core/Metadata</className>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class BoundStatement extends Statement

private ByteBuffer routingKey;

private final String operationType;

/**
* Creates a new {@code BoundStatement} from the provided prepared statement.
*
Expand All @@ -92,6 +94,7 @@ public BoundStatement(PreparedStatement statement) {
this.setSerialConsistencyLevel(statement.getSerialConsistencyLevel());
if (statement.isTracing()) this.enableTracing();
if (statement.getRetryPolicy() != null) this.setRetryPolicy(statement.getRetryPolicy());
this.operationType = statement.getOperationType();
if (statement.getOutgoingPayload() != null)
this.setOutgoingPayload(statement.getOutgoingPayload());
else
Expand All @@ -104,6 +107,10 @@ public BoundStatement(PreparedStatement statement) {
}
}

public String getOperationType() {
return operationType;
}

@Override
public boolean isLWT() {
return statement.isLWT();
Expand Down
23 changes: 23 additions & 0 deletions driver-core/src/main/java/com/datastax/driver/core/Cluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import com.datastax.driver.core.policies.ReconnectionPolicy;
import com.datastax.driver.core.policies.RetryPolicy;
import com.datastax.driver.core.policies.SpeculativeExecutionPolicy;
import com.datastax.driver.core.tracing.NoopTracingInfoFactory;
import com.datastax.driver.core.tracing.TracingInfoFactory;
import com.datastax.driver.core.utils.MoreFutures;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Functions;
Expand Down Expand Up @@ -146,6 +148,8 @@ public class Cluster implements Closeable {

final Manager manager;

private TracingInfoFactory tracingInfoFactory = new NoopTracingInfoFactory();

/**
* Constructs a new Cluster instance.
*
Expand Down Expand Up @@ -197,6 +201,25 @@ private Cluster(
this.manager = new Manager(name, contactPoints, configuration, listeners);
}

/**
* The tracingInfo factory class used by this Cluster.
*
* @return the factory used currently by this Cluster.
*/
public TracingInfoFactory getTracingInfoFactory() {
return tracingInfoFactory;
}

/**
* Sets desired factory for tracing information for this Cluster. By default it is {@link
* com.datastax.driver.core.tracing.NoopTracingInfoFactory}
*
* @param tracingInfoFactory the factory to be set for this Cluster.
*/
public void setTracingInfoFactory(TracingInfoFactory tracingInfoFactory) {
this.tracingInfoFactory = tracingInfoFactory;
}

/**
* Initialize this Cluster instance.
*
Expand Down
15 changes: 15 additions & 0 deletions driver-core/src/main/java/com/datastax/driver/core/CodecUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public final class CodecUtils {

private static final long EPOCH_AS_CQL_LONG = (1L << 31);

private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();

private CodecUtils() {}

/**
Expand Down Expand Up @@ -212,6 +214,19 @@ public static long fromDaysSinceEpochToCqlDate(int days) {
return ((long) days + EPOCH_AS_CQL_LONG);
}

public static String bytesToHex(byte[] bytes) {
final int INITIAL_CHARS = 2;
char[] hexChars = new char[INITIAL_CHARS + bytes.length * 2];
hexChars[0] = '0';
hexChars[1] = 'x';
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[INITIAL_CHARS + j * 2] = HEX_ARRAY[v >>> 4];
hexChars[INITIAL_CHARS + j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars);
}

private static int sizeOfCollectionSize(ProtocolVersion version) {
switch (version) {
case V1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class DefaultPreparedStatement implements PreparedStatement {
final Cluster cluster;
final boolean isLWT;
final Token.Factory partitioner;
final String operationType;

volatile ByteBuffer routingKey;

Expand All @@ -66,6 +67,7 @@ private DefaultPreparedStatement(
this.cluster = cluster;
this.isLWT = isLWT;
this.partitioner = partitioner;
this.operationType = null;
}

static DefaultPreparedStatement fromMessage(
Expand Down Expand Up @@ -315,4 +317,10 @@ public Boolean isIdempotent() {
public boolean isLWT() {
return isLWT;
}

/** {@inheritDoc} */
@Override
public String getOperationType() {
return operationType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,7 @@ public interface PreparedStatement {

/** Whether a prepared statement is LWT statement */
public boolean isLWT();

/** Type of prepared operation (e.g. SELECT) */
public String getOperationType();
}
Loading