Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit f86b8f1

Browse files
authored
Default mapping for knative_revision & autodetect labels (#252)
* Add default mapping for knative_revision Monitored Resource and autodetect as many labels as possible metadata * Split monitoredresource package into monitoredresource/gcp and monitoredresource/aws Only detect gcp labels. * Use golang deprecation notice format
1 parent ddbc64a commit f86b8f1

12 files changed

Lines changed: 781 additions & 209 deletions

monitoredresource/aws_identity_doc_utils.go renamed to monitoredresource/aws/aws_identity_doc_utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018, OpenCensus Authors
1+
// Copyright 2020, OpenCensus Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package monitoredresource
15+
package aws
1616

1717
import (
1818
"github.com/aws/aws-sdk-go/aws/ec2metadata"
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2020, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package aws
16+
17+
import (
18+
"fmt"
19+
"sync"
20+
)
21+
22+
// Interface is a type that represent monitor resource that satisfies monitoredresource.Interface
23+
type Interface interface {
24+
25+
// MonitoredResource returns the resource type and resource labels.
26+
MonitoredResource() (resType string, labels map[string]string)
27+
}
28+
29+
// EC2Instance represents aws_ec2_instance type monitored resource.
30+
// For definition refer to
31+
// https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance
32+
type EC2Instance struct {
33+
34+
// AWSAccount is the AWS account number for the VM.
35+
AWSAccount string
36+
37+
// InstanceID is the instance id of the instance.
38+
InstanceID string
39+
40+
// Region is the AWS region for the VM. The format of this field is "aws:{region}",
41+
// where supported values for {region} are listed at
42+
// http://docs.aws.amazon.com/general/latest/gr/rande.html.
43+
Region string
44+
}
45+
46+
// MonitoredResource returns resource type and resource labels for AWSEC2Instance
47+
func (aws *EC2Instance) MonitoredResource() (resType string, labels map[string]string) {
48+
labels = map[string]string{
49+
"aws_account": aws.AWSAccount,
50+
"instance_id": aws.InstanceID,
51+
"region": aws.Region,
52+
}
53+
return "aws_ec2_instance", labels
54+
}
55+
56+
// Autodetect auto detects monitored resources based on
57+
// the environment where the application is running.
58+
// It supports detection of following resource types
59+
// 1. aws_ec2_instance:
60+
//
61+
// Returns MonitoredResInterface which implements getLabels() and getType()
62+
// For resource definition go to https://cloud.google.com/monitoring/api/resources
63+
func Autodetect() Interface {
64+
return func() Interface {
65+
detectOnce.Do(func() {
66+
autoDetected = detectResourceType(retrieveAWSIdentityDocument())
67+
})
68+
return autoDetected
69+
}()
70+
71+
}
72+
73+
// createAWSEC2InstanceMonitoredResource creates a aws_ec2_instance monitored resource
74+
// awsIdentityDoc contains AWS EC2 specific attributes.
75+
func createEC2InstanceMonitoredResource(awsIdentityDoc *awsIdentityDocument) *EC2Instance {
76+
awsInstance := EC2Instance{
77+
AWSAccount: awsIdentityDoc.accountID,
78+
InstanceID: awsIdentityDoc.instanceID,
79+
Region: fmt.Sprintf("aws:%s", awsIdentityDoc.region),
80+
}
81+
return &awsInstance
82+
}
83+
84+
// detectOnce is used to make sure AWS metadata detect function executes only once.
85+
var detectOnce sync.Once
86+
87+
// autoDetected is the metadata detected after the first execution of Autodetect function.
88+
var autoDetected Interface
89+
90+
// detectResourceType determines the resource type.
91+
// awsIdentityDoc contains AWS EC2 attributes. nil if it is not AWS EC2 environment
92+
func detectResourceType(awsIdentityDoc *awsIdentityDocument) Interface {
93+
if awsIdentityDoc != nil {
94+
return createEC2InstanceMonitoredResource(awsIdentityDoc)
95+
}
96+
return nil
97+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2020, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package aws
16+
17+
import (
18+
"testing"
19+
)
20+
21+
func TestAWSEC2InstanceMonitoredResources(t *testing.T) {
22+
awsIdentityDoc := &awsIdentityDocument{
23+
"123456789012",
24+
"i-1234567890abcdef0",
25+
"us-west-2",
26+
}
27+
autoDetected := detectResourceType(awsIdentityDoc)
28+
29+
if autoDetected == nil {
30+
t.Fatal("AWSEC2InstanceMonitoredResource nil")
31+
}
32+
resType, labels := autoDetected.MonitoredResource()
33+
if resType != "aws_ec2_instance" ||
34+
labels["instance_id"] != "i-1234567890abcdef0" ||
35+
labels["aws_account"] != "123456789012" ||
36+
labels["region"] != "aws:us-west-2" {
37+
t.Errorf("AWSEC2InstanceMonitoredResource Failed: %v", autoDetected)
38+
}
39+
}

monitoredresource/deprecated.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2020, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package monitoredresource
16+
17+
import (
18+
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource/aws"
19+
"contrib.go.opencensus.io/exporter/stackdriver/monitoredresource/gcp"
20+
)
21+
22+
// GKEContainer represents gke_container type monitored resource.
23+
// For definition refer to
24+
// https://cloud.google.com/monitoring/api/resources#tag_gke_container
25+
// Deprecated: please use gcp.GKEContainer from "contrib.go.opencensus.io/exporter/stackdriver/monitoredresource/gcp".
26+
type GKEContainer struct {
27+
// ProjectID is the identifier of the GCP project associated with this resource, such as "my-project".
28+
ProjectID string
29+
30+
// InstanceID is the numeric VM instance identifier assigned by Compute Engine.
31+
InstanceID string
32+
33+
// ClusterName is the name for the cluster the container is running in.
34+
ClusterName string
35+
36+
// ContainerName is the name of the container.
37+
ContainerName string
38+
39+
// NamespaceID is the identifier for the cluster namespace the container is running in
40+
NamespaceID string
41+
42+
// PodID is the identifier for the pod the container is running in.
43+
PodID string
44+
45+
// Zone is the Compute Engine zone in which the VM is running.
46+
Zone string
47+
48+
// LoggingMonitoringV2Enabled is the identifier if user enabled V2 logging and monitoring for GKE
49+
LoggingMonitoringV2Enabled bool
50+
}
51+
52+
// MonitoredResource returns resource type and resource labels for GKEContainer
53+
func (gke *GKEContainer) MonitoredResource() (resType string, labels map[string]string) {
54+
gcpGKE := gcp.GKEContainer(*gke)
55+
return gcpGKE.MonitoredResource()
56+
}
57+
58+
// GCEInstance represents gce_instance type monitored resource.
59+
// For definition refer to
60+
// https://cloud.google.com/monitoring/api/resources#tag_gce_instance
61+
// Deprecated: please use gcp.GCEInstance from "contrib.go.opencensus.io/exporter/stackdriver/monitoredresource/gcp".
62+
type GCEInstance struct {
63+
64+
// ProjectID is the identifier of the GCP project associated with this resource, such as "my-project".
65+
ProjectID string
66+
67+
// InstanceID is the numeric VM instance identifier assigned by Compute Engine.
68+
InstanceID string
69+
70+
// Zone is the Compute Engine zone in which the VM is running.
71+
Zone string
72+
}
73+
74+
// MonitoredResource returns resource type and resource labels for GCEInstance
75+
func (gce *GCEInstance) MonitoredResource() (resType string, labels map[string]string) {
76+
gcpGCE := gcp.GCEInstance(*gce)
77+
return gcpGCE.MonitoredResource()
78+
}
79+
80+
// AWSEC2Instance represents aws_ec2_instance type monitored resource.
81+
// For definition refer to
82+
// https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance
83+
// Deprecated: please use aws.EC2Container from "contrib.go.opencensus.io/exporter/stackdriver/monitoredresource/aws".
84+
type AWSEC2Instance struct {
85+
// AWSAccount is the AWS account number for the VM.
86+
AWSAccount string
87+
88+
// InstanceID is the instance id of the instance.
89+
InstanceID string
90+
91+
// Region is the AWS region for the VM. The format of this field is "aws:{region}",
92+
// where supported values for {region} are listed at
93+
// http://docs.aws.amazon.com/general/latest/gr/rande.html.
94+
Region string
95+
}
96+
97+
// MonitoredResource returns resource type and resource labels for AWSEC2Instance
98+
func (ec2 *AWSEC2Instance) MonitoredResource() (resType string, labels map[string]string) {
99+
awsEC2 := aws.EC2Instance(*ec2)
100+
return awsEC2.MonitoredResource()
101+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2020, OpenCensus Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package monitoredresource
16+
17+
import (
18+
"os"
19+
"testing"
20+
)
21+
22+
const (
23+
GCPProjectIDStr = "gcp-project"
24+
GCPInstanceIDStr = "instance"
25+
GCPZoneStr = "us-east1"
26+
GKENamespaceStr = "namespace"
27+
GKEPodIDStr = "pod-id"
28+
GKEContainerNameStr = "container"
29+
GKEClusterNameStr = "cluster"
30+
)
31+
32+
func TestGKEContainerMonitoredResources(t *testing.T) {
33+
os.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1")
34+
autoDetected := GKEContainer{
35+
InstanceID: GCPInstanceIDStr,
36+
ProjectID: GCPProjectIDStr,
37+
Zone: GCPZoneStr,
38+
ClusterName: GKEClusterNameStr,
39+
ContainerName: GKEContainerNameStr,
40+
NamespaceID: GKENamespaceStr,
41+
PodID: GKEPodIDStr,
42+
}
43+
44+
resType, labels := autoDetected.MonitoredResource()
45+
if resType != "gke_container" ||
46+
labels["instance_id"] != GCPInstanceIDStr ||
47+
labels["project_id"] != GCPProjectIDStr ||
48+
labels["cluster_name"] != GKEClusterNameStr ||
49+
labels["container_name"] != GKEContainerNameStr ||
50+
labels["zone"] != GCPZoneStr ||
51+
labels["namespace_id"] != GKENamespaceStr ||
52+
labels["pod_id"] != GKEPodIDStr {
53+
t.Errorf("GKEContainerMonitoredResource Failed: %v", autoDetected)
54+
}
55+
}
56+
57+
func TestGKEContainerMonitoredResourcesV2(t *testing.T) {
58+
os.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1")
59+
autoDetected := GKEContainer{
60+
InstanceID: GCPInstanceIDStr,
61+
ProjectID: GCPProjectIDStr,
62+
Zone: GCPZoneStr,
63+
ClusterName: GKEClusterNameStr,
64+
ContainerName: GKEContainerNameStr,
65+
NamespaceID: GKENamespaceStr,
66+
PodID: GKEPodIDStr,
67+
LoggingMonitoringV2Enabled: true,
68+
}
69+
70+
resType, labels := autoDetected.MonitoredResource()
71+
if resType != "k8s_container" ||
72+
labels["project_id"] != GCPProjectIDStr ||
73+
labels["cluster_name"] != GKEClusterNameStr ||
74+
labels["container_name"] != GKEContainerNameStr ||
75+
labels["location"] != GCPZoneStr ||
76+
labels["namespace_name"] != GKENamespaceStr ||
77+
labels["pod_name"] != GKEPodIDStr {
78+
t.Errorf("GKEContainerMonitoredResourceV2 Failed: %v", autoDetected)
79+
}
80+
}
81+
82+
func TestGCEInstanceMonitoredResources(t *testing.T) {
83+
os.Setenv("KUBERNETES_SERVICE_HOST", "")
84+
autoDetected := GCEInstance{
85+
InstanceID: GCPInstanceIDStr,
86+
ProjectID: GCPProjectIDStr,
87+
Zone: GCPZoneStr,
88+
}
89+
90+
resType, labels := autoDetected.MonitoredResource()
91+
if resType != "gce_instance" ||
92+
labels["instance_id"] != GCPInstanceIDStr ||
93+
labels["project_id"] != GCPProjectIDStr ||
94+
labels["zone"] != GCPZoneStr {
95+
t.Errorf("GCEInstanceMonitoredResource Failed: %v", autoDetected)
96+
}
97+
}
98+
99+
func TestAWSEC2InstanceMonitoredResources(t *testing.T) {
100+
autoDetected := AWSEC2Instance{
101+
AWSAccount: "123456789012",
102+
InstanceID: "i-1234567890abcdef0",
103+
Region: "aws:us-west-2",
104+
}
105+
106+
resType, labels := autoDetected.MonitoredResource()
107+
if resType != "aws_ec2_instance" ||
108+
labels["instance_id"] != "i-1234567890abcdef0" ||
109+
labels["aws_account"] != "123456789012" ||
110+
labels["region"] != "aws:us-west-2" {
111+
t.Errorf("AWSEC2InstanceMonitoredResource Failed: %v", autoDetected)
112+
}
113+
}

monitoredresource/gcp_metadata_config.go renamed to monitoredresource/gcp/gcp_metadata_config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018, OpenCensus Authors
1+
// Copyright 2020, OpenCensus Authors
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package monitoredresource
15+
package gcp
1616

1717
import (
1818
"context"

0 commit comments

Comments
 (0)