-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
YANYZP
committed
Jul 27, 2020
1 parent
5c2cfc3
commit 05eaf53
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |