Skip to content

Commit

Permalink
chore: built-in metrics attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 committed Sep 25, 2024
1 parent 216e18e commit 9774ab9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ public class BuiltInMetricsConstant {
public static final AttributeKey<String> LOCATION_ID_KEY = AttributeKey.stringKey("location");
public static final AttributeKey<String> INSTANCE_CONFIG_ID_KEY =
AttributeKey.stringKey("instance_config");
public static final AttributeKey<String> CLIENT_HASH_KEY = AttributeKey.stringKey("client_hash");

// These metric labels will be promoted to the spanner monitored resource fields
public static final Set<AttributeKey<String>> SPANNER_PROMOTED_RESOURCE_LABELS =
ImmutableSet.of(PROJECT_ID_KEY, INSTANCE_ID_KEY, INSTANCE_CONFIG_ID_KEY, LOCATION_ID_KEY);
ImmutableSet.of(
PROJECT_ID_KEY,
INSTANCE_ID_KEY,
INSTANCE_CONFIG_ID_KEY,
LOCATION_ID_KEY,
CLIENT_HASH_KEY);

public static final AttributeKey<String> DATABASE_KEY = AttributeKey.stringKey("database");
public static final AttributeKey<String> CLIENT_UID_KEY = AttributeKey.stringKey("client_uid");
Expand All @@ -87,6 +93,7 @@ public class BuiltInMetricsConstant {
LOCATION_ID_KEY,
INSTANCE_CONFIG_ID_KEY,
CLIENT_UID_KEY,
CLIENT_HASH_KEY,
METHOD_KEY,
STATUS_KEY,
DATABASE_KEY,
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.opentelemetry.detection.GCPPlatformDetector.SupportedPlatform.GOOGLE_KUBERNETES_ENGINE;
import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_HASH_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_NAME_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_UID_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.INSTANCE_CONFIG_ID_KEY;
Expand Down Expand Up @@ -83,6 +84,7 @@ Map<String, String> createClientAttributes(String projectId, String client_name)
clientAttributes.put(INSTANCE_CONFIG_ID_KEY.getKey(), "unknown");
clientAttributes.put(CLIENT_NAME_KEY.getKey(), client_name);
clientAttributes.put(CLIENT_UID_KEY.getKey(), getDefaultTaskValue());
clientAttributes.put(CLIENT_HASH_KEY.getKey(), "cloud_spanner_client_raw_metrics");
return clientAttributes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public static void setup() {
BuiltInOpenTelemetryMetricsProvider.detectClientLocation())
.put(BuiltInMetricsConstant.CLIENT_NAME_KEY, client_name)
.put(BuiltInMetricsConstant.CLIENT_UID_KEY, attributes.get("client_uid"))
.put(BuiltInMetricsConstant.CLIENT_HASH_KEY, "cloud_spanner_client_raw_metrics")
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.cloud.spanner;

import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_HASH_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_NAME_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.CLIENT_UID_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.DATABASE_KEY;
Expand Down Expand Up @@ -76,6 +77,8 @@ public class SpannerCloudMonitoringExporterTest {
private static final String locationId = "global";
private static final String databaseId = "fake-database";
private static final String clientName = "spanner-java";

private static final String clientHash = "spanner-test";
private static final String instanceConfigId = "fake-instance-config-id";

@Rule public final MockitoRule mockitoRule = MockitoJUnit.rule();
Expand All @@ -101,6 +104,7 @@ public void setUp() {
.put(INSTANCE_CONFIG_ID_KEY, instanceConfigId)
.put(DATABASE_KEY, databaseId)
.put(CLIENT_NAME_KEY, clientName)
.put(CLIENT_HASH_KEY, clientHash)
.put(String.valueOf(DIRECT_PATH_ENABLED_KEY), true)
.put(String.valueOf(DIRECT_PATH_USED_KEY), true)
.build();
Expand Down Expand Up @@ -153,9 +157,10 @@ public void testExportingSumData() {
PROJECT_ID_KEY.getKey(), projectId,
INSTANCE_ID_KEY.getKey(), instanceId,
LOCATION_ID_KEY.getKey(), locationId,
INSTANCE_CONFIG_ID_KEY.getKey(), instanceConfigId);
INSTANCE_CONFIG_ID_KEY.getKey(), instanceConfigId,
CLIENT_HASH_KEY.getKey(), clientHash);

assertThat(timeSeries.getResource().getLabelsMap()).hasSize(4);
assertThat(timeSeries.getResource().getLabelsMap()).hasSize(5);

assertThat(timeSeries.getMetric().getLabelsMap())
.containsExactly(
Expand Down Expand Up @@ -218,13 +223,14 @@ public void testExportingHistogramData() {

TimeSeries timeSeries = request.getTimeSeriesList().get(0);

assertThat(timeSeries.getResource().getLabelsMap()).hasSize(4);
assertThat(timeSeries.getResource().getLabelsMap()).hasSize(5);
assertThat(timeSeries.getResource().getLabelsMap())
.containsExactly(
PROJECT_ID_KEY.getKey(), projectId,
INSTANCE_ID_KEY.getKey(), instanceId,
LOCATION_ID_KEY.getKey(), locationId,
INSTANCE_CONFIG_ID_KEY.getKey(), instanceConfigId);
INSTANCE_CONFIG_ID_KEY.getKey(), instanceConfigId,
CLIENT_HASH_KEY.getKey(), clientHash);

assertThat(timeSeries.getMetric().getLabelsMap()).hasSize(4);
assertThat(timeSeries.getMetric().getLabelsMap())
Expand Down Expand Up @@ -296,13 +302,14 @@ public void testExportingSumDataInBatches() {
timeSeries = secondRequest.getTimeSeriesList().get(i - 200);
}

assertThat(timeSeries.getResource().getLabelsMap()).hasSize(4);
assertThat(timeSeries.getResource().getLabelsMap()).hasSize(5);
assertThat(timeSeries.getResource().getLabelsMap())
.containsExactly(
PROJECT_ID_KEY.getKey(), projectId,
INSTANCE_ID_KEY.getKey(), instanceId,
LOCATION_ID_KEY.getKey(), locationId,
INSTANCE_CONFIG_ID_KEY.getKey(), instanceConfigId);
INSTANCE_CONFIG_ID_KEY.getKey(), instanceConfigId,
CLIENT_HASH_KEY.getKey(), clientHash);

assertThat(timeSeries.getMetric().getLabelsMap()).hasSize(5);
assertThat(timeSeries.getMetric().getLabelsMap())
Expand Down

0 comments on commit 9774ab9

Please sign in to comment.