From 2cfaf27576a6776c92f377bff017118bf46620b7 Mon Sep 17 00:00:00 2001 From: YANYZP Date: Fri, 10 Jul 2020 17:02:53 -0400 Subject: [PATCH 1/3] first --- detect/gke/gke.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 detect/gke/gke.go diff --git a/detect/gke/gke.go b/detect/gke/gke.go new file mode 100644 index 00000000000..c3fac1331c9 --- /dev/null +++ b/detect/gke/gke.go @@ -0,0 +1,70 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package gke + +// import ( +// "context" +// "log" +// "os" +// "strings" + +// "cloud.google.com/go/compute/metadata" +// "contrib.go.opencensus.io/resource/gcp" // how to import local unpublished package?? + +// // TODO: import "go.opentelemetry.io/otel/sdk/resource/resourcekeys" after publishing it +// // for now, the resourcekeys is in const.go +// "go.opentelemetry.io/otel/api/kv" +// "go.opentelemetry.io/otel/sdk/resource" +// ) + +// // Detect detects associated resources when running in GKE environment. +// func Detect(ctx context.Context) (*resource.Resource, error) { +// if os.Getenv("KUBERNETES_SERVICE_HOST") == "" { +// return nil, nil +// } + +// labels := []kv.KeyValue{} +// clusterName, err := metadata.InstanceAttributeValue("cluster-name") +// logError(err) + +// if clusterName != "" { +// labels = append(labels, kv.String(K8SKeyClusterName, clusterName)) +// } + +// labels = append(labels, kv.String(K8SKeyNamespaceName, os.Getenv("NAMESPACE"))) + +// labels = append(labels, kv.String(K8SKeyPodName, os.Getenv("HOSTNAME"))) + +// labels = append(labels, kv.String(ContainerKeyName, os.Getenv("CONTAINER_NAME"))) + +// k8sLabelRes := resource.New(labels...), nil + +// gceLablRes, err := gce.Detect(ctx) + +// if err != nil { +// return nil, nil +// } + +// return resource.Merge(gceLablRes, k8sLabelRes), nil +// } + +// // logError logs error only if the error is present and it is not 'not defined' +// func logError(err error) { +// if err != nil { +// if !strings.Contains(err.Error(), "not defined") { +// log.Printf("Error retrieving gcp metadata: %v", err) +// } +// } +// } From 8321555a3ec92479ca8acb81ea312b8dfab5d6a2 Mon Sep 17 00:00:00 2001 From: YANYZP Date: Fri, 10 Jul 2020 17:29:30 -0400 Subject: [PATCH 2/3] gke detector --- detect/gke/gke.go | 83 ++++++++++--------- go.mod | 1 + go.sum | 2 + .../github.com/emicklei/go-restful/go.sum | 1 + .../github.com/gin-gonic/gin/go.sum | 1 + instrumentation/github.com/gorilla/mux/go.sum | 1 + .../github.com/labstack/echo/go.sum | 1 + .../go.mongodb.org/mongo-driver/go.sum | 1 + instrumentation/gopkg.in/macaron.v1/go.sum | 1 + 9 files changed, 51 insertions(+), 41 deletions(-) diff --git a/detect/gke/gke.go b/detect/gke/gke.go index c3fac1331c9..0b5c8d247ec 100644 --- a/detect/gke/gke.go +++ b/detect/gke/gke.go @@ -14,57 +14,58 @@ package gke -// import ( -// "context" -// "log" -// "os" -// "strings" +import ( + "context" + "log" + "os" + "strings" -// "cloud.google.com/go/compute/metadata" -// "contrib.go.opencensus.io/resource/gcp" // how to import local unpublished package?? + "cloud.google.com/go/compute/metadata" -// // TODO: import "go.opentelemetry.io/otel/sdk/resource/resourcekeys" after publishing it -// // for now, the resourcekeys is in const.go -// "go.opentelemetry.io/otel/api/kv" -// "go.opentelemetry.io/otel/sdk/resource" -// ) + "go.opentelemetry.io/otel/api/kv" + "go.opentelemetry.io/otel/api/standard" + "go.opentelemetry.io/otel/sdk/resource" -// // Detect detects associated resources when running in GKE environment. -// func Detect(ctx context.Context) (*resource.Resource, error) { -// if os.Getenv("KUBERNETES_SERVICE_HOST") == "" { -// return nil, nil -// } + "go.opentelemetry.io/contrib/detect/gcp" +) -// labels := []kv.KeyValue{} -// clusterName, err := metadata.InstanceAttributeValue("cluster-name") -// logError(err) +// Detect detects associated resources when running in GKE environment. +func Detect(ctx context.Context) (*resource.Resource, error) { + if os.Getenv("KUBERNETES_SERVICE_HOST") == "" { + return nil, nil + } -// if clusterName != "" { -// labels = append(labels, kv.String(K8SKeyClusterName, clusterName)) -// } + labels := []kv.KeyValue{ + standard.K8SNamespaceNameKey.String(os.Getenv("NAMESPACE")), + standard.K8SPodNameKey.String(os.Getenv("HOSTNAME")), + standard.ContainerNameKey.String(os.Getenv("CONTAINER_NAME")), + } -// labels = append(labels, kv.String(K8SKeyNamespaceName, os.Getenv("NAMESPACE"))) + clusterName, err := metadata.InstanceAttributeValue("cluster-name") + logError(err) -// labels = append(labels, kv.String(K8SKeyPodName, os.Getenv("HOSTNAME"))) + if clusterName != "" { + labels = append(labels, standard.K8SClusterNameKey.String(clusterName)) + } -// labels = append(labels, kv.String(ContainerKeyName, os.Getenv("CONTAINER_NAME"))) + k8sLabelRes := resource.New(labels...) -// k8sLabelRes := resource.New(labels...), nil + gcpDetecor := gcp.GCP{} -// gceLablRes, err := gce.Detect(ctx) + gceLablRes, err := gcpDetecor.Detect(ctx) -// if err != nil { -// return nil, nil -// } + if err != nil { + return nil, err + } -// return resource.Merge(gceLablRes, k8sLabelRes), nil -// } + return resource.Merge(gceLablRes, k8sLabelRes), nil +} -// // logError logs error only if the error is present and it is not 'not defined' -// func logError(err error) { -// if err != nil { -// if !strings.Contains(err.Error(), "not defined") { -// log.Printf("Error retrieving gcp metadata: %v", err) -// } -// } -// } +// logError logs error only if the error is present and it is not 'not defined' +func logError(err error) { + if err != nil { + if !strings.Contains(err.Error(), "not defined") { + log.Printf("Error retrieving gcp metadata: %v", err) + } + } +} diff --git a/go.mod b/go.mod index d5e1f9101f1..e975e13371e 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.14 require ( cloud.google.com/go v0.26.0 go.opentelemetry.io/otel v0.7.0 + golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect google.golang.org/grpc v1.30.0 ) diff --git a/go.sum b/go.sum index 7a9c90bef57..39f5222f76b 100644 --- a/go.sum +++ b/go.sum @@ -61,6 +61,8 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/instrumentation/github.com/emicklei/go-restful/go.sum b/instrumentation/github.com/emicklei/go-restful/go.sum index 24d9401a95e..ef23ffd818e 100644 --- a/instrumentation/github.com/emicklei/go-restful/go.sum +++ b/instrumentation/github.com/emicklei/go-restful/go.sum @@ -65,6 +65,7 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/instrumentation/github.com/gin-gonic/gin/go.sum b/instrumentation/github.com/gin-gonic/gin/go.sum index 232c6f42581..0a848e7bfb4 100644 --- a/instrumentation/github.com/gin-gonic/gin/go.sum +++ b/instrumentation/github.com/gin-gonic/gin/go.sum @@ -89,6 +89,7 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/instrumentation/github.com/gorilla/mux/go.sum b/instrumentation/github.com/gorilla/mux/go.sum index e10bc46d1f9..0fec3e3e357 100644 --- a/instrumentation/github.com/gorilla/mux/go.sum +++ b/instrumentation/github.com/gorilla/mux/go.sum @@ -61,6 +61,7 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/instrumentation/github.com/labstack/echo/go.sum b/instrumentation/github.com/labstack/echo/go.sum index eac8f460814..0f39dc10f54 100644 --- a/instrumentation/github.com/labstack/echo/go.sum +++ b/instrumentation/github.com/labstack/echo/go.sum @@ -82,6 +82,7 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b h1:0mm1VjtFUOIlE1SbDlwjYaDxZVDP2S5ou6y0gSgXHu8= golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= diff --git a/instrumentation/go.mongodb.org/mongo-driver/go.sum b/instrumentation/go.mongodb.org/mongo-driver/go.sum index 0e630f3c091..4c5aaeed113 100644 --- a/instrumentation/go.mongodb.org/mongo-driver/go.sum +++ b/instrumentation/go.mongodb.org/mongo-driver/go.sum @@ -136,6 +136,7 @@ golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/instrumentation/gopkg.in/macaron.v1/go.sum b/instrumentation/gopkg.in/macaron.v1/go.sum index 5422f30b568..ca3037b7931 100644 --- a/instrumentation/gopkg.in/macaron.v1/go.sum +++ b/instrumentation/gopkg.in/macaron.v1/go.sum @@ -80,6 +80,7 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= From 22d917b3e226ec5bd6cc782279ce7d24258aea21 Mon Sep 17 00:00:00 2001 From: YANYZP Date: Fri, 10 Jul 2020 18:00:53 -0400 Subject: [PATCH 3/3] using Get func --- detect/gke/gke.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detect/gke/gke.go b/detect/gke/gke.go index 0b5c8d247ec..fc8ea7b21af 100644 --- a/detect/gke/gke.go +++ b/detect/gke/gke.go @@ -41,7 +41,7 @@ func Detect(ctx context.Context) (*resource.Resource, error) { standard.ContainerNameKey.String(os.Getenv("CONTAINER_NAME")), } - clusterName, err := metadata.InstanceAttributeValue("cluster-name") + clusterName, err := metadata.Get("cluster-name") logError(err) if clusterName != "" {