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

feature(backend): adding analytics events for test run checkpoints #2375

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions server/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,26 @@ func Ready() bool {
return defaultClient != nil && defaultClient.Ready()
}

func SendEvent(name, category, clientID string) error {
func SendEvent(name, category, clientID string, data *map[string]string) error {
fmt.Printf(`sending event "%s" (%s)%s`, name, category, "\n")
if !Ready() {
err := fmt.Errorf("uninitalized client. Call analytics.Init")
fmt.Printf(`could not send event "%s" (%s): %s%s`, name, category, err.Error(), "\n")
return err
}
err := defaultClient.Track(name, map[string]string{

eventData := map[string]string{
"category": category,
"clientID": clientID,
})
}

if data != nil {
for k, v := range *data {
eventData[k] = v
}
}

err := defaultClient.Track(name, eventData)
if err != nil {
fmt.Printf(`could not send event "%s" (%s): %s%s`, name, category, err.Error(), "\n")
} else {
Expand Down
4 changes: 2 additions & 2 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (app *App) Start(opts ...appOption) error {

fmt.Println("New install?", isNewInstall)
if isNewInstall {
err = analytics.SendEvent("Install", "beacon", "")
err = analytics.SendEvent("Install", "beacon", "", nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func (app *App) Start(opts ...appOption) error {
rf.assertionRunner.Stop()
})

err = analytics.SendEvent("Server Started", "beacon", "")
err = analytics.SendEvent("Server Started", "beacon", "", nil)
if err != nil {
return err
}
Expand Down
12 changes: 11 additions & 1 deletion server/executor/assertion_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"time"

"github.com/kubeshop/tracetest/server/analytics"
"github.com/kubeshop/tracetest/server/expression"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/model/events"
Expand Down Expand Up @@ -122,7 +123,12 @@ func (e *defaultAssertionRunner) runAssertionsAndUpdateResult(ctx context.Contex
log.Printf("[AssertionRunner] Test %s Run %d: fail to emit TestSpecsRunError event: %s\n", request.Test.ID, request.Run.ID, anotherErr.Error())
}

return model.Run{}, e.updater.Update(ctx, run.AssertionFailed(err))
run = run.AssertionFailed(err)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was trying to find a single place where we finished a test run no matter the outcome, but I didn't find it, it feels that wherever the error is found we are just updating the required fields.

I know that the run.go model has a Finish method but it didn't feel right to execute an analytics event from a model.

We need to consider having a single spot to finish a test run that is not the model where we could execute side effects in the future, and not having to scatter the logic across these files.

analytics.SendEvent("test_run_finished", "error", "", &map[string]string{
"finalState": string(run.State),
})

return model.Run{}, e.updater.Update(ctx, run)
}
log.Printf("[AssertionRunner] Test %s Run %d: Success. pass: %d, fail: %d\n", request.Test.ID, request.Run.ID, run.Pass, run.Fail)

Expand Down Expand Up @@ -177,6 +183,10 @@ func (e *defaultAssertionRunner) executeAssertions(ctx context.Context, req Asse
allPassed,
)

analytics.SendEvent("test_run_finished", "successful", "", &map[string]string{
"finalState": string(run.State),
})

return run, nil
}

Expand Down
9 changes: 8 additions & 1 deletion server/executor/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"strings"

"github.com/kubeshop/tracetest/server/analytics"
"github.com/kubeshop/tracetest/server/executor/trigger"
"github.com/kubeshop/tracetest/server/expression"
"github.com/kubeshop/tracetest/server/model"
Expand Down Expand Up @@ -272,7 +273,13 @@ func (r persistentRunner) processExecQueue(job execReq) {
func (r persistentRunner) handleExecutionResult(run model.Run, response trigger.Response, err error) model.Run {
run = run.TriggerCompleted(response.Result)
if err != nil {
return run.TriggerFailed(err)
run = run.TriggerFailed(err)

analytics.SendEvent("test_run_finished", "error", "", &map[string]string{
"finalState": string(run.State),
})

return run
}

return run.SuccessfullyTriggered()
Expand Down
8 changes: 7 additions & 1 deletion server/executor/trace_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"time"

"github.com/kubeshop/tracetest/server/analytics"
"github.com/kubeshop/tracetest/server/executor/pollingprofile"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/model/events"
Expand Down Expand Up @@ -213,7 +214,12 @@ func (tp tracePoller) handleTraceDBError(job PollingRequest, err error) (bool, s
fmt.Println("[TracePoller] Unknown error", err)
}

tp.handleDBError(tp.updater.Update(job.ctx, run.TraceFailed(err)))
run = run.TraceFailed(err)
analytics.SendEvent("test_run_finished", "error", "", &map[string]string{
"finalState": string(run.State),
})

tp.handleDBError(tp.updater.Update(job.ctx, run))

tp.subscriptionManager.PublishUpdate(subscription.Message{
ResourceID: run.TransactionStepResourceID(),
Expand Down
2 changes: 1 addition & 1 deletion server/http/custom_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func toWords(str string) string {
func (c *customController) analytics(name string, f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
machineID := r.Header.Get("x-client-id")
analytics.SendEvent(toWords(name), "test", machineID)
analytics.SendEvent(toWords(name), "test", machineID, nil)

f(w, r)
}
Expand Down