Repository navigation
feat: add anonymous execution telemetry #943
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
Open
gnaponie
wants to merge
1
commit into
redhat-developer:main
Choose a base branch
from
gnaponie:RHAI-10798/telemetry
base: main
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
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,40 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func TestTelemetryCounted(t *testing.T) { | ||
| root := &cobra.Command{Use: "mapt"} | ||
| aws := &cobra.Command{Use: "aws", RunE: noopRunE} | ||
| hosts := &cobra.Command{Use: "hosts"} | ||
| create := &cobra.Command{Use: "create", RunE: noopRunE} | ||
| list := &cobra.Command{Use: "list"} | ||
|
|
||
| root.AddCommand(aws) | ||
| aws.AddCommand(hosts) | ||
| hosts.AddCommand(create, list) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| cmd *cobra.Command | ||
| want bool | ||
| }{ | ||
| {"root shows help", root, false}, | ||
| {"group with RunE", aws, false}, | ||
| {"intermediate group", hosts, false}, | ||
| {"leaf with RunE", create, true}, | ||
| {"leaf without RunE shows help", list, false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := telemetryCounted(tt.cmd); got != tt.want { | ||
| t.Fatalf("telemetryCounted(%q) = %t, want %t", tt.cmd.CommandPath(), got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func noopRunE(cmd *cobra.Command, args []string) error { return nil } |
This file contains hidden or 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,12 @@ | ||
| package telemetry | ||
|
|
||
| // The token below is PostHog's project capture token (phc_). It is | ||
| // intentionally committed in plain text: it is write-only, can only send | ||
| // events into this project, and PostHog documents it as safe to expose in | ||
| // client-side code. It must never be replaced with a personal API key | ||
| // (phx_), which can read and modify project data. | ||
| const ( | ||
| posthogHost = "https://eu.i.posthog.com" | ||
| posthogToken = "phc_BX33W4L8vxDghW9RYfoTn7mTauApz53sDnERYZpUCMQf" | ||
| capturePath = "/i/v0/e/" | ||
| ) |
This file contains hidden or 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,107 @@ | ||
| package telemetry | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "runtime" | ||
| "runtime/debug" | ||
| "time" | ||
|
|
||
| "github.com/redhat-developer/mapt/pkg/util/logging" | ||
| ) | ||
|
|
||
| const sendTimeout = 2 * time.Second | ||
|
|
||
| type Event struct { | ||
| InstallID string | ||
| Version string | ||
| Command string | ||
| } | ||
|
|
||
| type posthogEvent struct { | ||
| APIKey string `json:"api_key"` | ||
| Event string `json:"event"` | ||
| DistinctID string `json:"distinct_id"` | ||
| Properties map[string]any `json:"properties"` | ||
| Timestamp string `json:"timestamp"` | ||
| } | ||
|
|
||
| func (c *Client) Emit(event Event) <-chan struct{} { | ||
| done := make(chan struct{}) | ||
| go func() { | ||
| defer close(done) | ||
| c.send(event) | ||
| }() | ||
| return done | ||
| } | ||
|
|
||
| func (c *Client) Wait(done <-chan struct{}, timeout time.Duration) { | ||
| if done == nil { | ||
| return | ||
| } | ||
| timer := time.NewTimer(timeout) | ||
| defer timer.Stop() | ||
| select { | ||
| case <-done: | ||
| case <-timer.C: | ||
| } | ||
| } | ||
|
|
||
| // version is injected at build time by the Makefile via | ||
| // -ldflags "-X .../pkg/telemetry.version=<VERSION>", mirroring the version | ||
| // variables of the other integrations. When unset (plain `go build`), Version | ||
| // falls back to the module version from build info. | ||
| var version = "" | ||
|
|
||
| func Version() string { | ||
| if version != "" { | ||
| return version | ||
| } | ||
| info, ok := debug.ReadBuildInfo() | ||
| if !ok || info.Main.Version == "" || info.Main.Version == "(devel)" { | ||
| return "dev" | ||
| } | ||
| return info.Main.Version | ||
| } | ||
|
|
||
| func (c *Client) send(event Event) { | ||
| payload := posthogEvent{ | ||
| APIKey: c.apiKey, | ||
| Event: "mapt.execution", | ||
| DistinctID: event.InstallID, | ||
| Properties: map[string]any{ | ||
| "mapt_version": event.Version, | ||
| "command": event.Command, | ||
| "os": runtime.GOOS, | ||
| "arch": runtime.GOARCH, | ||
| // PostHog reads the client IP from every capture request and uses | ||
| // it for GeoIP enrichment by default; disable it so no location | ||
| // data is derived from users' network addresses. | ||
| "$geoip_disable": true, | ||
| }, | ||
| Timestamp: time.Now().UTC().Format(time.RFC3339), | ||
| } | ||
| data, err := json.Marshal(payload) | ||
| if err != nil { | ||
| logging.Debugf("telemetry delivery failed: %v", err) | ||
| return | ||
| } | ||
| request, err := http.NewRequest(http.MethodPost, c.endpoint, bytes.NewReader(data)) | ||
| if err != nil { | ||
| logging.Debugf("telemetry delivery failed: %v", err) | ||
| return | ||
| } | ||
| request.Header.Set("Content-Type", "application/json") | ||
| response, err := c.httpClient.Do(request) | ||
| if err != nil { | ||
| logging.Debugf("telemetry delivery failed: %v", err) | ||
| return | ||
| } | ||
| defer func() { _ = response.Body.Close() }() | ||
| if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices { | ||
| logging.Debugf("telemetry delivery failed: HTTP status %s", fmt.Sprint(response.Status)) | ||
| } | ||
| } | ||
|
|
||
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.
Script executed:
Repository: redhat-developer/mapt
Length of output: 41613
Script executed:
Repository: redhat-developer/mapt
Length of output: 41065
Inject the application version into telemetry builds.
The root
Makefilebuild targets pass only integration values throughLDFLAGS. They do not inject a version consumed bypkg/telemetry. A release that uses these targets can therefore report"(devel)"throughdebug.ReadBuildInfo()and emit"dev"instead of its release version. Add the release version to the telemetry build-time version source.