24
24
import static com .google .cloud .spanner .MetricRegistryConstants .MAX_ALLOWED_SESSIONS_DESCRIPTION ;
25
25
import static com .google .cloud .spanner .MetricRegistryConstants .MAX_IN_USE_SESSIONS ;
26
26
import static com .google .cloud .spanner .MetricRegistryConstants .MAX_IN_USE_SESSIONS_DESCRIPTION ;
27
+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_ACQUIRED_SESSIONS ;
28
+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_ACQUIRED_SESSIONS_DESCRIPTION ;
29
+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_RELEASED_SESSIONS ;
30
+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_RELEASED_SESSIONS_DESCRIPTION ;
27
31
import static com .google .cloud .spanner .MetricRegistryConstants .SESSIONS_TIMEOUTS_DESCRIPTION ;
28
32
import static com .google .cloud .spanner .MetricRegistryConstants .SPANNER_DEFAULT_LABEL_VALUES ;
29
33
import static com .google .cloud .spanner .MetricRegistryConstants .SPANNER_LABEL_KEYS ;
@@ -796,6 +800,7 @@ public ApiFuture<Empty> asyncClose() {
796
800
public void close () {
797
801
synchronized (lock ) {
798
802
numSessionsInUse --;
803
+ numSessionsReleased ++;
799
804
}
800
805
leakedException = null ;
801
806
if (lastException != null && isSessionNotFound (lastException )) {
@@ -1122,6 +1127,12 @@ private static enum Position {
1122
1127
@ GuardedBy ("lock" )
1123
1128
private int maxSessionsInUse = 0 ;
1124
1129
1130
+ @ GuardedBy ("lock" )
1131
+ private long numSessionsAcquired = 0 ;
1132
+
1133
+ @ GuardedBy ("lock" )
1134
+ private long numSessionsReleased = 0 ;
1135
+
1125
1136
private AtomicLong numWaiterTimeouts = new AtomicLong ();
1126
1137
1127
1138
@ GuardedBy ("lock" )
@@ -1448,6 +1459,7 @@ private PooledSession replaceSession(
1448
1459
if (!options .isFailIfSessionNotFound () && session .allowReplacing ) {
1449
1460
synchronized (lock ) {
1450
1461
numSessionsInUse --;
1462
+ numSessionsReleased ++;
1451
1463
}
1452
1464
session .leakedException = null ;
1453
1465
invalidateSession (session );
@@ -1468,6 +1480,7 @@ private void incrementNumSessionsInUse() {
1468
1480
if (maxSessionsInUse < ++numSessionsInUse ) {
1469
1481
maxSessionsInUse = numSessionsInUse ;
1470
1482
}
1483
+ numSessionsAcquired ++;
1471
1484
}
1472
1485
}
1473
1486
@@ -1857,6 +1870,24 @@ private void initMetricsCollection(MetricRegistry metricRegistry, List<LabelValu
1857
1870
.setLabelKeys (SPANNER_LABEL_KEYS )
1858
1871
.build ());
1859
1872
1873
+ DerivedLongCumulative numAcquiredSessionsMetric =
1874
+ metricRegistry .addDerivedLongCumulative (
1875
+ NUM_ACQUIRED_SESSIONS ,
1876
+ MetricOptions .builder ()
1877
+ .setDescription (NUM_ACQUIRED_SESSIONS_DESCRIPTION )
1878
+ .setUnit (COUNT )
1879
+ .setLabelKeys (SPANNER_LABEL_KEYS )
1880
+ .build ());
1881
+
1882
+ DerivedLongCumulative numReleasedSessionsMetric =
1883
+ metricRegistry .addDerivedLongCumulative (
1884
+ NUM_RELEASED_SESSIONS ,
1885
+ MetricOptions .builder ()
1886
+ .setDescription (NUM_RELEASED_SESSIONS_DESCRIPTION )
1887
+ .setUnit (COUNT )
1888
+ .setLabelKeys (SPANNER_LABEL_KEYS )
1889
+ .build ());
1890
+
1860
1891
// The value of a maxSessionsInUse is observed from a callback function. This function is
1861
1892
// invoked whenever metrics are collected.
1862
1893
maxInUseSessionsMetric .createTimeSeries (
@@ -1904,5 +1935,25 @@ public long applyAsLong(SessionPool sessionPool) {
1904
1935
return sessionPool .getNumWaiterTimeouts ();
1905
1936
}
1906
1937
});
1938
+
1939
+ numAcquiredSessionsMetric .createTimeSeries (
1940
+ labelValues ,
1941
+ this ,
1942
+ new ToLongFunction <SessionPool >() {
1943
+ @ Override
1944
+ public long applyAsLong (SessionPool sessionPool ) {
1945
+ return sessionPool .numSessionsAcquired ;
1946
+ }
1947
+ });
1948
+
1949
+ numReleasedSessionsMetric .createTimeSeries (
1950
+ labelValues ,
1951
+ this ,
1952
+ new ToLongFunction <SessionPool >() {
1953
+ @ Override
1954
+ public long applyAsLong (SessionPool sessionPool ) {
1955
+ return sessionPool .numSessionsReleased ;
1956
+ }
1957
+ });
1907
1958
}
1908
1959
}
0 commit comments