Skip to content

Commit 2116c41

Browse files
authored
Merge pull request #624 from zhenlineo/4.0-metrics
Improved java doc for metrics
2 parents 42925a6 + 2c2690b commit 2116c41

24 files changed

+294
-426
lines changed

driver/src/main/java/org/neo4j/driver/ConnectionPoolMetrics.java

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,93 +22,89 @@
2222

2323
public interface ConnectionPoolMetrics
2424
{
25-
enum PoolStatus
26-
{
27-
OPEN, CLOSED
28-
}
2925

3026
/**
31-
* An unique name that identifies this connection pool metrics among all others
27+
* An unique id that identifies this pool metrics.
3228
* @return An unique name
3329
*/
3430
String id();
3531

3632
/**
37-
* The status of the pool.
38-
* @return The status of the pool.
39-
*/
40-
PoolStatus poolStatus();
41-
42-
/**
43-
* The amount of connections that are currently in-use (borrowed out of the pool).
33+
* The amount of connections that are currently in-use (borrowed out of the pool). The amount can increase or decrease over time.
4434
* @return The amount of connections that are currently in-use
4535
*/
4636
int inUse();
4737

4838
/**
49-
* The amount of connections that are currently idle (buffered inside the pool).
39+
* The amount of connections that are currently idle (buffered inside the pool). The amount can increase or decrease over time.
5040
* @return The amount of connections that are currently idle.
5141
*/
5242
int idle();
5343

5444
/**
55-
* The amount of connections that are currently waiting to be created.
45+
* The amount of connections that are currently in the process of being created.
5646
* The amount is increased by one when the pool noticed a request to create a new connection.
5747
* The amount is decreased by one when the pool noticed a new connection is created successfully or failed to create.
48+
* The amount can increase or decrease over time.
5849
* @return The amount of connections that are waiting to be created.
5950
*/
6051
int creating();
6152

6253
/**
63-
* An increasing-only number to record how many connections have been created by this pool successfully since the pool is created.
54+
* A counter to record how many connections have been successfully created with this pool since the pool is created.
55+
* This number increases every time when a connection is successfully created.
6456
* @return The amount of connections have ever been created by this pool.
6557
*/
6658
long created();
6759

6860
/**
69-
* An increasing-only number to record how many connections have been failed to create.
70-
* @return The amount of connections have been failed to create by this pool.
61+
* A counter to record how many connections that have failed to be created.
62+
* This number increases every time when a connection failed to be created.
63+
* @return The amount of connections have failed to be created by this pool.
7164
*/
7265
long failedToCreate();
7366

7467
/**
75-
* An increasing-only number to record how many connections have been closed by this pool.
68+
* A counter to record how many connections have been closed by this pool.
69+
* This number increases every time when a connection is closed.
7670
* @return The amount of connections have been closed by this pool.
7771
*/
7872
long closed();
7973

8074
/**
81-
* The current count of application requests to wait for acquiring a connection from the pool.
82-
* The reason to wait could be waiting for creating a new connection, or waiting for a connection to be free by application when the pool is full.
83-
* @return The current amount of application request to wait for acquiring a connection from the pool.
75+
* The number of connection acquisition requests that are currently in progress.
76+
* These requests can be waiting or blocked if there are no connections immediately available in the pool.
77+
* A request will wait for a new connection to be created, or it will be blocked if the pool is at its maximum size but all connections are already in use.
78+
* The amount can increase or decrease over time.
79+
* @return The number of connection acquisition requests that are currently in progress.
8480
*/
8581
int acquiring();
8682

8783
/**
88-
* An increasing-only number to record how many connections have been acquired from the pool since the pool is created.
89-
* The connections acquired could hold either a newly created connection or a reused connection from the pool.
84+
* A counter to record how many connections have been acquired from the pool since the pool is created.
85+
* This number increases every time when a connection is acquired.
9086
* @return The amount of connections that have been acquired from the pool.
9187
*/
9288
long acquired();
9389

9490
/**
95-
* An increasing-only number to record how many times that we've failed to acquire a connection from the pool within configured maximum acquisition timeout
91+
* A counter to record how many times that we've failed to acquire a connection from the pool within configured maximum acquisition timeout
9692
* set by {@link Config.ConfigBuilder#withConnectionAcquisitionTimeout(long, TimeUnit)}.
97-
* The connection acquired could hold either a newly created connection or a reused connection from the pool.
93+
* This number increases every time when a connection is timed out when acquiring.
9894
* @return The amount of failures to acquire a connection from the pool within maximum connection acquisition timeout.
9995
*/
10096
long timedOutToAcquire();
10197

10298
/**
103-
* The total acquisition time in milliseconds of all connection acquisition requests since the pool is created.
99+
* A counter to record the total acquisition time in milliseconds of all connection acquisition requests since the pool is created.
100+
* This number increases every time when a connection is acquired.
104101
* See {@link ConnectionPoolMetrics#acquired()} for the total amount of connection acquired since the driver is created.
105102
* The average acquisition time can be calculated using the code below:
106103
* <h2>Example</h2>
107104
* <pre>
108105
* {@code
109-
* ConnectionPoolMetrics previous = ConnectionPoolMetrics.EMPTY;
106+
* ConnectionPoolMetrics previous, current;
110107
* ...
111-
* ConnectionPoolMetrics current = poolMetrics.snapshot();
112108
* double average = computeAverage(current.totalAcquisitionTime(), previous.totalAcquisitionTime(), current.acquired(), previous.acquired());
113109
* previous = current;
114110
* ...
@@ -124,15 +120,15 @@ enum PoolStatus
124120
long totalAcquisitionTime();
125121

126122
/**
127-
* The total time in milliseconds spent to establishing new socket connections since the pool is created.
123+
* A counter to record the total time in milliseconds spent to establishing new socket connections since the pool is created.
124+
* This number increases every time when a connection is established.
128125
* See {@link ConnectionPoolMetrics#created()} for the total amount of connections established since the pool is created.
129126
* The average connection time can be calculated using the code below:
130127
* <h2>Example</h2>
131128
* <pre>
132129
* {@code
133-
* ConnectionPoolMetrics previous = ConnectionPoolMetrics.EMPTY;
130+
* ConnectionPoolMetrics previous, current;
134131
* ...
135-
* ConnectionPoolMetrics current = poolMetrics.snapshot();
136132
* double average = computeAverage(current.totalConnectionTime(), previous.totalConnectionTime(), current.created(), previous.created());
137133
* previous = current;
138134
* ...
@@ -148,15 +144,16 @@ enum PoolStatus
148144
long totalConnectionTime();
149145

150146
/**
151-
* The total time in milliseconds connections are borrowed out of the pool, such as the time spent in user's application code to run cypher queries.
147+
* A counter to record the total time in milliseconds connections are borrowed out of the pool,
148+
* such as the time spent in user's application code to run cypher queries.
149+
* This number increases every time when a connection is returned back to the pool.
152150
* See {@link ConnectionPoolMetrics#totalInUseCount()} for the total amount of connections that are borrowed out of the pool.
153151
* The average in-use time can be calculated using the code below:
154152
* <h2>Example</h2>
155153
* <pre>
156154
* {@code
157-
* ConnectionPoolMetrics previous = ConnectionPoolMetrics.EMPTY;
155+
* ConnectionPoolMetrics previous, current;
158156
* ...
159-
* ConnectionPoolMetrics current = poolMetrics.snapshot();
160157
* double average = computeAverage(current.totalInUseTime(), previous.totalInUseTime(), current.totalInUseCount(), previous.totalInUseCount());
161158
* previous = current;
162159
* ...
@@ -173,13 +170,8 @@ enum PoolStatus
173170

174171
/**
175172
* The total amount of connections that are borrowed outside the pool since the pool is created.
173+
* This number increases every time when a connection is returned back to the pool.
176174
* @return the total amount of connection that are borrowed outside the pool.
177175
*/
178176
long totalInUseCount();
179-
180-
/**
181-
* Returns a snapshot of this connection pool metrics.
182-
* @return a snapshot of this connection pool metrics.
183-
*/
184-
ConnectionPoolMetrics snapshot();
185177
}

driver/src/main/java/org/neo4j/driver/Driver.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ public interface Driver extends AutoCloseable
155155
@Experimental
156156
Metrics metrics();
157157

158+
/**
159+
* Returns true if the driver metrics reporting is enabled via {@link Config.ConfigBuilder#withDriverMetrics()}, otherwise false.
160+
*
161+
* @return true if the metrics reporting is enabled.
162+
*/
163+
@Experimental
164+
boolean isMetricsEnabled();
165+
158166
/**
159167
* This will return the type system supported by the driver.
160168
* The types supported on a particular server a session is connected against might not contain all of the types defined here.

driver/src/main/java/org/neo4j/driver/GraphDatabase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020

2121
import java.net.URI;
2222

23+
import org.neo4j.driver.exceptions.ServiceUnavailableException;
2324
import org.neo4j.driver.internal.DriverFactory;
2425
import org.neo4j.driver.internal.cluster.RoutingSettings;
2526
import org.neo4j.driver.internal.retry.RetrySettings;
26-
import org.neo4j.driver.exceptions.ServiceUnavailableException;
2727

2828
import static org.neo4j.driver.internal.DriverFactory.BOLT_ROUTING_URI_SCHEME;
2929

driver/src/main/java/org/neo4j/driver/Metrics.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@
1818
*/
1919
package org.neo4j.driver;
2020

21-
import java.util.Map;
21+
import java.util.Collection;
22+
23+
import org.neo4j.driver.util.Experimental;
2224

2325
public interface Metrics
2426
{
2527
/**
26-
* A map of connection pool metrics.
27-
* The {@link ConnectionPoolMetrics#id()} are used as the keys of the map.
28-
* @return The connection pool metrics.
29-
*/
30-
Map<String, ConnectionPoolMetrics> connectionPoolMetrics();
31-
32-
/**
33-
* Returns a snapshot of this metrics.
34-
* @return a snapshot of this metrics.
28+
* Connection pool metrics records metrics of connection pools that are currently in use.
29+
* As the connection pools are dynamically added and removed while the server topology changes, the metrics collection changes over time.
30+
* @return Connection pool metrics for all current active pools.
3531
*/
36-
Metrics snapshot();
32+
@Experimental
33+
Collection<ConnectionPoolMetrics> connectionPoolMetrics();
3734
}

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ protected static MetricsProvider createDriverMetrics( Config config, Clock clock
123123
{
124124
if( config.isMetricsEnabled() )
125125
{
126-
return new InternalMetricsProvider( clock );
126+
return new InternalMetricsProvider( clock, config.logging() );
127127
}
128128
else
129129
{

driver/src/main/java/org/neo4j/driver/internal/InternalDriver.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public Metrics metrics()
9999
return metricsProvider.metrics();
100100
}
101101

102+
@Override
103+
public boolean isMetricsEnabled()
104+
{
105+
return metricsProvider.isMetricsEnabled();
106+
}
107+
102108
@Override
103109
public boolean isEncrypted()
104110
{

driver/src/main/java/org/neo4j/driver/internal/async/NetworkConnection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.neo4j.driver.internal.util.ServerVersion;
4242

4343
import static java.util.Collections.emptyMap;
44+
import static org.neo4j.driver.internal.async.connection.ChannelAttributes.poolId;
4445
import static org.neo4j.driver.internal.async.connection.ChannelAttributes.setTerminationReason;
4546

4647
/**
@@ -76,7 +77,7 @@ public NetworkConnection( Channel channel, ChannelPool channelPool, Clock clock,
7677
this.clock = clock;
7778
this.metricsListener = metricsListener;
7879
this.inUseEvent = metricsListener.createListenerEvent();
79-
metricsListener.afterConnectionCreated( this.serverAddress, this.inUseEvent );
80+
metricsListener.afterConnectionCreated( poolId( this.channel ), this.inUseEvent );
8081
}
8182

8283
@Override
@@ -166,7 +167,7 @@ public CompletionStage<Void> release()
166167
channelPool, messageDispatcher, clock, releaseFuture );
167168

168169
writeResetMessageIfNeeded( handler, false );
169-
metricsListener.afterConnectionReleased( this.serverAddress, this.inUseEvent );
170+
metricsListener.afterConnectionReleased( poolId( this.channel ), this.inUseEvent );
170171
}
171172
return releaseFuture;
172173
}
@@ -180,7 +181,7 @@ public void terminateAndRelease( String reason )
180181
channel.close();
181182
channelPool.release( channel );
182183
releaseFuture.complete( null );
183-
metricsListener.afterConnectionReleased( this.serverAddress, this.inUseEvent );
184+
metricsListener.afterConnectionReleased( poolId( this.channel ), this.inUseEvent );
184185
}
185186
}
186187

driver/src/main/java/org/neo4j/driver/internal/async/connection/ChannelAttributes.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public final class ChannelAttributes
3131
{
3232
private static final AttributeKey<String> CONNECTION_ID = newInstance( "connectionId" );
33+
private static final AttributeKey<String> POOL_ID = newInstance( "poolId" );
3334
private static final AttributeKey<Integer> PROTOCOL_VERSION = newInstance( "protocolVersion" );
3435
private static final AttributeKey<BoltServerAddress> ADDRESS = newInstance( "serverAddress" );
3536
private static final AttributeKey<ServerVersion> SERVER_VERSION = newInstance( "serverVersion" );
@@ -52,6 +53,16 @@ public static void setConnectionId( Channel channel, String id )
5253
setOnce( channel, CONNECTION_ID, id );
5354
}
5455

56+
public static String poolId( Channel channel )
57+
{
58+
return get( channel, POOL_ID );
59+
}
60+
61+
public static void setPoolId( Channel channel, String id )
62+
{
63+
setOnce( channel, POOL_ID, id );
64+
}
65+
5566
public static int protocolVersion( Channel channel )
5667
{
5768
return get( channel, PROTOCOL_VERSION );

0 commit comments

Comments
 (0)