-
Notifications
You must be signed in to change notification settings - Fork 538
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
Open
Michaelvll
wants to merge
10
commits into
master
Choose a base branch
from
send-heartbeat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
61c55c9
Add heartbeat usage
Michaelvll a7b6189
upgrade loki 3.3
Michaelvll 6e2727b
fix type
Michaelvll a0d918f
embed version in type
Michaelvll 8647138
Use the same type
Michaelvll b318dbb
use schema 1
Michaelvll 0bd5e97
remove entrypoint from labels
Michaelvll 6f43d00
Add interval seconds
Michaelvll dc3ec63
Respect env var for skylet
Michaelvll 1d235dc
format
Michaelvll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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,41 @@ 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) | ||
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? |
||
|
||
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 +323,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 +416,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 +426,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. | ||
|
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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