Skip to content

Commit b7d0003

Browse files
authored
Add version property to Instance and AsyncInstance classes (#11)
TL;DR ----- Lets Applications report their version information alongside instance status Details -------- Extends the existing instance telemetry infrastructure with a `set_version()` method that mirrors the established `set_status()` pattern. Version information transmits through the `X-Replicated-VersionLabel` header during instance reporting, ensuring it reaches the vendor's dashboard alongside other telemetry data. Both synchronous and asynchronous instance classes now support version reporting, and the example scripts demonstrate this capability through a `--version` command-line argument. The change also fixes an important detail in the customer service by setting "Stable" as the default channel instead of None, preventing potential issues with unspecified channels during customer creation. Warning ------- Do not merge until replicatedhq/vandoor#8194 lands.
1 parent 2863a34 commit b7d0003

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

examples/basic_example.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ def main():
3131
)
3232
parser.add_argument("--channel", help="Channel for the customer (optional)")
3333
parser.add_argument("--customer-name", help="Customer name (optional)")
34+
parser.add_argument(
35+
"--status",
36+
choices=["missing", "unavailable", "ready", "updating", "degraded"],
37+
default="ready",
38+
help="Instance status (default: ready)",
39+
)
40+
parser.add_argument(
41+
"--version",
42+
default="",
43+
help="Application version (optional)",
44+
)
3445

3546
args = parser.parse_args()
3647

@@ -65,6 +76,15 @@ def main():
6576
instance = customer.get_or_create_instance()
6677
print(f"✓ Instance created/retrieved - ID: {instance.instance_id}")
6778

79+
# Set instance status
80+
instance.set_status(args.status)
81+
print(f"✓ Instance status set to: {args.status}")
82+
83+
# Set instance version if provided
84+
if args.version:
85+
instance.set_version(args.version)
86+
print(f"✓ Instance version set to: {args.version}")
87+
6888
print("\n🎉 Basic example completed successfully!")
6989
print(f"Customer ID: {customer.customer_id}")
7090
print(f"Instance ID: {instance.instance_id}")

examples/metrics_example.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ async def main():
3838
default="ready",
3939
help="Instance status (default: ready)",
4040
)
41+
parser.add_argument(
42+
"--version",
43+
default="",
44+
help="Application version (optional)",
45+
)
4146

4247
args = parser.parse_args()
4348

@@ -80,6 +85,11 @@ async def main():
8085
await instance.set_status(args.status)
8186
print(f"✓ Instance status set to: {args.status}")
8287

88+
# Set instance version if provided
89+
if args.version:
90+
await instance.set_version(args.version)
91+
print(f"✓ Instance version set to: {args.version}")
92+
8393
# Send some metrics concurrently
8494
await asyncio.gather(
8595
instance.send_metric("cpu_usage", 0.83),

replicated/resources.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def __init__(
6767
self._machine_id = client._machine_id
6868
self._data = kwargs
6969
self._status = "ready"
70+
self._version = ""
7071
self._metrics: dict[str, Union[int, float, str]] = {}
7172

7273
def send_metric(self, name: str, value: Union[int, float, str]) -> None:
@@ -100,6 +101,14 @@ def set_status(self, status: str) -> None:
100101
self._status = status
101102
self._report_instance()
102103

104+
def set_version(self, version: str) -> None:
105+
"""Set the version of this instance for telemetry reporting."""
106+
if not self.instance_id:
107+
self._ensure_instance()
108+
109+
self._version = version
110+
self._report_instance()
111+
103112
def _ensure_instance(self) -> None:
104113
"""Ensure the instance ID is generated and cached."""
105114
if self.instance_id:
@@ -154,6 +163,7 @@ def _report_instance(self) -> None:
154163
"X-Replicated-InstanceID": self.instance_id,
155164
"X-Replicated-ClusterID": self._machine_id,
156165
"X-Replicated-AppStatus": self._status,
166+
"X-Replicated-VersionLabel": self._version,
157167
"X-Replicated-InstanceTagData": instance_tags_b64,
158168
}
159169

@@ -188,6 +198,7 @@ def __init__(
188198
self._machine_id = client._machine_id
189199
self._data = kwargs
190200
self._status = "ready"
201+
self._version = ""
191202
self._metrics: dict[str, Union[int, float, str]] = {}
192203

193204
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:
221232
self._status = status
222233
await self._report_instance()
223234

235+
async def set_version(self, version: str) -> None:
236+
"""Set the version of this instance for telemetry reporting."""
237+
if not self.instance_id:
238+
await self._ensure_instance()
239+
240+
self._version = version
241+
await self._report_instance()
242+
224243
async def _ensure_instance(self) -> None:
225244
"""Ensure the instance ID is generated and cached."""
226245
if self.instance_id:
@@ -275,6 +294,7 @@ async def _report_instance(self) -> None:
275294
"X-Replicated-InstanceID": self.instance_id,
276295
"X-Replicated-ClusterID": self._machine_id,
277296
"X-Replicated-AppStatus": self._status,
297+
"X-Replicated-VersionLabel": self._version,
278298
"X-Replicated-InstanceTagData": instance_tags_b64,
279299
}
280300

replicated/services.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, client: "ReplicatedClient") -> None:
1616
def get_or_create(
1717
self,
1818
email_address: str,
19-
channel: Optional[str] = None,
19+
channel: str = "Stable",
2020
name: Optional[str] = None,
2121
) -> Customer:
2222
"""Get or create a customer."""
@@ -82,7 +82,7 @@ def __init__(self, client: "AsyncReplicatedClient") -> None:
8282
async def get_or_create(
8383
self,
8484
email_address: str,
85-
channel: Optional[str] = None,
85+
channel: str = "Stable",
8686
name: Optional[str] = None,
8787
) -> AsyncCustomer:
8888
"""Get or create a customer."""

0 commit comments

Comments
 (0)