From bc797ce0678b475c46ed7e85ba2a20e0b59ccb6a Mon Sep 17 00:00:00 2001 From: YANYZP Date: Fri, 24 Jul 2020 15:17:24 -0400 Subject: [PATCH 1/3] first --- detectors/gcp/gke.go | 71 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 detectors/gcp/gke.go diff --git a/detectors/gcp/gke.go b/detectors/gcp/gke.go new file mode 100644 index 00000000000..c0a8c1db810 --- /dev/null +++ b/detectors/gcp/gke.go @@ -0,0 +1,71 @@ +// 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 gcp + +import ( + "context" + "fmt" + "os" + + "cloud.google.com/go/compute/metadata" + + "go.opentelemetry.io/otel/api/kv" + "go.opentelemetry.io/otel/api/standard" + "go.opentelemetry.io/otel/sdk/resource" +) + +// GKE collects resource information of GKE computing instances +type GKE struct{} + +// compile time assertion that GCE implements the resource.Detector interface. +var _ resource.Detector = (*GKE)(nil) + +// Detect detects associated resources when running in GKE environment. +func (gke *GKE) Detect(ctx context.Context) (*resource.Resource, error) { + gcpDetecor := GCE{} + gceLablRes, err := gcpDetecor.Detect(ctx) + + if os.Getenv("KUBERNETES_SERVICE_HOST") == "" { + return gceLablRes, err + } + + var errInfo []string + if err != nil { + errInfo = append(errInfo, err.Error()) + } + + labels := []kv.KeyValue{ + standard.K8SNamespaceNameKey.String(os.Getenv("NAMESPACE")), + standard.K8SPodNameKey.String(os.Getenv("HOSTNAME")), + } + + if containerName := os.Getenv("CONTAINER_NAME"); containerName != "" { + labels = append(labels, standard.ContainerNameKey.String(containerName)) + } + + if clusterName, err := metadata.Get("cluster-name"); hasProblem(err) { + errInfo = append(errInfo, err.Error()) + } else if clusterName != "" { + labels = append(labels, standard.K8SClusterNameKey.String(clusterName)) + } + + k8sLabelRes := resource.New(labels...) + var aggregatedErr error + if len(errInfo) > 0 { + aggregatedErr = fmt.Errorf("detecting GKE resources: %s", errInfo) + } + + return resource.Merge(gceLablRes, k8sLabelRes), aggregatedErr +} From ad57051b447685fef8bf2481fa12ce0743ddc2c9 Mon Sep 17 00:00:00 2001 From: YANYZP Date: Fri, 24 Jul 2020 15:21:11 -0400 Subject: [PATCH 2/3] used InstanceAttributeValue to get cluster name --- detectors/gcp/gke.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/detectors/gcp/gke.go b/detectors/gcp/gke.go index c0a8c1db810..5734dac79fe 100644 --- a/detectors/gcp/gke.go +++ b/detectors/gcp/gke.go @@ -55,7 +55,7 @@ func (gke *GKE) Detect(ctx context.Context) (*resource.Resource, error) { labels = append(labels, standard.ContainerNameKey.String(containerName)) } - if clusterName, err := metadata.Get("cluster-name"); hasProblem(err) { + if clusterName, err := metadata.InstanceAttributeValue("cluster-name"); hasProblem(err) { errInfo = append(errInfo, err.Error()) } else if clusterName != "" { labels = append(labels, standard.K8SClusterNameKey.String(clusterName)) From 7908e80594a63f1b7baf3c7f6c61872b30b95c36 Mon Sep 17 00:00:00 2001 From: YANYZP Date: Mon, 27 Jul 2020 09:49:45 -0400 Subject: [PATCH 3/3] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 439888200f2..e9f8d61f865 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Added +- Create a detector that collects resources from GKE machines. (#139) - Create a detector that collects resources from GCE machines. (#132) - Add instrumentation for Kafka (github.com/Shopify/sarama). (#134) - Add links and status message for mock span. (#134)