Skip to content

Commit

Permalink
feat: Added extra fields for sentry (keploy#1203)
Browse files Browse the repository at this point in the history
* fix: added extra fields for sentry

Signed-off-by: Pranshu Srivastava <[email protected]>

* fix: removed unnecessary changes

Signed-off-by: Pranshu Srivastava <[email protected]>

* fix: fix merge conflicts

Signed-off-by: Pranshu Srivastava <[email protected]>

* fix: addressed the comments

Signed-off-by: Pranshu Srivastava <[email protected]>

* fix: removed unnecessary files

Signed-off-by: Pranshu Srivastava <[email protected]>

* fix: fix indentation

Signed-off-by: Pranshu Srivastava <[email protected]>

* fix: remove binary

Signed-off-by: Pranshu Srivastava <[email protected]>

---------

Signed-off-by: Pranshu Srivastava <[email protected]>
Former-commit-id: 1dccf36
  • Loading branch information
PranshuSrivastava authored Dec 18, 2023
1 parent 54a9992 commit 96979e0
Show file tree
Hide file tree
Showing 18 changed files with 101 additions and 26 deletions.
11 changes: 10 additions & 1 deletion cmd/mockrecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,29 @@ func (mr *MockRecord) GetCmd() *cobra.Command {
return
}

enableTele, err := cmd.Flags().GetBool("enableTele")
if err != nil {
mr.logger.Error(Emoji + "failed to read the enableTele flag")
return
}


proxyPort, err := cmd.Flags().GetUint32("proxyport")
if err != nil {
mr.logger.Error(Emoji + "failed to read the proxy port")
return
}

mr.mockRecorder.MockRecord(path, proxyPort, pid, dir)
mr.mockRecorder.MockRecord(path, proxyPort, pid, dir, enableTele)
},
}

serveCmd.Flags().Uint32("pid", 0, "Process id of your application.")
serveCmd.MarkFlagRequired("pid")

serveCmd.Flags().Uint32("proxyport", 0, "Choose a port to run Keploy Proxy.")
serveCmd.Flags().Bool("enableTele", true, "Switch for telemetry")
serveCmd.Flags().MarkHidden("enableTele")

serveCmd.Flags().StringP("path", "p", "", "Path to local directory where generated testcases/mocks are stored")
serveCmd.MarkFlagRequired("path")
Expand Down
10 changes: 9 additions & 1 deletion cmd/mocktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,19 @@ func (s *MockTest) GetCmd() *cobra.Command {
return
}

enableTele, err := cmd.Flags().GetBool("enableTele")
if err != nil {
s.logger.Error(Emoji + "failed to read the enableTele flag")
return
}

proxyPort, err := cmd.Flags().GetUint32("proxyport")
if err != nil {
s.logger.Error(Emoji + "failed to read the proxyport")
return
}

s.mockTester.MockTest(path, proxyPort, pid, dir)
s.mockTester.MockTest(path, proxyPort, pid, dir, enableTele)
},
}

Expand All @@ -81,6 +87,8 @@ func (s *MockTest) GetCmd() *cobra.Command {
serveCmd.Flags().StringP("path", "p", "", "Path to local directory where generated testcases/mocks are stored")
serveCmd.MarkFlagRequired("path")
serveCmd.Flags().Uint32("proxyport", 0, "Choose a port to run Keploy Proxy.")
serveCmd.Flags().Bool("enableTele", true, "Switch for telemetry")
serveCmd.Flags().MarkHidden("enableTele")

serveCmd.Flags().StringP("mockName", "m", "", "User provided test suite")
serveCmd.MarkFlagRequired("mockName")
Expand Down
11 changes: 10 additions & 1 deletion cmd/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ func (r *Record) GetCmd() *cobra.Command {
return err
}

enableTele, err := cmd.Flags().GetBool("enableTele")
if err != nil {
r.logger.Error("failed to read the disable telemetry flag")
return err
}

err = r.GetRecordConfig(&path, &proxyPort, &appCmd, &appContainer, &networkName, &delay, &buildDelay, &ports, configPath)
if err != nil {
if err == errFileNotFound {
Expand Down Expand Up @@ -204,7 +210,7 @@ func (r *Record) GetCmd() *cobra.Command {
}

r.logger.Debug("the ports are", zap.Any("ports", ports))
r.recorder.CaptureTraffic(path, proxyPort, appCmd, appContainer, networkName, delay, buildDelay, ports, &filters)
r.recorder.CaptureTraffic(path, proxyPort, appCmd, appContainer, networkName, delay, buildDelay, ports, &filters, enableTele)
return nil
},
}
Expand All @@ -227,6 +233,9 @@ func (r *Record) GetCmd() *cobra.Command {

recordCmd.Flags().String("config-path", ".", "Path to the local directory where keploy configuration file is stored")

recordCmd.Flags().Bool("enableTele", true, "Switch for telemetry")
recordCmd.Flags().MarkHidden("enableTele")

recordCmd.SilenceUsage = true
recordCmd.SilenceErrors = true

Expand Down
34 changes: 32 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"os/exec"
"runtime"
"time"

"github.com/TheZeroSlave/zapsentry"
sentry "github.com/getsentry/sentry-go"
"github.com/spf13/cobra"
"go.keploy.io/server/pkg/platform/fs"
"go.keploy.io/server/utils"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
Expand Down Expand Up @@ -124,8 +127,35 @@ func modifyToSentryLogger(log *zap.Logger, client *sentry.Client) *zap.Logger {
}

log = zapsentry.AttachCoreToLogger(core, log)

return log.With(zapsentry.NewScope())
kernelVersion := ""
if runtime.GOOS == "linux" {
cmd := exec.Command("uname", "-r")
kernelBytes, err := cmd.Output()
if err != nil {
log.Debug("failed to get kernel version", zap.Error(err))
} else {
kernelVersion = string(kernelBytes)
}
}
arch := runtime.GOARCH
installationID, err := fs.NewTeleFS(log).Get(false)
if err != nil {
log.Debug("failed to get installationID", zap.Error(err))
}
if installationID == "" {
installationID, err = fs.NewTeleFS(log).Get(true)
if err != nil {
log.Debug("failed to get installationID for new user.", zap.Error(err))
}
}
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTag("Keploy Version", utils.KeployVersion)
scope.SetTag("Linux Kernel Version", kernelVersion)
scope.SetTag("Architecture", arch)
scope.SetTag("Installation ID", installationID)
// Add more context as needed
})
return log
}

func customTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
Expand Down
10 changes: 9 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,20 @@ func (s *Serve) GetCmd() *cobra.Command {
return
}

enableTele, err := cmd.Flags().GetBool("enableTele")
if err != nil {
s.logger.Error("failed to read the disable telemetry flag")
return
}

proxyPort, err := cmd.Flags().GetUint32("proxyport")
if err != nil {
s.logger.Error("failed to read the proxy port")
return
}
s.logger.Debug("the ports are", zap.Any("ports", ports))

s.server.Serve(path, proxyPort, testReportPath, delay, pid, port, language, ports, apiTimeout, appCmd)
s.server.Serve(path, proxyPort, testReportPath, delay, pid, port, language, ports, apiTimeout, appCmd, enableTele)
},
}

Expand All @@ -125,6 +131,8 @@ func (s *Serve) GetCmd() *cobra.Command {
serveCmd.Flags().Uint64("apiTimeout", 5, "User provided timeout for calling its application")

serveCmd.Flags().UintSlice("passThroughPorts", []uint{}, "Ports of Outgoing dependency calls to be ignored as mocks")
serveCmd.Flags().Bool("enableTele", true, "Switch for telemetry")
serveCmd.Flags().MarkHidden("enableTele")

serveCmd.Flags().StringP("language", "l", "", "application programming language")
serveCmd.Flags().StringP("command", "c", "", "Command to start the user application")
Expand Down
11 changes: 10 additions & 1 deletion cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ func (t *Test) GetCmd() *cobra.Command {
return err
}

enableTele, err := cmd.Flags().GetBool("enableTele")
if err != nil {
t.logger.Error("failed to read the disable telemetry flag")
return err
}

tests := map[string][]string{}

testsets, err := cmd.Flags().GetStringSlice("testsets")
Expand Down Expand Up @@ -278,7 +284,7 @@ func (t *Test) GetCmd() *cobra.Command {
TestsetNoise: testsetNoise,
WithCoverage: withCoverage,
CoverageReportPath: coverageReportPath,
})
}, enableTele)

return nil
},
Expand Down Expand Up @@ -309,6 +315,9 @@ func (t *Test) GetCmd() *cobra.Command {

testCmd.Flags().String("coverageReportPath", "", "Write a go coverage profile to the file in the given directory.")

testCmd.Flags().Bool("enableTele", true, "Switch for telemetry")
testCmd.Flags().MarkHidden("enableTele")

testCmd.Flags().Bool("withCoverage", false, "Capture the code coverage of the go binary in the command flag.")
testCmd.Flags().Lookup("withCoverage").NoOptDefVal = "true"
testCmd.SilenceUsage = true
Expand Down
4 changes: 2 additions & 2 deletions pkg/platform/fs/tele.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ type Telemetry struct {

func UserHomeDir(isNewConfigPath bool) string {

var configFolder = "/keploy-config"
var configFolder = "/.keploy-config"
if isNewConfigPath {
configFolder = "/.keploy-config"
configFolder = "/.keploy"
}

if runtime.GOOS == "windows" {
Expand Down
4 changes: 2 additions & 2 deletions pkg/service/mockrecord/mockrecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func NewMockRecorder(logger *zap.Logger) MockRecorder {
}
}

func (s *mockRecorder) MockRecord(path string, proxyPort uint32, pid uint32, mockName string) {
func (s *mockRecorder) MockRecord(path string, proxyPort uint32, pid uint32, mockName string, enableTele bool) {

models.SetMode(models.MODE_RECORD)
teleFS := fs.NewTeleFS(s.logger)
tele := telemetry.NewTelemetry(true, false, teleFS, s.logger, "", nil)
tele := telemetry.NewTelemetry(enableTele, false, teleFS, s.logger, "", nil)
tele.Ping(false)
ys := yaml.NewYamlStore(path, path, "", mockName, s.logger, tele)
routineId := pkg.GenerateRandomID()
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/mockrecord/service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package mockrecord

type MockRecorder interface {
MockRecord(path string, proxyPort uint32, pid uint32, dir string)
MockRecord(path string, proxyPort uint32, pid uint32, dir string, enableTele bool)
}
4 changes: 2 additions & 2 deletions pkg/service/mocktest/mocktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func NewMockTester(logger *zap.Logger) MockTester {
}
}

func (s *mockTester) MockTest(path string, proxyPort, pid uint32, mockName string) {
func (s *mockTester) MockTest(path string, proxyPort, pid uint32, mockName string, enableTele bool) {

models.SetMode(models.MODE_TEST)
teleFS := fs.NewTeleFS(s.logger)
tele := telemetry.NewTelemetry(true, false, teleFS, s.logger, "", nil)
tele := telemetry.NewTelemetry(enableTele, false, teleFS, s.logger, "", nil)
tele.Ping(false)
ys := yaml.NewYamlStore(path, path, "", mockName, s.logger, tele)
s.logger.Debug("path of mocks : " + path)
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/mocktest/service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package mocktest

type MockTester interface {
MockTest(path string, proxyPort uint32, pid uint32, dir string)
MockTest(path string, proxyPort uint32, pid uint32, dir string, enableTele bool)
}
5 changes: 2 additions & 3 deletions pkg/service/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ func NewRecorder(logger *zap.Logger) Recorder {
}
}

func (r *recorder) CaptureTraffic(path string, proxyPort uint32, appCmd, appContainer, appNetwork string, Delay uint64, buildDelay time.Duration, ports []uint, filters *models.Filters) {
func (r *recorder) CaptureTraffic(path string, proxyPort uint32, appCmd, appContainer, appNetwork string, Delay uint64, buildDelay time.Duration, ports []uint, filters *models.Filters, enableTele bool) {

var ps *proxy.ProxySet
stopper := make(chan os.Signal, 1)
signal.Notify(stopper, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGKILL)

models.SetMode(models.MODE_RECORD)

teleFS := fs.NewTeleFS(r.Logger)
tele := telemetry.NewTelemetry(true, false, teleFS, r.Logger, "", nil)
tele := telemetry.NewTelemetry(enableTele, false, teleFS, r.Logger, "", nil)
tele.Ping(false)

dirName, err := yaml.NewSessionIndex(path, r.Logger)
Expand Down
5 changes: 3 additions & 2 deletions pkg/service/record/service.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package record

import (
"go.keploy.io/server/pkg/models"
"time"

"go.keploy.io/server/pkg/models"
)

type Recorder interface {
CaptureTraffic(path string, proxyPort uint32, appCmd, appContainer, networkName string, Delay uint64, buildDelay time.Duration, ports []uint, filters *models.Filters)
CaptureTraffic(path string, proxyPort uint32, appCmd, appContainer, networkName string, Delay uint64, buildDelay time.Duration, ports []uint, filters *models.Filters, enableTele bool)
}
4 changes: 2 additions & 2 deletions pkg/service/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewServer(logger *zap.Logger) Server {
const defaultPort = 6789

// Serve is called by the serve command and is used to run a graphql server, to run tests separately via apis.
func (s *server) Serve(path string, proxyPort uint32, testReportPath string, Delay uint64, pid, port uint32, lang string, passThroughPorts []uint, apiTimeout uint64, appCmd string) {
func (s *server) Serve(path string, proxyPort uint32, testReportPath string, Delay uint64, pid, port uint32, lang string, passThroughPorts []uint, apiTimeout uint64, appCmd string, enableTele bool) {
var ps *proxy.ProxySet

if port == 0 {
Expand All @@ -58,7 +58,7 @@ func (s *server) Serve(path string, proxyPort uint32, testReportPath string, Del
tester := test.NewTester(s.logger)
testReportFS := yaml.NewTestReportFS(s.logger)
teleFS := fs.NewTeleFS(s.logger)
tele := telemetry.NewTelemetry(true, false, teleFS, s.logger, "", nil)
tele := telemetry.NewTelemetry(enableTele, false, teleFS, s.logger, "", nil)
tele.Ping(false)
ys := yaml.NewYamlStore("", "", "", "", s.logger, tele)
routineId := pkg.GenerateRandomID()
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/serve/service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package serve

type Server interface {
Serve(path string, proxyPort uint32, testReportPath string, Delay uint64, pid, port uint32, lang string, passThroughPorts []uint, apiTimeout uint64, appCmd string)
Serve(path string, proxyPort uint32, testReportPath string, Delay uint64, pid, port uint32, lang string, passThroughPorts []uint, apiTimeout uint64, appCmd string, enableTele bool)
}
2 changes: 1 addition & 1 deletion pkg/service/test/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Tester interface {
Test(path string, testReportPath string, appCmd string, options TestOptions) bool
Test(path string, testReportPath string, appCmd string, options TestOptions, enableTele bool) bool
RunTestSet(testSet, path, testReportPath, appCmd, appContainer, appNetwork string, delay uint64, buildDelay time.Duration, pid uint32, ys platform.TestCaseDB, loadedHook *hooks.Hook, testReportfs platform.TestReportDB, testRunChan chan string, apiTimeout uint64, ctx context.Context, testcases map[string]bool, noiseConfig models.GlobalNoise, serveTest bool) models.TestRunStatus
InitialiseTest(cfg *TestConfig) (InitialiseTestReturn, error)
InitialiseRunTestSet(cfg *RunTestSetConfig) InitialiseRunTestSetReturn
Expand Down
5 changes: 3 additions & 2 deletions pkg/service/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (t *tester) InitialiseTest(cfg *TestConfig) (InitialiseTestReturn, error) {
models.SetMode(models.MODE_TEST)

teleFS := fs.NewTeleFS(t.logger)
tele := telemetry.NewTelemetry(true, false, teleFS, t.logger, "", nil)
tele := telemetry.NewTelemetry(cfg.EnableTele, false, teleFS, t.logger, "", nil)

returnVal.TestReportFS = yaml.NewTestReportFS(t.logger)
// fetch the recorded testcases with their mocks
Expand Down Expand Up @@ -189,7 +189,7 @@ func (t *tester) InitialiseTest(cfg *TestConfig) (InitialiseTestReturn, error) {
return returnVal, nil
}

func (t *tester) Test(path string, testReportPath string, appCmd string, options TestOptions) bool {
func (t *tester) Test(path string, testReportPath string, appCmd string, options TestOptions, enableTele bool) bool {

testRes := false
result := true
Expand All @@ -209,6 +209,7 @@ func (t *tester) Test(path string, testReportPath string, appCmd string, options
MongoPassword: options.MongoPassword,
WithCoverage: options.WithCoverage,
CoverageReportPath: options.CoverageReportPath,
EnableTele: enableTele,
}
initialisedValues, err := t.InitialiseTest(cfg)
// Recover from panic and gracfully shutdown
Expand Down
1 change: 1 addition & 0 deletions pkg/service/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type TestConfig struct {
ApiTimeout uint64
WithCoverage bool
CoverageReportPath string
EnableTele bool
}

type RunTestSetConfig struct {
Expand Down

0 comments on commit 96979e0

Please sign in to comment.