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 resource provides functionality for resource, which capture identifying information about the entities for which signals are exported.
package resource

import (
	
	
	
	
	
	
	
)
Environment variables used by FromEnv to decode a resource.
const (
	EnvVarType   = "OC_RESOURCE_TYPE"
	EnvVarLabels = "OC_RESOURCE_LABELS"
)
Resource describes an entity about which identifying information and metadata is exposed. For example, a type "k8s.io/container" may hold labels describing the pod name and namespace.
type Resource struct {
	Type   string
	Labels map[string]string
}
EncodeLabels encodes a labels map to a string as provided via the OC_RESOURCE_LABELS environment variable.
func ( map[string]string) string {
	 := make([]string, 0, len())
	for  := range  {
		 = append(, )
	}
	sort.Strings()

	 := ""
	for ,  := range  {
		if  > 0 {
			 += ","
		}
		 +=  + "=" + strconv.Quote([])
	}
	return 
}

var labelRegex = regexp.MustCompile(`^\s*([[:ascii:]]{1,256}?)=("[[:ascii:]]{0,256}?")\s*,`)
DecodeLabels decodes a serialized label map as used in the OC_RESOURCE_LABELS variable. A list of labels of the form `<key1>="<value1>",<key2>="<value2>",...` is accepted. Domain names and paths are accepted as label keys. Most users will want to use FromEnv instead.
Ensure a trailing comma, which allows us to keep the regex simpler
	 = strings.TrimRight(strings.TrimSpace(), ",") + ","

	for len() > 0 {
		 := labelRegex.FindStringSubmatch()
		if len() == 0 {
			return nil, fmt.Errorf("invalid label formatting, remainder: %s", )
		}
		 := [2]
		if  == "" {
			 = [3]
		} else {
			var  error
			if ,  = strconv.Unquote();  != nil {
				return nil, fmt.Errorf("invalid label formatting, remainder: %s, err: %s", , )
			}
		}
		[[1]] = 

		 = [len([0]):]
	}
	return , nil
}
FromEnv is a detector that loads resource information from the OC_RESOURCE_TYPE and OC_RESOURCE_labelS environment variables.
func (context.Context) (*Resource, error) {
	 := &Resource{
		Type: strings.TrimSpace(os.Getenv(EnvVarType)),
	}
	 := strings.TrimSpace(os.Getenv(EnvVarLabels))
	if  == "" {
		return , nil
	}
	var  error
	if .Labels,  = DecodeLabels();  != nil {
		return nil, 
	}
	return , nil
}

var _ Detector = FromEnv
merge resource information from b into a. In case of a collision, a takes precedence.
func (,  *Resource) *Resource {
	if  == nil {
		return 
	}
	if  == nil {
		return 
	}
	 := &Resource{
		Type:   .Type,
		Labels: map[string]string{},
	}
	if .Type == "" {
		.Type = .Type
	}
	for ,  := range .Labels {
		.Labels[] = 
Labels from resource a overwrite labels from resource b.
	for ,  := range .Labels {
		.Labels[] = 
	}
	return 
}
Detector attempts to detect resource information. If the detector cannot find resource information, the returned resource is nil but no error is returned. An error is only returned on unexpected failures.
MultiDetector returns a Detector that calls all input detectors in order and merges each result with the previous one. In case a type of label key is already set, the first set value is takes precedence. It returns on the first error that a sub-detector encounters.
func ( ...Detector) Detector {
	return func( context.Context) (*Resource, error) {
		return detectAll(, ...)
	}
}
detectall calls all input detectors sequentially an merges each result with the previous one. It returns on the first error that a sub-detector encounters.
func ( context.Context,  ...Detector) (*Resource, error) {
	var  *Resource
	for ,  := range  {
		,  := ()
		if  != nil {
			return nil, 
		}
		 = merge(, )
	}
	return , nil