-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add heartbeat for usage collection and update to Loki 3.3 #4499
base: master
Are you sure you want to change the base?
Changes from all commits
61c55c9
a7b6189
6e2727b
a0d918f
8647138
b318dbb
0bd5e97
6f43d00
dc3ec63
1d235dc
6940666
9ff8013
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,7 @@ def _get_current_timestamp_ns() -> int: | |
class MessageType(enum.Enum): | ||
"""Types for messages to be sent to Loki.""" | ||
USAGE = 'usage' | ||
HEARTBEAT = 'heartbeat' | ||
# TODO(zhwu): Add more types, e.g., cluster_lifecycle. | ||
|
||
|
||
|
@@ -59,8 +60,9 @@ def get_properties(self) -> Dict[str, Any]: | |
properties = self.__dict__.copy() | ||
return {k: v for k, v in properties.items() if not k.startswith('_')} | ||
|
||
def __repr__(self): | ||
raise NotImplementedError | ||
def __repr__(self) -> str: | ||
d = self.get_properties() | ||
return json.dumps(d) | ||
|
||
|
||
class UsageMessageToReport(MessageToReport): | ||
|
@@ -152,10 +154,6 @@ def __init__(self) -> None: | |
self.exception: Optional[str] = None # entrypoint_context | ||
self.stacktrace: Optional[str] = None # entrypoint_context | ||
|
||
def __repr__(self) -> str: | ||
d = self.get_properties() | ||
return json.dumps(d) | ||
|
||
def update_entrypoint(self, msg: str): | ||
self.entrypoint = msg | ||
|
||
|
@@ -267,16 +265,43 @@ def update_runtime(self, name_or_fn: str): | |
name_or_fn) | ||
|
||
|
||
class HeartbeatMessageToReport(MessageToReport): | ||
"""Message to be reported to Grafana Loki for heartbeat on a cluster.""" | ||
|
||
def __init__(self, interval_seconds: int = 600): | ||
super().__init__(constants.USAGE_MESSAGE_SCHEMA_VERSION) | ||
# This interval_seconds is mainly for recording the heartbeat interval | ||
# in the heartbeat message, so that the collector can use it. | ||
self.interval_seconds = interval_seconds | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this variable used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it will be included as part of the payload sent when we are calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a comment for it. |
||
|
||
def get_properties(self) -> Dict[str, Any]: | ||
properties = super().get_properties() | ||
# The run id is set by the skylet, which will always be the same for | ||
# the entire lifetime of the run. | ||
with open(os.path.expanduser(constants.USAGE_RUN_ID_FILE), | ||
'r', | ||
encoding='utf-8') as f: | ||
properties['run_id'] = f.read().strip() | ||
return properties | ||
|
||
|
||
class MessageCollection: | ||
"""A collection of messages.""" | ||
|
||
def __init__(self): | ||
self._messages = {MessageType.USAGE: UsageMessageToReport()} | ||
self._messages = { | ||
MessageType.USAGE: UsageMessageToReport(), | ||
MessageType.HEARTBEAT: HeartbeatMessageToReport() | ||
} | ||
|
||
@property | ||
def usage(self): | ||
def usage(self) -> UsageMessageToReport: | ||
return self._messages[MessageType.USAGE] | ||
|
||
@property | ||
def heartbeat(self) -> HeartbeatMessageToReport: | ||
return self._messages[MessageType.HEARTBEAT] | ||
|
||
def reset(self, message_type: MessageType): | ||
self._messages[message_type] = self._messages[message_type].__class__() | ||
|
||
|
@@ -300,13 +325,22 @@ def _send_to_loki(message_type: MessageType): | |
|
||
message = messages[message_type] | ||
|
||
# In case the message has no start time, set it to the current time. | ||
message.start() | ||
message.send_time = _get_current_timestamp_ns() | ||
log_timestamp = message.start_time | ||
|
||
environment = 'prod' | ||
if env_options.Options.IS_DEVELOPER.get(): | ||
environment = 'dev' | ||
prom_labels = {'type': message_type.value, 'environment': environment} | ||
prom_labels = { | ||
'type': message_type.value, | ||
'environment': environment, | ||
'schema_version': message.schema_version, | ||
} | ||
if message_type == MessageType.USAGE: | ||
prom_labels['new_cluster'] = (message.original_cluster_status != 'UP' | ||
and message.final_cluster_status == 'UP') | ||
|
||
headers = {'Content-type': 'application/json'} | ||
payload = { | ||
|
@@ -384,7 +418,7 @@ def prepare_json_from_yaml_config( | |
def _send_local_messages(): | ||
"""Send all messages not been uploaded to Loki.""" | ||
for msg_type, message in messages.items(): | ||
if not message.message_sent: | ||
if not message.message_sent and msg_type != MessageType.HEARTBEAT: | ||
# Avoid the fallback entrypoint to send the message again | ||
# in normal case. | ||
try: | ||
|
@@ -394,6 +428,11 @@ def _send_local_messages(): | |
f'exception caught: {type(e)}({e})') | ||
|
||
|
||
def send_heartbeat(interval_seconds: int = 600): | ||
messages.heartbeat.interval_seconds = interval_seconds | ||
_send_to_loki(MessageType.HEARTBEAT) | ||
|
||
|
||
@contextlib.contextmanager | ||
def entrypoint_context(name: str, fallback: bool = False): | ||
"""Context manager for entrypoint. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a little bit curious why we need this interval seconds argument. Doesnt skylet already has a interval mechanism..?
skypilot/sky/skylet/events.py
Lines 46 to 56 in 061d4bd