Created
November 7, 2025 19:19
-
-
Save jasonnerothin/f883cdfe2ad22348a4e79e72cd5e4166 to your computer and use it in GitHub Desktop.
Unified OTLP Resource Naming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Common telemetry initialization utilities. | |
| Provides standardized resource attribute creation for logging, metrics, and tracing | |
| across all DWS components. | |
| """ | |
| import os | |
| from typing import Optional | |
| def create_resource_attributes( | |
| service_name: str, | |
| service_instance_id: str, | |
| service_version: Optional[str] = None, | |
| deployment_environment: Optional[str] = None | |
| ) -> dict: | |
| """ | |
| Create standardized resource attributes for all telemetry signals. | |
| These attributes should be used uniformly across logging, metrics, and tracing | |
| to ensure proper correlation in observability platforms. | |
| Args: | |
| service_name: Logical service type (e.g., "striker", "ares", "drone-command-sidecar") | |
| - For multi-instance: Docker image name | |
| - For singletons: Docker image name | |
| - For sidecars: Container name from docker-compose | |
| service_instance_id: Specific instance identifier | |
| - For multi-instance: Runtime ID (e.g., "striker-001") | |
| - For singletons: Same as service_name | |
| - For sidecars: Same as service_name | |
| service_version: Optional version string (default: "1.0.0") | |
| deployment_environment: Optional environment (default: from DEPLOYMENT_ENVIRONMENT env var or "dev") | |
| Returns: | |
| Dictionary of resource attributes following OpenTelemetry semantic conventions | |
| Example: | |
| # Multi-instance service (striker-001) | |
| attrs = create_resource_attributes("striker", "striker-001") | |
| # Singleton service (ares) | |
| attrs = create_resource_attributes("ares", "ares") | |
| # Sidecar service | |
| attrs = create_resource_attributes("drone-command-sidecar", "drone-command-sidecar") | |
| """ | |
| # Set defaults | |
| if service_version is None: | |
| service_version = "1.0.0" | |
| if deployment_environment is None: | |
| deployment_environment = os.environ.get("DEPLOYMENT_ENVIRONMENT", "dev") | |
| return { | |
| "service.name": service_name, | |
| "service.instance.id": service_instance_id, | |
| "service.version": service_version, | |
| "deployment.environment": deployment_environment, | |
| "telemetry.sdk.name": "opentelemetry", | |
| "telemetry.sdk.language": "python" | |
| } | |
| def get_service_identity_from_env( | |
| service_name_constant: str, | |
| instance_id_env_var: Optional[str] = None | |
| ) -> tuple: | |
| """ | |
| Helper to extract service identity from environment for different service patterns. | |
| Args: | |
| service_name_constant: The constant service name (e.g., "striker", "ares") | |
| instance_id_env_var: Environment variable containing instance ID (e.g., "DRONE_ID") | |
| If None or not set, uses service_name_constant as instance ID | |
| Returns: | |
| Tuple of (service_name, service_instance_id) | |
| Example: | |
| # Multi-instance service (striker) | |
| service_name, instance_id = get_service_identity_from_env("striker", "DRONE_ID") | |
| # Returns: ("striker", "striker-001") if DRONE_ID=striker-001 | |
| # Singleton service (ares) | |
| service_name, instance_id = get_service_identity_from_env("ares") | |
| # Returns: ("ares", "ares") | |
| """ | |
| service_name = service_name_constant | |
| if instance_id_env_var: | |
| service_instance_id = os.environ.get(instance_id_env_var, service_name_constant) | |
| else: | |
| service_instance_id = service_name_constant | |
| return service_name, service_instance_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment