Copyright 2020, 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 gcp

import (
	
	
)
Interface is a type that represent monitor resource that satisfies monitoredresource.Interface
type Interface interface {
MonitoredResource returns the resource type and resource labels.
	MonitoredResource() (resType string, labels map[string]string)
}
GKEContainer represents gke_container type monitored resource. For definition refer to https://cloud.google.com/monitoring/api/resources#tag_gke_container
type GKEContainer struct {
ProjectID is the identifier of the GCP project associated with this resource, such as "my-project".
InstanceID is the numeric VM instance identifier assigned by Compute Engine.
ClusterName is the name for the cluster the container is running in.
ContainerName is the name of the container.
NamespaceID is the identifier for the cluster namespace the container is running in
PodID is the identifier for the pod the container is running in.
Zone is the Compute Engine zone in which the VM is running.
LoggingMonitoringV2Enabled is the identifier if user enabled V2 logging and monitoring for GKE
MonitoredResource returns resource type and resource labels for GKEContainer
func ( *GKEContainer) () ( string,  map[string]string) {
	 = map[string]string{
		"project_id":     .ProjectID,
		"cluster_name":   .ClusterName,
		"container_name": .ContainerName,
	}
	var  string
	if .LoggingMonitoringV2Enabled {
		 = "k8s_container"
		["pod_name"] = .PodID
		["namespace_name"] = .NamespaceID
		["location"] = .Zone
	} else {
		 = "gke_container"
		["pod_id"] = .PodID
		["namespace_id"] = .NamespaceID
		["zone"] = .Zone
		["instance_id"] = .InstanceID
	}
	return , 
}
GCEInstance represents gce_instance type monitored resource. For definition refer to https://cloud.google.com/monitoring/api/resources#tag_gce_instance
type GCEInstance struct {
ProjectID is the identifier of the GCP project associated with this resource, such as "my-project".
InstanceID is the numeric VM instance identifier assigned by Compute Engine.
Zone is the Compute Engine zone in which the VM is running.
MonitoredResource returns resource type and resource labels for GCEInstance
func ( *GCEInstance) () ( string,  map[string]string) {
	 = map[string]string{
		"project_id":  .ProjectID,
		"instance_id": .InstanceID,
		"zone":        .Zone,
	}
	return "gce_instance", 
}
Autodetect auto detects monitored resources based on the environment where the application is running. It supports detection of following resource types 1. gke_container: 2. gce_instance: Returns MonitoredResInterface which implements getLabels() and getType() For resource definition go to https://cloud.google.com/monitoring/api/resources
createGCEInstanceMonitoredResource creates a gce_instance monitored resource gcpMetadata contains GCP (GKE or GCE) specific attributes.
func ( *gcpMetadata) *GCEInstance {
	 := GCEInstance{
		ProjectID:  .projectID,
		InstanceID: .instanceID,
		Zone:       .zone,
	}
	return &
}
createGKEContainerMonitoredResource creates a gke_container monitored resource gcpMetadata contains GCP (GKE or GCE) specific attributes.
func ( *gcpMetadata) *GKEContainer {
	 := GKEContainer{
		ProjectID:                  .projectID,
		InstanceID:                 .instanceID,
		Zone:                       .zone,
		ContainerName:              .containerName,
		ClusterName:                .clusterName,
		NamespaceID:                .namespaceID,
		PodID:                      .podID,
		LoggingMonitoringV2Enabled: .monitoringV2,
	}
	return &
}
detectOnce is used to make sure GCP metadata detect function executes only once.
autoDetected is the metadata detected after the first execution of Autodetect function.
detectResourceType determines the resource type. gcpMetadata contains GCP (GKE or GCE) specific attributes.
func ( *gcpMetadata) Interface {
	if os.Getenv("KUBERNETES_SERVICE_HOST") != "" &&
		 != nil && .instanceID != "" {
		return createGKEContainerMonitoredResource()
	} else if  != nil && .instanceID != "" {
		return createGCEInstanceMonitoredResource()
	}
	return nil