Skip to content

Commit

Permalink
Pass client name and version directly to StatsRecorderWrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkaregar committed Jan 25, 2024
1 parent 564c05c commit bc389d2
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.opencensus.tags.TagKey;

/** Built-in metrics that will be readable under bigtable.googleapis.com/client namespace */
public class BuiltinMeasureConstants {
class BuiltinMeasureConstants {
// Monitored resource TagKeys
static final TagKey PROJECT_ID = TagKey.create("project_id");
static final TagKey INSTANCE_ID = TagKey.create("instance");
Expand All @@ -34,8 +34,7 @@ public class BuiltinMeasureConstants {
static final TagKey METHOD = TagKey.create("method");
static final TagKey STREAMING = TagKey.create("streaming");
static final TagKey STATUS = TagKey.create("status");
static final TagKey CLIENT_NAME = TagKey.create("client_name");
public static final TagKey CLIENT_VERSION = TagKey.create("client_version");
public static final TagKey CLIENT_NAME = TagKey.create("client_name");

// Units
private static final String COUNT = "1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.opencensus.tags.Tagger;
import io.opencensus.tags.Tags;
import java.util.Map;
import java.util.Objects;

/** A wrapper to record built-in metrics */
@InternalApi("For internal use only")
Expand Down Expand Up @@ -124,21 +123,12 @@ private TagContextBuilder newTagContextBuilder(String tableId, String zone, Stri
TagContextBuilder tagContextBuilder =
tagger
.toBuilder(parentContext)
.putLocal(
BuiltinMeasureConstants.CLIENT_NAME,
TagValue.create(
"bigtable-java/"
+ statsAttributes.get(BuiltinMeasureConstants.CLIENT_VERSION.getName())))
.putLocal(BuiltinMeasureConstants.METHOD, TagValue.create(spanName.toString()))
.putLocal(BuiltinMeasureConstants.TABLE, TagValue.create(tableId))
.putLocal(BuiltinMeasureConstants.ZONE, TagValue.create(zone))
.putLocal(BuiltinMeasureConstants.CLUSTER, TagValue.create(cluster));
for (Map.Entry<String, String> entry : statsAttributes.entrySet()) {
// Client version is appended to the client name to keep metric attributes constant.
if (!Objects.equals(entry.getKey(), BuiltinMeasureConstants.CLIENT_VERSION.getName())) {
tagContextBuilder.putLocal(
TagKey.create(entry.getKey()), TagValue.create(entry.getValue()));
}
tagContextBuilder.putLocal(TagKey.create(entry.getKey()), TagValue.create(entry.getValue()));
}
return tagContextBuilder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ public class StatsRecorderWrapperTest {
private final String TABLE_ID = "fake-table-id";
private final String ZONE = "fake-zone";
private final String CLUSTER = "fake-cluster";

private final String VERSION = "fake-version";
private final String CLIENT_AND_VERSION = "bigtable-java/fake-version";

private final StatsComponent statsComponent = new StatsComponentImpl();
Expand All @@ -76,8 +74,8 @@ public void testStreamingOperation() throws InterruptedException {
INSTANCE_ID,
BuiltinMeasureConstants.APP_PROFILE.getName(),
APP_PROFILE_ID,
BuiltinMeasureConstants.CLIENT_VERSION.getName(),
VERSION),
BuiltinMeasureConstants.CLIENT_NAME.getName(),
CLIENT_AND_VERSION),
statsComponent.getStatsRecorder());

long operationLatency = 1234;
Expand Down Expand Up @@ -277,7 +275,7 @@ public void testUnaryOperations() throws InterruptedException {
BuiltinMeasureConstants.PROJECT_ID.getName(), PROJECT_ID,
BuiltinMeasureConstants.INSTANCE_ID.getName(), INSTANCE_ID,
BuiltinMeasureConstants.APP_PROFILE.getName(), APP_PROFILE_ID,
BuiltinMeasureConstants.CLIENT_VERSION.getName(), VERSION),
BuiltinMeasureConstants.CLIENT_NAME.getName(), CLIENT_AND_VERSION),
statsComponent.getStatsRecorder());

long operationLatency = 1234;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ public static ApiTracerFactory createBigtableTracerFactory(
.put("project_id", projectId)
.put("instance", instanceId)
.put("app_profile", appProfileId)
.put("client_name", "bigtable-java/" + Version.VERSION)
.build();

return new CompositeTracerFactory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.google.api.gax.tracing.ApiTracerFactory;
import com.google.api.gax.tracing.BaseApiTracerFactory;
import com.google.api.gax.tracing.SpanName;
import com.google.cloud.bigtable.Version;
import com.google.cloud.bigtable.stats.BuiltinMeasureConstants;
import com.google.cloud.bigtable.stats.StatsWrapper;
import com.google.common.collect.ImmutableMap;

Expand All @@ -39,16 +37,7 @@ public static BuiltinMetricsTracerFactory create(ImmutableMap<String, String> st
}

private BuiltinMetricsTracerFactory(ImmutableMap<String, String> statsAttributes) {
this.statsAttributes = addClientVersionToAttributes(statsAttributes);
}

private ImmutableMap<String, String> addClientVersionToAttributes(
ImmutableMap<String, String> statsAttributes) {
String clientVersionKey = BuiltinMeasureConstants.CLIENT_VERSION.getName();
return new ImmutableMap.Builder<String, String>()
.putAll(statsAttributes)
.put(clientVersionKey, Version.VERSION)
.build();
this.statsAttributes = statsAttributes;
}

@Override
Expand Down

0 comments on commit bc389d2

Please sign in to comment.