Class QueryLogger
- java.lang.Object
-
- com.datastax.driver.core.QueryLogger
-
- All Implemented Interfaces:
LatencyTracker
- Direct Known Subclasses:
QueryLogger.ConstantThresholdQueryLogger,QueryLogger.DynamicThresholdQueryLogger
public abstract class QueryLogger extends Object implements LatencyTracker
A configurableLatencyTrackerthat logs all executed statements.Typically, client applications would instantiate one single query logger (using its
QueryLogger.Builder), configure it and register it on the relevantClusterinstance, e.g.:Cluster cluster = ... QueryLogger queryLogger = QueryLogger.builder() .withConstantThreshold(...) .withMaxQueryStringLength(...) .build(); cluster.register(queryLogger);Refer to the
QueryLogger.Builderdocumentation for more information on configuration settings for the query logger.Once registered, the query logger will log every
RegularStatement,BoundStatementorBatchStatementexecuted by the driver; note that it will never log other types of statement, null statements nor any special statement used internally by the driver.There is one log for each request to a Cassandra node; because the driver sometimes retries the same statement on multiple nodes, a single statement execution (for example, a single call to
Session.execute(Statement)) can produce multiple logs on different nodes.For more flexibility, the query logger uses 3 different
Loggerinstances:NORMAL_LOGGER: used to log normal queries, i.e., queries that completed successfully within a configurable threshold in milliseconds.SLOW_LOGGER: used to log slow queries, i.e., queries that completed successfully but that took longer than a configurable threshold in milliseconds to complete.ERROR_LOGGER: used to log unsuccessful queries, i.e., queries that did not completed normally and threw an exception. Note this this logger will also print the full stack trace of the reported exception.
The appropriate logger is chosen according to the following algorithm:
- if an exception has been thrown: use
ERROR_LOGGER; - otherwise, if the reported latency is greater than the configured threshold in
milliseconds: use
SLOW_LOGGER; - otherwise, use
NORMAL_LOGGER.
All loggers are activated by setting their levels to
DEBUGorTRACE(includingERROR_LOGGER). If the level is set toTRACEand the statement being logged is aBoundStatement, then the query parameters (if any) will be logged as well (names and actual values).Constant thresholds vs. Dynamic thresholds
Currently the QueryLogger can track slow queries in two different ways: using a
QueryLogger.Builder.withConstantThreshold(long)constant threshold} in milliseconds (which is the default behavior), or using adynamic thresholdbased on latency percentiles.This class is thread-safe.
- Since:
- 2.0.10
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classQueryLogger.BuilderHelper class to buildQueryLoggerinstances with a fluent API.static classQueryLogger.ConstantThresholdQueryLoggerA QueryLogger that uses a constant threshold in milliseconds to track slow queries.static classQueryLogger.DynamicThresholdQueryLoggerA QueryLogger that uses a dynamic threshold in milliseconds to track slow queries.
-
Field Summary
Fields Modifier and Type Field Description protected Clusterclusterstatic intDEFAULT_MAX_LOGGED_PARAMETERSThe default maximum number of query parameters that can be logged by the driver.static intDEFAULT_MAX_PARAMETER_VALUE_LENGTHThe default maximum length of a query parameter value that can be logged verbatim by the driver.static intDEFAULT_MAX_QUERY_STRING_LENGTHThe default maximum length of a CQL query string that can be logged verbatim by the driver.static longDEFAULT_SLOW_QUERY_THRESHOLD_MSThe default latency threshold in milliseconds beyond which queries are considered 'slow' and logged as such by the driver.static doubleDEFAULT_SLOW_QUERY_THRESHOLD_PERCENTILEThe default latency percentile beyond which queries are considered 'slow' and logged as such by the driver.static org.slf4j.LoggerERROR_LOGGERThe logger used to log unsuccessful queries, i.e., queries that did not complete normally and threw an exception.protected intmaxLoggedParametersprotected intmaxParameterValueLengthprotected intmaxQueryStringLengthstatic org.slf4j.LoggerNORMAL_LOGGERThe logger used to log normal queries, i.e., queries that completed successfully within a configurable threshold in milliseconds.static org.slf4j.LoggerSLOW_LOGGERThe logger used to log slow queries, i.e., queries that completed successfully but whose execution time exceeded a configurable threshold in milliseconds.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected intappend(Statement statement, StringBuilder buffer, int remaining)protected intappend(CharSequence str, StringBuilder buffer, int remaining)protected intappendParameters(BoundStatement statement, StringBuilder buffer, int remaining)protected intappendParameters(BuiltStatement statement, StringBuilder buffer, int remaining)protected intappendParameters(SimpleStatement statement, StringBuilder buffer, int remaining)static QueryLogger.Builderbuilder()Creates a newQueryLogger.Builderinstance.protected intcountBoundValues(BatchStatement bs)intgetMaxLoggedParameters()Return the maximum number of query parameters that can be logged by the driver.intgetMaxParameterValueLength()Return the maximum length of a query parameter value that can be logged verbatim by the driver.intgetMaxQueryStringLength()Return the maximum length of a CQL query string that can be logged verbatim by the driver.protected voidlogQuery(Statement statement, Exception exception, org.slf4j.Logger logger, String message)protected voidmaybeLogErrorQuery(Host host, Statement statement, Exception exception, long latencyMs)protected abstract voidmaybeLogNormalOrSlowQuery(Host host, Statement statement, long latencyMs)protected voidmaybeLogNormalQuery(Host host, Statement statement, long latencyMs)voidonRegister(Cluster cluster)Gets invoked when the tracker is registered with a cluster, or at cluster startup if the tracker was registered at initialization withCluster.register(LatencyTracker).voidonUnregister(Cluster cluster)Gets invoked when the tracker is unregistered from a cluster, or at cluster shutdown if the tracker was not unregistered.protected StringparameterValueAsString(ColumnDefinitions.Definition definition, ByteBuffer raw)protected StringparameterValueAsString(Object value)voidsetMaxLoggedParameters(int maxLoggedParameters)Set the maximum number of query parameters that can be logged by the driver.voidsetMaxParameterValueLength(int maxParameterValueLength)Set the maximum length of a query parameter value that can be logged verbatim by the driver.voidsetMaxQueryStringLength(int maxQueryStringLength)Set the maximum length of a CQL query string that can be logged verbatim by the driver.protected StringstatementAsString(Statement statement)voidupdate(Host host, Statement statement, Exception exception, long newLatencyNanos)A method that is called after each request to a Cassandra node with the duration of that operation.
-
-
-
Field Detail
-
DEFAULT_SLOW_QUERY_THRESHOLD_MS
public static final long DEFAULT_SLOW_QUERY_THRESHOLD_MS
The default latency threshold in milliseconds beyond which queries are considered 'slow' and logged as such by the driver.- See Also:
- Constant Field Values
-
DEFAULT_SLOW_QUERY_THRESHOLD_PERCENTILE
public static final double DEFAULT_SLOW_QUERY_THRESHOLD_PERCENTILE
The default latency percentile beyond which queries are considered 'slow' and logged as such by the driver.- See Also:
- Constant Field Values
-
DEFAULT_MAX_QUERY_STRING_LENGTH
public static final int DEFAULT_MAX_QUERY_STRING_LENGTH
The default maximum length of a CQL query string that can be logged verbatim by the driver. Query strings longer than this value will be truncated when logged.- See Also:
- Constant Field Values
-
DEFAULT_MAX_PARAMETER_VALUE_LENGTH
public static final int DEFAULT_MAX_PARAMETER_VALUE_LENGTH
The default maximum length of a query parameter value that can be logged verbatim by the driver. Parameter values longer than this value will be truncated when logged.- See Also:
- Constant Field Values
-
DEFAULT_MAX_LOGGED_PARAMETERS
public static final int DEFAULT_MAX_LOGGED_PARAMETERS
The default maximum number of query parameters that can be logged by the driver. Queries with a number of parameters higher than this value will not have all their parameters logged.- See Also:
- Constant Field Values
-
NORMAL_LOGGER
public static final org.slf4j.Logger NORMAL_LOGGER
The logger used to log normal queries, i.e., queries that completed successfully within a configurable threshold in milliseconds.This logger is activated by setting its level to
DEBUGorTRACE. Additionally, if the level is set toTRACEand the statement being logged is aBoundStatementor aSimpleStatement, then the query parameters (if any) will be logged. For aBoundStatementnames and actual values are logged and for aSimpleStatementvalues are logged in positional order and named values are logged with names and value.The name of this logger is
com.datastax.driver.core.QueryLogger.NORMAL.
-
SLOW_LOGGER
public static final org.slf4j.Logger SLOW_LOGGER
The logger used to log slow queries, i.e., queries that completed successfully but whose execution time exceeded a configurable threshold in milliseconds.This logger is activated by setting its level to
DEBUGorTRACE. Additionally, if the level is set toTRACEand the statement being logged is aBoundStatementor aSimpleStatement, then the query parameters (if any) will be logged. For aBoundStatementnames and actual values are logged and for aSimpleStatementvalues are logged in positional order and named values are logged with names and value.The name of this logger is
com.datastax.driver.core.QueryLogger.SLOW.
-
ERROR_LOGGER
public static final org.slf4j.Logger ERROR_LOGGER
The logger used to log unsuccessful queries, i.e., queries that did not complete normally and threw an exception.This logger is activated by setting its level to
DEBUGorTRACE. Additionally, if the level is set toTRACEand the statement being logged is aBoundStatementor aSimpleStatement, then the query parameters (if any) will be logged. For aBoundStatementnames and actual values are logged and for aSimpleStatementvalues are logged in positional order and named values are logged with names and value. Note this this logger will also print the full stack trace of the reported exception.The name of this logger is
com.datastax.driver.core.QueryLogger.ERROR.
-
cluster
protected volatile Cluster cluster
-
maxQueryStringLength
protected volatile int maxQueryStringLength
-
maxParameterValueLength
protected volatile int maxParameterValueLength
-
maxLoggedParameters
protected volatile int maxLoggedParameters
-
-
Method Detail
-
builder
public static QueryLogger.Builder builder()
Creates a newQueryLogger.Builderinstance.This is a convenience method for
new QueryLogger.Builder().- Returns:
- the new QueryLogger builder.
- Throws:
NullPointerException- ifclusterisnull.
-
onRegister
public void onRegister(Cluster cluster)
Description copied from interface:LatencyTrackerGets invoked when the tracker is registered with a cluster, or at cluster startup if the tracker was registered at initialization withCluster.register(LatencyTracker).- Specified by:
onRegisterin interfaceLatencyTracker- Parameters:
cluster- the cluster that this tracker is registered with.
-
onUnregister
public void onUnregister(Cluster cluster)
Description copied from interface:LatencyTrackerGets invoked when the tracker is unregistered from a cluster, or at cluster shutdown if the tracker was not unregistered.- Specified by:
onUnregisterin interfaceLatencyTracker- Parameters:
cluster- the cluster that this tracker was registered with.
-
getMaxQueryStringLength
public int getMaxQueryStringLength()
Return the maximum length of a CQL query string that can be logged verbatim by the driver. Query strings longer than this value will be truncated when logged. The default value isDEFAULT_MAX_QUERY_STRING_LENGTH.- Returns:
- The maximum length of a CQL query string that can be logged verbatim by the driver.
-
setMaxQueryStringLength
public void setMaxQueryStringLength(int maxQueryStringLength)
Set the maximum length of a CQL query string that can be logged verbatim by the driver. Query strings longer than this value will be truncated when logged.- Parameters:
maxQueryStringLength- The maximum length of a CQL query string that can be logged verbatim by the driver. It must be strictly positive or-1, in which case the query is never truncated (use with care).- Throws:
IllegalArgumentException- ifmaxQueryStringLength <= 0 && maxQueryStringLength != -1.
-
getMaxParameterValueLength
public int getMaxParameterValueLength()
Return the maximum length of a query parameter value that can be logged verbatim by the driver. Parameter values longer than this value will be truncated when logged. The default value isDEFAULT_MAX_PARAMETER_VALUE_LENGTH.- Returns:
- The maximum length of a query parameter value that can be logged verbatim by the driver.
-
setMaxParameterValueLength
public void setMaxParameterValueLength(int maxParameterValueLength)
Set the maximum length of a query parameter value that can be logged verbatim by the driver. Parameter values longer than this value will be truncated when logged.- Parameters:
maxParameterValueLength- The maximum length of a query parameter value that can be logged verbatim by the driver. It must be strictly positive or-1, in which case the parameter value is never truncated (use with care).- Throws:
IllegalArgumentException- ifmaxParameterValueLength <= 0 && maxParameterValueLength != -1.
-
getMaxLoggedParameters
public int getMaxLoggedParameters()
Return the maximum number of query parameters that can be logged by the driver. Queries with a number of parameters higher than this value will not have all their parameters logged. The default value isDEFAULT_MAX_LOGGED_PARAMETERS.- Returns:
- The maximum number of query parameters that can be logged by the driver.
-
setMaxLoggedParameters
public void setMaxLoggedParameters(int maxLoggedParameters)
Set the maximum number of query parameters that can be logged by the driver. Queries with a number of parameters higher than this value will not have all their parameters logged.- Parameters:
maxLoggedParameters- the maximum number of query parameters that can be logged by the driver. It must be strictly positive or-1, in which case all parameters will be logged, regardless of their number (use with care).- Throws:
IllegalArgumentException- ifmaxLoggedParameters <= 0 && maxLoggedParameters != -1.
-
update
public void update(Host host, Statement statement, Exception exception, long newLatencyNanos)
A method that is called after each request to a Cassandra node with the duration of that operation.Note that there is no guarantee that this method won't be called concurrently by multiple threads, so implementations should synchronize internally if need be.
- Specified by:
updatein interfaceLatencyTracker- Parameters:
host- The Cassandra host on which a request has been performed. This parameter is nevernull.statement- TheStatementthat has been executed. This parameter is nevernull.exception- AnExceptionthrown when receiving the response, ornullif the response was successful.newLatencyNanos- the latency in nanoseconds of the operation. This latency corresponds to the time elapsed between when the query was sent tohostand when the response was received by the driver (or the operation timed out, in whichnewLatencyNanoswill approximately be the timeout value).
-
maybeLogNormalOrSlowQuery
protected abstract void maybeLogNormalOrSlowQuery(Host host, Statement statement, long latencyMs)
-
maybeLogNormalQuery
protected void maybeLogNormalQuery(Host host, Statement statement, long latencyMs)
-
maybeLogErrorQuery
protected void maybeLogErrorQuery(Host host, Statement statement, Exception exception, long latencyMs)
-
logQuery
protected void logQuery(Statement statement, Exception exception, org.slf4j.Logger logger, String message)
-
countBoundValues
protected int countBoundValues(BatchStatement bs)
-
appendParameters
protected int appendParameters(BoundStatement statement, StringBuilder buffer, int remaining)
-
parameterValueAsString
protected String parameterValueAsString(ColumnDefinitions.Definition definition, ByteBuffer raw)
-
appendParameters
protected int appendParameters(SimpleStatement statement, StringBuilder buffer, int remaining)
-
appendParameters
protected int appendParameters(BuiltStatement statement, StringBuilder buffer, int remaining)
-
append
protected int append(Statement statement, StringBuilder buffer, int remaining)
-
append
protected int append(CharSequence str, StringBuilder buffer, int remaining)
-
-