refactor: replace GA with GA4

This commit is contained in:
lilong.129
2023-07-20 23:08:51 +08:00
parent 503b3fd38f
commit 17b6d95b3b
8 changed files with 48 additions and 51 deletions

View File

@@ -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()

View File

@@ -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...)

View File

@@ -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).

View File

@@ -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)
}

View File

@@ -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")
}

View File

@@ -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()).

View File

@@ -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
}

View File

@@ -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()