1818
1919import static com .google .cloud .spanner .MetricRegistryConstants .COUNT ;
2020import static com .google .cloud .spanner .MetricRegistryConstants .GET_SESSION_TIMEOUTS ;
21- import static com .google .cloud .spanner .MetricRegistryConstants .IN_USE_SESSIONS ;
22- import static com .google .cloud .spanner .MetricRegistryConstants .IN_USE_SESSIONS_DESCRIPTION ;
2321import static com .google .cloud .spanner .MetricRegistryConstants .MAX_ALLOWED_SESSIONS ;
2422import static com .google .cloud .spanner .MetricRegistryConstants .MAX_ALLOWED_SESSIONS_DESCRIPTION ;
2523import static com .google .cloud .spanner .MetricRegistryConstants .MAX_IN_USE_SESSIONS ;
2624import static com .google .cloud .spanner .MetricRegistryConstants .MAX_IN_USE_SESSIONS_DESCRIPTION ;
2725import static com .google .cloud .spanner .MetricRegistryConstants .NUM_ACQUIRED_SESSIONS ;
2826import static com .google .cloud .spanner .MetricRegistryConstants .NUM_ACQUIRED_SESSIONS_DESCRIPTION ;
27+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_IN_USE_SESSIONS ;
28+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_READ_SESSIONS ;
2929import static com .google .cloud .spanner .MetricRegistryConstants .NUM_RELEASED_SESSIONS ;
3030import static com .google .cloud .spanner .MetricRegistryConstants .NUM_RELEASED_SESSIONS_DESCRIPTION ;
31+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_SESSIONS_BEING_PREPARED ;
32+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_SESSIONS_IN_POOL ;
33+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_SESSIONS_IN_POOL_DESCRIPTION ;
34+ import static com .google .cloud .spanner .MetricRegistryConstants .NUM_WRITE_SESSIONS ;
3135import static com .google .cloud .spanner .MetricRegistryConstants .SESSIONS_TIMEOUTS_DESCRIPTION ;
3236import static com .google .cloud .spanner .MetricRegistryConstants .SPANNER_DEFAULT_LABEL_VALUES ;
3337import static com .google .cloud .spanner .MetricRegistryConstants .SPANNER_LABEL_KEYS ;
38+ import static com .google .cloud .spanner .MetricRegistryConstants .SPANNER_LABEL_KEYS_WITH_TYPE ;
3439import static com .google .cloud .spanner .SpannerExceptionFactory .newSpannerException ;
3540
3641import com .google .api .core .ApiFuture ;
7176import io .opencensus .trace .Status ;
7277import io .opencensus .trace .Tracer ;
7378import io .opencensus .trace .Tracing ;
79+ import java .util .ArrayList ;
7480import java .util .Arrays ;
7581import java .util .HashSet ;
7682import java .util .Iterator ;
@@ -2030,15 +2036,6 @@ private void initMetricsCollection(MetricRegistry metricRegistry, List<LabelValu
20302036 .setLabelKeys (SPANNER_LABEL_KEYS )
20312037 .build ());
20322038
2033- DerivedLongGauge numInUseSessionsMetric =
2034- metricRegistry .addDerivedLongGauge (
2035- IN_USE_SESSIONS ,
2036- MetricOptions .builder ()
2037- .setDescription (IN_USE_SESSIONS_DESCRIPTION )
2038- .setUnit (COUNT )
2039- .setLabelKeys (SPANNER_LABEL_KEYS )
2040- .build ());
2041-
20422039 DerivedLongCumulative sessionsTimeouts =
20432040 metricRegistry .addDerivedLongCumulative (
20442041 GET_SESSION_TIMEOUTS ,
@@ -2066,6 +2063,15 @@ private void initMetricsCollection(MetricRegistry metricRegistry, List<LabelValu
20662063 .setLabelKeys (SPANNER_LABEL_KEYS )
20672064 .build ());
20682065
2066+ DerivedLongGauge numSessionsInPoolMetric =
2067+ metricRegistry .addDerivedLongGauge (
2068+ NUM_SESSIONS_IN_POOL ,
2069+ MetricOptions .builder ()
2070+ .setDescription (NUM_SESSIONS_IN_POOL_DESCRIPTION )
2071+ .setUnit (COUNT )
2072+ .setLabelKeys (SPANNER_LABEL_KEYS_WITH_TYPE )
2073+ .build ());
2074+
20692075 // The value of a maxSessionsInUse is observed from a callback function. This function is
20702076 // invoked whenever metrics are collected.
20712077 maxInUseSessionsMetric .createTimeSeries (
@@ -2090,18 +2096,6 @@ public long applyAsLong(SessionPoolOptions options) {
20902096 }
20912097 });
20922098
2093- // The value of a numSessionsInUse is observed from a callback function. This function is
2094- // invoked whenever metrics are collected.
2095- numInUseSessionsMetric .createTimeSeries (
2096- labelValues ,
2097- this ,
2098- new ToLongFunction <SessionPool >() {
2099- @ Override
2100- public long applyAsLong (SessionPool sessionPool ) {
2101- return sessionPool .numSessionsInUse ;
2102- }
2103- });
2104-
21052099 // The value of a numWaiterTimeouts is observed from a callback function. This function is
21062100 // invoked whenever metrics are collected.
21072101 sessionsTimeouts .createTimeSeries (
@@ -2133,5 +2127,53 @@ public long applyAsLong(SessionPool sessionPool) {
21332127 return sessionPool .numSessionsReleased ;
21342128 }
21352129 });
2130+
2131+ List <LabelValue > labelValuesWithBeingPreparedType = new ArrayList <>(labelValues );
2132+ labelValuesWithBeingPreparedType .add (NUM_SESSIONS_BEING_PREPARED );
2133+ numSessionsInPoolMetric .createTimeSeries (
2134+ labelValuesWithBeingPreparedType ,
2135+ this ,
2136+ new ToLongFunction <SessionPool >() {
2137+ @ Override
2138+ public long applyAsLong (SessionPool sessionPool ) {
2139+ return sessionPool .numSessionsBeingPrepared ;
2140+ }
2141+ });
2142+
2143+ List <LabelValue > labelValuesWithInUseType = new ArrayList <>(labelValues );
2144+ labelValuesWithInUseType .add (NUM_IN_USE_SESSIONS );
2145+ numSessionsInPoolMetric .createTimeSeries (
2146+ labelValuesWithInUseType ,
2147+ this ,
2148+ new ToLongFunction <SessionPool >() {
2149+ @ Override
2150+ public long applyAsLong (SessionPool sessionPool ) {
2151+ return sessionPool .numSessionsInUse ;
2152+ }
2153+ });
2154+
2155+ List <LabelValue > labelValuesWithReadType = new ArrayList <>(labelValues );
2156+ labelValuesWithReadType .add (NUM_READ_SESSIONS );
2157+ numSessionsInPoolMetric .createTimeSeries (
2158+ labelValuesWithReadType ,
2159+ this ,
2160+ new ToLongFunction <SessionPool >() {
2161+ @ Override
2162+ public long applyAsLong (SessionPool sessionPool ) {
2163+ return sessionPool .readSessions .size ();
2164+ }
2165+ });
2166+
2167+ List <LabelValue > labelValuesWithWriteType = new ArrayList <>(labelValues );
2168+ labelValuesWithWriteType .add (NUM_WRITE_SESSIONS );
2169+ numSessionsInPoolMetric .createTimeSeries (
2170+ labelValuesWithWriteType ,
2171+ this ,
2172+ new ToLongFunction <SessionPool >() {
2173+ @ Override
2174+ public long applyAsLong (SessionPool sessionPool ) {
2175+ return sessionPool .writePreparedSessions .size ();
2176+ }
2177+ });
21362178 }
21372179}
0 commit comments