Skip to content
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

Adding edge canary on laptop #182

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Adding edge canary on laptop #182

wants to merge 9 commits into from

Conversation

timmarkhuff
Copy link
Contributor

@timmarkhuff timmarkhuff commented Feb 14, 2025

This PR adds an edge-endpoint canary that runs on a laptop.

Completed so far:

  1. a script that installs a three cron jobs. One that periodically runs a load test and checks the result, another than restarts the laptop daily, and other that logs when the system restarts.
  2. The installation script creates a heartbeat detector and a Groundlight alert for that detector, if it doesn't already exist. The idea is at the end of every successful load test, the script will submit one image query to the heartbeat detector. It will be submitted to the cloud, not edge. If the heartbeat detector doesn't receive an image query for a given period of time, Groundlight will send an email alert.
  3. This canary writes local logs like this:
Cron job started at Fri Feb 21 05:05:01 PM PST 2025
2025-02-21 17:05:02 - INFO - Heartbeat alert already exists. Skipping...
2025-02-21 17:05:04 - INFO - Starting laptop edge canary test. Submitting 1000 image queries to det_2sxWe4bhmSjcAeqNk2R9wc2xaIb...
2025-02-21 17:08:00 - INFO - Processed 1000 image queries in 175.91 seconds at 5.68 queries per second.
2025-02-21 17:08:00 - INFO - Finished with an average confidence of 0.95.
2025-02-21 17:08:00 - INFO - Laptop edge canary seems to be online and functioning properly. Submitting heartbeat...
2025-02-21 17:08:01 - INFO - Heartbeat submitted!
Cron job finished at Fri Feb 21 05:08:01 PM PST 2025

What remains to be done:

  1. Figure out how edge-endpoint picks up new docker images. Is this happening automatically? Probably need to add some GHA that uploads images with a canaries or staging tag prior to pushing to latest. Maybe that should be a separate PR?

@timmarkhuff timmarkhuff requested a review from a team as a code owner February 14, 2025 01:05
@timmarkhuff timmarkhuff marked this pull request as draft February 14, 2025 01:05
@timmarkhuff timmarkhuff marked this pull request as ready for review February 17, 2025 17:29
@timmarkhuff timmarkhuff changed the title Adding edge canary Adding edge canary on laptop Feb 17, 2025
@robotrapta robotrapta self-requested a review February 17, 2025 18:21
Copy link
Member

@robotrapta robotrapta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few things to change.

import numpy as np

# TODO update this to the real pager duty inbox
PAGER_DUTY_INBOX = '[email protected]'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change this to '[email protected]' - I set it up as an email alias.

logger = logging.getLogger(__name__)

# Connect to Groundlight Cloud
cloud_endpoint = 'https://api.groundlight.ai/device-api'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shoudn't explicitly set the endpoint. This is the default, and setting it here likely makes it harder to configure/override through environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The endpoint here is explicitly set so that this Groundlight client can point to the cloud and send a heartbeat image query. If I do not explicitly set it here, it will pick up the edge endpoint from the environment variables.

gl = groundlight.Groundlight()

# TODO parameterize this
detector_id = "det_2sxWe4bhmSjcAeqNk2R9wc2xaIb"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say the best practice here would be to read this from an environment variable. Expect that whatever user is running this has the environment variables set. You need to do something special in cron to make sure those environment variables get passed into cronjobs. But then just have the code error out if the environment variables aren't set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this whole directory under cicd/ in the repo.

if [ ! -f "$ENV_FILE" ]; then
echo ".env file not found! Creating a default one..."
cat <<EOF > "$ENV_FILE"
GROUNDLIGHT_API_TOKEN="api_xxxxxx"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be customized somehow. We don't want to check in the real token. And it's a bad practice to modify the script before running it - because that has a tendency to lead to accidentally checking in the real token. Instead, require whatever account is running this script to have the environment variable set. In bash, check that the token is set, and if it isn't raise an error and tell them to set it.

cat <<EOF > "$ENV_FILE"
GROUNDLIGHT_API_TOKEN="api_xxxxxx"
GROUNDLIGHT_ENDPOINT="http://localhost:30101"
RTSP_URL="rtsp://..."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same pattern here as for API token


# Check that the query rate is sufficiently fast (edge speed)
MINIMUM_EXPECTED_BINARY_QUERY_RATE = 5.0 # this is a little conservative to avoid alerting too much
if query_rate < MINIMUM_EXPECTED_BINARY_QUERY_RATE:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should log the observed query rate - pass or fail.


# Load environment variables from .env
if [ -f "$ENV_FILE" ]; then
export $(grep -v '^#' "$ENV_FILE" | xargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good pattern. You can also just source $(ENV_FILE) and have the file say export FOO=BAR...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants