Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Move constants into own package #1

Merged
merged 3 commits into from
Oct 11, 2018
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
6 changes: 3 additions & 3 deletions auto/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (

// Detect sequentially runs resource detection from environment varibales, AWS, and GCP.
func Detect(ctx context.Context) (*resource.Resource, error) {
return resource.DetectAll(ctx,
resource.FromEnvVars,
return resource.ChainedDetector(
resource.FromEnv,
gcp.DetectGCEInstance,
aws.DetectEC2Instance,
)
)(ctx)
}
15 changes: 5 additions & 10 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ package aws
import (
"context"

"contrib.go.opencensus.io/resource/resourcekeys"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"go.opencensus.io/resource"
)

const (
KeyAccountID = "aws.com/account_id"
KeyRegion = "aws.com/region"
KeyInstanceID = "aws.com/ec2/instance_id"
)

func DetectEC2Instance(context.Context) (*resource.Resource, error) {
c := ec2metadata.New(session.New())
if !c.Available() {
Expand All @@ -38,11 +33,11 @@ func DetectEC2Instance(context.Context) (*resource.Resource, error) {
return nil, err
}
return &resource.Resource{
Type: "aws.com/ec2/instance",
Type: resourcekeys.AWSTypeEC2Instance,
Tags: map[string]string{
KeyRegion: doc.Region,
KeyAccountID: doc.AccountID,
KeyInstanceID: doc.InstanceID,
resourcekeys.AWSKeyEC2Region: doc.Region,
resourcekeys.AWSKeyEC2AccountID: doc.AccountID,
resourcekeys.AWSKeyEC2InstanceID: doc.InstanceID,
},
}, nil
}
22 changes: 5 additions & 17 deletions gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,34 @@ import (
"strings"

"cloud.google.com/go/compute/metadata"
"contrib.go.opencensus.io/resource/resourcekeys"
"go.opencensus.io/resource"
)

const (
KeyProjectID = "cloud.google.com/project_id"
KeyZone = "cloud.google.com/zone"
KeyInstanceID = "cloud.google.com/gce/instance_id"
KeyClusterName = "cloud.google.com/gce/attributes/cluster_name"
)

func DetectGCEInstance(context.Context) (*resource.Resource, error) {
if !metadata.OnGCE() {
return nil, nil
}
res := &resource.Resource{
Type: "cloud.google.com/gce/instance",
Type: resourcekeys.GCPTypeGCEInstance,
Tags: map[string]string{},
}
instanceID, err := metadata.InstanceID()
logError(err)
if instanceID != "" {
res.Tags[KeyInstanceID] = instanceID
res.Tags[resourcekeys.GCPKeyGCEInstanceID] = instanceID
}

projectID, err := metadata.ProjectID()
logError(err)
if projectID != "" {
res.Tags[KeyProjectID] = projectID
res.Tags[resourcekeys.GCPKeyGCEProjectID] = projectID
}

zone, err := metadata.Zone()
logError(err)
if zone != "" {
res.Tags[KeyZone] = zone
}

clusterName, err := metadata.InstanceAttributeValue("cluster-name")
logError(err)
if clusterName != "" {
res.Tags[KeyClusterName] = strings.TrimSpace(clusterName)
res.Tags[resourcekeys.GCPKeyGCEZone] = zone
}

return res, nil
Expand Down
49 changes: 49 additions & 0 deletions resourcekeys/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2018, OpenCensus 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 resourcekeys contains well known type and label keys for resources.
package resourcekeys

// Constants for Kubernetes resources.
const (
K8STypeContainer = "k8s.io/container"

// A uniquely identifying name for the Kubernetes cluster. Kubernetes
// does not have cluster names as an internal concept so this may be
// set to any meaningful value within the environment. For example,
// GKE clusters have a name which can be used for this label.
K8SKeyClusterName = "k8s.io/cluster/name"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is not an official k8s terminology please comment what it is and why it is here.

K8SKeyNamespaceName = "k8s.io/namespace/name"
K8SKeyPodName = "k8s.io/pod/name"
K8SKeyContainerName = "k8s.io/container/name"
)

// Constants for AWS resources.
const (
AWSTypeEC2Instance = "aws.com/ec2/instance"

AWSKeyEC2AccountID = "aws.com/ec2/account_id"
AWSKeyEC2Region = "aws.com/ec2/region"
AWSKeyEC2InstanceID = "aws.com/ec2/instance_id"
)

// Constants for GCP resources.
const (
GCPTypeGCEInstance = "cloud.google.com/gce/instance"

// ProjectID of the GCE VM. This is not the project ID of the used client credentials.
GCPKeyGCEProjectID = "cloud.google.com/gce/project_id"
GCPKeyGCEZone = "cloud.google.com/gce/zone"
GCPKeyGCEInstanceID = "cloud.google.com/gce/instance_id"
)