Skip to content

Commit

Permalink
chore: add open census metric label is_multiplexed for multiplexed se… (
Browse files Browse the repository at this point in the history
#3033)

* chore: add open census metric label is_multiplexed for multiplexed session.

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
arpan14 and gcf-owl-bot[bot] committed Apr 16, 2024
1 parent ebb2182 commit ca213bb
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class MetricRegistryConstants {
LabelKey.create("instance_id", "Name of the instance");
private static final LabelKey LIBRARY_VERSION =
LabelKey.create("library_version", "Library version");
static final LabelKey IS_MULTIPLEXED_KEY =
LabelKey.create("is_multiplexed", "Multiplexed Session");

private static final LabelKey SESSION_TYPE = LabelKey.create("Type", "Type of the Sessions");

/** The label value is used to represent missing value. */
Expand Down Expand Up @@ -62,6 +65,9 @@ class MetricRegistryConstants {
static final ImmutableList<LabelValue> SPANNER_DEFAULT_LABEL_VALUES =
ImmutableList.of(UNSET_LABEL, UNSET_LABEL, UNSET_LABEL, UNSET_LABEL);

static final ImmutableList<LabelKey> SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS =
ImmutableList.of(CLIENT_ID, DATABASE, INSTANCE_ID, LIBRARY_VERSION, IS_MULTIPLEXED_KEY);

/** Unit to represent counts. */
static final String COUNT = "1";

Expand All @@ -80,7 +86,6 @@ class MetricRegistryConstants {
static final String NUM_SESSIONS_AVAILABLE = "spanner/num_available_sessions";
static final String SESSIONS_TYPE = "session_type";
static final String IS_MULTIPLEXED = "is_multiplexed";

static final String MAX_IN_USE_SESSIONS_DESCRIPTION =
"The maximum number of sessions in use during the last 10 minute interval.";
static final String MAX_ALLOWED_SESSIONS_DESCRIPTION =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static com.google.cloud.spanner.MetricRegistryConstants.SESSIONS_TYPE;
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_DEFAULT_LABEL_VALUES;
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS;
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS;
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_TYPE;
import static com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException;
import static com.google.common.base.Preconditions.checkState;
Expand Down Expand Up @@ -3687,7 +3688,7 @@ private void initOpenCensusMetricsCollection(
MetricOptions.builder()
.setDescription(NUM_ACQUIRED_SESSIONS_DESCRIPTION)
.setUnit(COUNT)
.setLabelKeys(SPANNER_LABEL_KEYS)
.setLabelKeys(SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS)
.build());

DerivedLongCumulative numReleasedSessionsMetric =
Expand All @@ -3696,7 +3697,7 @@ private void initOpenCensusMetricsCollection(
MetricOptions.builder()
.setDescription(NUM_RELEASED_SESSIONS_DESCRIPTION)
.setUnit(COUNT)
.setLabelKeys(SPANNER_LABEL_KEYS)
.setLabelKeys(SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS)
.build());

DerivedLongGauge numSessionsInPoolMetric =
Expand Down Expand Up @@ -3725,13 +3726,28 @@ private void initOpenCensusMetricsCollection(
sessionsTimeouts.removeTimeSeries(labelValues);
sessionsTimeouts.createTimeSeries(labelValues, this, SessionPool::getNumWaiterTimeouts);

numAcquiredSessionsMetric.removeTimeSeries(labelValues);
List<LabelValue> labelValuesWithRegularSessions = new ArrayList<>(labelValues);
List<LabelValue> labelValuesWithMultiplexedSessions = new ArrayList<>(labelValues);
labelValuesWithMultiplexedSessions.add(LabelValue.create("true"));
labelValuesWithRegularSessions.add(LabelValue.create("false"));

numAcquiredSessionsMetric.removeTimeSeries(labelValuesWithRegularSessions);
numAcquiredSessionsMetric.createTimeSeries(
labelValuesWithRegularSessions, this, sessionPool -> sessionPool.numSessionsAcquired);
numAcquiredSessionsMetric.removeTimeSeries(labelValuesWithMultiplexedSessions);
numAcquiredSessionsMetric.createTimeSeries(
labelValues, this, sessionPool -> sessionPool.numSessionsAcquired);
labelValuesWithMultiplexedSessions,
this,
sessionPool -> sessionPool.numMultiplexedSessionsAcquired);

numReleasedSessionsMetric.removeTimeSeries(labelValues);
numReleasedSessionsMetric.removeTimeSeries(labelValuesWithRegularSessions);
numReleasedSessionsMetric.createTimeSeries(
labelValuesWithRegularSessions, this, sessionPool -> sessionPool.numSessionsReleased);
numReleasedSessionsMetric.removeTimeSeries(labelValuesWithMultiplexedSessions);
numReleasedSessionsMetric.createTimeSeries(
labelValues, this, sessionPool -> sessionPool.numSessionsReleased);
labelValuesWithMultiplexedSessions,
this,
sessionPool -> sessionPool.numMultiplexedSessionsReleased);

List<LabelValue> labelValuesWithBeingPreparedType = new ArrayList<>(labelValues);
labelValuesWithBeingPreparedType.add(NUM_SESSIONS_BEING_PREPARED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.cloud.spanner;

import static com.google.cloud.spanner.MetricRegistryConstants.GET_SESSION_TIMEOUTS;
import static com.google.cloud.spanner.MetricRegistryConstants.IS_MULTIPLEXED_KEY;
import static com.google.cloud.spanner.MetricRegistryConstants.MAX_ALLOWED_SESSIONS;
import static com.google.cloud.spanner.MetricRegistryConstants.MAX_IN_USE_SESSIONS;
import static com.google.cloud.spanner.MetricRegistryConstants.METRIC_PREFIX;
Expand All @@ -31,6 +32,7 @@
import static com.google.cloud.spanner.MetricRegistryConstants.NUM_WRITE_SESSIONS;
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_DEFAULT_LABEL_VALUES;
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS;
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS;
import static com.google.cloud.spanner.MetricRegistryConstants.SPANNER_LABEL_KEYS_WITH_TYPE;
import static com.google.cloud.spanner.SpannerOptionsTest.runWithSystemProperty;
import static com.google.common.truth.Truth.assertThat;
Expand Down Expand Up @@ -1769,19 +1771,70 @@ public void testOpenCensusSessionMetrics() throws Exception {
assertThat(getSessionsTimeouts.get(0).keys()).isEqualTo(SPANNER_LABEL_KEYS);
assertThat(getSessionsTimeouts.get(0).values()).isEqualTo(labelValues);

List<LabelValue> labelValuesWithRegularSessions = new ArrayList<>(labelValues);
labelValuesWithRegularSessions.add(LabelValue.create("false"));
List<LabelValue> labelValuesWithMultiplexedSessions = new ArrayList<>(labelValues);
labelValuesWithMultiplexedSessions.add(LabelValue.create("true"));
List<PointWithFunction> numAcquiredSessions =
record.getMetrics().get(METRIC_PREFIX + NUM_ACQUIRED_SESSIONS);
assertThat(numAcquiredSessions.size()).isEqualTo(1);
assertThat(numAcquiredSessions.get(0).value()).isEqualTo(2L);
assertThat(numAcquiredSessions.get(0).keys()).isEqualTo(SPANNER_LABEL_KEYS);
assertThat(numAcquiredSessions.get(0).values()).isEqualTo(labelValues);
assertThat(numAcquiredSessions.size()).isEqualTo(2);
PointWithFunction regularSessionMetric =
numAcquiredSessions.stream()
.filter(
x ->
x.keys().contains(IS_MULTIPLEXED_KEY)
&& x.values().contains(LabelValue.create("false")))
.findFirst()
.get();
PointWithFunction multiplexedSessionMetric =
numAcquiredSessions.stream()
.filter(
x ->
x.keys().contains(IS_MULTIPLEXED_KEY)
&& x.values().contains(LabelValue.create("true")))
.findFirst()
.get();
// verify metrics for regular sessions
assertThat(regularSessionMetric.value()).isEqualTo(2L);
assertThat(regularSessionMetric.keys()).isEqualTo(SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS);
assertThat(regularSessionMetric.values()).isEqualTo(labelValuesWithRegularSessions);

// verify metrics for multiplexed sessions
assertThat(multiplexedSessionMetric.value()).isEqualTo(0L);
assertThat(multiplexedSessionMetric.keys())
.isEqualTo(SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS);
assertThat(multiplexedSessionMetric.values()).isEqualTo(labelValuesWithMultiplexedSessions);

List<PointWithFunction> numReleasedSessions =
record.getMetrics().get(METRIC_PREFIX + NUM_RELEASED_SESSIONS);
assertThat(numReleasedSessions.size()).isEqualTo(1);
assertThat(numReleasedSessions.get(0).value()).isEqualTo(0);
assertThat(numReleasedSessions.get(0).keys()).isEqualTo(SPANNER_LABEL_KEYS);
assertThat(numReleasedSessions.get(0).values()).isEqualTo(labelValues);
assertThat(numReleasedSessions.size()).isEqualTo(2);

regularSessionMetric =
numReleasedSessions.stream()
.filter(
x ->
x.keys().contains(IS_MULTIPLEXED_KEY)
&& x.values().contains(LabelValue.create("false")))
.findFirst()
.get();
multiplexedSessionMetric =
numReleasedSessions.stream()
.filter(
x ->
x.keys().contains(IS_MULTIPLEXED_KEY)
&& x.values().contains(LabelValue.create("true")))
.findFirst()
.get();
// verify metrics for regular sessions
assertThat(regularSessionMetric.value()).isEqualTo(0L);
assertThat(regularSessionMetric.keys()).isEqualTo(SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS);
assertThat(regularSessionMetric.values()).isEqualTo(labelValuesWithRegularSessions);

// verify metrics for multiplexed sessions
assertThat(multiplexedSessionMetric.value()).isEqualTo(0L);
assertThat(multiplexedSessionMetric.keys())
.isEqualTo(SPANNER_LABEL_KEYS_WITH_MULTIPLEXED_SESSIONS);
assertThat(multiplexedSessionMetric.values()).isEqualTo(labelValuesWithMultiplexedSessions);

List<PointWithFunction> maxAllowedSessions =
record.getMetrics().get(METRIC_PREFIX + MAX_ALLOWED_SESSIONS);
Expand Down Expand Up @@ -1847,12 +1900,46 @@ public void testOpenCensusSessionMetrics() throws Exception {

session1.close();
numAcquiredSessions = record.getMetrics().get(METRIC_PREFIX + NUM_ACQUIRED_SESSIONS);
assertThat(numAcquiredSessions.size()).isEqualTo(1);
assertThat(numAcquiredSessions.get(0).value()).isEqualTo(3L);
assertThat(numAcquiredSessions.size()).isEqualTo(2);
regularSessionMetric =
numAcquiredSessions.stream()
.filter(
x ->
x.keys().contains(IS_MULTIPLEXED_KEY)
&& x.values().contains(LabelValue.create("false")))
.findFirst()
.get();
multiplexedSessionMetric =
numAcquiredSessions.stream()
.filter(
x ->
x.keys().contains(IS_MULTIPLEXED_KEY)
&& x.values().contains(LabelValue.create("true")))
.findFirst()
.get();
assertThat(regularSessionMetric.value()).isEqualTo(3L);
assertThat(multiplexedSessionMetric.value()).isEqualTo(0L);

numReleasedSessions = record.getMetrics().get(METRIC_PREFIX + NUM_RELEASED_SESSIONS);
assertThat(numReleasedSessions.size()).isEqualTo(1);
assertThat(numReleasedSessions.get(0).value()).isEqualTo(3L);
assertThat(numReleasedSessions.size()).isEqualTo(2);
regularSessionMetric =
numReleasedSessions.stream()
.filter(
x ->
x.keys().contains(IS_MULTIPLEXED_KEY)
&& x.values().contains(LabelValue.create("false")))
.findFirst()
.get();
multiplexedSessionMetric =
numReleasedSessions.stream()
.filter(
x ->
x.keys().contains(IS_MULTIPLEXED_KEY)
&& x.values().contains(LabelValue.create("true")))
.findFirst()
.get();
assertThat(regularSessionMetric.value()).isEqualTo(3L);
assertThat(multiplexedSessionMetric.value()).isEqualTo(0L);

maxInUseSessions = record.getMetrics().get(METRIC_PREFIX + MAX_IN_USE_SESSIONS);
assertThat(maxInUseSessions.size()).isEqualTo(1);
Expand Down

0 comments on commit ca213bb

Please sign in to comment.