/**
 * 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.
 */ import { StringUtils } from '../internal/string-utils' ; import { Labels , Resource } from './types' ; /**
 * Resource represents a resource, which capture identifying information about
 * the entities for which signals (stats or traces) are reported. It further
 * provides a framework for detection of resource information from the
 * environment and progressive population as signals propagate from the core
 * instrumentation library to a backend's exporter.
 */ export class CoreResource { private static readonly MAX_LENGTH = 255 ; private static readonly COMMA_SEPARATOR = ',' ; private static readonly LABEL_KEY_VALUE_SPLITTER = '=' ; private static ENV_TYPE = CoreResource . parseResourceType ( process . env . OC_RESOURCE_TYPE ) ; private static ENV_LABEL_MAP = CoreResource . parseResourceLabels ( process . env . OC_RESOURCE_LABELS ) ; private static readonly ERROR_MESSAGE_INVALID_CHARS = 'should be a ASCII string with a length greater than 0 and not exceed ' + CoreResource . MAX_LENGTH + ' characters.' ; private static readonly ERROR_MESSAGE_INVALID_VALUE = 'should be a ASCII string with a length not exceed ' + CoreResource . MAX_LENGTH + ' characters.' ; returns static createFromEnvironmentVariables ( ) { return { type : CoreResource . ENV_TYPE , labels : CoreResource . ENV_LABEL_MAP } ; } param resources returns static mergeResources ( resources ) { if ( resources . length === 0 ) return { type : 'global' , labels : { } } ; let currentResource = resources [ 0 ] ; for ( let i = 1 ; i < resources . length ; i ++ ) { currentResource = this . merge ( currentResource , resources [ i ] ) ; } return currentResource ; } param rawEnvType returns private static parseResourceType ( rawEnvType ? ) { if ( ! rawEnvType ) return null ; if ( ! CoreResource . isValidAndNotEmpty ( rawEnvType ) ) { throw new Error ( `Type ${ CoreResource . ERROR_MESSAGE_INVALID_CHARS }` ) ; } return rawEnvType . trim ( ) ; } param rawEnvLabels returns private static parseResourceLabels ( rawEnvLabels ? ) { if ( ! rawEnvLabels ) return { } ; const labels = { } ; const rawLabels = rawEnvLabels . split ( this . COMMA_SEPARATOR , - 1 ) ; for ( const rawLabel of rawLabels ) { const keyValuePair = rawLabel . split ( this . LABEL_KEY_VALUE_SPLITTER , - 1 ) ; if ( keyValuePair . length !== 2 ) { continue ; } let [ key , value ] = keyValuePair ; key = key . trim ( ) ; value = value . trim ( ) . split ( '^"|"$' ) . join ( '' ) ; if ( ! CoreResource . isValidAndNotEmpty ( key ) ) { throw new Error ( `Label key ${ CoreResource . ERROR_MESSAGE_INVALID_CHARS }` ) ; } if ( ! CoreResource . isValid ( value ) ) { throw new Error ( `Label value ${ CoreResource . ERROR_MESSAGE_INVALID_VALUE }` ) ; } labels [ key ] = value ; } return labels ; } param resource param otherResource returns private static merge ( resource , otherResource ) { if ( ! resource ) { return otherResource ; } if ( ! otherResource ) { return resource ; } return { type : resource . type || otherResource . type , labels : Object . assign ( { } , otherResource . labels , resource . labels ) , } ; } param str returns private static isValid ( name ) { return ( name . length <= CoreResource . MAX_LENGTH && StringUtils . isPrintableString ( name ) ) ; } param str returns private static isValidAndNotEmpty ( name ) { return name . length > 0 && CoreResource . isValid ( name ) ; } /** TEST_ONLY */ static setup ( ) { CoreResource . ENV_TYPE = CoreResource . parseResourceType ( process . env . OC_RESOURCE_TYPE ) ; CoreResource . ENV_LABEL_MAP = CoreResource . parseResourceLabels ( process . env . OC_RESOURCE_LABELS ) ; } }