Skip to content

Commit

Permalink
GKE detector (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
YANYZP committed Jul 27, 2020
1 parent 5c2cfc3 commit 05eaf53
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
71 changes: 71 additions & 0 deletions detectors/gcp/gke.go
Original file line number Diff line number Diff line change
@@ -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.InstanceAttributeValue("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
}

0 comments on commit 05eaf53

Please sign in to comment.