Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84d30af

Browse files
authored
Get mesh_id local label from "CSM_MESH_ID" environment variable, rather than parsing from bootstrap file (#11621)
1 parent b692b9d commit 84d30af

File tree

3 files changed

+27
-134
lines changed

3 files changed

+27
-134
lines changed

gcp-csm-observability/src/main/java/io/grpc/gcp/csm/observability/MetadataExchanger.java

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package io.grpc.gcp.csm.observability;
1818

19-
import com.google.common.annotations.VisibleForTesting;
2019
import com.google.common.base.Preconditions;
2120
import com.google.common.io.BaseEncoding;
2221
import com.google.protobuf.Struct;
@@ -29,12 +28,9 @@
2928
import io.grpc.ServerCallHandler;
3029
import io.grpc.ServerInterceptor;
3130
import io.grpc.Status;
32-
import io.grpc.internal.JsonParser;
33-
import io.grpc.internal.JsonUtil;
3431
import io.grpc.opentelemetry.InternalOpenTelemetryPlugin;
3532
import io.grpc.protobuf.ProtoUtils;
3633
import io.grpc.xds.ClusterImplLoadBalancerProvider;
37-
import io.grpc.xds.InternalGrpcBootstrapperImpl;
3834
import io.opentelemetry.api.common.AttributeKey;
3935
import io.opentelemetry.api.common.Attributes;
4036
import io.opentelemetry.api.common.AttributesBuilder;
@@ -43,16 +39,13 @@
4339
import java.net.URI;
4440
import java.util.Map;
4541
import java.util.function.Consumer;
46-
import java.util.logging.Level;
47-
import java.util.logging.Logger;
4842

4943
/**
5044
* OpenTelemetryPlugin implementing metadata-based workload property exchange for both client and
5145
* server. Is responsible for determining the metadata, communicating the metadata, and adding local
5246
* and remote details to metrics.
5347
*/
5448
final class MetadataExchanger implements InternalOpenTelemetryPlugin {
55-
private static final Logger logger = Logger.getLogger(MetadataExchanger.class.getName());
5649

5750
private static final AttributeKey<String> CLOUD_PLATFORM =
5851
AttributeKey.stringKey("cloud.platform");
@@ -89,11 +82,10 @@ final class MetadataExchanger implements InternalOpenTelemetryPlugin {
8982
public MetadataExchanger() {
9083
this(
9184
addOtelResourceAttributes(new GCPResourceProvider().getAttributes()),
92-
System::getenv,
93-
InternalGrpcBootstrapperImpl::getJsonContent);
85+
System::getenv);
9486
}
9587

96-
MetadataExchanger(Attributes platformAttributes, Lookup env, Supplier<String> xdsBootstrap) {
88+
MetadataExchanger(Attributes platformAttributes, Lookup env) {
9789
String type = platformAttributes.get(CLOUD_PLATFORM);
9890
String canonicalService = env.get("CSM_CANONICAL_SERVICE_NAME");
9991
Struct.Builder struct = Struct.newBuilder();
@@ -121,7 +113,7 @@ public MetadataExchanger() {
121113
localMetadata = BaseEncoding.base64().encode(struct.build().toByteArray());
122114

123115
localAttributes = Attributes.builder()
124-
.put("csm.mesh_id", nullIsUnknown(getMeshId(xdsBootstrap)))
116+
.put("csm.mesh_id", nullIsUnknown(env.get("CSM_MESH_ID")))
125117
.put("csm.workload_canonical_service", nullIsUnknown(canonicalService))
126118
.build();
127119
}
@@ -162,29 +154,6 @@ private static Attributes addOtelResourceAttributes(Attributes platformAttribute
162154
return builder.build();
163155
}
164156

165-
@VisibleForTesting
166-
static String getMeshId(Supplier<String> xdsBootstrap) {
167-
try {
168-
@SuppressWarnings("unchecked")
169-
Map<String, ?> rawBootstrap = (Map<String, ?>) JsonParser.parse(xdsBootstrap.get());
170-
Map<String, ?> node = JsonUtil.getObject(rawBootstrap, "node");
171-
String id = JsonUtil.getString(node, "id");
172-
Preconditions.checkNotNull(id, "id");
173-
String[] parts = id.split("/", 6);
174-
if (!(parts.length == 6
175-
&& parts[0].equals("projects")
176-
&& parts[2].equals("networks")
177-
&& parts[3].startsWith("mesh:")
178-
&& parts[4].equals("nodes"))) {
179-
throw new Exception("node id didn't match mesh format: " + id);
180-
}
181-
return parts[3].substring("mesh:".length());
182-
} catch (Exception e) {
183-
logger.log(Level.INFO, "Failed to determine mesh ID for CSM", e);
184-
return null;
185-
}
186-
}
187-
188157
private void addLabels(AttributesBuilder to, Struct struct) {
189158
to.putAll(localAttributes);
190159
Map<String, Value> remote = struct.getFieldsMap();

gcp-csm-observability/src/test/java/io/grpc/gcp/csm/observability/CsmObservabilityTest.java

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,14 @@ public void tearDown() {
7777

7878
@Test
7979
public void unknownDataExchange() throws Exception {
80-
String xdsBootstrap = "";
8180
MetadataExchanger clientExchanger = new MetadataExchanger(
8281
Attributes.builder().build(),
83-
ImmutableMap.<String, String>of()::get,
84-
() -> xdsBootstrap);
82+
ImmutableMap.<String, String>of()::get);
8583
CsmObservability.Builder clientCsmBuilder = new CsmObservability.Builder(clientExchanger)
8684
.sdk(openTelemetryTesting.getOpenTelemetry());
8785
MetadataExchanger serverExchanger = new MetadataExchanger(
8886
Attributes.builder().build(),
89-
ImmutableMap.<String, String>of()::get,
90-
() -> xdsBootstrap);
87+
ImmutableMap.<String, String>of()::get);
9188
CsmObservability.Builder serverCsmBuilder = new CsmObservability.Builder(serverExchanger)
9289
.sdk(openTelemetryTesting.getOpenTelemetry());
9390

@@ -140,11 +137,9 @@ public void unknownDataExchange() throws Exception {
140137

141138
@Test
142139
public void nonCsmServer() throws Exception {
143-
String xdsBootstrap = "";
144140
MetadataExchanger clientExchanger = new MetadataExchanger(
145141
Attributes.builder().build(),
146-
ImmutableMap.<String, String>of()::get,
147-
() -> xdsBootstrap);
142+
ImmutableMap.<String, String>of()::get);
148143
CsmObservability.Builder clientCsmBuilder = new CsmObservability.Builder(clientExchanger)
149144
.sdk(openTelemetryTesting.getOpenTelemetry());
150145

@@ -205,19 +200,16 @@ public void nonCsmServer() throws Exception {
205200

206201
@Test
207202
public void nonCsmClient() throws Exception {
208-
String xdsBootstrap = "";
209203
MetadataExchanger clientExchanger = new MetadataExchanger(
210204
Attributes.builder()
211205
.put(stringKey("cloud.platform"), "gcp_kubernetes_engine")
212206
.build(),
213-
ImmutableMap.<String, String>of()::get,
214-
() -> xdsBootstrap);
207+
ImmutableMap.<String, String>of()::get);
215208
CsmObservability.Builder clientCsmBuilder = new CsmObservability.Builder(clientExchanger)
216209
.sdk(openTelemetryTesting.getOpenTelemetry());
217210
MetadataExchanger serverExchanger = new MetadataExchanger(
218211
Attributes.builder().build(),
219-
ImmutableMap.<String, String>of()::get,
220-
() -> xdsBootstrap);
212+
ImmutableMap.<String, String>of()::get);
221213
CsmObservability.Builder serverCsmBuilder = new CsmObservability.Builder(serverExchanger)
222214
.sdk(openTelemetryTesting.getOpenTelemetry());
223215

@@ -262,11 +254,6 @@ public void nonCsmClient() throws Exception {
262254

263255
@Test
264256
public void k8sExchange() throws Exception {
265-
// Purposefully use a different project ID in the bootstrap than the resource, as the mesh could
266-
// be in a different project than the running account.
267-
String clientBootstrap = "{\"node\": {"
268-
+ "\"id\": \"projects/12/networks/mesh:mymesh/nodes/a6420022-cbc5-4e10-808c-507e3fc95f2e\""
269-
+ "}}";
270257
MetadataExchanger clientExchanger = new MetadataExchanger(
271258
Attributes.builder()
272259
.put(stringKey("cloud.platform"), "gcp_kubernetes_engine")
@@ -277,13 +264,10 @@ public void k8sExchange() throws Exception {
277264
.build(),
278265
ImmutableMap.of(
279266
"CSM_CANONICAL_SERVICE_NAME", "canon-service-is-a-client",
280-
"CSM_WORKLOAD_NAME", "best-client")::get,
281-
() -> clientBootstrap);
267+
"CSM_WORKLOAD_NAME", "best-client",
268+
"CSM_MESH_ID", "mymesh")::get);
282269
CsmObservability.Builder clientCsmBuilder = new CsmObservability.Builder(clientExchanger)
283270
.sdk(openTelemetryTesting.getOpenTelemetry());
284-
String serverBootstrap = "{\"node\": {"
285-
+ "\"id\": \"projects/34/networks/mesh:meshhh/nodes/4969ef19-24b6-44c0-baf3-86d188ff5967\""
286-
+ "}}";
287271
MetadataExchanger serverExchanger = new MetadataExchanger(
288272
Attributes.builder()
289273
.put(stringKey("cloud.platform"), "gcp_kubernetes_engine")
@@ -295,8 +279,8 @@ public void k8sExchange() throws Exception {
295279
.build(),
296280
ImmutableMap.of(
297281
"CSM_CANONICAL_SERVICE_NAME", "server-has-a-single-name",
298-
"CSM_WORKLOAD_NAME", "fast-server")::get,
299-
() -> serverBootstrap);
282+
"CSM_WORKLOAD_NAME", "fast-server",
283+
"CSM_MESH_ID", "meshhh")::get);
300284
CsmObservability.Builder serverCsmBuilder = new CsmObservability.Builder(serverExchanger)
301285
.sdk(openTelemetryTesting.getOpenTelemetry());
302286

@@ -366,11 +350,6 @@ public void k8sExchange() throws Exception {
366350

367351
@Test
368352
public void gceExchange() throws Exception {
369-
// Purposefully use a different project ID in the bootstrap than the resource, as the mesh could
370-
// be in a different project than the running account.
371-
String clientBootstrap = "{\"node\": {"
372-
+ "\"id\": \"projects/12/networks/mesh:mymesh/nodes/a6420022-cbc5-4e10-808c-507e3fc95f2e\""
373-
+ "}}";
374353
MetadataExchanger clientExchanger = new MetadataExchanger(
375354
Attributes.builder()
376355
.put(stringKey("cloud.platform"), "gcp_compute_engine")
@@ -379,13 +358,10 @@ public void gceExchange() throws Exception {
379358
.build(),
380359
ImmutableMap.of(
381360
"CSM_CANONICAL_SERVICE_NAME", "canon-service-is-a-client",
382-
"CSM_WORKLOAD_NAME", "best-client")::get,
383-
() -> clientBootstrap);
361+
"CSM_WORKLOAD_NAME", "best-client",
362+
"CSM_MESH_ID", "mymesh")::get);
384363
CsmObservability.Builder clientCsmBuilder = new CsmObservability.Builder(clientExchanger)
385364
.sdk(openTelemetryTesting.getOpenTelemetry());
386-
String serverBootstrap = "{\"node\": {"
387-
+ "\"id\": \"projects/34/networks/mesh:meshhh/nodes/4969ef19-24b6-44c0-baf3-86d188ff5967\""
388-
+ "}}";
389365
MetadataExchanger serverExchanger = new MetadataExchanger(
390366
Attributes.builder()
391367
.put(stringKey("cloud.platform"), "gcp_compute_engine")
@@ -395,8 +371,8 @@ public void gceExchange() throws Exception {
395371
.build(),
396372
ImmutableMap.of(
397373
"CSM_CANONICAL_SERVICE_NAME", "server-has-a-single-name",
398-
"CSM_WORKLOAD_NAME", "fast-server")::get,
399-
() -> serverBootstrap);
374+
"CSM_WORKLOAD_NAME", "fast-server",
375+
"CSM_MESH_ID", "meshhh")::get);
400376
CsmObservability.Builder serverCsmBuilder = new CsmObservability.Builder(serverExchanger)
401377
.sdk(openTelemetryTesting.getOpenTelemetry());
402378

@@ -456,9 +432,6 @@ public void gceExchange() throws Exception {
456432

457433
@Test
458434
public void trailersOnly() throws Exception {
459-
String clientBootstrap = "{\"node\": {"
460-
+ "\"id\": \"projects/12/networks/mesh:mymesh/nodes/a6420022-cbc5-4e10-808c-507e3fc95f2e\""
461-
+ "}}";
462435
MetadataExchanger clientExchanger = new MetadataExchanger(
463436
Attributes.builder()
464437
.put(stringKey("cloud.platform"), "gcp_compute_engine")
@@ -467,13 +440,11 @@ public void trailersOnly() throws Exception {
467440
.build(),
468441
ImmutableMap.of(
469442
"CSM_CANONICAL_SERVICE_NAME", "canon-service-is-a-client",
470-
"CSM_WORKLOAD_NAME", "best-client")::get,
471-
() -> clientBootstrap);
443+
"CSM_WORKLOAD_NAME", "best-client",
444+
"CSM_MESH_ID", "mymesh")::get);
472445
CsmObservability.Builder clientCsmBuilder = new CsmObservability.Builder(clientExchanger)
473446
.sdk(openTelemetryTesting.getOpenTelemetry());
474-
String serverBootstrap = "{\"node\": {"
475-
+ "\"id\": \"projects/34/networks/mesh:meshhh/nodes/4969ef19-24b6-44c0-baf3-86d188ff5967\""
476-
+ "}}";
447+
477448
MetadataExchanger serverExchanger = new MetadataExchanger(
478449
Attributes.builder()
479450
.put(stringKey("cloud.platform"), "gcp_compute_engine")
@@ -483,8 +454,8 @@ public void trailersOnly() throws Exception {
483454
.build(),
484455
ImmutableMap.of(
485456
"CSM_CANONICAL_SERVICE_NAME", "server-has-a-single-name",
486-
"CSM_WORKLOAD_NAME", "fast-server")::get,
487-
() -> serverBootstrap);
457+
"CSM_WORKLOAD_NAME", "fast-server",
458+
"CSM_MESH_ID", "meshhh")::get);
488459
CsmObservability.Builder serverCsmBuilder = new CsmObservability.Builder(serverExchanger)
489460
.sdk(openTelemetryTesting.getOpenTelemetry());
490461

gcp-csm-observability/src/test/java/io/grpc/gcp/csm/observability/MetadataExchangerTest.java

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,56 +33,11 @@
3333
/** Tests for {@link MetadataExchanger}. */
3434
@RunWith(JUnit4.class)
3535
public final class MetadataExchangerTest {
36-
@Test
37-
public void getMeshId_findsMeshId() {
38-
assertThat(MetadataExchanger.getMeshId(() ->
39-
"{\"node\":{\"id\":\"projects/12/networks/mesh:mine/nodes/uu-id\"}}"))
40-
.isEqualTo("mine");
41-
assertThat(MetadataExchanger.getMeshId(() ->
42-
"{\"node\":{\"id\":\"projects/1234567890/networks/mesh:mine/nodes/uu-id\", "
43-
+ "\"unknown\": \"\"}, \"unknown\": \"\"}"))
44-
.isEqualTo("mine");
45-
}
46-
47-
@Test
48-
public void getMeshId_returnsNullOnBadMeshId() {
49-
assertThat(MetadataExchanger.getMeshId(
50-
() -> "[\"node\"]"))
51-
.isNull();
52-
assertThat(MetadataExchanger.getMeshId(
53-
() -> "{\"node\":[\"id\"]}}"))
54-
.isNull();
55-
assertThat(MetadataExchanger.getMeshId(
56-
() -> "{\"node\":{\"id\":[\"projects/12/networks/mesh:mine/nodes/uu-id\"]}}"))
57-
.isNull();
58-
59-
assertThat(MetadataExchanger.getMeshId(
60-
() -> "{\"NODE\":{\"id\":\"projects/12/networks/mesh:mine/nodes/uu-id\"}}"))
61-
.isNull();
62-
assertThat(MetadataExchanger.getMeshId(
63-
() -> "{\"node\":{\"ID\":\"projects/12/networks/mesh:mine/nodes/uu-id\"}}"))
64-
.isNull();
65-
assertThat(MetadataExchanger.getMeshId(
66-
() -> "{\"node\":{\"id\":\"projects/12/networks/mesh:mine\"}}"))
67-
.isNull();
68-
assertThat(MetadataExchanger.getMeshId(
69-
() -> "{\"node\":{\"id\":\"PROJECTS/12/networks/mesh:mine/nodes/uu-id\"}}"))
70-
.isNull();
71-
assertThat(MetadataExchanger.getMeshId(
72-
() -> "{\"node\":{\"id\":\"projects/12/NETWORKS/mesh:mine/nodes/uu-id\"}}"))
73-
.isNull();
74-
assertThat(MetadataExchanger.getMeshId(
75-
() -> "{\"node\":{\"id\":\"projects/12/networks/MESH:mine/nodes/uu-id\"}}"))
76-
.isNull();
77-
assertThat(MetadataExchanger.getMeshId(
78-
() -> "{\"node\":{\"id\":\"projects/12/networks/mesh:mine/NODES/uu-id\"}}"))
79-
.isNull();
80-
}
8136

8237
@Test
8338
public void enablePluginForChannel_matches() {
8439
MetadataExchanger exchanger =
85-
new MetadataExchanger(Attributes.builder().build(), (name) -> null, () -> "");
40+
new MetadataExchanger(Attributes.builder().build(), (name) -> null);
8641
assertThat(exchanger.enablePluginForChannel("xds:///testing")).isTrue();
8742
assertThat(exchanger.enablePluginForChannel("xds:/testing")).isTrue();
8843
assertThat(exchanger.enablePluginForChannel(
@@ -92,7 +47,7 @@ public void enablePluginForChannel_matches() {
9247
@Test
9348
public void enablePluginForChannel_doesNotMatch() {
9449
MetadataExchanger exchanger =
95-
new MetadataExchanger(Attributes.builder().build(), (name) -> null, () -> "");
50+
new MetadataExchanger(Attributes.builder().build(), (name) -> null);
9651
assertThat(exchanger.enablePluginForChannel("dns:///localhost")).isFalse();
9752
assertThat(exchanger.enablePluginForChannel("xds:///[]")).isFalse();
9853
assertThat(exchanger.enablePluginForChannel("xds://my-xds-server/testing")).isFalse();
@@ -101,7 +56,7 @@ public void enablePluginForChannel_doesNotMatch() {
10156
@Test
10257
public void addLabels_receivedWrongType() {
10358
MetadataExchanger exchanger =
104-
new MetadataExchanger(Attributes.builder().build(), (name) -> null, () -> "");
59+
new MetadataExchanger(Attributes.builder().build(), (name) -> null);
10560
Metadata metadata = new Metadata();
10661
metadata.put(Metadata.Key.of("x-envoy-peer-metadata", Metadata.ASCII_STRING_MARSHALLER),
10762
BaseEncoding.base64().encode(Struct.newBuilder()
@@ -122,7 +77,7 @@ public void addLabels_receivedWrongType() {
12277
@Test
12378
public void addLabelsFromExchange_unknownGcpType() {
12479
MetadataExchanger exchanger =
125-
new MetadataExchanger(Attributes.builder().build(), (name) -> null, () -> "");
80+
new MetadataExchanger(Attributes.builder().build(), (name) -> null);
12681
Metadata metadata = new Metadata();
12782
metadata.put(Metadata.Key.of("x-envoy-peer-metadata", Metadata.ASCII_STRING_MARSHALLER),
12883
BaseEncoding.base64().encode(Struct.newBuilder()
@@ -153,8 +108,7 @@ public void addMetadata_k8s() throws Exception {
153108
.build(),
154109
ImmutableMap.of(
155110
"CSM_CANONICAL_SERVICE_NAME", "myservice1",
156-
"CSM_WORKLOAD_NAME", "myworkload1")::get,
157-
() -> "");
111+
"CSM_WORKLOAD_NAME", "myworkload1")::get);
158112
Metadata metadata = new Metadata();
159113
exchanger.newClientCallPlugin().addMetadata(metadata);
160114

@@ -182,8 +136,7 @@ public void addMetadata_gce() throws Exception {
182136
.build(),
183137
ImmutableMap.of(
184138
"CSM_CANONICAL_SERVICE_NAME", "myservice1",
185-
"CSM_WORKLOAD_NAME", "myworkload1")::get,
186-
() -> "");
139+
"CSM_WORKLOAD_NAME", "myworkload1")::get);
187140
Metadata metadata = new Metadata();
188141
exchanger.newClientCallPlugin().addMetadata(metadata);
189142

0 commit comments

Comments
 (0)