{"record":{"id":"fcc4b56a171b7e10","repo":"upstash/context7","slug":"invalid-otel-exporter-prometheus-port-configuredport","errorCode":null,"errorMessage":"Invalid OTEL_EXPORTER_PROMETHEUS_PORT: '${configuredPort}'","messagePattern":"Invalid OTEL_EXPORTER_PROMETHEUS_PORT: '(.+?)'","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/mcp/src/lib/telemetry-provider.ts","lineNumber":19,"sourceCode":"import { metrics } from \"@opentelemetry/api\";\nimport { PrometheusExporter } from \"@opentelemetry/exporter-prometheus\";\nimport { RuntimeNodeInstrumentation } from \"@opentelemetry/instrumentation-runtime-node\";\nimport { defaultResource, resourceFromAttributes } from \"@opentelemetry/resources\";\nimport { MeterProvider } from \"@opentelemetry/sdk-metrics\";\nimport { embeddedPrometheusIsEnabled } from \"./telemetry-config.js\";\n\nconst DEFAULT_PROMETHEUS_HOST = \"127.0.0.1\";\nconst DEFAULT_PROMETHEUS_PORT = 9464;\n\nlet embeddedRuntimeInstrumentation: RuntimeNodeInstrumentation | undefined;\n\nfunction prometheusPort(environment: NodeJS.ProcessEnv): number {\n  const configuredPort = environment.OTEL_EXPORTER_PROMETHEUS_PORT;\n  if (!configuredPort) return DEFAULT_PROMETHEUS_PORT;\n\n  const port = Number(configuredPort);\n  if (!Number.isInteger(port) || port < 1 || port > 65_535) {\n    throw new Error(`Invalid OTEL_EXPORTER_PROMETHEUS_PORT: '${configuredPort}'`);\n  }\n  return port;\n}\n\n/**\n * Installs the embedded Prometheus MetricReader for HTTP serving. A provider\n * installed by an OpenTelemetry preload script wins, so no second SDK is\n * installed. This module is dynamically imported only when the embedded\n * exporter is enabled; stdio and SDK-disabled processes never load it.\n */\nexport async function startPrometheusMetrics(\n  serviceVersion: string,\n  environment: NodeJS.ProcessEnv = process.env\n): Promise<MeterProvider | undefined> {\n  if (!embeddedPrometheusIsEnabled(environment)) return undefined;\n\n  let provider: MeterProvider | undefined;\n  let providerWasRegistered = false;","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/upstash/context7/blob/4416fb855b8f752be735e34f943b5d0762701aad/packages/mcp/src/lib/telemetry-provider.ts#L1-L37","documentation":"prometheusPort parses the OTEL_EXPORTER_PROMETHEUS_PORT environment variable for the embedded Prometheus MetricReader. When set, the value must parse as an integer between 1 and 65535; otherwise the provider throws instead of silently falling back to the default port.","triggerScenarios":"Setting OTEL_EXPORTER_PROMETHEUS_PORT to a non-numeric string ('http'), a float ('8080.5'), 0, a negative number, a value above 65535, or a value with whitespace/suffix characters so Number() yields NaN or an out-of-range integer.","commonSituations":"Copy-pasting a full URL instead of a port into the env var; typos like '808o'; using port 0 expecting an ephemeral port; quoting issues in .env files adding stray characters; container orchestrator templates passing '${PORT}' literally.","solutions":["Set OTEL_EXPORTER_PROMETHEUS_PORT to a plain integer between 1 and 65535 (e.g. 9464)","Remove the env var entirely to use DEFAULT_PROMETHEUS_PORT","Check for whitespace, quotes, or suffixes (e.g. '9464/tcp') in the value and strip them","Verify with `node -e \"console.log(Number(process.env.OTEL_EXPORTER_PROMETHEUS_PORT))\"` that the value parses as an integer"],"exampleFix":"// before\nOTEL_EXPORTER_PROMETHEUS_PORT=http://localhost:9464\n// after\nOTEL_EXPORTER_PROMETHEUS_PORT=9464","handlingStrategy":"validation","validationCode":"function isValidPort(v) { const n = Number(v); return Number.isInteger(n) && n >= 1 && n <= 65535; }","typeGuard":"function isValidPrometheusPort(v: unknown): v is number {\n  const n = Number(v);\n  return Number.isInteger(n) && n >= 1 && n <= 65535;\n}","tryCatchPattern":"try {\n  startTelemetry();\n} catch (e) {\n  if (String(e.message).startsWith('Invalid OTEL_EXPORTER_PROMETHEUS_PORT')) {\n    console.error('Fix OTEL_EXPORTER_PROMETHEUS_PORT (integer 1-65535) or unset it');\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["Store ports as bare integers in env files; never URLs or 'host:port' strings","Validate env vars at startup with a schema library (zod, envalid) before constructing providers","Document that unsetting the variable falls back to the default port","Watch for orchestrator templating that can leave literal '${VAR}' strings in values"],"tags":["config","env-var","telemetry","validation"],"backgroundTag":"invalid-env-var-value","analyzedSha":"4416fb855b8f752be735e34f943b5d0762701aad","analyzedAt":"2026-09-16T20:28:07.148Z","contentChangedAt":"2026-09-16T20:28:07.148Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}