From c8c0bfb1a5e22f5838eb6bb86608b29ef0eca0ef Mon Sep 17 00:00:00 2001 From: msukruth <57657460+msukruth@users.noreply.github.com> Date: Mon, 15 Jun 2020 13:49:28 -0700 Subject: [PATCH] Add ProxyV2 resource map (#240) --- retrieval/resource_map.go | 15 +++++++++++++ retrieval/resource_map_test.go | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/retrieval/resource_map.go b/retrieval/resource_map.go index dcdaf821..a9f3cfb1 100644 --- a/retrieval/resource_map.go +++ b/retrieval/resource_map.go @@ -117,10 +117,25 @@ var ProxyResourceMap = ResourceMap{ }, } +var ProxyV2ResourceMap = ResourceMap{ + Type: "apigee.googleapis.com/ProxyV2", + MatchLabel: "__meta_kubernetes_pod_label_type_proxy_v2", + LabelMap: map[string]labelTranslation{ + ProjectIDLabel: constValue("resource_container"), + KubernetesLocationLabel: constValue("location"), + "org": constValue("org"), + "env": constValue("env"), + "proxy_name": constValue("proxy_name"), + "runtime_version": constValue("runtime_version"), + "instance_id": constValue("instance_id"), + }, +} + type ResourceMapList []ResourceMap // When you add new elements, you also probably want to update TestResourceMappingsOrder. var ResourceMappings = ResourceMapList{ + ProxyV2ResourceMap, ProxyResourceMap, DevappResourceMap, { diff --git a/retrieval/resource_map_test.go b/retrieval/resource_map_test.go index 5d2e2d29..7debe09e 100644 --- a/retrieval/resource_map_test.go +++ b/retrieval/resource_map_test.go @@ -205,6 +205,44 @@ func TestTranslateProxy(t *testing.T) { } } +func TestTranslateProxyV2(t *testing.T) { + discoveredLabels := labels.Labels{ + {"__meta_kubernetes_pod_label_type_proxy_v2", "true"}, + {ProjectIDLabel, "my-project"}, + {KubernetesLocationLabel, "us-central1-a"}, + } + metricLabels := labels.Labels{ + {"proxy_name", "my-name"}, + {"org", "my-org"}, + {"env", "my-env"}, + {"runtime_version", "my-revision"}, + {"instance_id", "my-instance"}, + {"extra_label", "my-label"}, + } + expectedLabels := map[string]string{ + "resource_container": "my-project", + "location": "us-central1-a", + "org": "my-org", + "env": "my-env", + "proxy_name": "my-name", + "runtime_version": "my-revision", + "instance_id": "my-instance", + } + expectedFinalLabels := labels.Labels{ + {"extra_label", "my-label"}, + } + if labels, finalLabels := ProxyV2ResourceMap.Translate(discoveredLabels, metricLabels); labels == nil { + t.Errorf("Expected %v, actual nil", expectedLabels) + } else { + if diff := cmp.Diff(expectedLabels, labels); len(diff) > 0 { + t.Error(diff) + } + if diff := cmp.Diff(expectedFinalLabels, finalLabels); len(diff) > 0 { + t.Error(diff) + } + } +} + func (m *ResourceMapList) getByType(t string) (*ResourceMap, bool) { for _, m := range *m { if m.Type == t { @@ -236,6 +274,7 @@ func TestResourceMappingsOrder(t *testing.T) { {"k8s_pod", "k8s_node"}, {"k8s_node", "gce_instance"}, {"k8s_node", "aws_ec2_instance"}, + {"apigee.googleapis.com/ProxyV2", "k8s_container"}, {"apigee.googleapis.com/Proxy", "k8s_container"}, {"apigee.googleapis.com/Devapp", "k8s_container"}, }