Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
debafec
impl(o11y): introduce gcp.client.version
diegomarquezp Mar 13, 2026
ab32cc6
test: update goldens
diegomarquezp Mar 13, 2026
21647c7
chore: regenerate showcase
diegomarquezp Mar 13, 2026
8e9336f
test: add region tags for showcase test
diegomarquezp Mar 13, 2026
9b63214
chore: format
diegomarquezp Mar 13, 2026
b892bb9
test: add test for ApiTracerContextTest
diegomarquezp Mar 13, 2026
73c81b0
chore: update goldens
diegomarquezp Mar 13, 2026
bc8ce53
build: update release please config
diegomarquezp Mar 13, 2026
e90158d
test: update bazel goldens
diegomarquezp Mar 13, 2026
7c5d731
chore: lint generator
diegomarquezp Mar 13, 2026
16eac3a
test: restore test case
diegomarquezp Mar 13, 2026
18da4eb
build: ignore Version.java changes by generator
diegomarquezp Mar 13, 2026
6028a0d
Merge branch 'main' into observability/tracing-attr/gcp.client.version-2
diegomarquezp Mar 13, 2026
baf4332
Merge branch 'observability/tracing-attr/gcp.client.version-2' of htt…
diegomarquezp Mar 13, 2026
954826d
fix: generate Version.java as package private
diegomarquezp Mar 16, 2026
1317237
fix: revert unnecessary changes
diegomarquezp Mar 16, 2026
61f383d
docs: do not use snapshot version in javadoc
diegomarquezp Mar 16, 2026
a47349d
Merge remote-tracking branch 'origin/main' into observability/tracing…
diegomarquezp Mar 16, 2026
5056ffb
fix: use setVersion instead of setLibraryVersion
diegomarquezp Mar 16, 2026
151cda2
chore: update showcase
diegomarquezp Mar 16, 2026
2ebbc44
test: do not test version in showcase
diegomarquezp Mar 16, 2026
4cffbfe
build: update owlbot yaml template
diegomarquezp Mar 16, 2026
e1dd69f
build: update rlease please config
diegomarquezp Mar 16, 2026
78545ff
test: update bazel goldens
diegomarquezp Mar 16, 2026
529d91c
chore: regenerate showcase
diegomarquezp Mar 17, 2026
400f7df
fix: use stub package for vesion
diegomarquezp Mar 17, 2026
6bf6efb
test: update it goldens
diegomarquezp Mar 17, 2026
abb8dde
Merge remote-tracking branch 'origin/main' into observability/tracing…
diegomarquezp Mar 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public static List<GapicClass> generateServiceClasses(GapicContext context) {
clazzes.addAll(generateClientSettingsClasses(context));
clazzes.addAll(generateMockClasses(context, context.services()));
clazzes.addAll(generateTestClasses(context));
clazzes.addAll(generateVersionClasses(context));
return clazzes;
}

Expand Down Expand Up @@ -197,6 +198,15 @@ public static List<GapicClass> generateTestClasses(GapicContext context) {
return clazzes;
}

public static List<GapicClass> generateVersionClasses(GapicContext context) {
return context.services().stream()
.collect(Collectors.toMap(Service::pakkage, s -> s, (s1, s2) -> s1))
.values()
.stream()
.map(service -> LibraryVersionClassComposer.instance().generate(context, service))
.collect(Collectors.toList());
}

@VisibleForTesting
static List<GapicClass> prepareExecutableSamples(List<GapicClass> clazzes) {
// Include license header, apiShortName, and apiVersion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2026 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
//
// http://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.api.generator.gapic.composer;

import com.google.api.generator.engine.ast.AnnotationNode;
import com.google.api.generator.engine.ast.AssignmentExpr;
import com.google.api.generator.engine.ast.ClassDefinition;
import com.google.api.generator.engine.ast.CommentStatement;
import com.google.api.generator.engine.ast.ConcreteReference;
import com.google.api.generator.engine.ast.ExprStatement;
import com.google.api.generator.engine.ast.LineComment;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.StringObjectValue;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.Service;
import java.util.Arrays;

public class LibraryVersionClassComposer {
private static final LibraryVersionClassComposer INSTANCE = new LibraryVersionClassComposer();

private LibraryVersionClassComposer() {}

public static LibraryVersionClassComposer instance() {
return INSTANCE;
}

public GapicClass generate(GapicContext context, Service service) {
String packageName = service.pakkage() + ".stub";
String className = "Version";

String artifact = context.artifact();
String artifactId = artifact;
if (artifact != null && artifact.contains(":")) {
artifactId = artifact.split(":")[1];
}

VariableExpr versionVarExpr =
VariableExpr.withVariable(
Variable.builder().setType(TypeNode.STRING).setName("VERSION").build());
AssignmentExpr versionAssignmentExpr =
AssignmentExpr.builder()
.setVariableExpr(
versionVarExpr.toBuilder()
.setIsDecl(true)
.setScope(ScopeNode.PUBLIC)
.setIsStatic(true)
.setIsFinal(true)
.build())
.setValueExpr(ValueExpr.withValue(StringObjectValue.withValue("0.0.0-SNAPSHOT")))
.build();

ClassDefinition classDef =
ClassDefinition.builder()
.setPackageString(packageName)
.setAnnotations(
Arrays.asList(
AnnotationNode.builder()
.setType(
TypeNode.withReference(
ConcreteReference.withClazz(com.google.api.core.InternalApi.class)))
.setDescription("For internal use only")
.build()))
.setScope(ScopeNode.LOCAL)
.setIsFinal(true)
.setName(className)
.setStatements(
Arrays.asList(
CommentStatement.withComment(
LineComment.withComment(
String.format("{x-version-update-start:%s:current}", artifactId))),
ExprStatement.withExpr(versionAssignmentExpr),
CommentStatement.withComment(
LineComment.withComment("{x-version-update-end}"))))
.build();

return GapicClass.create(GapicClass.Kind.MAIN, classDef);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ private List<MethodDefinition> createClassMethods(
SettingsCommentComposer.NEW_BUILDER_METHOD_COMMENT));
javaMethods.addAll(createBuilderHelperMethods(service, typeStore));
javaMethods.add(createClassConstructor(service, methodSettingsMemberVarExprs, typeStore));
javaMethods.add(createGetLibraryMetadataMethod(context));
javaMethods.add(createGetLibraryMetadataMethod(context, service));
return javaMethods;
}

Expand Down Expand Up @@ -2107,7 +2107,7 @@ private static MethodDefinition createNestedClassBuildMethod(
.build();
}

private MethodDefinition createGetLibraryMetadataMethod(GapicContext context) {
private MethodDefinition createGetLibraryMetadataMethod(GapicContext context, Service service) {
TypeNode returnType = FIXED_TYPESTORE.get("LibraryMetadata");
MethodInvocationExpr libraryMetadataBuilderExpr =
MethodInvocationExpr.builder()
Expand All @@ -2133,6 +2133,23 @@ private MethodDefinition createGetLibraryMetadataMethod(GapicContext context) {
.build();
}

libraryMetadataBuilderExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(libraryMetadataBuilderExpr)
.setMethodName("setVersion")
.setArguments(
VariableExpr.builder()
.setStaticReferenceType(
TypeNode.withReference(
VaporReference.builder()
.setName("Version")
.setPakkage(String.format("%s.stub", service.pakkage()))
.build()))
.setVariable(
Variable.builder().setName("VERSION").setType(TypeNode.STRING).build())
.build())
.build();

Expr returnExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(libraryMetadataBuilderExpr)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2026 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
//
// http://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.api.generator.gapic.composer;

import com.google.api.generator.gapic.model.GapicClass;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.test.framework.Assert;
import com.google.api.generator.test.protoloader.GrpcTestProtoLoader;
import org.junit.Test;

public class LibraryVersionClassComposerTest {

@Test
public void generateVersionClasses() {
GapicContext context = GrpcTestProtoLoader.instance().parseShowcaseEcho();
Service service = context.services().get(0);
GapicClass clazz = LibraryVersionClassComposer.instance().generate(context, service);

Assert.assertGoldenClass(this.getClass(), clazz, "EchoVersion.golden");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.google.showcase.v1beta1.stub;

import com.google.api.core.InternalApi;

@InternalApi("For internal use only")
final class Version {
// {x-version-update-start:gapic-showcase:current}
public static final String VERSION = "0.0.0-SNAPSHOT";
// {x-version-update-end}

}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public class EchoWithVersionStubSettings extends StubSettings<EchoWithVersionStu

@Override
protected LibraryMetadata getLibraryMetadata() {
return LibraryMetadata.newBuilder().build();
return LibraryMetadata.newBuilder().setVersion(Version.VERSION).build();
}

/** Builder for EchoWithVersionStubSettings. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public class DeprecatedServiceStubSettings extends StubSettings<DeprecatedServic

@Override
protected LibraryMetadata getLibraryMetadata() {
return LibraryMetadata.newBuilder().build();
return LibraryMetadata.newBuilder().setVersion(Version.VERSION).build();
}

/** Builder for DeprecatedServiceStubSettings. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public class EchoServiceShouldGeneratePartialUsualStubSettings

@Override
protected LibraryMetadata getLibraryMetadata() {
return LibraryMetadata.newBuilder().build();
return LibraryMetadata.newBuilder().setVersion(Version.VERSION).build();
}

/** Builder for EchoServiceShouldGeneratePartialUsualStubSettings. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public class EchoStubSettings extends StubSettings<EchoStubSettings> {
return LibraryMetadata.newBuilder()
.setArtifactName("com.google.cloud:gapic-showcase")
.setRepository("googleapis/sdk-platform-java")
.setVersion(Version.VERSION)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public class JobServiceStubSettings extends StubSettings<JobServiceStubSettings>

@Override
protected LibraryMetadata getLibraryMetadata() {
return LibraryMetadata.newBuilder().build();
return LibraryMetadata.newBuilder().setVersion(Version.VERSION).build();
}

/** Builder for JobServiceStubSettings. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ public class LoggingServiceV2StubSettings extends StubSettings<LoggingServiceV2S

@Override
protected LibraryMetadata getLibraryMetadata() {
return LibraryMetadata.newBuilder().build();
return LibraryMetadata.newBuilder()
.setArtifactName("com.google.cloud:google-cloud-logging")
.setVersion(Version.VERSION)
.build();
}

/** Builder for LoggingServiceV2StubSettings. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public class PublisherStubSettings extends StubSettings<PublisherStubSettings> {

@Override
protected LibraryMetadata getLibraryMetadata() {
return LibraryMetadata.newBuilder().build();
return LibraryMetadata.newBuilder().setVersion(Version.VERSION).build();
}

/** Builder for PublisherStubSettings. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ public class EchoStubSettings extends StubSettings<EchoStubSettings> {
return LibraryMetadata.newBuilder()
.setArtifactName("com.google.cloud:gapic-showcase")
.setRepository("googleapis/sdk-platform-java")
.setVersion(Version.VERSION)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public class WickedStubSettings extends StubSettings<WickedStubSettings> {
return LibraryMetadata.newBuilder()
.setArtifactName("com.google.cloud:gapic-showcase")
.setRepository("googleapis/sdk-platform-java")
.setVersion(Version.VERSION)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public class ComplianceStubSettings extends StubSettings<ComplianceStubSettings>

@Override
protected LibraryMetadata getLibraryMetadata() {
return LibraryMetadata.newBuilder().build();
return LibraryMetadata.newBuilder().setVersion(Version.VERSION).build();
}

/** Builder for ComplianceStubSettings. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public class EchoWithVersionStubSettings extends StubSettings<EchoWithVersionStu

@Override
protected LibraryMetadata getLibraryMetadata() {
return LibraryMetadata.newBuilder().build();
return LibraryMetadata.newBuilder().setVersion(Version.VERSION).build();
}

/** Builder for EchoWithVersionStubSettings. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class TestProtoLoader {
+ " to have the values echoed in the response trailers.";
private static final String ECHO_SERVICE_REPOSITORY = "googleapis/sdk-platform-java";
private static final String ECHO_SERVICE_ARTIFACT = "com.google.cloud:gapic-showcase";
private static final String LOGGING_SERVICE_ARTIFACT = "com.google.cloud:google-cloud-logging";
private final String testFilesDirectory;
private final Transport transport;

Expand Down Expand Up @@ -505,6 +506,7 @@ public GapicContext parseLogging() {
.setServiceConfig(config)
.setHelperResourceNames(outputResourceNames)
.setTransport(transport)
.setArtifact(LOGGING_SERVICE_ARTIFACT)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public abstract class LibraryMetadata {
@Nullable
public abstract String artifactName();

/**
* Returns the version of the client library.
*
* <p>Example: {@code "2.74.1"}. This maps to the {@code gcp.client.version} attribute.
*
* @return the version, or {@code null} if not set
*/
@Nullable
public abstract String version();

Expand All @@ -73,7 +80,7 @@ public static LibraryMetadata empty() {
}

public boolean isEmpty() {
return repository() == null && artifactName() == null;
return repository() == null && artifactName() == null && version() == null;
}

public static LibraryMetadata.Builder newBuilder() {
Expand All @@ -86,7 +93,7 @@ public abstract static class Builder {

public abstract Builder setArtifactName(@Nullable String artifactName);

public abstract Builder setVersion(@Nullable String version);
public abstract Builder setVersion(@Nullable String libraryVersion);

public abstract LibraryMetadata build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
/**
* @return a map of attributes to be included in attempt-level spans
*/
public Map<String, Object> getAttemptAttributes() {

Check failure on line 170 in gax-java/gax/src/main/java/com/google/api/gax/tracing/ApiTracerContext.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=googleapis_gapic-generator-java&issues=AZz9b_UfwOU3-kZRAQ9Z&open=AZz9b_UfwOU3-kZRAQ9Z&pullRequest=4140

Check failure on line 170 in gax-java/gax/src/main/java/com/google/api/gax/tracing/ApiTracerContext.java

View check run for this annotation

SonarQubeCloud / [java_showcase_integration_tests] SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=googleapis_gapic-generator-java_integration_tests&issues=AZz9c9DPzo1W2FDFboS3&open=AZz9c9DPzo1W2FDFboS3&pullRequest=4140
Map<String, Object> attributes = new HashMap<>();
if (!Strings.isNullOrEmpty(serverAddress())) {
attributes.put(ObservabilityAttributes.SERVER_ADDRESS_ATTRIBUTE, serverAddress());
Expand All @@ -186,6 +186,9 @@
attributes.put(
ObservabilityAttributes.ARTIFACT_ATTRIBUTE, libraryMetadata().artifactName());
}
if (!Strings.isNullOrEmpty(libraryMetadata().version())) {
attributes.put(ObservabilityAttributes.VERSION_ATTRIBUTE, libraryMetadata().version());
}
}
if (transport() == Transport.GRPC && !Strings.isNullOrEmpty(fullMethodName())) {
attributes.put(ObservabilityAttributes.GRPC_RPC_METHOD_ATTRIBUTE, fullMethodName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public class ObservabilityAttributes {
/** The artifact name of the client library (e.g., "google-cloud-vision"). */
public static final String ARTIFACT_ATTRIBUTE = "gcp.client.artifact";

/** The version of the client library (e.g., "1.2.3"). */
public static final String VERSION_ATTRIBUTE = "gcp.client.version";

/** The full RPC method name, including package, service, and method. */
public static final String GRPC_RPC_METHOD_ATTRIBUTE = "rpc.method";

Expand Down
Loading
Loading