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 stackdriver // import "contrib.go.opencensus.io/exporter/stackdriver"

import (
	
	

	
	
	
	monitoredrespb 
)
Resource labels that are generally internal to the exporter. Consider exposing these labels and a type identifier in the future to allow for customization.
const (
	stackdriverProjectID            = "contrib.opencensus.io/exporter/stackdriver/project_id"
	stackdriverLocation             = "contrib.opencensus.io/exporter/stackdriver/location"
	stackdriverClusterName          = "contrib.opencesus.io/exporter/stackdriver/cluster_name"
	stackdriverGenericTaskNamespace = "contrib.opencensus.io/exporter/stackdriver/generic_task/namespace"
	stackdriverGenericTaskJob       = "contrib.opencensus.io/exporter/stackdriver/generic_task/job"
	stackdriverGenericTaskID        = "contrib.opencensus.io/exporter/stackdriver/generic_task/task_id"

	knativeResType           = "knative_revision"
	knativeServiceName       = "service_name"
	knativeRevisionName      = "revision_name"
	knativeConfigurationName = "configuration_name"
	knativeNamespaceName     = "namespace_name"

	appEngineInstanceType = "gae_instance"

	appEngineService  = "appengine.service.id"
	appEngineVersion  = "appengine.version.id"
	appEngineInstance = "appengine.instance.id"
)

autodetectFunc returns a monitored resource that is autodetected. from the cloud environment at runtime.
autodetectOnce is used to lazy initialize autodetectedLabels.
autodetectedLabels stores all the labels from the autodetected monitored resource with a possible additional label for the GCP "location".
monitoredresource.Autodetect only makes calls to the metadata APIs once and caches the results
Mappings for the well-known OpenCensus resource label keys to applicable Stackdriver Monitored Resource label keys.
var k8sContainerMap = map[string]string{
	"project_id":     stackdriverProjectID,
	"location":       resourcekeys.CloudKeyZone,
	"cluster_name":   resourcekeys.K8SKeyClusterName,
	"namespace_name": resourcekeys.K8SKeyNamespaceName,
	"pod_name":       resourcekeys.K8SKeyPodName,
	"container_name": resourcekeys.ContainerKeyName,
}

var k8sPodMap = map[string]string{
	"project_id":     stackdriverProjectID,
	"location":       resourcekeys.CloudKeyZone,
	"cluster_name":   resourcekeys.K8SKeyClusterName,
	"namespace_name": resourcekeys.K8SKeyNamespaceName,
	"pod_name":       resourcekeys.K8SKeyPodName,
}

var k8sNodeMap = map[string]string{
	"project_id":   stackdriverProjectID,
	"location":     resourcekeys.CloudKeyZone,
	"cluster_name": resourcekeys.K8SKeyClusterName,
	"node_name":    resourcekeys.HostKeyName,
}

var gcpResourceMap = map[string]string{
	"project_id":  stackdriverProjectID,
	"instance_id": resourcekeys.HostKeyID,
	"zone":        resourcekeys.CloudKeyZone,
}

var awsResourceMap = map[string]string{
	"project_id":  stackdriverProjectID,
	"instance_id": resourcekeys.HostKeyID,
	"region":      resourcekeys.CloudKeyRegion,
	"aws_account": resourcekeys.CloudKeyAccountID,
}

var appEngineInstanceMap = map[string]string{
	"project_id":  stackdriverProjectID,
	"location":    resourcekeys.CloudKeyRegion,
	"module_id":   appEngineService,
	"version_id":  appEngineVersion,
	"instance_id": appEngineInstance,
}
getAutodetectedLabels returns all the labels from the Monitored Resource detected from the environment by calling monitoredresource.Autodetect. If a "zone" label is detected, a "location" label is added with the same value to account for differences between Legacy Stackdriver and Stackdriver Kubernetes Engine Monitoring, see https://cloud.google.com/monitoring/kubernetes-engine/migration.
accept "zone" value for "location" because values for location can be a zone or region, see https://cloud.google.com/docs/geography-and-regions
			if ,  := ["zone"];  {
				["location"] = ["zone"]
			}

			autodetectedLabels = 
		}
	})

	return autodetectedLabels
}
returns transformed label map and true if all labels in match are found in input except optional project_id. It returns false if at least one label other than project_id is missing.
func (,  map[string]string) (map[string]string, bool) {
	 := make(map[string]string, len())
	for ,  := range  {
		if ,  := [];  {
			[] = 
			continue
		}
attempt to autodetect missing labels, autodetected label keys should match destination label keys
		if ,  := getAutodetectedLabels()[];  {
			[] = 
			continue
		}

		if  != "project_id" {
			return nil, true
		}
	}
	return , false
}
DefaultMapResource implements default resource mapping for well-known resource types
func ( *resource.Resource) *monitoredrespb.MonitoredResource {
	 := genericResourceMap
	 := &monitoredrespb.MonitoredResource{
		Type: "global",
	}
	if  == nil || .Labels == nil {
		return 
	}

	switch {
	case .Type == resourcekeys.ContainerType:
		.Type = "k8s_container"
		 = k8sContainerMap
	case .Type == resourcekeys.K8SType:
		.Type = "k8s_pod"
		 = k8sPodMap
	case .Type == resourcekeys.HostType && .Labels[resourcekeys.K8SKeyClusterName] != "":
		.Type = "k8s_node"
		 = k8sNodeMap
	case .Type == appEngineInstanceType:
		.Type = appEngineInstanceType
		 = appEngineInstanceMap
	case .Labels[resourcekeys.CloudKeyProvider] == resourcekeys.CloudProviderGCP:
		.Type = "gce_instance"
		 = gcpResourceMap
	case .Labels[resourcekeys.CloudKeyProvider] == resourcekeys.CloudProviderAWS:
		.Type = "aws_ec2_instance"
		 = awsResourceMap
	case .Type == knativeResType:
		.Type = .Type
		 = knativeRevisionResourceMap
	}

	var  bool
	.Labels,  = transformResource(, .Labels)
	if  {
if project id specified then transform it.
		if ,  := .Labels[stackdriverProjectID];  {
			.Labels = make(map[string]string, 1)
			.Labels["project_id"] = 
		}
		return 
	}
	if .Type == "aws_ec2_instance" {
		if ,  := .Labels["region"];  {
			.Labels["region"] = fmt.Sprintf("aws:%s", )
		}
	}
	return