From 1af8925747c22f4e8d50a76dc60f6fafddac20d2 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Tue, 13 Apr 2021 14:01:48 -0400 Subject: [PATCH] chore: improve embedded version handling (#715) * chore: improve embedded version handling Use new feature in releasetool (https://github.com/googleapis/releasetool/pull/317) to manage the client version instead of maven tricks introduced in https://github.com/googleapis/java-bigtable/pull/451 * migrate to new version scheme * rename back to Version * config release-please for version bumps * make sure file doesnt get clobbered * add a couple of tests * fmt --- .github/release-please.yml | 4 +- google-cloud-bigtable/pom.xml | 10 --- .../com/google/cloud/bigtable/Version.java | 7 +- .../data/v2/stub/EnhancedBigtableStub.java | 6 +- .../google/cloud/bigtable/VersionTest.java | 33 ++++++++++ .../v2/stub/EnhancedBigtableStubTest.java | 66 +++++++++++++++++++ synth.py | 2 + 7 files changed, 110 insertions(+), 18 deletions(-) rename google-cloud-bigtable/src/main/{templates => java}/com/google/cloud/bigtable/Version.java (80%) create mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/VersionTest.java diff --git a/.github/release-please.yml b/.github/release-please.yml index dce2c84509..7442886ff0 100644 --- a/.github/release-please.yml +++ b/.github/release-please.yml @@ -1,2 +1,4 @@ releaseType: java-yoshi -bumpMinorPreMajor: true \ No newline at end of file +bumpMinorPreMajor: true +extraFiles: + - google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/ClientVersion.java diff --git a/google-cloud-bigtable/pom.xml b/google-cloud-bigtable/pom.xml index 8c9b525b9d..34007cdd7a 100644 --- a/google-cloud-bigtable/pom.xml +++ b/google-cloud-bigtable/pom.xml @@ -427,16 +427,6 @@ - - - src/main/templates - - **/*.java - - true - ${project.build.directory}/generated-sources/java - - org.codehaus.mojo diff --git a/google-cloud-bigtable/src/main/templates/com/google/cloud/bigtable/Version.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/Version.java similarity index 80% rename from google-cloud-bigtable/src/main/templates/com/google/cloud/bigtable/Version.java rename to google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/Version.java index 7f87f249dc..9a18460b1d 100644 --- a/google-cloud-bigtable/src/main/templates/com/google/cloud/bigtable/Version.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/Version.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Google LLC + * Copyright 2021 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,7 @@ @InternalApi("For internal use only") public final class Version { - // The released version, populated by maven. - public static String VERSION = "${java-bigtable.version}"; + // {x-version-update-start:google-cloud-bigtable:current} + public static String VERSION = "1.22.1-SNAPSHOT"; + // {x-version-update-end} } 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 9f52ddd8de..c08f0aec1e 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 @@ -22,7 +22,6 @@ import com.google.api.gax.batching.FlowController; import com.google.api.gax.core.BackgroundResource; import com.google.api.gax.core.FixedCredentialsProvider; -import com.google.api.gax.core.GaxProperties; import com.google.api.gax.grpc.GaxGrpcProperties; import com.google.api.gax.grpc.GrpcCallSettings; import com.google.api.gax.grpc.GrpcRawCallableFactory; @@ -55,6 +54,7 @@ import com.google.bigtable.v2.ReadRowsResponse; import com.google.bigtable.v2.SampleRowKeysRequest; import com.google.bigtable.v2.SampleRowKeysResponse; +import com.google.cloud.bigtable.Version; import com.google.cloud.bigtable.data.v2.internal.RequestContext; import com.google.cloud.bigtable.data.v2.models.BulkMutation; import com.google.cloud.bigtable.data.v2.models.ConditionalRowMutation; @@ -199,9 +199,7 @@ public static EnhancedBigtableStubSettings finalizeSettings( // Also annotate traces with library versions .put("gax", GaxGrpcProperties.getGaxGrpcVersion()) .put("grpc", GaxGrpcProperties.getGrpcVersion()) - .put( - "gapic", - GaxProperties.getLibraryVersion(EnhancedBigtableStubSettings.class)) + .put("gapic", Version.VERSION) .build()), // Add OpenCensus Metrics MetricsTracerFactory.create(tagger, stats, attributes), diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/VersionTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/VersionTest.java new file mode 100644 index 0000000000..e07f7a4a5a --- /dev/null +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/VersionTest.java @@ -0,0 +1,33 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.cloud.bigtable; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Smoke test to ensure that release tooling doesn't accidentally corrupt the version */ +@RunWith(JUnit4.class) +public class VersionTest { + @Test + public void testVersion() { + assertThat(Version.VERSION).matches("\\d+\\.\\d+\\.\\d(?:-SNAPSHOT)?"); + + assertThat(Version.VERSION).isGreaterThan("1.22.0"); + } +} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubTest.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubTest.java index 804d51a2e6..b66596fb1a 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubTest.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubTest.java @@ -22,11 +22,13 @@ import com.google.api.gax.batching.FlowControlSettings; import com.google.api.gax.batching.FlowController.LimitExceededBehavior; import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.grpc.GaxGrpcProperties; import com.google.api.gax.rpc.ServerStreamingCallable; import com.google.bigtable.v2.BigtableGrpc; import com.google.bigtable.v2.ReadRowsRequest; import com.google.bigtable.v2.ReadRowsResponse; import com.google.bigtable.v2.RowSet; +import com.google.cloud.bigtable.Version; import com.google.cloud.bigtable.admin.v2.internal.NameUtil; import com.google.cloud.bigtable.data.v2.BigtableDataSettings; import com.google.cloud.bigtable.data.v2.FakeServiceHelper; @@ -43,8 +45,17 @@ import io.grpc.ServerCall.Listener; import io.grpc.ServerCallHandler; import io.grpc.ServerInterceptor; +import io.grpc.internal.GrpcUtil; import io.grpc.stub.StreamObserver; +import io.opencensus.common.Scope; +import io.opencensus.trace.AttributeValue; +import io.opencensus.trace.Tracing; +import io.opencensus.trace.export.SpanData; +import io.opencensus.trace.export.SpanExporter.Handler; +import io.opencensus.trace.samplers.Samplers; import java.io.IOException; +import java.util.Collection; +import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.TimeUnit; import org.junit.After; @@ -157,6 +168,61 @@ public void testUserAgent() throws InterruptedException { .containsMatch("bigtable-java/\\d+\\.\\d+\\.\\d+(?:-SNAPSHOT)?"); } + @Test + public void testSpanAttributes() throws InterruptedException { + final BlockingQueue spans = new ArrayBlockingQueue<>(100); + + // inject a temporary trace exporter + String handlerName = "stub-test-exporter"; + + Tracing.getExportComponent() + .getSpanExporter() + .registerHandler( + handlerName, + new Handler() { + @Override + public void export(Collection collection) { + spans.addAll(collection); + } + }); + + SpanData foundSpanData = null; + // Issue the rpc and grab the span + try { + try (Scope ignored = + Tracing.getTracer() + .spanBuilder("fake-parent-span") + .setSampler(Samplers.alwaysSample()) + .startScopedSpan()) { + enhancedBigtableStub.readRowCallable().call(Query.create("table-id").rowKey("row-key")); + } + + for (int i = 0; i < 100; i++) { + SpanData spanData = spans.poll(10, TimeUnit.SECONDS); + if ("Bigtable.ReadRow".equals(spanData.getName())) { + foundSpanData = spanData; + break; + } + } + } finally { + // cleanup + Tracing.getExportComponent().getSpanExporter().unregisterHandler(handlerName); + } + + // Examine the caught span + assertThat(foundSpanData).isNotNull(); + assertThat(foundSpanData.getAttributes().getAttributeMap()) + .containsEntry("gapic", AttributeValue.stringAttributeValue(Version.VERSION)); + assertThat(foundSpanData.getAttributes().getAttributeMap()) + .containsEntry( + "grpc", + AttributeValue.stringAttributeValue( + GrpcUtil.getGrpcBuildVersion().getImplementationVersion())); + assertThat(foundSpanData.getAttributes().getAttributeMap()) + .containsEntry( + "gax", AttributeValue.stringAttributeValue(GaxGrpcProperties.getGaxGrpcVersion())); + } + @Test public void testBulkMutationFlowControllerConfigured() throws Exception { BigtableDataSettings.Builder settings = diff --git a/synth.py b/synth.py index da5f09cf0f..4e48e756b0 100644 --- a/synth.py +++ b/synth.py @@ -43,6 +43,8 @@ def main(): 'CONTRIBUTING.md', # exclude autogen 'codecov.yaml' + # needed for extraFiles + '.github/release-please.yml', ]) def generate_data_api(gapic):