Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GKE detector #154

Merged
merged 5 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}