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MAAM,IAAI,KAAK,CACb,iDAAiD,IAAI,CAAC,WAAW,wBAAwB,UAAU,IAAI,CACxG,CAAC;aACH;YAED,UAAU,CAAC,UAAU,CAAC,GAAG,YAAY,CAAC;SACvC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;CACF;AAEY,QAAA,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { Attributes } from '@opentelemetry/api';\nimport { diag } from '@opentelemetry/api';\nimport { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';\nimport type { ResourceDetectionConfig } from '../config';\nimport type { DetectedResource, ResourceDetector } from '../types';\nimport { getStringFromEnv } from '@opentelemetry/core';\n\n/**\n * EnvDetector can be used to detect the presence of and create a Resource\n * from the OTEL_RESOURCE_ATTRIBUTES environment variable.\n */\nclass EnvDetector implements ResourceDetector {\n // Type, attribute keys, and attribute values should not exceed 256 characters.\n private readonly _MAX_LENGTH = 255;\n\n // OTEL_RESOURCE_ATTRIBUTES is a comma-separated list of attributes.\n private readonly _COMMA_SEPARATOR = ',';\n\n // OTEL_RESOURCE_ATTRIBUTES contains key value pair separated by '='.\n private readonly _LABEL_KEY_VALUE_SPLITTER = '=';\n\n /**\n * Returns a {@link Resource} populated with attributes from the\n * OTEL_RESOURCE_ATTRIBUTES environment variable. Note this is an async\n * function to conform to the Detector interface.\n *\n * @param config The resource detection config\n */\n detect(_config?: ResourceDetectionConfig): DetectedResource {\n const attributes: Attributes = {};\n\n const rawAttributes = getStringFromEnv('OTEL_RESOURCE_ATTRIBUTES');\n const serviceName = getStringFromEnv('OTEL_SERVICE_NAME');\n\n if (rawAttributes) {\n try {\n const parsedAttributes = this._parseResourceAttributes(rawAttributes);\n Object.assign(attributes, parsedAttributes);\n } catch (e) {\n diag.debug(`EnvDetector failed: ${e instanceof Error ? e.message : e}`);\n }\n }\n\n if (serviceName) {\n attributes[ATTR_SERVICE_NAME] = serviceName;\n }\n\n return { attributes };\n }\n\n /**\n * Creates an attribute map from the OTEL_RESOURCE_ATTRIBUTES environment\n * variable.\n *\n * OTEL_RESOURCE_ATTRIBUTES: A comma-separated list of attributes in the\n * format \"key1=value1,key2=value2\". The ',' and '=' characters in keys\n * and values MUST be percent-encoded. Other characters MAY be percent-encoded.\n *\n * Per the spec, on any error (e.g., decoding failure), the entire environment\n * variable value is discarded.\n *\n * @param rawEnvAttributes The resource attributes as a comma-separated list\n * of key/value pairs.\n * @returns The parsed resource attributes.\n * @throws Error if parsing fails (caller handles by discarding all attributes)\n */\n private _parseResourceAttributes(rawEnvAttributes?: string): Attributes {\n if (!rawEnvAttributes) return {};\n\n const attributes: Attributes = {};\n const rawAttributes: string[] = rawEnvAttributes\n .split(this._COMMA_SEPARATOR)\n .filter(attr => attr.trim() !== ''); // Filter out empty entries\n\n for (const rawAttribute of rawAttributes) {\n const keyValuePair: string[] = rawAttribute.split(\n this._LABEL_KEY_VALUE_SPLITTER\n );\n\n // Per spec: ',' and '=' MUST be percent-encoded in keys and values.\n // If we get != 2 parts, there's an unencoded '=' which is an error.\n if (keyValuePair.length !== 2) {\n throw new Error(\n `Invalid format for OTEL_RESOURCE_ATTRIBUTES: \"${rawAttribute}\". ` +\n `Expected format: key=value. The ',' and '=' characters must be percent-encoded in keys and values.`\n );\n }\n\n const [rawKey, rawValue] = keyValuePair;\n const key = rawKey.trim();\n const value = rawValue.trim();\n\n if (key.length === 0) {\n throw new Error(\n `Invalid OTEL_RESOURCE_ATTRIBUTES: empty attribute key in \"${rawAttribute}\".`\n );\n }\n\n let decodedKey: string;\n let decodedValue: string;\n try {\n decodedKey = decodeURIComponent(key);\n decodedValue = decodeURIComponent(value);\n } catch (e) {\n throw new Error(\n `Failed to percent-decode OTEL_RESOURCE_ATTRIBUTES entry \"${rawAttribute}\": ${e instanceof Error ? e.message : e}`\n );\n }\n\n if (decodedKey.length > this._MAX_LENGTH) {\n throw new Error(\n `Attribute key exceeds the maximum length of ${this._MAX_LENGTH} characters: \"${decodedKey}\".`\n );\n }\n\n if (decodedValue.length > this._MAX_LENGTH) {\n throw new Error(\n `Attribute value exceeds the maximum length of ${this._MAX_LENGTH} characters for key \"${decodedKey}\".`\n );\n }\n\n attributes[decodedKey] = decodedValue;\n }\n\n return attributes;\n }\n}\n\nexport const envDetector = new EnvDetector();\n"]}