MCPcopy
hub / github.com/grpc/grpc-go / constructMetadataFromEnv

Function constructMetadataFromEnv

stats/opentelemetry/csm/pluginoption.go:197–231  ·  view source on GitHub ↗

constructMetadataFromEnv creates local labels and labels to send to the peer using metadata exchange based off resource detection and environment variables. Returns local labels, and base 64 encoded protobuf.Struct containing metadata exchange labels.

(ctx context.Context)

Source from the content-addressed store, hash-verified

195// Returns local labels, and base 64 encoded protobuf.Struct containing metadata
196// exchange labels.
197func constructMetadataFromEnv(ctx context.Context) (map[string]string, string) {
198 set := getAttrSetFromResourceDetector(ctx)
199
200 labels := make(map[string]string)
201 labels["type"] = getFromResource("cloud.platform", set)
202 labels["canonical_service"] = getEnv("CSM_CANONICAL_SERVICE_NAME")
203
204 // If type is not GCE or GKE only metadata exchange labels are "type" and
205 // "canonical_service".
206 cloudPlatformVal := labels["type"]
207 if cloudPlatformVal != "gcp_kubernetes_engine" && cloudPlatformVal != "gcp_compute_engine" {
208 return initializeLocalAndMetadataLabels(labels)
209 }
210
211 // GCE and GKE labels:
212 labels["workload_name"] = getEnv("CSM_WORKLOAD_NAME")
213
214 locationVal := "unknown"
215 if resourceVal, ok := set.Value("cloud.availability_zone"); ok && resourceVal.Type() == attribute.STRING {
216 locationVal = resourceVal.AsString()
217 } else if resourceVal, ok = set.Value("cloud.region"); ok && resourceVal.Type() == attribute.STRING {
218 locationVal = resourceVal.AsString()
219 }
220 labels["location"] = locationVal
221
222 labels["project_id"] = getFromResource("cloud.account.id", set)
223 if cloudPlatformVal == "gcp_compute_engine" {
224 return initializeLocalAndMetadataLabels(labels)
225 }
226
227 // GKE specific labels:
228 labels["namespace_name"] = getFromResource("k8s.namespace.name", set)
229 labels["cluster_name"] = getFromResource("k8s.cluster.name", set)
230 return initializeLocalAndMetadataLabels(labels)
231}
232
233// initializeLocalAndMetadataLabels csm local labels for a CSM Plugin Option to
234// record. It also builds out a base 64 encoded protobuf.Struct containing the

Callers 2

TestSetLabelsMethod · 0.85
newPluginOptionFunction · 0.85

Calls 5

getFromResourceFunction · 0.85
getEnvFunction · 0.85
TypeMethod · 0.65
ValueMethod · 0.45

Tested by 1

TestSetLabelsMethod · 0.68