{"version":3,"file":"ResourceAttributesEnvDetector.mjs","names":[],"sources":["../../src/detectors/ResourceAttributesEnvDetector.ts"],"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 type { ResourceDetectionConfig } from '../config';\nimport type { DetectedResource, ResourceDetector } from '../types';\nimport { getStringFromEnv } from '@opentelemetry/core';\n\n/**\n * A detector that reads attributes from the OTEL_RESOURCE_ATTRIBUTES\n * environment variable.\n */\nexport class ResourceAttributesEnvDetector 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  detect(_config?: ResourceDetectionConfig): DetectedResource {\n    const attributes: Attributes = {};\n\n    const rawAttributes = getStringFromEnv('OTEL_RESOURCE_ATTRIBUTES');\n    if (rawAttributes) {\n      try {\n        const parsedAttributes = this._parseResourceAttributes(rawAttributes);\n        Object.assign(attributes, parsedAttributes);\n      } catch (e) {\n        diag.debug(\n          `ResourceAttributesEnvDetector failed: ${e instanceof Error ? e.message : e}`\n        );\n      }\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  protected _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          { cause: 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 resourceAttributesEnvDetector =\n  new ResourceAttributesEnvDetector();\n"],"mappings":";;;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