|
| 1 | +// Copyright 2018, 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 | + "fmt" |
| 19 | + "os" |
| 20 | + "sync" |
| 21 | +) |
| 22 | + |
| 23 | +// Interface is a type that represent monitor resource that satisfies monitoredresource.Interface |
| 24 | +type Interface interface { |
| 25 | + |
| 26 | + // MonitoredResource returns the resource type and resource labels. |
| 27 | + MonitoredResource() (resType string, labels map[string]string) |
| 28 | +} |
| 29 | + |
| 30 | +// GKEContainer represents gke_container type monitored resource. |
| 31 | +// For definition refer to |
| 32 | +// https://cloud.google.com/monitoring/api/resources#tag_gke_container |
| 33 | +type GKEContainer struct { |
| 34 | + |
| 35 | + // ProjectID is the identifier of the GCP project associated with this resource, such as "my-project". |
| 36 | + ProjectID string |
| 37 | + |
| 38 | + // InstanceID is the numeric VM instance identifier assigned by Compute Engine. |
| 39 | + InstanceID string |
| 40 | + |
| 41 | + // ClusterName is the name for the cluster the container is running in. |
| 42 | + ClusterName string |
| 43 | + |
| 44 | + // ContainerName is the name of the container. |
| 45 | + ContainerName string |
| 46 | + |
| 47 | + // NamespaceID is the identifier for the cluster namespace the container is running in |
| 48 | + NamespaceID string |
| 49 | + |
| 50 | + // PodID is the identifier for the pod the container is running in. |
| 51 | + PodID string |
| 52 | + |
| 53 | + // Zone is the Compute Engine zone in which the VM is running. |
| 54 | + Zone string |
| 55 | +} |
| 56 | + |
| 57 | +// MonitoredResource returns resource type and resource labels for GKEContainer |
| 58 | +func (gke *GKEContainer) MonitoredResource() (resType string, labels map[string]string) { |
| 59 | + labels = map[string]string{ |
| 60 | + "project_id": gke.ProjectID, |
| 61 | + "instance_id": gke.InstanceID, |
| 62 | + "zone": gke.Zone, |
| 63 | + "cluster_name": gke.ClusterName, |
| 64 | + "container_name": gke.ContainerName, |
| 65 | + "namespace_id": gke.NamespaceID, |
| 66 | + "pod_id": gke.PodID, |
| 67 | + } |
| 68 | + return "gke_container", labels |
| 69 | +} |
| 70 | + |
| 71 | +// GCEInstance represents gce_instance type monitored resource. |
| 72 | +// For definition refer to |
| 73 | +// https://cloud.google.com/monitoring/api/resources#tag_gce_instance |
| 74 | +type GCEInstance struct { |
| 75 | + |
| 76 | + // ProjectID is the identifier of the GCP project associated with this resource, such as "my-project". |
| 77 | + ProjectID string |
| 78 | + |
| 79 | + // InstanceID is the numeric VM instance identifier assigned by Compute Engine. |
| 80 | + InstanceID string |
| 81 | + |
| 82 | + // Zone is the Compute Engine zone in which the VM is running. |
| 83 | + Zone string |
| 84 | +} |
| 85 | + |
| 86 | +// MonitoredResource returns resource type and resource labels for GCEInstance |
| 87 | +func (gce *GCEInstance) MonitoredResource() (resType string, labels map[string]string) { |
| 88 | + labels = map[string]string{ |
| 89 | + "project_id": gce.ProjectID, |
| 90 | + "instance_id": gce.InstanceID, |
| 91 | + "zone": gce.Zone, |
| 92 | + } |
| 93 | + return "gce_instance", labels |
| 94 | +} |
| 95 | + |
| 96 | +// AWSEC2Instance represents aws_ec2_instance type monitored resource. |
| 97 | +// For definition refer to |
| 98 | +// https://cloud.google.com/monitoring/api/resources#tag_aws_ec2_instance |
| 99 | +type AWSEC2Instance struct { |
| 100 | + |
| 101 | + // AWSAccount is the AWS account number for the VM. |
| 102 | + AWSAccount string |
| 103 | + |
| 104 | + // InstanceID is the instance id of the instance. |
| 105 | + InstanceID string |
| 106 | + |
| 107 | + // Region is the AWS region for the VM. The format of this field is "aws:{region}", |
| 108 | + // where supported values for {region} are listed at |
| 109 | + // http://docs.aws.amazon.com/general/latest/gr/rande.html. |
| 110 | + Region string |
| 111 | +} |
| 112 | + |
| 113 | +// MonitoredResource returns resource type and resource labels for AWSEC2Instance |
| 114 | +func (aws *AWSEC2Instance) MonitoredResource() (resType string, labels map[string]string) { |
| 115 | + labels = map[string]string{ |
| 116 | + "aws_account": aws.AWSAccount, |
| 117 | + "instance_id": aws.InstanceID, |
| 118 | + "region": aws.Region, |
| 119 | + } |
| 120 | + return "aws_ec2_instance", labels |
| 121 | +} |
| 122 | + |
| 123 | +// Autodetect auto detects monitored resources based on |
| 124 | +// the environment where the application is running. |
| 125 | +// It supports detection of following resource types |
| 126 | +// 1. gke_container: |
| 127 | +// 2. gce_instance: |
| 128 | +// 3. aws_ec2_instance: |
| 129 | +// |
| 130 | +// Returns MonitoredResInterface which implements getLabels() and getType() |
| 131 | +// For resource definition go to https://cloud.google.com/monitoring/api/resources |
| 132 | +func Autodetect() Interface { |
| 133 | + return func() Interface { |
| 134 | + var autoDetected Interface |
| 135 | + var awsIdentityDoc *awsIdentityDocument |
| 136 | + var gcpMetadata *gcpMetadata |
| 137 | + detectOnce.Do(func() { |
| 138 | + |
| 139 | + // First attempts to retrieve AWS Identity Doc and GCP metadata. |
| 140 | + // It then determines the resource type |
| 141 | + // In GCP and AWS environment both func finishes quickly. However, |
| 142 | + // in an environment other than those (e.g local laptop) it |
| 143 | + // takes 2 seconds for GCP and 5-6 for AWS. |
| 144 | + var wg sync.WaitGroup |
| 145 | + wg.Add(2) |
| 146 | + |
| 147 | + go func() { |
| 148 | + defer wg.Done() |
| 149 | + awsIdentityDoc = retrieveAWSIdentityDocument() |
| 150 | + }() |
| 151 | + go func() { |
| 152 | + defer wg.Done() |
| 153 | + gcpMetadata = retrieveGCPMetadata() |
| 154 | + }() |
| 155 | + |
| 156 | + wg.Wait() |
| 157 | + autoDetected = detectResourceType(awsIdentityDoc, gcpMetadata) |
| 158 | + }) |
| 159 | + return autoDetected |
| 160 | + }() |
| 161 | + |
| 162 | +} |
| 163 | + |
| 164 | +// createAWSEC2InstanceMonitoredResource creates a aws_ec2_instance monitored resource |
| 165 | +// awsIdentityDoc contains AWS EC2 specific attributes. |
| 166 | +func createAWSEC2InstanceMonitoredResource(awsIdentityDoc *awsIdentityDocument) *AWSEC2Instance { |
| 167 | + awsInstance := AWSEC2Instance{ |
| 168 | + AWSAccount: awsIdentityDoc.accountID, |
| 169 | + InstanceID: awsIdentityDoc.instanceID, |
| 170 | + Region: fmt.Sprintf("aws:%s", awsIdentityDoc.region), |
| 171 | + } |
| 172 | + return &awsInstance |
| 173 | +} |
| 174 | + |
| 175 | +// createGCEInstanceMonitoredResource creates a gce_instance monitored resource |
| 176 | +// gcpMetadata contains GCP (GKE or GCE) specific attributes. |
| 177 | +func createGCEInstanceMonitoredResource(gcpMetadata *gcpMetadata) *GCEInstance { |
| 178 | + gceInstance := GCEInstance{ |
| 179 | + ProjectID: gcpMetadata.projectID, |
| 180 | + InstanceID: gcpMetadata.instanceID, |
| 181 | + Zone: gcpMetadata.zone, |
| 182 | + } |
| 183 | + return &gceInstance |
| 184 | +} |
| 185 | + |
| 186 | +// createGKEContainerMonitoredResource creates a gke_container monitored resource |
| 187 | +// gcpMetadata contains GCP (GKE or GCE) specific attributes. |
| 188 | +func createGKEContainerMonitoredResource(gcpMetadata *gcpMetadata) *GKEContainer { |
| 189 | + gkeContainer := GKEContainer{ |
| 190 | + ProjectID: gcpMetadata.projectID, |
| 191 | + InstanceID: gcpMetadata.instanceID, |
| 192 | + Zone: gcpMetadata.zone, |
| 193 | + ContainerName: gcpMetadata.containerName, |
| 194 | + ClusterName: gcpMetadata.clusterName, |
| 195 | + NamespaceID: gcpMetadata.namespaceID, |
| 196 | + PodID: gcpMetadata.podID, |
| 197 | + } |
| 198 | + return &gkeContainer |
| 199 | +} |
| 200 | + |
| 201 | +// detectOnce is used to make sure GCP and AWS metadata detect function executes only once. |
| 202 | +var detectOnce sync.Once |
| 203 | + |
| 204 | +// detectResourceType determines the resource type. |
| 205 | +// awsIdentityDoc contains AWS EC2 attributes. nil if it is not AWS EC2 environment |
| 206 | +// gcpMetadata contains GCP (GKE or GCE) specific attributes. |
| 207 | +func detectResourceType(awsIdentityDoc *awsIdentityDocument, gcpMetadata *gcpMetadata) Interface { |
| 208 | + if os.Getenv("KUBERNETES_SERVICE_HOST") != "" && |
| 209 | + gcpMetadata != nil && gcpMetadata.instanceID != "" { |
| 210 | + return createGKEContainerMonitoredResource(gcpMetadata) |
| 211 | + } else if gcpMetadata != nil && gcpMetadata.instanceID != "" { |
| 212 | + return createGCEInstanceMonitoredResource(gcpMetadata) |
| 213 | + } else if awsIdentityDoc != nil { |
| 214 | + return createAWSEC2InstanceMonitoredResource(awsIdentityDoc) |
| 215 | + } |
| 216 | + return nil |
| 217 | +} |
0 commit comments