This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Thorsten Klein <[email protected]>
- Loading branch information
1 parent
7a90a37
commit 18c37f8
Showing
12 changed files
with
128 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package usage | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/acorn-io/runtime/pkg/config" | ||
"github.com/acorn-io/runtime/pkg/version" | ||
"github.com/acorn-io/z" | ||
"github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
kclient "sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type RuntimeComponent string | ||
type RuntimeAction string | ||
|
||
const ( | ||
BaseURL = "" | ||
|
||
ComponentCLI RuntimeComponent = "cli" | ||
ComponentAPIServer RuntimeComponent = "api-server" | ||
ComponentController RuntimeComponent = "controller" | ||
|
||
ActionInstall RuntimeAction = "install" // subpaths: /install/{version} | ||
|
||
ActionHeartbeat RuntimeAction = "heartbeat" // subpaths: /heartbeat/{version} | ||
|
||
EnvUsageMetrics = "ACORN_USAGE_METRICS" | ||
) | ||
|
||
func Pulse(ctx context.Context, c kclient.Client, component RuntimeComponent, action RuntimeAction, elements ...string) error { | ||
if !UsageMetricsEnabled(ctx, c) { | ||
return nil | ||
} | ||
|
||
url := fmt.Sprintf("%s/%s/%s", BaseURL, component, action) | ||
for _, e := range elements { | ||
url += "/" + e | ||
} | ||
|
||
resp, err := http.Get(url) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Heartbeat(ctx context.Context, c kclient.Client, component RuntimeComponent, interval time.Duration, elements ...string) { | ||
wait.UntilWithContext(ctx, func(_ context.Context) { | ||
err := Pulse(ctx, c, component, ActionHeartbeat, elements...) | ||
if err != nil { | ||
logrus.Warnf("failed to send heartbeat for %q: %v", component, err) | ||
} | ||
}, interval) | ||
} | ||
|
||
// UsageMetricsEnabled returns true if usage metrics are enabled. | ||
// Usage Metrics are enabled by default, but are disabled by | ||
// a) setting the ACORN_USAGE_METRICS environment variable to "disabled" | ||
// b) setting the DisableUsageMetrics field in the acorn config to true | ||
// c) running a development build (dirty or tag ending in -dev) | ||
// d) running in an unofficial build (BaseURL is empty) | ||
func UsageMetricsEnabled(ctx context.Context, c kclient.Client) bool { | ||
enabled := true | ||
if c != nil { | ||
cfg, err := config.Get(ctx, c) | ||
if err != nil { | ||
return false | ||
} | ||
enabled = !z.Dereference(cfg.DisableUsageMetrics) | ||
} | ||
return enabled && os.Getenv(EnvUsageMetrics) != "disabled" && !version.Get().Dirty && !strings.HasSuffix(version.Get().Tag, "-dev") && BaseURL != "" | ||
} |