From 17b6d95b3b5912c126cfc031c8f9696945e27e7b Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 20 Jul 2023 23:08:51 +0800 Subject: [PATCH] refactor: replace GA with GA4 --- hrp/boomer.go | 17 +++++++---------- hrp/internal/pytest/main.go | 5 +---- hrp/internal/scaffold/main.go | 5 +---- hrp/internal/sdk/ga4.go | 27 +++++++++++++++++---------- hrp/internal/wiki/main.go | 5 +---- hrp/pkg/convert/main.go | 12 +++++++----- hrp/plugin.go | 11 +++++------ hrp/runner.go | 17 +++++++++-------- 8 files changed, 48 insertions(+), 51 deletions(-) diff --git a/hrp/boomer.go b/hrp/boomer.go index 0f35e34d..2a9ed700 100644 --- a/hrp/boomer.go +++ b/hrp/boomer.go @@ -93,17 +93,14 @@ func (b *HRPBoomer) SetPython3Venv(venv string) *HRPBoomer { // Run starts to run load test for one or multiple testcases. func (b *HRPBoomer) Run(testcases ...ITestCase) { - event := sdk.EventTracking{ - Category: "RunLoadTests", - Action: "hrp boom", - } - // report start event - go sdk.SendEvent(event) - // report execution timing event - defer sdk.SendEvent(event.StartTiming("execution")) - - // quit all plugins + startTime := time.Now() defer func() { + // report boom event + sdk.SendGA4Event("hrp_boom", map[string]interface{}{ + "engagement_time_msec": time.Since(startTime).Milliseconds(), + }) + + // quit all plugins pluginMap.Range(func(key, value interface{}) bool { if plugin, ok := value.(funplugin.IPlugin); ok { plugin.Quit() diff --git a/hrp/internal/pytest/main.go b/hrp/internal/pytest/main.go index c9bec74b..31acdca9 100644 --- a/hrp/internal/pytest/main.go +++ b/hrp/internal/pytest/main.go @@ -6,10 +6,7 @@ import ( ) func RunPytest(args []string) error { - sdk.SendEvent(sdk.EventTracking{ - Category: "RunAPITests", - Action: "hrp pytest", - }) + sdk.SendGA4Event("hrp_pytest", nil) args = append([]string{"run"}, args...) return myexec.ExecPython3Command("httprunner", args...) diff --git a/hrp/internal/scaffold/main.go b/hrp/internal/scaffold/main.go index e52ebc1a..3c808224 100644 --- a/hrp/internal/scaffold/main.go +++ b/hrp/internal/scaffold/main.go @@ -55,10 +55,7 @@ func CopyFile(templateFile, targetFile string) error { func CreateScaffold(projectName string, pluginType PluginType, venv string, force bool) error { // report event - sdk.SendEvent(sdk.EventTracking{ - Category: "Scaffold", - Action: "hrp startproject", - }) + sdk.SendGA4Event("hrp_startproject", nil) log.Info(). Str("projectName", projectName). diff --git a/hrp/internal/sdk/ga4.go b/hrp/internal/sdk/ga4.go index b7adee79..395b6a65 100644 --- a/hrp/internal/sdk/ga4.go +++ b/hrp/internal/sdk/ga4.go @@ -3,7 +3,6 @@ package sdk import ( "bytes" "encoding/json" - "errors" "fmt" "io/ioutil" "math/rand" @@ -13,6 +12,7 @@ import ( "time" "github.com/denisbrodbeck/machineid" + "github.com/pkg/errors" "github.com/rs/zerolog/log" uuid "github.com/satori/go.uuid" @@ -159,18 +159,17 @@ func (g *GA4Client) SendEvent(event Event) error { Msg("send GA4 event") } if err != nil { - return err + return errors.Wrap(err, "marshal GA4 request payload failed") } body := bytes.NewReader(bs) res, err := g.httpClient.Post(uri, "application/json", body) if err != nil { - return err + return errors.Wrap(err, "request GA4 failed") } if res.StatusCode >= 300 { - log.Error().Int("statusCode", res.StatusCode).Msg("validation response got unexpected status") - return errors.New("validation response got unexpected status") + return fmt.Errorf("validation response got unexpected status %d", res.StatusCode) } if !g.debug { @@ -179,13 +178,13 @@ func (g *GA4Client) SendEvent(event Event) error { bs, err = ioutil.ReadAll(res.Body) if err != nil { - return err + return errors.Wrap(err, "read GA4 response body failed") } validationResponse := ValidationResponse{} err = json.Unmarshal(bs, &validationResponse) if err != nil { - return fmt.Errorf("unmarshal response body error: %w", err) + return errors.Wrap(err, "unmarshal GA4 response body failed") } log.Debug(). @@ -195,10 +194,18 @@ func (g *GA4Client) SendEvent(event Event) error { return nil } -func SendGA4Event(e IEvent) error { +func SendGA4Event(name string, params map[string]interface{}) { if env.DISABLE_GA == "true" { // do not send GA4 events in CI environment - return nil + return + } + + event := Event{ + Name: name, + Params: params, + } + err := ga4Client.SendEvent(event) + if err != nil { + log.Error().Err(err).Msg("send GA4 event failed") } - return gaClient.SendEvent(e) } diff --git a/hrp/internal/wiki/main.go b/hrp/internal/wiki/main.go index 2557e499..1d9b3680 100644 --- a/hrp/internal/wiki/main.go +++ b/hrp/internal/wiki/main.go @@ -8,10 +8,7 @@ import ( ) func OpenWiki() error { - sdk.SendEvent(sdk.EventTracking{ - Category: "OpenWiki", - Action: "hrp wiki", - }) + sdk.SendGA4Event("hrp_wiki", nil) log.Info().Msgf("%s https://httprunner.com", openCmd) return myexec.RunCommand(openCmd, "https://httprunner.com") } diff --git a/hrp/pkg/convert/main.go b/hrp/pkg/convert/main.go index 7ac5a238..6be392f1 100644 --- a/hrp/pkg/convert/main.go +++ b/hrp/pkg/convert/main.go @@ -2,7 +2,6 @@ package convert import ( _ "embed" - "fmt" "path/filepath" "github.com/rs/zerolog/log" @@ -141,10 +140,13 @@ func (c *TCaseConverter) loadCase(casePath string, fromType FromType) error { func (c *TCaseConverter) Convert(casePath string, fromType FromType, outputType OutputType) error { // report event - sdk.SendEvent(sdk.EventTracking{ - Category: "ConvertTests", - Action: fmt.Sprintf("hrp convert --to-%s", outputType.String()), - }) + sdk.SendGA4Event( + "hrp_convert", + map[string]interface{}{ + "from": fromType.String(), + "to": outputType.String(), + }, + ) log.Info().Str("path", casePath). Str("fromType", fromType.String()). Str("outputType", outputType.String()). diff --git a/hrp/plugin.go b/hrp/plugin.go index a23c7dd2..41b6d599 100644 --- a/hrp/plugin.go +++ b/hrp/plugin.go @@ -90,15 +90,14 @@ func initPlugin(path, venv string, logOn bool) (plugin funplugin.IPlugin, err er pluginMap.Store(pluginPath, plugin) // report event for initializing plugin - event := sdk.EventTracking{ - Category: "InitPlugin", - Action: fmt.Sprintf("Init %s plugin", plugin.Type()), - Value: 0, // success + params := map[string]interface{}{ + "type": plugin.Type(), + "result": "success", } if err != nil { - event.Value = 1 // failed + params["result"] = "failed" } - go sdk.SendEvent(event) + go sdk.SendGA4Event("init_plugin", params) return } diff --git a/hrp/runner.go b/hrp/runner.go index 51e78eed..86cfd98d 100644 --- a/hrp/runner.go +++ b/hrp/runner.go @@ -194,14 +194,15 @@ func (r *HRPRunner) GenHTMLReport() *HRPRunner { // Run starts to execute one or multiple testcases. func (r *HRPRunner) Run(testcases ...ITestCase) (err error) { log.Info().Str("hrp_version", version.VERSION).Msg("start running") - event := sdk.EventTracking{ - Category: "RunAPITests", - Action: "hrp run", - } - // report start event - go sdk.SendEvent(event) - // report execution timing event - defer sdk.SendEvent(event.StartTiming("execution")) + + startTime := time.Now() + defer func() { + // report run event + sdk.SendGA4Event("hrp_run", map[string]interface{}{ + "engagement_time_msec": time.Since(startTime).Milliseconds(), + }) + }() + // record execution data to summary s := newOutSummary()