Skip to content

Commit

Permalink
add exporter and test for exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf committed Dec 10, 2021
1 parent 86b27b7 commit 11c5204
Show file tree
Hide file tree
Showing 17 changed files with 438 additions and 494 deletions.
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.bigtable.data.v2;

import com.google.api.MonitoredResource;
import com.google.api.core.ApiFunction;
import com.google.api.core.BetaApi;
import com.google.api.gax.batching.Batcher;
Expand All @@ -23,14 +24,20 @@
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.bigtable.veneer.repackaged.io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration;
import com.google.bigtable.veneer.repackaged.io.opencensus.stats.Stats;
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.stub.BigtableBatchingCallSettings;
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinViews;
import com.google.cloud.bigtable.data.v2.stub.metrics.exporter.BigtableStackdriverStatsExporter;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -70,6 +77,7 @@ public final class BigtableDataSettings {

private static final Logger LOGGER = Logger.getLogger(BigtableDataSettings.class.getName());
private static final String BIGTABLE_EMULATOR_HOST_ENV_VAR = "BIGTABLE_EMULATOR_HOST";
private static final AtomicBoolean exporterRegistered = new AtomicBoolean(false);

private final EnhancedBigtableStubSettings stubSettings;

Expand Down Expand Up @@ -242,6 +250,12 @@ public Long getBatchMutationsTargetRpcLatencyMs() {
return stubSettings.bulkMutateRowsSettings().getTargetRpcLatencyMs();
}

/** Gets if builtin metrics are published to Stackdriver. */
@BetaApi("Builtin metrics is not currently stable and may change in the future.")
public boolean isBuiltinMetricsEnabled() {
return BigtableDataSettings.exporterRegistered.get();
}

/** Returns the underlying RPC settings. */
public EnhancedBigtableStubSettings getStubSettings() {
return stubSettings;
Expand Down Expand Up @@ -466,6 +480,40 @@ public Long getTargetRpcLatencyMsForBatchMutation() {
return stubSettings.bulkMutateRowsSettings().getTargetRpcLatencyMs();
}

/** Enable publishing builtin metrics to Stackdriver. */
@BetaApi("Builtin metrics is not currently stable and may change in the future.")
public Builder enableBuiltinMetrics() throws IOException {
if (BigtableDataSettings.exporterRegistered.compareAndSet(false, true)) {
BuiltinViews.registerBigtableBuiltinViews(Stats.getViewManager());
BigtableStackdriverStatsExporter.createAndRegister(
StackdriverStatsConfiguration.builder()
.setProjectId(stubSettings.getProjectId())
.setMonitoredResource(
MonitoredResource.newBuilder().setType("bigtable_table").build())
.setMetricNamePrefix("bigtable.googleapis.com/client/")
.setExportInterval(
com.google.bigtable.veneer.repackaged.io.opencensus.common.Duration.create(
10, 0))
.build());
}
return this;
}

/** Disable publishing builtin metrics to Stackdriver. */
@BetaApi("Builtin metrics is not currently stable and may change in the future.")
public Builder disableBuiltinMetrics() {
if (BigtableDataSettings.exporterRegistered.compareAndSet(true, false)) {
BigtableStackdriverStatsExporter.unregister();
}
return this;
}

/** Gets if builtin metrics are published to Stackdriver. */
@BetaApi("Builtin metrics is not currently stable and may change in the future.")
public boolean isBuiltinMetricsEnabled() {
return BigtableDataSettings.exporterRegistered.get();
}

/**
* Returns the underlying settings for making RPC calls. The settings should be changed with
* care.
Expand Down
Expand Up @@ -57,9 +57,6 @@
import com.google.bigtable.v2.ReadRowsResponse;
import com.google.bigtable.v2.SampleRowKeysRequest;
import com.google.bigtable.v2.SampleRowKeysResponse;
import com.google.bigtable.veneer.repackaged.io.opencensus.common.Duration;
import com.google.bigtable.veneer.repackaged.io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration;
import com.google.bigtable.veneer.repackaged.io.opencensus.stats.ViewManager;
import com.google.cloud.bigtable.Version;
import com.google.cloud.bigtable.data.v2.internal.JwtCredentialsWithAudience;
import com.google.cloud.bigtable.data.v2.internal.RequestContext;
Expand All @@ -75,16 +72,14 @@
import com.google.cloud.bigtable.data.v2.models.RowMutationEntry;
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracerStreamingCallable;
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracerUnaryCallable;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMeasureConstants;
import com.google.cloud.bigtable.data.v2.stub.metrics.BuiltinMetricsTracerFactory;
import com.google.cloud.bigtable.data.v2.stub.metrics.CompositeTracerFactory;
import com.google.cloud.bigtable.data.v2.stub.metrics.MetricsTracerFactory;
import com.google.cloud.bigtable.data.v2.stub.metrics.RpcMeasureConstants;
import com.google.cloud.bigtable.data.v2.stub.metrics.StatsHeadersServerStreamingCallable;
import com.google.cloud.bigtable.data.v2.stub.metrics.StatsHeadersUnaryCallable;
import com.google.cloud.bigtable.data.v2.stub.metrics.TracedBatcherUnaryCallable;
import com.google.cloud.bigtable.data.v2.stub.metrics.builtin.BuiltinMeasureConstants;
import com.google.cloud.bigtable.data.v2.stub.metrics.builtin.BuiltinMetricsTracerFactory;
import com.google.cloud.bigtable.data.v2.stub.metrics.builtin.BuiltinViews;
import com.google.cloud.bigtable.data.v2.stub.metrics.builtin.exporter.BigtableStackdriverStatsExporter;
import com.google.cloud.bigtable.data.v2.stub.mutaterows.BulkMutateRowsUserFacingCallable;
import com.google.cloud.bigtable.data.v2.stub.mutaterows.MutateRowsBatchingDescriptor;
import com.google.cloud.bigtable.data.v2.stub.mutaterows.MutateRowsRetryingCallable;
Expand Down Expand Up @@ -113,7 +108,6 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -149,20 +143,9 @@ public class EnhancedBigtableStub implements AutoCloseable {
private final UnaryCallable<ConditionalRowMutation, Boolean> checkAndMutateRowCallable;
private final UnaryCallable<ReadModifyWriteRow, Row> readModifyWriteRowCallable;

private static AtomicBoolean exporterRegistered = new AtomicBoolean(false);

public static EnhancedBigtableStub create(EnhancedBigtableStubSettings settings)
throws IOException {
settings = finalizeSettings(settings, Tags.getTagger(), Stats.getStatsRecorder());
// TODO: register monitored resource and move project id, instance id to Monitored resource
if (EnhancedBigtableStub.exporterRegistered.compareAndSet(false, true)) {
BigtableStackdriverStatsExporter.createAndRegister(
StackdriverStatsConfiguration.builder()
.setProjectId(settings.getProjectId())
.setExportInterval(Duration.create(10, 0))
.build());
}

return new EnhancedBigtableStub(settings, ClientContext.create(settings));
}

Expand All @@ -173,16 +156,14 @@ public static EnhancedBigtableStubSettings finalizeSettings(
settings,
tagger,
stats,
com.google.bigtable.veneer.repackaged.io.opencensus.stats.Stats.getStatsRecorder(),
com.google.bigtable.veneer.repackaged.io.opencensus.stats.Stats.getViewManager());
com.google.bigtable.veneer.repackaged.io.opencensus.stats.Stats.getStatsRecorder());
}

public static EnhancedBigtableStubSettings finalizeSettings(
EnhancedBigtableStubSettings settings,
Tagger tagger,
StatsRecorder stats,
com.google.bigtable.veneer.repackaged.io.opencensus.stats.StatsRecorder builtinStats,
ViewManager builtinViewManager)
com.google.bigtable.veneer.repackaged.io.opencensus.stats.StatsRecorder builtinStats)
throws IOException {
EnhancedBigtableStubSettings.Builder builder = settings.toBuilder();

Expand Down Expand Up @@ -278,9 +259,6 @@ public static EnhancedBigtableStubSettings finalizeSettings(
builtinAttributes),
// Add user configured tracer
settings.getTracerFactory())));

BuiltinViews.registerBigtableBuiltinViews(builtinViewManager);

return builder.build();
}

Expand Down Expand Up @@ -655,8 +633,11 @@ private UnaryCallable<BulkMutation, Void> createBulkMutateRowsCallable() {

SpanName spanName = getSpanName("MutateRows");

UnaryCallable<BulkMutation, Void> tracedBatcherUnaryCallable =
new TracedBatcherUnaryCallable<>(userFacing);

UnaryCallable<BulkMutation, Void> withBigtableTracer =
new BigtableTracerUnaryCallable<>(userFacing);
new BigtableTracerUnaryCallable<>(tracedBatcherUnaryCallable);
UnaryCallable<BulkMutation, Void> traced =
new TracedUnaryCallable<>(withBigtableTracer, clientContext.getTracerFactory(), spanName);

Expand Down
Expand Up @@ -62,7 +62,7 @@ public void recordGfeMetadata(@Nullable Long latency, @Nullable Throwable throwa
public void batchRequestThrottled(long throttledTimeMs) {
// noop
}

/** Set the Bigtable zone and cluster so metrics can be tagged with location information. */
public void setLocations(String zone, String cluster) {
// noop
Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2.stub.metrics.builtin;
package com.google.cloud.bigtable.data.v2.stub.metrics;

import static com.google.bigtable.veneer.repackaged.io.opencensus.stats.Measure.MeasureLong;

Expand All @@ -30,6 +30,7 @@ public class BuiltinMeasureConstants {
public static final TagKey STREAMING = TagKey.create("streaming");
public static final TagKey STATUS = TagKey.create("status");
public static final TagKey CLIENT_NAME = TagKey.create("client_name");
public static final TagKey CLIENT_ID = TagKey.create("client_id");
public static final TagKey ERROR_CODE = TagKey.create("error_code");

// Monitored resource TagKeys
Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2.stub.metrics.builtin;
package com.google.cloud.bigtable.data.v2.stub.metrics;

import com.google.api.gax.tracing.ApiTracerFactory.OperationType;
import com.google.api.gax.tracing.SpanName;
Expand All @@ -24,8 +24,6 @@
import com.google.bigtable.veneer.repackaged.io.opencensus.tags.TagKey;
import com.google.bigtable.veneer.repackaged.io.opencensus.tags.TagValue;
import com.google.bigtable.veneer.repackaged.io.opencensus.tags.Tagger;
import com.google.cloud.bigtable.data.v2.stub.metrics.BigtableTracer;
import com.google.cloud.bigtable.data.v2.stub.metrics.Util;
import com.google.common.base.Stopwatch;
import java.util.Map;
import java.util.concurrent.CancellationException;
Expand Down Expand Up @@ -65,6 +63,10 @@ public class BuiltinMetricsTracer extends BigtableTracer {
private String zone = "undefined";
private String cluster = "undefined";

// Gfe metrics
private final AtomicLong connectivityErrorCounts = new AtomicLong(0);
private volatile long gfeLatency;

BuiltinMetricsTracer(
OperationType operationType,
Tagger tagger,
Expand Down Expand Up @@ -194,19 +196,13 @@ public int getAttempt() {

@Override
public void recordGfeMetadata(@Nullable Long latency, @Nullable Throwable throwable) {
MeasureMap measures = statsRecorder.newMeasureMap();
// Record the metrics and put in the map after the attempt is done so we can have cluster and
// zone information
if (latency != null) {
measures
.put(BuiltinMeasureConstants.SERVER_LATENCIES, latency)
.put(BuiltinMeasureConstants.CONNECTIVITY_ERROR_COUNT, 0L);
this.gfeLatency = latency;
} else {
measures.put(BuiltinMeasureConstants.CONNECTIVITY_ERROR_COUNT, 1L);
this.connectivityErrorCounts.incrementAndGet();
}
measures.record(
newTagCtxBuilder()
.putLocal(
BuiltinMeasureConstants.STATUS, TagValue.create(Util.extractStatus(throwable)))
.build());
}

@Override
Expand All @@ -219,6 +215,13 @@ public void setLocations(String zone, String cluster) {
}
}

@Override
public void batchRequestThrottled(long throttledTimeMs) {
MeasureMap measures = statsRecorder.newMeasureMap();
measures.put(BuiltinMeasureConstants.THROTTLING_LATENCIES, throttledTimeMs);
measures.record(newTagContextBuilder().build());
}

private void recordOperationCompletion(@Nullable Throwable throwable) {
if (!opFinished.compareAndSet(false, true)) {
return;
Expand Down Expand Up @@ -247,7 +250,7 @@ private void recordOperationCompletion(@Nullable Throwable throwable) {
}

TagContextBuilder tagCtx =
tagContextBuilderWithLocations()
newTagContextBuilder()
.putLocal(
BuiltinMeasureConstants.STATUS, TagValue.create(Util.extractStatus(throwable)));

Expand All @@ -265,31 +268,33 @@ private void recordAttemptCompletion(@Nullable Throwable throwable) {
.newMeasureMap()
.put(
BuiltinMeasureConstants.ATTEMPT_LATENCIES,
attemptTimer.elapsed(TimeUnit.MILLISECONDS));
attemptTimer.elapsed(TimeUnit.MILLISECONDS))
.put(BuiltinMeasureConstants.SERVER_LATENCIES, gfeLatency)
.put(BuiltinMeasureConstants.CONNECTIVITY_ERROR_COUNT, connectivityErrorCounts.get());

gfeLatency = 0;
connectivityErrorCounts.set(0);

TagContextBuilder tagCtx =
tagContextBuilderWithLocations()
newTagContextBuilder()
.putLocal(
BuiltinMeasureConstants.STATUS, TagValue.create(Util.extractStatus(throwable)));

measures.record(tagCtx.build());
}

private TagContextBuilder newTagCtxBuilder() {
private TagContextBuilder newTagContextBuilder() {
TagContextBuilder tagContextBuilder =
tagger
.toBuilder(parentContext)
.putLocal(BuiltinMeasureConstants.CLIENT_NAME, TagValue.create("bigtable-java"))
.putLocal(BuiltinMeasureConstants.METHOD, TagValue.create(spanName.toString()))
.putLocal(BuiltinMeasureConstants.TABLE, TagValue.create(tableId));
.putLocal(BuiltinMeasureConstants.TABLE, TagValue.create(tableId))
.putLocal(BuiltinMeasureConstants.ZONE, TagValue.create(zone))
.putLocal(BuiltinMeasureConstants.CLUSTER, TagValue.create(cluster));
for (Map.Entry<TagKey, TagValue> entry : statsAttributes.entrySet()) {
tagContextBuilder.putLocal(entry.getKey(), entry.getValue());
}
return tagContextBuilder;
}

private TagContextBuilder tagContextBuilderWithLocations() {
return newTagCtxBuilder()
.putLocal(BuiltinMeasureConstants.ZONE, TagValue.create(zone))
.putLocal(BuiltinMeasureConstants.CLUSTER, TagValue.create(cluster));
}
}
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.data.v2.stub.metrics.builtin;
package com.google.cloud.bigtable.data.v2.stub.metrics;

import com.google.api.core.InternalApi;
import com.google.api.gax.tracing.ApiTracer;
Expand Down

0 comments on commit 11c5204

Please sign in to comment.