Skip to content

Commit 54841ef

Browse files
authored
stats/opentelemetry/csm: Get mesh_id local label from "CSM_MESH_ID" environment variable, rather than parsing from bootstrap file (#7740)
1 parent ad81c20 commit 54841ef

File tree

3 files changed

+18
-98
lines changed

3 files changed

+18
-98
lines changed

stats/opentelemetry/csm/observability_test.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232
"google.golang.org/grpc/encoding/gzip"
3333
istats "google.golang.org/grpc/internal/stats"
3434
"google.golang.org/grpc/internal/stubserver"
35-
"google.golang.org/grpc/internal/testutils"
36-
"google.golang.org/grpc/internal/testutils/xds/e2e"
3735
testgrpc "google.golang.org/grpc/interop/grpc_testing"
3836
testpb "google.golang.org/grpc/interop/grpc_testing"
3937
"google.golang.org/grpc/metadata"
@@ -46,12 +44,11 @@ import (
4644
// Env Vars as well, and mocks the resource detector's returned attribute set to
4745
// simulate the environment. It registers a cleanup function on the provided t
4846
// to restore the environment to its original state.
49-
func setupEnv(t *testing.T, resourceDetectorEmissions map[string]string, nodeID, csmCanonicalServiceName, csmWorkloadName string) {
50-
bootstrapContents := e2e.DefaultBootstrapContents(t, nodeID, "xds_server_uri")
51-
testutils.CreateBootstrapFileForTesting(t, bootstrapContents)
52-
47+
func setupEnv(t *testing.T, resourceDetectorEmissions map[string]string, meshID, csmCanonicalServiceName, csmWorkloadName string) {
48+
oldCSMMeshID, csmMeshIDPresent := os.LookupEnv("CSM_MESH_ID")
5349
oldCSMCanonicalServiceName, csmCanonicalServiceNamePresent := os.LookupEnv("CSM_CANONICAL_SERVICE_NAME")
5450
oldCSMWorkloadName, csmWorkloadNamePresent := os.LookupEnv("CSM_WORKLOAD_NAME")
51+
os.Setenv("CSM_MESH_ID", meshID)
5552
os.Setenv("CSM_CANONICAL_SERVICE_NAME", csmCanonicalServiceName)
5653
os.Setenv("CSM_WORKLOAD_NAME", csmWorkloadName)
5754

@@ -67,6 +64,11 @@ func setupEnv(t *testing.T, resourceDetectorEmissions map[string]string, nodeID,
6764
return &attrSet
6865
}
6966
t.Cleanup(func() {
67+
if csmMeshIDPresent {
68+
os.Setenv("CSM_MESH_ID", oldCSMMeshID)
69+
} else {
70+
os.Unsetenv("CSM_MESH_ID")
71+
}
7072
if csmCanonicalServiceNamePresent {
7173
os.Setenv("CSM_CANONICAL_SERVICE_NAME", oldCSMCanonicalServiceName)
7274
} else {
@@ -99,10 +101,10 @@ func (s) TestCSMPluginOptionUnary(t *testing.T) {
99101
"k8s.namespace.name": "k8s_namespace_name_val",
100102
"k8s.cluster.name": "k8s_cluster_name_val",
101103
}
102-
const nodeID = "projects/12345/networks/mesh:mesh_id/nodes/aaaa-aaaa-aaaa-aaaa"
104+
const meshID = "mesh_id"
103105
const csmCanonicalServiceName = "csm_canonical_service_name"
104106
const csmWorkloadName = "csm_workload_name"
105-
setupEnv(t, resourceDetectorEmissions, nodeID, csmCanonicalServiceName, csmWorkloadName)
107+
setupEnv(t, resourceDetectorEmissions, meshID, csmCanonicalServiceName, csmWorkloadName)
106108

107109
attributesWant := map[string]string{
108110
"csm.workload_canonical_service": csmCanonicalServiceName, // from env
@@ -266,10 +268,10 @@ func (s) TestCSMPluginOptionStreaming(t *testing.T) {
266268
"k8s.namespace.name": "k8s_namespace_name_val",
267269
"k8s.cluster.name": "k8s_cluster_name_val",
268270
}
269-
const nodeID = "projects/12345/networks/mesh:mesh_id/nodes/aaaa-aaaa-aaaa-aaaa"
271+
const meshID = "mesh_id"
270272
const csmCanonicalServiceName = "csm_canonical_service_name"
271273
const csmWorkloadName = "csm_workload_name"
272-
setupEnv(t, resourceDetectorEmissions, nodeID, csmCanonicalServiceName, csmWorkloadName)
274+
setupEnv(t, resourceDetectorEmissions, meshID, csmCanonicalServiceName, csmWorkloadName)
273275

274276
attributesWant := map[string]string{
275277
"csm.workload_canonical_service": csmCanonicalServiceName, // from env

stats/opentelemetry/csm/pluginoption.go

+1-36
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,10 @@ import (
2424
"encoding/base64"
2525
"net/url"
2626
"os"
27-
"strings"
2827

2928
"google.golang.org/grpc/grpclog"
30-
"google.golang.org/grpc/internal/xds/bootstrap"
3129
"google.golang.org/grpc/metadata"
3230
"google.golang.org/grpc/stats/opentelemetry/internal"
33-
3431
"google.golang.org/protobuf/proto"
3532
"google.golang.org/protobuf/types/known/structpb"
3633

@@ -233,24 +230,6 @@ func constructMetadataFromEnv(ctx context.Context) (map[string]string, string) {
233230
return initializeLocalAndMetadataLabels(labels)
234231
}
235232

236-
// parseMeshIDString parses the mesh id from the node id according to the format
237-
// "projects/[GCP Project number]/networks/mesh:[Mesh ID]/nodes/[UUID]". Returns
238-
// "unknown" if there is a syntax error in the node ID.
239-
func parseMeshIDFromNodeID(nodeID string) string {
240-
meshSplit := strings.Split(nodeID, "/")
241-
if len(meshSplit) != 6 {
242-
return "unknown"
243-
}
244-
if meshSplit[0] != "projects" || meshSplit[2] != "networks" || meshSplit[4] != "nodes" {
245-
return "unknown"
246-
}
247-
meshID, ok := strings.CutPrefix(meshSplit[3], "mesh:")
248-
if !ok { // errors become "unknown"
249-
return "unknown"
250-
}
251-
return meshID
252-
}
253-
254233
// initializeLocalAndMetadataLabels csm local labels for a CSM Plugin Option to
255234
// record. It also builds out a base 64 encoded protobuf.Struct containing the
256235
// metadata exchange labels to be sent as part of metadata exchange from a CSM
@@ -261,9 +240,7 @@ func initializeLocalAndMetadataLabels(labels map[string]string) (map[string]stri
261240
val := labels["canonical_service"]
262241
localLabels := make(map[string]string)
263242
localLabels["csm.workload_canonical_service"] = val
264-
// Get the CSM Mesh ID from the bootstrap file.
265-
nodeID := getNodeID()
266-
localLabels["csm.mesh_id"] = parseMeshIDFromNodeID(nodeID)
243+
localLabels["csm.mesh_id"] = getEnv("CSM_MESH_ID")
267244

268245
// Metadata exchange labels - can go ahead and encode into proto, and then
269246
// base64.
@@ -288,18 +265,6 @@ func initializeLocalAndMetadataLabels(labels map[string]string) (map[string]stri
288265
return localLabels, metadataExchangeLabelsEncoded
289266
}
290267

291-
// getNodeID gets the Node ID from the bootstrap data.
292-
func getNodeID() string {
293-
cfg, err := bootstrap.GetConfiguration()
294-
if err != nil {
295-
return "" // will become "unknown"
296-
}
297-
if cfg.Node() == nil {
298-
return ""
299-
}
300-
return cfg.Node().GetId()
301-
}
302-
303268
// metadataExchangeKey is the key for HTTP metadata exchange.
304269
const metadataExchangeKey = "x-envoy-peer-metadata"
305270

stats/opentelemetry/csm/pluginoption_test.go

+5-52
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ import (
2929

3030
"google.golang.org/grpc/internal/envconfig"
3131
"google.golang.org/grpc/internal/grpctest"
32-
"google.golang.org/grpc/internal/testutils"
33-
"google.golang.org/grpc/internal/testutils/xds/e2e"
3432
"google.golang.org/grpc/metadata"
3533

3634
"github.com/google/go-cmp/cmp"
@@ -304,51 +302,6 @@ func (s) TestDetermineTargetCSM(t *testing.T) {
304302
}
305303
}
306304

307-
func (s) TestBootstrap(t *testing.T) {
308-
tests := []struct {
309-
name string
310-
nodeID string
311-
meshIDWant string
312-
}{
313-
{
314-
name: "malformed-node-id-unknown",
315-
nodeID: "malformed",
316-
meshIDWant: "unknown",
317-
},
318-
{
319-
name: "node-id-parsed",
320-
nodeID: "projects/12345/networks/mesh:mesh_id/nodes/aaaa-aaaa-aaaa-aaaa",
321-
meshIDWant: "mesh_id",
322-
},
323-
{
324-
name: "wrong-syntax-unknown",
325-
nodeID: "wrong-syntax/12345/networks/mesh:mesh_id/nodes/aaaa-aaaa-aaaa-aaaa",
326-
meshIDWant: "unknown",
327-
},
328-
{
329-
name: "node-id-parsed",
330-
nodeID: "projects/12345/networks/mesh:/nodes/aaaa-aaaa-aaaa-aaaa",
331-
meshIDWant: "",
332-
},
333-
}
334-
335-
for _, test := range tests {
336-
t.Run(test.name, func(t *testing.T) {
337-
bootstrapContents := e2e.DefaultBootstrapContents(t, test.nodeID, "xds_server_uri")
338-
testutils.CreateBootstrapFileForTesting(t, bootstrapContents)
339-
nodeIDGot := getNodeID() // this should return the node ID plumbed into bootstrap above
340-
if nodeIDGot != test.nodeID {
341-
t.Fatalf("getNodeID: got %v, want %v", nodeIDGot, test.nodeID)
342-
}
343-
344-
meshIDGot := parseMeshIDFromNodeID(nodeIDGot)
345-
if meshIDGot != test.meshIDWant {
346-
t.Fatalf("parseMeshIDFromNodeID(%v): got %v, want %v", nodeIDGot, meshIDGot, test.meshIDWant)
347-
}
348-
})
349-
}
350-
}
351-
352305
// TestSetLabels tests the setting of labels, which snapshots the resource and
353306
// environment. It mocks the resource and environment, and then calls into
354307
// labels creation. It verifies to local labels created and metadata exchange
@@ -360,14 +313,14 @@ func (s) TestSetLabels(t *testing.T) {
360313
resourceKeyValues map[string]string
361314
csmCanonicalServiceNamePopulated bool
362315
csmWorkloadNamePopulated bool
363-
bootstrapGeneratorPopulated bool
316+
meshIDPopulated bool
364317
localLabelsWant map[string]string
365318
metadataExchangeLabelsWant map[string]string
366319
}{
367320
{
368321
name: "no-type",
369322
csmCanonicalServiceNamePopulated: true,
370-
bootstrapGeneratorPopulated: true,
323+
meshIDPopulated: true,
371324
resourceKeyValues: map[string]string{},
372325
localLabelsWant: map[string]string{
373326
"csm.workload_canonical_service": "canonical_service_name_val", // env var populated so should be set.
@@ -480,9 +433,9 @@ func (s) TestSetLabels(t *testing.T) {
480433
os.Setenv("CSM_WORKLOAD_NAME", "workload_name_val")
481434
defer os.Unsetenv("CSM_WORKLOAD_NAME")
482435
}
483-
if test.bootstrapGeneratorPopulated {
484-
bootstrapContents := e2e.DefaultBootstrapContents(t, "projects/12345/networks/mesh:mesh_id/nodes/aaaa-aaaa-aaaa-aaaa", "xds_server_uri")
485-
testutils.CreateBootstrapFileForTesting(t, bootstrapContents)
436+
if test.meshIDPopulated {
437+
os.Setenv("CSM_MESH_ID", "mesh_id")
438+
defer os.Unsetenv("CSM_MESH_ID")
486439
}
487440
var attributes []attribute.KeyValue
488441
for k, v := range test.resourceKeyValues {

0 commit comments

Comments
 (0)