-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.go
49 lines (40 loc) · 1.26 KB
/
platform.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"time"
)
// This endpoint is common with most cloud providers, aka should work on GCP, AWS, Azure, etc.
// We use this to determine if we are running on a cloud VM, and log a warning if we aren't.
const metadataUrl = "169.254.169.254"
func runningOnCloudVM(ctx context.Context) (bool, error) {
timedCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(timedCtx, "GET", "http://"+metadataUrl, nil)
if err != nil {
return false, fmt.Errorf("new request: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return false, nil
}
return false, fmt.Errorf("do request: %w", err)
}
return resp.StatusCode == http.StatusOK, nil
}
func logWarningIfNotOnCloudVM(ctx context.Context, logger *slog.Logger) error {
onCloudVM, err := runningOnCloudVM(ctx)
if err != nil {
return fmt.Errorf("failed to check if running on cloud vm: %w", err)
}
if !onCloudVM {
logger.Warn(
"script is likely not running on a cloud VM. this benchmark is designed to be run from a VM in the same region as the turbopuffer deployment, results will likely be inaccurate",
)
}
return nil
}