From 18d692d23dbcacebed9b2e8784c7d2c3caf10de4 Mon Sep 17 00:00:00 2001 From: Jyotika Garg Date: Mon, 5 Jun 2023 11:47:16 +0530 Subject: [PATCH 1/9] adding git action --- .github/workflows/download-info.yml | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 .github/workflows/download-info.yml diff --git a/.github/workflows/download-info.yml b/.github/workflows/download-info.yml new file mode 100644 index 00000000..376707fa --- /dev/null +++ b/.github/workflows/download-info.yml @@ -0,0 +1,42 @@ +name: Download Info + +on: + push: + branches: + - main + +jobs: + print-download-info: + runs-on: ubuntu-latest + + steps: + - name: Check out code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' # Replace with the desired Python version + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install requests + + - name: Get Download Info + run: | + python - < Date: Thu, 6 Jul 2023 16:02:08 +0530 Subject: [PATCH 2/9] Telemetry intial commit --- cmd/modern/main.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/cmd/modern/main.go b/cmd/modern/main.go index 4b358b48..a72487ec 100644 --- a/cmd/modern/main.go +++ b/cmd/modern/main.go @@ -12,6 +12,11 @@ package main import ( + "path" + + "fmt" + "time" + "github.com/microsoft/go-sqlcmd/internal" "github.com/microsoft/go-sqlcmd/internal/cmdparser" "github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency" @@ -22,10 +27,11 @@ import ( "github.com/microsoft/go-sqlcmd/internal/pal" "github.com/microsoft/go-sqlcmd/pkg/sqlcmd" "github.com/spf13/cobra" - "path" "os" + "github.com/microsoft/ApplicationInsights-Go/appinsights" + legacyCmd "github.com/microsoft/go-sqlcmd/cmd/sqlcmd" ) @@ -38,6 +44,17 @@ var version = "local-build" // overridden in pipeline builds with: -ldflags="-X // If the first argument is a modern CLI subcommand, the modern CLI is // executed. Otherwise, the legacy CLI is executed. func main() { + + client := initializeAppInsights() + + defer client.Channel().Close() + appinsights.NewDiagnosticsMessageListener(func(msg string) error { + fmt.Printf("[%s] %s\n", time.Now().Format(time.UnixDate), msg) + return nil + }) + + // Example telemetry tracking + dependencies := dependency.Options{ Output: output.New(output.Options{ StandardWriter: os.Stdout, @@ -46,11 +63,25 @@ func main() { rootCmd = cmdparser.New[*Root](dependencies) if isFirstArgModernCliSubCommand() { cmdparser.Initialize(initializeCallback) + event := appinsights.NewEventTelemetry("sqlcmd") + event.Properties["sqlcmd"] = "modern" + client.Track(event) rootCmd.Execute() } else { initializeEnvVars() + event := appinsights.NewEventTelemetry("sqlcmd") + event.Properties["sqlcmd"] = "legacy" + client.Track(event) legacyCmd.Execute(version) } + client.Channel().Flush() +} + +func initializeAppInsights() appinsights.TelemetryClient { + instrumentationKey := "7d1a32e5-4dbb-4b11-ae2d-b8591bcf4cba" + config := appinsights.NewTelemetryConfiguration(instrumentationKey) + client := appinsights.NewTelemetryClientFromConfig(config) + return client } // initializeEnvVars intializes SQLCMDSERVER, SQLCMDUSER and SQLCMDPASSWORD From 186253edb6861980e9b05bbbd8bb953a279129a1 Mon Sep 17 00:00:00 2001 From: Jyotika Garg Date: Tue, 18 Jul 2023 15:48:45 +0530 Subject: [PATCH 3/9] telemetry package --- build/build.cmd | 2 +- cmd/modern/main.go | 42 ++++++-------------- cmd/modern/root.go | 1 - cmd/modern/root/config.go | 2 + internal/cmdparser/cmd.go | 10 ++++- internal/telemetry/appinsights.go | 66 +++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 internal/telemetry/appinsights.go diff --git a/build/build.cmd b/build/build.cmd index f8db919d..ea83b80c 100644 --- a/build/build.cmd +++ b/build/build.cmd @@ -26,7 +26,7 @@ if not exist %gopath%\bin\gotext.exe ( ) REM go-winres likes to append instead of overwrite so delete existing resource file -del %~dp0..\cmd\modern\*.syso + REM generates translations file and resources go generate %~dp0../... 2> %~dp0generate.txt diff --git a/cmd/modern/main.go b/cmd/modern/main.go index a72487ec..42474f05 100644 --- a/cmd/modern/main.go +++ b/cmd/modern/main.go @@ -12,11 +12,9 @@ package main import ( + "os" "path" - "fmt" - "time" - "github.com/microsoft/go-sqlcmd/internal" "github.com/microsoft/go-sqlcmd/internal/cmdparser" "github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency" @@ -28,9 +26,7 @@ import ( "github.com/microsoft/go-sqlcmd/pkg/sqlcmd" "github.com/spf13/cobra" - "os" - - "github.com/microsoft/ApplicationInsights-Go/appinsights" + "github.com/microsoft/go-sqlcmd/internal/telemetry" legacyCmd "github.com/microsoft/go-sqlcmd/cmd/sqlcmd" ) @@ -45,43 +41,31 @@ var version = "local-build" // overridden in pipeline builds with: -ldflags="-X // executed. Otherwise, the legacy CLI is executed. func main() { - client := initializeAppInsights() + // appinsights.NewDiagnosticsMessageListener(func(msg string) error { + // fmt.Printf("[%s] %s\n", time.Now().Format(time.UnixDate), msg) + // return nil + // }) - defer client.Channel().Close() - appinsights.NewDiagnosticsMessageListener(func(msg string) error { - fmt.Printf("[%s] %s\n", time.Now().Format(time.UnixDate), msg) - return nil - }) - - // Example telemetry tracking + telemetry.InitializeAppInsights() + defer telemetry.FlushTelemetry() dependencies := dependency.Options{ Output: output.New(output.Options{ StandardWriter: os.Stdout, ErrorHandler: checkErr, - HintHandler: displayHints})} + HintHandler: displayHints}), + } rootCmd = cmdparser.New[*Root](dependencies) if isFirstArgModernCliSubCommand() { cmdparser.Initialize(initializeCallback) - event := appinsights.NewEventTelemetry("sqlcmd") - event.Properties["sqlcmd"] = "modern" - client.Track(event) + telemetry.TrackEvent("modern") rootCmd.Execute() } else { initializeEnvVars() - event := appinsights.NewEventTelemetry("sqlcmd") - event.Properties["sqlcmd"] = "legacy" - client.Track(event) + telemetry.TrackEvent("legacy") legacyCmd.Execute(version) } - client.Channel().Flush() -} - -func initializeAppInsights() appinsights.TelemetryClient { - instrumentationKey := "7d1a32e5-4dbb-4b11-ae2d-b8591bcf4cba" - config := appinsights.NewTelemetryConfiguration(instrumentationKey) - client := appinsights.NewTelemetryClientFromConfig(config) - return client + //telemetry.FlushTelemetry() } // initializeEnvVars intializes SQLCMDSERVER, SQLCMDUSER and SQLCMDPASSWORD diff --git a/cmd/modern/root.go b/cmd/modern/root.go index b22fb6bc..e41f8477 100644 --- a/cmd/modern/root.go +++ b/cmd/modern/root.go @@ -16,7 +16,6 @@ import ( // all the sub-commands, like install, query, config etc. type Root struct { cmdparser.Cmd - configFilename string loggingLevel int outputType string diff --git a/cmd/modern/root/config.go b/cmd/modern/root/config.go index 936c8c84..488a07e6 100644 --- a/cmd/modern/root/config.go +++ b/cmd/modern/root/config.go @@ -10,6 +10,7 @@ import ( "github.com/microsoft/go-sqlcmd/internal/cmdparser" "github.com/microsoft/go-sqlcmd/internal/localizer" "github.com/microsoft/go-sqlcmd/internal/pal" + "github.com/microsoft/go-sqlcmd/internal/telemetry" ) // Config defines the `sqlcmd config` sub-commands @@ -38,6 +39,7 @@ func (c *Config) DefineCommand(...cmdparser.CommandOptions) { } c.Cmd.DefineCommand(options) + telemetry.TrackEvent("config") } // SubCommands sets up all the sub-commands for `sqlcmd config` diff --git a/internal/cmdparser/cmd.go b/internal/cmdparser/cmd.go index f6dcd50b..64d49080 100644 --- a/internal/cmdparser/cmd.go +++ b/internal/cmdparser/cmd.go @@ -5,11 +5,13 @@ package cmdparser import ( "fmt" + "strings" + "github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency" "github.com/microsoft/go-sqlcmd/internal/pal" - "strings" "github.com/microsoft/go-sqlcmd/internal/output" + "github.com/microsoft/go-sqlcmd/internal/telemetry" "github.com/spf13/cobra" ) @@ -273,6 +275,10 @@ func (c *Cmd) run(_ *cobra.Command, args []string) { if c.options.FirstArgAlternativeForFlag.Value == nil { panic("Must set Value") } + // Track telemetry for the sub-command + command := c.options.Use + subCommand := args[0] + telemetry.TrackSubCommand(command, subCommand) *c.options.FirstArgAlternativeForFlag.Value = args[0] } } @@ -283,6 +289,8 @@ func (c *Cmd) run(_ *cobra.Command, args []string) { err := c.command.Help() c.CheckErr(err) } else { + command := c.options.Use + telemetry.TrackSubCommand(command, "") c.options.Run() } } diff --git a/internal/telemetry/appinsights.go b/internal/telemetry/appinsights.go new file mode 100644 index 00000000..be17df84 --- /dev/null +++ b/internal/telemetry/appinsights.go @@ -0,0 +1,66 @@ +package telemetry + +import ( + "fmt" + "time" + + "github.com/microsoft/ApplicationInsights-Go/appinsights" +) + +var telemetryClient appinsights.TelemetryClient + +func SetTelemetryClient(client appinsights.TelemetryClient) { + telemetryClient = client +} + +type Telemetry struct { + Client appinsights.TelemetryClient +} + +func InitializeAppInsights() { + instrumentationKey := "7d1a32e5-4dbb-4b11-ae2d-b8591bcf4cba" + config := appinsights.NewTelemetryConfiguration(instrumentationKey) + telemetryClient = appinsights.NewTelemetryClientFromConfig(config) + SetTelemetryClient(telemetryClient) + // Add a diagnostics listener for printing telemetry messages + appinsights.NewDiagnosticsMessageListener(func(msg string) error { + fmt.Printf("[%s] %s\n", time.Now().Format(time.UnixDate), msg) + return nil + }) +} + +// TrackCommand tracks a command execution event +func TrackCommand(command string) { + event := appinsights.NewEventTelemetry("command") + event.Properties["command"] = command + telemetryClient.Track(event) + FlushTelemetry() +} + +// TrackSubCommand tracks a sub-command execution event +func TrackSubCommand(command, subCommand string) { + event := appinsights.NewEventTelemetry("sub-command") + event.Properties["command"] = command + event.Properties["sub-command"] = subCommand + telemetryClient.Track(event) + FlushTelemetry() +} + +func TrackEvent(eventName string) { + event := appinsights.NewEventTelemetry(eventName) + event.Properties["command"] = eventName + telemetryClient.Track(event) +} + +func FlushTelemetry() { + telemetryClient.Channel().Flush() +} + +// func SetTelemetryClient(client appinsights.TelemetryClient) { +// telemetryClient = client +// } + +func LogMessage(msg string) error { + fmt.Printf("[%s] %s\n", time.Now().Format(time.UnixDate), msg) + return nil +} From f7d5292a952686488664842ee4910f34441f78f8 Mon Sep 17 00:00:00 2001 From: Jyotika Garg Date: Mon, 24 Jul 2023 15:39:19 +0530 Subject: [PATCH 4/9] working copy sending commands --- cmd/modern/main.go | 10 ++-------- go.mod | 3 +++ go.sum | 16 ++++++++++++++++ internal/telemetry/appinsights.go | 11 ++++------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/cmd/modern/main.go b/cmd/modern/main.go index 42474f05..604d3129 100644 --- a/cmd/modern/main.go +++ b/cmd/modern/main.go @@ -40,14 +40,7 @@ var version = "local-build" // overridden in pipeline builds with: -ldflags="-X // If the first argument is a modern CLI subcommand, the modern CLI is // executed. Otherwise, the legacy CLI is executed. func main() { - - // appinsights.NewDiagnosticsMessageListener(func(msg string) error { - // fmt.Printf("[%s] %s\n", time.Now().Format(time.UnixDate), msg) - // return nil - // }) - telemetry.InitializeAppInsights() - defer telemetry.FlushTelemetry() dependencies := dependency.Options{ Output: output.New(output.Options{ @@ -65,7 +58,8 @@ func main() { telemetry.TrackEvent("legacy") legacyCmd.Execute(version) } - //telemetry.FlushTelemetry() + telemetry.FlushTelemetry() + telemetry.CloseTelemetry() } // initializeEnvVars intializes SQLCMDSERVER, SQLCMDUSER and SQLCMDPASSWORD diff --git a/go.mod b/go.mod index 35f04035..8fe80c3d 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( ) require ( + code.cloudfoundry.org/clock v0.0.0-20180518195852-02e53af36e6c // indirect github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0 // indirect github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0 // indirect github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect @@ -38,6 +39,7 @@ require ( github.com/docker/go-units v0.5.0 // indirect github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/gofrs/uuid v3.3.0+incompatible // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect @@ -49,6 +51,7 @@ require ( github.com/magiconair/properties v1.8.6 // indirect github.com/mattn/go-runewidth v0.0.3 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect + github.com/microsoft/ApplicationInsights-Go v0.4.4 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/moby/term v0.0.0-20221120202655-abb19827d345 // indirect github.com/morikuni/aec v1.0.0 // indirect diff --git a/go.sum b/go.sum index 8be64098..e4d6f123 100644 --- a/go.sum +++ b/go.sum @@ -35,6 +35,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= +code.cloudfoundry.org/clock v0.0.0-20180518195852-02e53af36e6c h1:5eeuG0BHx1+DHeT3AP+ISKZ2ht1UjGhm581ljqYpVeQ= +code.cloudfoundry.org/clock v0.0.0-20180518195852-02e53af36e6c/go.mod h1:QD9Lzhd/ux6eNQVUDVRJX/RKTigpewimNYBi7ivZKY8= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0 h1:8kDqDngH+DmVBiCtIjCFTGa7MBnsIOkF9IccInFEbjk= github.com/Azure/azure-sdk-for-go/sdk/azcore v1.6.0/go.mod h1:bjGvMhVMb+EEm3VRNQawDMUyMMjo+S5ewNjflkep/0Q= @@ -103,6 +105,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -115,6 +118,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9 github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gofrs/uuid v3.3.0+incompatible h1:8K4tyRfvU1CYPgJsveYFQMhpFd/wXNM7iK6rR7UHz84= +github.com/gofrs/uuid v3.3.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= @@ -198,6 +203,7 @@ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= @@ -236,6 +242,8 @@ github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8Bz github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/microsoft/ApplicationInsights-Go v0.4.4 h1:G4+H9WNs6ygSCe6sUyxRc2U81TI5Es90b2t/MwX5KqY= +github.com/microsoft/ApplicationInsights-Go v0.4.4/go.mod h1:fKRUseBqkw6bDiXTs3ESTiU/4YTIHsQS4W3fP2ieF4U= github.com/microsoft/go-mssqldb v1.3.0 h1:JcPVl+acL8Z/cQcJc9zP0OkjQ+l20bco/cCDpMbmGJk= github.com/microsoft/go-mssqldb v1.3.0/go.mod h1:lmWsjHD8XX/Txr0f8ZqgbEZSC+BZjmEQy/Ms+rLrvho= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -252,6 +260,9 @@ github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= @@ -328,6 +339,7 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs= github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= +github.com/tedsuo/ifrit v0.0.0-20180802180643-bea94bb476cc/go.mod h1:eyZnKCc955uh98WQvzOm0dgAeLnf2O0Rz0LPoC5ze+0= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -390,6 +402,7 @@ golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -451,6 +464,7 @@ golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -677,8 +691,10 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/telemetry/appinsights.go b/internal/telemetry/appinsights.go index be17df84..fb7679cb 100644 --- a/internal/telemetry/appinsights.go +++ b/internal/telemetry/appinsights.go @@ -18,7 +18,7 @@ type Telemetry struct { } func InitializeAppInsights() { - instrumentationKey := "7d1a32e5-4dbb-4b11-ae2d-b8591bcf4cba" + instrumentationKey := "5587abc4-2843-4455-b7dd-c3eb1f2e45f6" config := appinsights.NewTelemetryConfiguration(instrumentationKey) telemetryClient = appinsights.NewTelemetryClientFromConfig(config) SetTelemetryClient(telemetryClient) @@ -34,7 +34,6 @@ func TrackCommand(command string) { event := appinsights.NewEventTelemetry("command") event.Properties["command"] = command telemetryClient.Track(event) - FlushTelemetry() } // TrackSubCommand tracks a sub-command execution event @@ -43,7 +42,6 @@ func TrackSubCommand(command, subCommand string) { event.Properties["command"] = command event.Properties["sub-command"] = subCommand telemetryClient.Track(event) - FlushTelemetry() } func TrackEvent(eventName string) { @@ -52,14 +50,13 @@ func TrackEvent(eventName string) { telemetryClient.Track(event) } +func CloseTelemetry() { + telemetryClient.Channel().Close() +} func FlushTelemetry() { telemetryClient.Channel().Flush() } -// func SetTelemetryClient(client appinsights.TelemetryClient) { -// telemetryClient = client -// } - func LogMessage(msg string) error { fmt.Printf("[%s] %s\n", time.Now().Format(time.UnixDate), msg) return nil From b9879f147f6d9ea5c9efcb940fce7437358e660a Mon Sep 17 00:00:00 2001 From: Jyotika Garg Date: Tue, 25 Jul 2023 12:15:39 +0530 Subject: [PATCH 5/9] adding generic command event --- cmd/modern/main.go | 5 +++-- cmd/modern/root/config.go | 2 -- internal/telemetry/appinsights.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/modern/main.go b/cmd/modern/main.go index 604d3129..6e285c21 100644 --- a/cmd/modern/main.go +++ b/cmd/modern/main.go @@ -23,11 +23,10 @@ import ( "github.com/microsoft/go-sqlcmd/internal/output" "github.com/microsoft/go-sqlcmd/internal/output/verbosity" "github.com/microsoft/go-sqlcmd/internal/pal" + "github.com/microsoft/go-sqlcmd/internal/telemetry" "github.com/microsoft/go-sqlcmd/pkg/sqlcmd" "github.com/spf13/cobra" - "github.com/microsoft/go-sqlcmd/internal/telemetry" - legacyCmd "github.com/microsoft/go-sqlcmd/cmd/sqlcmd" ) @@ -115,6 +114,8 @@ func isFirstArgModernCliSubCommand() (isNewCliCommand bool) { if len(os.Args) > 1 { if rootCmd.IsValidSubCommand(os.Args[1]) { isNewCliCommand = true + command := os.Args[1] + telemetry.TrackCommand(command) } } return diff --git a/cmd/modern/root/config.go b/cmd/modern/root/config.go index 488a07e6..936c8c84 100644 --- a/cmd/modern/root/config.go +++ b/cmd/modern/root/config.go @@ -10,7 +10,6 @@ import ( "github.com/microsoft/go-sqlcmd/internal/cmdparser" "github.com/microsoft/go-sqlcmd/internal/localizer" "github.com/microsoft/go-sqlcmd/internal/pal" - "github.com/microsoft/go-sqlcmd/internal/telemetry" ) // Config defines the `sqlcmd config` sub-commands @@ -39,7 +38,6 @@ func (c *Config) DefineCommand(...cmdparser.CommandOptions) { } c.Cmd.DefineCommand(options) - telemetry.TrackEvent("config") } // SubCommands sets up all the sub-commands for `sqlcmd config` diff --git a/internal/telemetry/appinsights.go b/internal/telemetry/appinsights.go index fb7679cb..2b9c8cca 100644 --- a/internal/telemetry/appinsights.go +++ b/internal/telemetry/appinsights.go @@ -18,7 +18,7 @@ type Telemetry struct { } func InitializeAppInsights() { - instrumentationKey := "5587abc4-2843-4455-b7dd-c3eb1f2e45f6" + instrumentationKey := "3fdeec77-2951-456f-bcb2-9f003b512a3f" config := appinsights.NewTelemetryConfiguration(instrumentationKey) telemetryClient = appinsights.NewTelemetryClientFromConfig(config) SetTelemetryClient(telemetryClient) From ad3bf3fb68df6a90b9731ac534d66901a1e75a57 Mon Sep 17 00:00:00 2001 From: Jyotika Garg Date: Tue, 1 Aug 2023 15:40:48 +0530 Subject: [PATCH 6/9] telemetry intialize --- internal/cmdparser/test.go | 8 ++++++-- internal/telemetry/appinsights.go | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/cmdparser/test.go b/internal/cmdparser/test.go index 7984f953..a4654203 100644 --- a/internal/cmdparser/test.go +++ b/internal/cmdparser/test.go @@ -4,15 +4,17 @@ package cmdparser import ( + "regexp" + "testing" + "github.com/microsoft/go-sqlcmd/internal" "github.com/microsoft/go-sqlcmd/internal/buffer" "github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency" "github.com/microsoft/go-sqlcmd/internal/config" "github.com/microsoft/go-sqlcmd/internal/output" "github.com/microsoft/go-sqlcmd/internal/output/verbosity" + "github.com/microsoft/go-sqlcmd/internal/telemetry" "github.com/microsoft/go-sqlcmd/pkg/sqlcmd" - "regexp" - "testing" ) // Test.go contains functions useful for creating compact unit tests for the @@ -59,6 +61,8 @@ func TestCmd[T PtrAsReceiverWrapper[pointerType], pointerType any](args ...strin } func testCmd[T PtrAsReceiverWrapper[pointerType], pointerType any](args ...string) (result string, err error) { + telemetry.SetTelemetryClientFromInstrumentationKey("") + buf := buffer.NewMemoryBuffer() defer func() { buf.Close() }() c := New[T](dependency.Options{ diff --git a/internal/telemetry/appinsights.go b/internal/telemetry/appinsights.go index 2b9c8cca..3703b3e8 100644 --- a/internal/telemetry/appinsights.go +++ b/internal/telemetry/appinsights.go @@ -13,6 +13,10 @@ func SetTelemetryClient(client appinsights.TelemetryClient) { telemetryClient = client } +func SetTelemetryClientFromInstrumentationKey(instrumentationKey string) { + telemetryClient = appinsights.NewTelemetryClient(instrumentationKey) +} + type Telemetry struct { Client appinsights.TelemetryClient } From 682ae275d28693d03dbf4f4adeae90c9eb9450c5 Mon Sep 17 00:00:00 2001 From: Jyotika Garg Date: Tue, 1 Aug 2023 16:12:21 +0530 Subject: [PATCH 7/9] telemetry intialize in another test --- internal/cmdparser/cmd_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/cmdparser/cmd_test.go b/internal/cmdparser/cmd_test.go index ebc1acd1..b252bb99 100644 --- a/internal/cmdparser/cmd_test.go +++ b/internal/cmdparser/cmd_test.go @@ -5,11 +5,13 @@ package cmdparser import ( "fmt" + "testing" + "github.com/microsoft/go-sqlcmd/internal/cmdparser/dependency" "github.com/microsoft/go-sqlcmd/internal/output" + "github.com/microsoft/go-sqlcmd/internal/telemetry" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" - "testing" ) func TestCmd_run(t *testing.T) { @@ -37,6 +39,7 @@ func TestCmd_run(t *testing.T) { Usage: "name", String: &s, }) + telemetry.SetTelemetryClientFromInstrumentationKey("") c.DefineCommand() c.run(nil, []string{"name-value"}) } From ed1470b22abdfa6d1be0eb1b0856960abbbc3723 Mon Sep 17 00:00:00 2001 From: Jyotika Garg Date: Mon, 7 Aug 2023 11:55:58 +0530 Subject: [PATCH 8/9] review comment incorporated --- build/build.cmd | 2 +- cmd/modern/main.go | 15 +++++++++++---- internal/telemetry/appinsights.go | 6 ++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/build/build.cmd b/build/build.cmd index ea83b80c..f8db919d 100644 --- a/build/build.cmd +++ b/build/build.cmd @@ -26,7 +26,7 @@ if not exist %gopath%\bin\gotext.exe ( ) REM go-winres likes to append instead of overwrite so delete existing resource file - +del %~dp0..\cmd\modern\*.syso REM generates translations file and resources go generate %~dp0../... 2> %~dp0generate.txt diff --git a/cmd/modern/main.go b/cmd/modern/main.go index 6e285c21..703fe6ec 100644 --- a/cmd/modern/main.go +++ b/cmd/modern/main.go @@ -41,6 +41,9 @@ var version = "local-build" // overridden in pipeline builds with: -ldflags="-X func main() { telemetry.InitializeAppInsights() + defer telemetry.FlushTelemetry() + defer telemetry.CloseTelemetry() + dependencies := dependency.Options{ Output: output.New(output.Options{ StandardWriter: os.Stdout, @@ -50,15 +53,19 @@ func main() { rootCmd = cmdparser.New[*Root](dependencies) if isFirstArgModernCliSubCommand() { cmdparser.Initialize(initializeCallback) - telemetry.TrackEvent("modern") + properties := map[string]string{ + "sqlcmd": "modern", + } + telemetry.TrackEvent("cli", properties) rootCmd.Execute() } else { initializeEnvVars() - telemetry.TrackEvent("legacy") + properties := map[string]string{ + "sqlcmd": "legacy", + } + telemetry.TrackEvent("cli", properties) legacyCmd.Execute(version) } - telemetry.FlushTelemetry() - telemetry.CloseTelemetry() } // initializeEnvVars intializes SQLCMDSERVER, SQLCMDUSER and SQLCMDPASSWORD diff --git a/internal/telemetry/appinsights.go b/internal/telemetry/appinsights.go index 3703b3e8..aaf92218 100644 --- a/internal/telemetry/appinsights.go +++ b/internal/telemetry/appinsights.go @@ -48,9 +48,11 @@ func TrackSubCommand(command, subCommand string) { telemetryClient.Track(event) } -func TrackEvent(eventName string) { +func TrackEvent(eventName string, properties map[string]string) { event := appinsights.NewEventTelemetry(eventName) - event.Properties["command"] = eventName + for key, value := range properties { + event.Properties[key] = value + } telemetryClient.Track(event) } From 826d3700af79152d7e22e636de286f444f14ba8d Mon Sep 17 00:00:00 2001 From: Jyotika Garg Date: Mon, 7 Aug 2023 12:33:35 +0530 Subject: [PATCH 9/9] test case fixed --- cmd/modern/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/modern/main.go b/cmd/modern/main.go index 703fe6ec..54ee1128 100644 --- a/cmd/modern/main.go +++ b/cmd/modern/main.go @@ -41,8 +41,8 @@ var version = "local-build" // overridden in pipeline builds with: -ldflags="-X func main() { telemetry.InitializeAppInsights() - defer telemetry.FlushTelemetry() defer telemetry.CloseTelemetry() + defer telemetry.FlushTelemetry() dependencies := dependency.Options{ Output: output.New(output.Options{