diff --git a/examples/basic_example.py b/examples/basic_example.py index 2cdec62..0366b6d 100644 --- a/examples/basic_example.py +++ b/examples/basic_example.py @@ -31,6 +31,17 @@ def main(): ) parser.add_argument("--channel", help="Channel for the customer (optional)") parser.add_argument("--customer-name", help="Customer name (optional)") + parser.add_argument( + "--status", + choices=["missing", "unavailable", "ready", "updating", "degraded"], + default="ready", + help="Instance status (default: ready)", + ) + parser.add_argument( + "--version", + default="", + help="Application version (optional)", + ) args = parser.parse_args() @@ -65,6 +76,15 @@ def main(): instance = customer.get_or_create_instance() print(f"āœ“ Instance created/retrieved - ID: {instance.instance_id}") + # Set instance status + instance.set_status(args.status) + print(f"āœ“ Instance status set to: {args.status}") + + # Set instance version if provided + if args.version: + instance.set_version(args.version) + print(f"āœ“ Instance version set to: {args.version}") + print("\nšŸŽ‰ Basic example completed successfully!") print(f"Customer ID: {customer.customer_id}") print(f"Instance ID: {instance.instance_id}") diff --git a/examples/metrics_example.py b/examples/metrics_example.py index d40158c..f0a37bc 100644 --- a/examples/metrics_example.py +++ b/examples/metrics_example.py @@ -38,6 +38,11 @@ async def main(): default="ready", help="Instance status (default: ready)", ) + parser.add_argument( + "--version", + default="", + help="Application version (optional)", + ) args = parser.parse_args() @@ -80,6 +85,11 @@ async def main(): await instance.set_status(args.status) print(f"āœ“ Instance status set to: {args.status}") + # Set instance version if provided + if args.version: + await instance.set_version(args.version) + print(f"āœ“ Instance version set to: {args.version}") + # Send some metrics concurrently await asyncio.gather( instance.send_metric("cpu_usage", 0.83), diff --git a/replicated/resources.py b/replicated/resources.py index 683e248..925c52c 100644 --- a/replicated/resources.py +++ b/replicated/resources.py @@ -67,6 +67,7 @@ def __init__( self._machine_id = client._machine_id self._data = kwargs self._status = "ready" + self._version = "" self._metrics: dict[str, Union[int, float, str]] = {} def send_metric(self, name: str, value: Union[int, float, str]) -> None: @@ -100,6 +101,14 @@ def set_status(self, status: str) -> None: self._status = status self._report_instance() + def set_version(self, version: str) -> None: + """Set the version of this instance for telemetry reporting.""" + if not self.instance_id: + self._ensure_instance() + + self._version = version + self._report_instance() + def _ensure_instance(self) -> None: """Ensure the instance ID is generated and cached.""" if self.instance_id: @@ -154,6 +163,7 @@ def _report_instance(self) -> None: "X-Replicated-InstanceID": self.instance_id, "X-Replicated-ClusterID": self._machine_id, "X-Replicated-AppStatus": self._status, + "X-Replicated-VersionLabel": self._version, "X-Replicated-InstanceTagData": instance_tags_b64, } @@ -188,6 +198,7 @@ def __init__( self._machine_id = client._machine_id self._data = kwargs self._status = "ready" + self._version = "" self._metrics: dict[str, Union[int, float, str]] = {} async def send_metric(self, name: str, value: Union[int, float, str]) -> None: @@ -221,6 +232,14 @@ async def set_status(self, status: str) -> None: self._status = status await self._report_instance() + async def set_version(self, version: str) -> None: + """Set the version of this instance for telemetry reporting.""" + if not self.instance_id: + await self._ensure_instance() + + self._version = version + await self._report_instance() + async def _ensure_instance(self) -> None: """Ensure the instance ID is generated and cached.""" if self.instance_id: @@ -275,6 +294,7 @@ async def _report_instance(self) -> None: "X-Replicated-InstanceID": self.instance_id, "X-Replicated-ClusterID": self._machine_id, "X-Replicated-AppStatus": self._status, + "X-Replicated-VersionLabel": self._version, "X-Replicated-InstanceTagData": instance_tags_b64, } diff --git a/replicated/services.py b/replicated/services.py index e27dcf4..d72bfdf 100644 --- a/replicated/services.py +++ b/replicated/services.py @@ -16,7 +16,7 @@ def __init__(self, client: "ReplicatedClient") -> None: def get_or_create( self, email_address: str, - channel: Optional[str] = None, + channel: str = "Stable", name: Optional[str] = None, ) -> Customer: """Get or create a customer.""" @@ -82,7 +82,7 @@ def __init__(self, client: "AsyncReplicatedClient") -> None: async def get_or_create( self, email_address: str, - channel: Optional[str] = None, + channel: str = "Stable", name: Optional[str] = None, ) -> AsyncCustomer: """Get or create a customer."""