Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit 07d3467

Browse files
chore: add non-generated typesafe names (#2817)
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: - [ ] Make sure to open an issue as a [bug/issue](https://togithub.com/googleapis/java-bigtable/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) - [ ] Rollback plan is reviewed and LGTMed - [ ] All new data plane features have a completed end to end testing plan Fixes #<issue_number_goes_here> ☕️ If you write sample code, please follow the [samples format]( https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md).
1 parent 279776d commit 07d3467

21 files changed

Lines changed: 304 additions & 34 deletions

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/BigtableDataClientFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package com.google.cloud.bigtable.data.v2;
1717

1818
import com.google.api.core.BetaApi;
19-
import com.google.bigtable.v2.InstanceName;
19+
import com.google.cloud.bigtable.data.v2.internal.api.InstanceName;
2020
import com.google.cloud.bigtable.data.v2.stub.BigtableClientContext;
2121
import com.google.cloud.bigtable.data.v2.stub.ClientOperationSettings;
2222
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStub;

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/RequestContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public abstract class RequestContext implements Serializable {
3636

3737
public static RequestContext create(ClientInfo clientInfo) {
3838
return create(
39-
clientInfo.getInstanceName().getProject(),
40-
clientInfo.getInstanceName().getInstance(),
39+
clientInfo.getInstanceName().getProjectId(),
40+
clientInfo.getInstanceName().getInstanceId(),
4141
clientInfo.getAppProfileId());
4242
}
4343

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigtable.data.v2.internal.api;
18+
19+
import com.google.auto.value.AutoValue;
20+
import com.google.common.base.Preconditions;
21+
import com.google.common.base.Splitter;
22+
import java.util.List;
23+
24+
@AutoValue
25+
public abstract class InstanceName {
26+
public abstract String getProjectId();
27+
28+
public abstract String getInstanceId();
29+
30+
@Override
31+
public final String toString() {
32+
return String.format("projects/%s/instances/%s", getProjectId(), getInstanceId());
33+
}
34+
35+
public static InstanceName of(String projectId, String instanceId) {
36+
return InstanceName.builder().setProjectId(projectId).setInstanceId(instanceId).build();
37+
}
38+
39+
public static Builder builder() {
40+
return new AutoValue_InstanceName.Builder();
41+
}
42+
43+
public static InstanceName parse(String name) {
44+
List<String> parts = Splitter.on('/').splitToList(name);
45+
Preconditions.checkArgument(parts.size() == 4, "Invalid instance name: %s", name);
46+
Preconditions.checkArgument(
47+
"projects".equals(parts.get(0)),
48+
"Invalid instance name: %s, must start with projects/",
49+
name);
50+
Preconditions.checkArgument(
51+
!parts.get(1).isEmpty(), "Invalid instance name %s, must have a project id", name);
52+
Preconditions.checkArgument(
53+
"instances".equals(parts.get(2)),
54+
"Invalid instance name: %s, must start with projects/$PROJECT_ID/instances/",
55+
name);
56+
Preconditions.checkArgument(
57+
!parts.get(3).isEmpty(), "Invalid instance name %s, must have an instance id", name);
58+
59+
return builder().setProjectId(parts.get(1)).setInstanceId(parts.get(3)).build();
60+
}
61+
62+
@AutoValue.Builder
63+
public abstract static class Builder {
64+
public abstract Builder setProjectId(String projectId);
65+
66+
public abstract Builder setInstanceId(String instanceId);
67+
68+
public abstract InstanceName build();
69+
}
70+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.bigtable.data.v2.internal.api;
18+
19+
import com.google.auto.value.AutoValue;
20+
import com.google.common.base.Preconditions;
21+
import com.google.common.base.Splitter;
22+
import java.util.List;
23+
24+
@AutoValue
25+
public abstract class TableName {
26+
public abstract String getProjectId();
27+
28+
public abstract String getInstanceId();
29+
30+
public abstract String getTableId();
31+
32+
public InstanceName getInstanceName() {
33+
return InstanceName.builder()
34+
.setProjectId(getProjectId())
35+
.setInstanceId(getInstanceId())
36+
.build();
37+
}
38+
39+
@Override
40+
public final String toString() {
41+
return String.format("%s/tables/%s", getInstanceName(), getTableId());
42+
}
43+
44+
public static Builder builder() {
45+
return new AutoValue_TableName.Builder();
46+
}
47+
48+
public static TableName parse(String name) {
49+
List<String> parts = Splitter.on('/').splitToList(name);
50+
Preconditions.checkArgument(parts.size() == 6, "Invalid table name: %s", name);
51+
Preconditions.checkArgument(
52+
"projects".equals(parts.get(0)), "Invalid table name: %s, must start with projects/", name);
53+
Preconditions.checkArgument(
54+
!parts.get(1).isEmpty(), "Invalid table name %s, must have a project id", name);
55+
Preconditions.checkArgument(
56+
"instances".equals(parts.get(2)),
57+
"Invalid table name: %s, must start with projects/$PROJECT_ID/instances/",
58+
name);
59+
Preconditions.checkArgument(
60+
!parts.get(3).isEmpty(), "Invalid table name %s, must have an instance id", name);
61+
Preconditions.checkArgument(
62+
"tables".equals(parts.get(4)),
63+
"Invalid table name: %s, must start with"
64+
+ " projects/$PROJECT_ID/instances/$INSTANCE_ID/tables",
65+
name);
66+
Preconditions.checkArgument(
67+
!parts.get(5).isEmpty(), "Invalid table name %s, must have table id", name);
68+
69+
return builder()
70+
.setProjectId(parts.get(1))
71+
.setInstanceId(parts.get(3))
72+
.setTableId(parts.get(5))
73+
.build();
74+
}
75+
76+
@AutoValue.Builder
77+
public abstract static class Builder {
78+
public abstract Builder setProjectId(String projectId);
79+
80+
public abstract Builder setInstanceId(String instanceId);
81+
82+
public abstract Builder setTableId(String tableId);
83+
84+
public abstract TableName build();
85+
}
86+
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/MetricsImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ private static ApiTracerFactory createOCTracingFactory(ClientInfo clientInfo) {
202202
// Annotate traces with the same tags as metrics
203203
.put(
204204
RpcMeasureConstants.BIGTABLE_PROJECT_ID.getName(),
205-
clientInfo.getInstanceName().getProject())
205+
clientInfo.getInstanceName().getProjectId())
206206
.put(
207207
RpcMeasureConstants.BIGTABLE_INSTANCE_ID.getName(),
208-
clientInfo.getInstanceName().getInstance())
208+
clientInfo.getInstanceName().getInstanceId())
209209
.put(
210210
RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID.getName(), clientInfo.getAppProfileId())
211211
// Also annotate traces with library versions
@@ -222,10 +222,10 @@ private static ApiTracerFactory createOCMetricsFactory(
222222
ImmutableMap.<TagKey, TagValue>builder()
223223
.put(
224224
RpcMeasureConstants.BIGTABLE_PROJECT_ID,
225-
TagValue.create(clientInfo.getInstanceName().getProject()))
225+
TagValue.create(clientInfo.getInstanceName().getProjectId()))
226226
.put(
227227
RpcMeasureConstants.BIGTABLE_INSTANCE_ID,
228-
TagValue.create(clientInfo.getInstanceName().getInstance()))
228+
TagValue.create(clientInfo.getInstanceName().getInstanceId()))
229229
.put(
230230
RpcMeasureConstants.BIGTABLE_APP_PROFILE_ID,
231231
TagValue.create(clientInfo.getAppProfileId()))

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/attributes/ClientInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
package com.google.cloud.bigtable.data.v2.internal.csm.attributes;
1818

1919
import com.google.auto.value.AutoValue;
20-
import com.google.bigtable.v2.InstanceName;
2120
import com.google.cloud.bigtable.Version;
21+
import com.google.cloud.bigtable.data.v2.internal.api.InstanceName;
2222

2323
/**
2424
* A value class to capture parameters that the client was instantiated with. These parameters will

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/exporter/BigtableCloudMonitoringExporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void onFailure(Throwable throwable) {
188188
" Need monitoring metric writer permission on project=%s. Follow"
189189
+ " https://cloud.google.com/bigtable/docs/client-side-metrics-setup"
190190
+ " to set up permissions.",
191-
clientInfo.getInstanceName().getProject());
191+
clientInfo.getInstanceName().getProjectId());
192192
}
193193
RuntimeException asyncWrapper = new RuntimeException("export failed", throwable);
194194
asyncWrapper.setStackTrace(stackTrace);

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/metrics/ClientPerConnectionErrorCount.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ public void record(ClientInfo clientInfo, long value) {
9999
Attributes attributes =
100100
getSchema()
101101
.createResourceAttrs(clientInfo)
102-
.put(MetricLabels.BIGTABLE_PROJECT_ID_KEY, clientInfo.getInstanceName().getProject())
103-
.put(MetricLabels.INSTANCE_ID_KEY, clientInfo.getInstanceName().getInstance())
102+
.put(
103+
MetricLabels.BIGTABLE_PROJECT_ID_KEY, clientInfo.getInstanceName().getProjectId())
104+
.put(MetricLabels.INSTANCE_ID_KEY, clientInfo.getInstanceName().getInstanceId())
104105
.put(MetricLabels.CLIENT_NAME, clientInfo.getClientName())
105106
.put(MetricLabels.APP_PROFILE_KEY, clientInfo.getAppProfileId())
106107
.build();

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/schema/ClientSchema.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ public ClientSchema() {
6464

6565
@Override
6666
public ProjectName extractProjectName(Attributes attrs, EnvInfo envInfo, ClientInfo clientInfo) {
67-
return ProjectName.of(clientInfo.getInstanceName().getProject());
67+
return ProjectName.of(clientInfo.getInstanceName().getProjectId());
6868
}
6969

7070
public AttributesBuilder createResourceAttrs(ClientInfo clientInfo) {
7171
return Attributes.builder()
72-
.put(BIGTABLE_PROJECT_ID_KEY, clientInfo.getInstanceName().getProject())
73-
.put(INSTANCE_ID_KEY, clientInfo.getInstanceName().getInstance())
72+
.put(BIGTABLE_PROJECT_ID_KEY, clientInfo.getInstanceName().getProjectId())
73+
.put(INSTANCE_ID_KEY, clientInfo.getInstanceName().getInstanceId())
7474
.put(APP_PROFILE_KEY, clientInfo.getAppProfileId())
7575
.put(CLIENT_NAME, clientInfo.getClientName());
7676
}

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/internal/csm/schema/GrpcClientSchema.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
public final class GrpcClientSchema extends Schema {
3131
// Unlike the normal ClientSchema, the bigtable resource ids must be injected during export time
3232
private static final DeferredAttr BIGTABLE_PROJECT_ID =
33-
DeferredAttr.fromClientInfo("project_id", ci -> ci.getInstanceName().getProject());
33+
DeferredAttr.fromClientInfo("project_id", ci -> ci.getInstanceName().getProjectId());
3434
private static final DeferredAttr INSTANCE_ID =
35-
DeferredAttr.fromClientInfo("instance", ci -> ci.getInstanceName().getInstance());
35+
DeferredAttr.fromClientInfo("instance", ci -> ci.getInstanceName().getInstanceId());
3636
private static final DeferredAttr APP_PROFILE_ID =
3737
DeferredAttr.fromClientInfo("app_profile", ClientInfo::getAppProfileId);
3838
private static final DeferredAttr CLIENT_NAME =
@@ -73,6 +73,6 @@ private GrpcClientSchema() {
7373
@Override
7474
public ProjectName extractProjectName(
7575
Attributes ignored, EnvInfo ignored2, ClientInfo clientInfo) {
76-
return ProjectName.of(clientInfo.getInstanceName().getProject());
76+
return ProjectName.of(clientInfo.getInstanceName().getProjectId());
7777
}
7878
}

0 commit comments

Comments
 (0)