From 9a2e054d6eb1a0daf2bc856397095afb4a4397df Mon Sep 17 00:00:00 2001 From: Mattie Fu Date: Mon, 11 Jul 2022 14:58:00 -0400 Subject: [PATCH] fix dependency --- google-cloud-bigtable-stats/pom.xml | 53 ++++++-- .../BigtableCreateTimeSeriesExporter.java | 10 +- .../stats/BigtableStackdriverExportUtils.java | 116 ++++++------------ .../BigtableStackdriverStatsExporter.java | 25 ++-- .../BigtableCreateTimeSeriesExporterTest.java | 2 - google-cloud-bigtable/pom.xml | 8 ++ .../data/v2/stub/EnhancedBigtableStub.java | 2 + 7 files changed, 106 insertions(+), 110 deletions(-) diff --git a/google-cloud-bigtable-stats/pom.xml b/google-cloud-bigtable-stats/pom.xml index 55e25eba1c..9ee8de9ed5 100644 --- a/google-cloud-bigtable-stats/pom.xml +++ b/google-cloud-bigtable-stats/pom.xml @@ -25,6 +25,21 @@ pom import + + io.opencensus + opencensus-exporter-metrics-util + 0.31.1 + + + com.google.cloud + google-cloud-monitoring + 3.3.1 + + + com.google.api.grpc + proto-google-cloud-monitoring-v3 + 3.3.1 + @@ -40,32 +55,52 @@ com.google.cloud google-cloud-monitoring - 3.3.1 io.opencensus opencensus-api + + com.google.auth + google-auth-library-credentials + + + com.google.api.grpc + proto-google-cloud-monitoring-v3 + + + com.google.api + gax-grpc + + + org.threeten + threetenbp + + + com.google.protobuf + protobuf-java + io.opencensus - opencensus-exporter-stats-stackdriver - - - com.google.cloud - google-cloud-monitoring - - + opencensus-exporter-metrics-util com.google.guava guava - + + com.google.api.grpc + proto-google-common-protos + io.opencensus opencensus-impl runtime + + com.google.code.findbugs + jsr305 + com.google.truth truth diff --git a/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java b/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java index 268ae03348..eec415f46c 100644 --- a/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java +++ b/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporter.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 Google LLC + * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,10 +34,12 @@ final class BigtableCreateTimeSeriesExporter extends MetricExporter { private static final Logger logger = Logger.getLogger(BigtableCreateTimeSeriesExporter.class.getName()); + + private static final String DOMAIN = "bigtable.googleapis.com/internal/client/"; + private final ProjectName projectName; private final MetricServiceClient metricServiceClient; private final MonitoredResource monitoredResource; - private final String domain; BigtableCreateTimeSeriesExporter( String projectId, @@ -46,7 +48,6 @@ final class BigtableCreateTimeSeriesExporter extends MetricExporter { this.projectName = ProjectName.newBuilder().setProject(projectId).build(); this.metricServiceClient = metricServiceClient; this.monitoredResource = monitoredResource; - this.domain = "bigtable.googleapis.com/client/"; } public void export(Collection metrics) { @@ -101,8 +102,7 @@ public void export(Collection metrics) { updatedValues, timeSeries, monitoredResourceBuilder.build(), - this.domain, - this.projectName.getProject())); + DOMAIN)); } } diff --git a/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverExportUtils.java b/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverExportUtils.java index ba4e49d6ea..d45f26d52a 100644 --- a/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverExportUtils.java +++ b/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverExportUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 Google LLC + * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,9 +26,7 @@ import com.google.monitoring.v3.TypedValue; import com.google.protobuf.Any; import com.google.protobuf.ByteString; -import io.opencensus.common.Function; import io.opencensus.common.Functions; -import io.opencensus.contrib.exemplar.util.AttachmentValueSpanContext; import io.opencensus.metrics.LabelKey; import io.opencensus.metrics.LabelValue; import io.opencensus.metrics.data.AttachmentValue; @@ -115,8 +113,7 @@ static TimeSeries convertTimeSeries( List labelValues, io.opencensus.metrics.export.TimeSeries timeSeries, MonitoredResource monitoredResource, - String domain, - String projectId) { + String domain) { TimeSeries.Builder builder = TimeSeries.newBuilder(); builder.setMetricKind(createMetricKind(metricType)); @@ -138,9 +135,9 @@ static com.google.api.Metric createMetric( Map stringTagMap = Maps.newHashMap(); for (int i = 0; i < labelValues.size(); ++i) { - String value = ((LabelValue) labelValues.get(i)).getValue(); + String value = labelValues.get(i).getValue(); if (value != null) { - stringTagMap.put(((LabelKey) labelKeys.get(i)).getKey(), value); + stringTagMap.put(labelKeys.get(i).getKey(), value); } } @@ -166,13 +163,12 @@ static com.google.monitoring.v3.Point createPoint( @VisibleForTesting static TypedValue createTypedValue(Value value) { - return (TypedValue) - value.match( - typedValueDoubleFunction, - typedValueLongFunction, - typedValueDistributionFunction, - typedValueSummaryFunction, - Functions.throwIllegalArgumentException()); + return value.match( + typedValueDoubleFunction, + typedValueLongFunction, + typedValueDistributionFunction, + typedValueSummaryFunction, + Functions.throwIllegalArgumentException()); } @VisibleForTesting @@ -197,9 +193,8 @@ static BucketOptions createBucketOptions( com.google.api.Distribution.BucketOptions.Builder builder = BucketOptions.newBuilder(); return bucketOptions == null ? builder.build() - : (BucketOptions) - bucketOptions.match( - bucketOptionsExplicitFunction, Functions.throwIllegalArgumentException()); + : bucketOptions.match( + bucketOptionsExplicitFunction, Functions.throwIllegalArgumentException()); } static MonitoredResource getDefaultResource() { @@ -227,16 +222,11 @@ private static com.google.api.Distribution.Exemplar toProtoExemplar( com.google.api.Distribution.Exemplar.newBuilder() .setValue(exemplar.getValue()) .setTimestamp(convertTimestamp(exemplar.getTimestamp())); - io.opencensus.trace.SpanContext spanContext = null; for (Map.Entry attachment : exemplar.getAttachments().entrySet()) { - String key = (String) attachment.getKey(); - AttachmentValue value = (AttachmentValue) attachment.getValue(); - if ("SpanContext".equals(key)) { - spanContext = ((AttachmentValueSpanContext) value).getSpanContext(); - } else { - builder.addAttachments(toProtoStringAttachment(value)); - } + String key = attachment.getKey(); + AttachmentValue value = attachment.getValue(); + builder.addAttachments(toProtoStringAttachment(value)); } return builder.build(); @@ -249,25 +239,6 @@ private static Any toProtoStringAttachment(AttachmentValue attachmentValue) { .build(); } - private static Any toProtoSpanContextAttachment( - com.google.monitoring.v3.SpanContext protoSpanContext) { - return Any.newBuilder() - .setTypeUrl("type.googleapis.com/google.monitoring.v3.SpanContext") - .setValue(protoSpanContext.toByteString()) - .build(); - } - - private static com.google.monitoring.v3.SpanContext toProtoSpanContext( - io.opencensus.trace.SpanContext spanContext, String projectId) { - String spanName = - String.format( - "projects/%s/traces/%s/spans/%s", - projectId, - spanContext.getTraceId().toLowerBase16(), - spanContext.getSpanId().toLowerBase16()); - return com.google.monitoring.v3.SpanContext.newBuilder().setSpanName(spanName).build(); - } - @VisibleForTesting static com.google.protobuf.Timestamp convertTimestamp( io.opencensus.common.Timestamp censusTimestamp) { @@ -284,48 +255,37 @@ private BigtableStackdriverExportUtils() {} static { logger = Logger.getLogger(BigtableStackdriverExportUtils.class.getName()); typedValueDoubleFunction = - new io.opencensus.common.Function() { - public TypedValue apply(Double arg) { - com.google.monitoring.v3.TypedValue.Builder builder = TypedValue.newBuilder(); - builder.setDoubleValue(arg); - return builder.build(); - } + arg -> { + TypedValue.Builder builder = TypedValue.newBuilder(); + builder.setDoubleValue(arg); + return builder.build(); }; typedValueLongFunction = - new io.opencensus.common.Function() { - public TypedValue apply(Long arg) { - com.google.monitoring.v3.TypedValue.Builder builder = TypedValue.newBuilder(); - builder.setInt64Value(arg); - return builder.build(); - } + arg -> { + TypedValue.Builder builder = TypedValue.newBuilder(); + builder.setInt64Value(arg); + return builder.build(); }; typedValueDistributionFunction = - new io.opencensus.common.Function() { - public TypedValue apply(io.opencensus.metrics.export.Distribution arg) { - com.google.monitoring.v3.TypedValue.Builder builder = TypedValue.newBuilder(); - return builder - .setDistributionValue(BigtableStackdriverExportUtils.createDistribution(arg)) - .build(); - } + arg -> { + TypedValue.Builder builder = TypedValue.newBuilder(); + return builder + .setDistributionValue(BigtableStackdriverExportUtils.createDistribution(arg)) + .build(); }; typedValueSummaryFunction = - new io.opencensus.common.Function() { - public TypedValue apply(Summary arg) { - com.google.monitoring.v3.TypedValue.Builder builder = TypedValue.newBuilder(); - return builder.build(); - } + arg -> { + TypedValue.Builder builder = TypedValue.newBuilder(); + return builder.build(); }; bucketOptionsExplicitFunction = - new Function() { - public BucketOptions apply(ExplicitOptions arg) { - com.google.api.Distribution.BucketOptions.Builder builder = BucketOptions.newBuilder(); - com.google.api.Distribution.BucketOptions.Explicit.Builder explicitBuilder = - Explicit.newBuilder(); - explicitBuilder.addBounds(0.0D); - explicitBuilder.addAllBounds(arg.getBucketBoundaries()); - builder.setExplicitBuckets(explicitBuilder.build()); - return builder.build(); - } + arg -> { + BucketOptions.Builder builder = BucketOptions.newBuilder(); + Explicit.Builder explicitBuilder = Explicit.newBuilder(); + explicitBuilder.addBounds(0.0D); + explicitBuilder.addAllBounds(arg.getBucketBoundaries()); + builder.setExplicitBuckets(explicitBuilder.build()); + return builder.build(); }; } } diff --git a/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverStatsExporter.java b/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverStatsExporter.java index c9c3474866..03dfc465e4 100644 --- a/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverStatsExporter.java +++ b/google-cloud-bigtable-stats/src/main/java/com/google/cloud/bigtable/stats/BigtableStackdriverStatsExporter.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 Google LLC + * Copyright 2022 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,17 +36,14 @@ @ThreadSafe @InternalApi public class BigtableStackdriverStatsExporter { - @VisibleForTesting static final Object monitor = new Object(); + static final Object monitor = new Object(); @Nullable @GuardedBy("monitor") private static BigtableStackdriverStatsExporter instance = null; - private static final String EXPORTER_SPAN_NAME = "ExportMetricsToStackdriver"; - // private static final String USER_AGENT_KEY = "user-agent"; - // private static final String USER_AGENT = "bigtable-java" + Version.VERSION; - // private static final HeaderProvider OPENCENSUS_USER_AGENT_HEADER_PROVIDER = - // FixedHeaderProvider.create(new String[] {USER_AGENT_KEY, USER_AGENT}); + private static final String EXPORTER_SPAN_NAME = "BigtableExportMetricsToStackdriver"; + private static final Duration EXPORT_INTERVAL = Duration.create(600, 0); private final IntervalMetricReader intervalMetricReader; private BigtableStackdriverStatsExporter( @@ -72,13 +69,14 @@ private BigtableStackdriverStatsExporter( public static void register(@Nullable Credentials credentials, String projectId) throws IOException { synchronized (monitor) { - Preconditions.checkState(instance == null, "Stackdriver stats exporter is already created"); + Preconditions.checkState( + instance == null, "Bigtable Stackdriver stats exporter is already created"); MetricServiceClient client = createMetricServiceClient(credentials, Duration.create(60L, 0)); instance = new BigtableStackdriverStatsExporter( projectId, client, - Duration.create(600, 0), + EXPORT_INTERVAL, BigtableStackdriverExportUtils.getDefaultResource()); } } @@ -88,13 +86,8 @@ public static void register(@Nullable Credentials credentials, String projectId) static MetricServiceClient createMetricServiceClient( @Nullable Credentials credentials, Duration deadline) throws IOException { com.google.cloud.monitoring.v3.MetricServiceSettings.Builder settingsBuilder = - (com.google.cloud.monitoring.v3.MetricServiceSettings.Builder) - MetricServiceSettings.newBuilder() - .setTransportChannelProvider( - InstantiatingGrpcChannelProvider.newBuilder() - // - // .setHeaderProvider(OPENCENSUS_USER_AGENT_HEADER_PROVIDER) - .build()); + MetricServiceSettings.newBuilder() + .setTransportChannelProvider(InstantiatingGrpcChannelProvider.newBuilder().build()); if (credentials != null) { settingsBuilder.setCredentialsProvider(FixedCredentialsProvider.create(credentials)); } diff --git a/google-cloud-bigtable-stats/src/test/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporterTest.java b/google-cloud-bigtable-stats/src/test/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporterTest.java index 931325591a..25b5a0b97e 100644 --- a/google-cloud-bigtable-stats/src/test/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporterTest.java +++ b/google-cloud-bigtable-stats/src/test/java/com/google/cloud/bigtable/stats/BigtableCreateTimeSeriesExporterTest.java @@ -21,8 +21,6 @@ import com.google.api.MonitoredResource; import com.google.api.gax.rpc.UnaryCallable; -import com.google.cloud.bigtable.stats.BigtableCreateTimeSeriesExporter; -import com.google.cloud.bigtable.stats.BuiltinMeasureConstants; import com.google.cloud.monitoring.v3.MetricServiceClient; import com.google.cloud.monitoring.v3.stub.MetricServiceStub; import com.google.monitoring.v3.CreateTimeSeriesRequest; diff --git a/google-cloud-bigtable/pom.xml b/google-cloud-bigtable/pom.xml index 8a3028af21..99c5cd491a 100644 --- a/google-cloud-bigtable/pom.xml +++ b/google-cloud-bigtable/pom.xml @@ -60,9 +60,17 @@ + com.google.cloud google-cloud-bigtable-stats + + + + io.opencensus + * + + diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java index ec237aabf7..2c7880ea41 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStub.java @@ -90,6 +90,7 @@ import com.google.cloud.bigtable.data.v2.stub.readrows.ReadRowsUserCallable; import com.google.cloud.bigtable.data.v2.stub.readrows.RowMergingCallable; import com.google.cloud.bigtable.gaxx.retrying.ApiResultRetryAlgorithm; +import com.google.cloud.bigtable.stats.BuiltinViews; import com.google.common.base.MoreObjects; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; @@ -228,6 +229,7 @@ public static EnhancedBigtableStubSettings finalizeSettings( BuiltinMetricsTracerFactory.create(builtinAttributes), // Add user configured tracer settings.getTracerFactory()))); + BuiltinViews.registerBigtableBuiltinViews(); return builder.build(); }