{"record":{"id":"15a97e0ad2989820","repo":"GoogleCloudPlatform/microservices-demo","slug":"product-catalog-service-addr-environment-variable","errorCode":null,"errorMessage":"PRODUCT_CATALOG_SERVICE_ADDR environment variable not set","messagePattern":"PRODUCT_CATALOG_SERVICE_ADDR environment variable not set","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"src/recommendationservice/recommendation_server.py","lineNumber":133,"sourceCode":"        trace.set_tracer_provider(TracerProvider())\n        otel_endpoint = os.getenv(\"COLLECTOR_SERVICE_ADDR\", \"localhost:4317\")\n        trace.get_tracer_provider().add_span_processor(\n          BatchSpanProcessor(\n              OTLPSpanExporter(\n              endpoint = otel_endpoint,\n              insecure = True\n            )\n          )\n        )\n    except (KeyError, DefaultCredentialsError):\n        logger.info(\"Tracing disabled.\")\n    except Exception as e:\n        logger.warn(f\"Exception on Cloud Trace setup: {traceback.format_exc()}, tracing disabled.\") \n\n    port = os.environ.get('PORT', \"8080\")\n    catalog_addr = os.environ.get('PRODUCT_CATALOG_SERVICE_ADDR', '')\n    if catalog_addr == \"\":\n        raise Exception('PRODUCT_CATALOG_SERVICE_ADDR environment variable not set')\n    logger.info(\"product catalog address: \" + catalog_addr)\n    channel = grpc.insecure_channel(catalog_addr)\n    product_catalog_stub = demo_pb2_grpc.ProductCatalogServiceStub(channel)\n\n    # create gRPC server\n    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))\n\n    # add class to gRPC server\n    service = RecommendationService()\n    demo_pb2_grpc.add_RecommendationServiceServicer_to_server(service, server)\n    health_pb2_grpc.add_HealthServicer_to_server(service, server)\n\n    # start server\n    logger.info(\"listening on port: \" + port)\n    server.add_insecure_port('[::]:'+port)\n    server.start()\n\n    # keep alive","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/GoogleCloudPlatform/microservices-demo/blob/72ba613a05f7fcee51cf1d0badff401b6ae7074d/src/recommendationservice/recommendation_server.py#L115-L151","documentation":"The recommendationservice startup requires PRODUCT_CATALOG_SERVICE_ADDR to know where the product catalog gRPC service lives. If the environment variable is empty or unset, it raises a bare Exception and the server exits before binding its gRPC port. It is a deliberate fail-fast configuration check.","triggerScenarios":"Starting recommendation_server.py (main) without PRODUCT_CATALOG_SERVICE_ADDR in the environment, e.g. running the container manually without the env var, or a Kubernetes/compose manifest missing the env entry.","commonSituations":"Local `python recommendation_server.py` runs where k8s normally injects the env var; docker run without -e PRODUCT_CATALOG_SERVICE_ADDR=productcatalogservice:3550; typos in deployment YAML env names.","solutions":["Set PRODUCT_CATALOG_SERVICE_ADDR before starting, e.g. export PRODUCT_CATALOG_SERVICE_ADDR=productcatalogservice:3550","In Kubernetes, add the env var (or use ConfigMap/envFrom) to the recommendationservice deployment","In docker run/compose, pass -e PRODUCT_CATALOG_SERVICE_ADDR=<host>:<port>","Verify the service name resolves (DNS) once the variable is set"],"exampleFix":"# before\npython recommendation_server.py\n# after\nexport PRODUCT_CATALOG_SERVICE_ADDR=productcatalogservice:3550 && python recommendation_server.py","handlingStrategy":"validation","validationCode":"import os\naddr = os.environ.get('PRODUCT_CATALOG_SERVICE_ADDR')\nif not addr:\n    raise SystemExit(\"Set PRODUCT_CATALOG_SERVICE_ADDR (e.g. productcatalogservice:3550) before starting\")\n# then start: PRODUCT_CATALOG_SERVICE_ADDR=productcatalogservice:3550 python recommendation_server.py","typeGuard":null,"tryCatchPattern":"try:\n    main()\nexcept Exception as e:\n    if 'PRODUCT_CATALOG_SERVICE_ADDR' in str(e):\n        print('Missing required env var PRODUCT_CATALOG_SERVICE_ADDR')\n        sys.exit(1)\n    raise","preventionTips":["Declare required env vars in the container/deployment manifest (env, envFrom, or ConfigMap)","Fail fast with a clear message at startup listing missing env vars","Document all required env vars in the service README / docker-compose file","Use entrypoint scripts that assert required variables before launching Python"],"tags":["configuration","env-var","python","grpc","startup"],"backgroundTag":"missing-env-var","analyzedSha":"72ba613a05f7fcee51cf1d0badff401b6ae7074d","analyzedAt":"2026-09-02T01:15:09.673Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}