Skip to content

Commit

Permalink
fix(instrumentation): Send transformed v1 data from IDEs (#5280)
Browse files Browse the repository at this point in the history
* fix: send v1 instrumentation if explicitly called

* chore: remove redundant log message

* chore: update gaf

* chore: upgrade gaf

* chore: update final gaf
  • Loading branch information
PeterSchafer committed Jun 3, 2024
1 parent 45f884a commit d33a773
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 10 deletions.
26 changes: 26 additions & 0 deletions cliv2/cmd/cliv2/instrumentation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"strings"

"github.com/snyk/go-application-framework/pkg/analytics"
"github.com/snyk/go-application-framework/pkg/configuration"
localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
"github.com/snyk/go-application-framework/pkg/utils"
"github.com/snyk/go-application-framework/pkg/workflow"
)

func shallSendInstrumentation(config configuration.Configuration, instrumentor analytics.InstrumentationCollector) bool {
instrumentationCommand := workflow.GetCommandFromWorkflowIdentifier(localworkflows.WORKFLOWID_REPORT_ANALYTICS)
category := strings.Join(instrumentor.GetCategory(), " ")
integration := config.GetString(configuration.INTEGRATION_NAME)

isSnykIde := utils.IsSnykIde(integration)
isReportCommand := strings.Contains(category, instrumentationCommand)

if isSnykIde && !isReportCommand {
return false
}

return true
}
28 changes: 28 additions & 0 deletions cliv2/cmd/cliv2/instrumentation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"testing"

"github.com/snyk/go-application-framework/pkg/analytics"
"github.com/snyk/go-application-framework/pkg/configuration"
"github.com/stretchr/testify/assert"
)

func Test_shallSendInstrumentation(t *testing.T) {
config := configuration.NewInMemory()
instrumentor := analytics.NewInstrumentationCollector()

// case: nothing configured
actual := shallSendInstrumentation(config, instrumentor)
assert.True(t, actual)

// case: any command called from an IDE
config.Set(configuration.INTEGRATION_NAME, "VS_CODE")
actual = shallSendInstrumentation(config, instrumentor)
assert.False(t, actual)

// case: analytics report command called from an IDE
instrumentor.SetCategory([]string{"analytics", "report", "inputData"})
actual = shallSendInstrumentation(config, instrumentor)
assert.True(t, actual)
}
17 changes: 10 additions & 7 deletions cliv2/cmd/cliv2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/snyk/cli-extension-dep-graph/pkg/depgraph"
"github.com/snyk/cli-extension-iac-rules/iacrules"
"github.com/snyk/cli-extension-sbom/pkg/sbom"
"github.com/snyk/cli/cliv2/internal/cliv2"
"github.com/snyk/cli/cliv2/internal/constants"
"github.com/snyk/container-cli/pkg/container"
"github.com/snyk/go-application-framework/pkg/analytics"
"github.com/snyk/go-application-framework/pkg/app"
Expand All @@ -30,6 +28,9 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/snyk/cli/cliv2/internal/cliv2"
"github.com/snyk/cli/cliv2/internal/constants"

localworkflows "github.com/snyk/go-application-framework/pkg/local_workflows"
"github.com/snyk/go-application-framework/pkg/local_workflows/content_type"
"github.com/snyk/go-application-framework/pkg/local_workflows/json_schemas"
Expand Down Expand Up @@ -252,9 +253,8 @@ func sendAnalytics(analytics analytics.Analytics, debugLogger *zerolog.Logger) {
}

func sendInstrumentation(eng workflow.Engine, instrumentor analytics.InstrumentationCollector, logger *zerolog.Logger) {
// Avoid duplicate data to be send for IDE integrations that use the CLI
integration := globalConfiguration.GetString(configuration.INTEGRATION_NAME)
if utils.IsSnykIde(integration) {
// Avoid duplicate data to be sent for IDE integrations that use the CLI
if !shallSendInstrumentation(eng.GetConfiguration(), instrumentor) {
logger.Print("Called from IDE, not sending instrumentation")
return
}
Expand All @@ -274,7 +274,6 @@ func sendInstrumentation(eng workflow.Engine, instrumentor analytics.Instrumenta
v2InstrumentationData,
)

logger.Trace().Msg("Reporting instrumentation data")
localConfiguration := globalConfiguration.Clone()
// the report analytics workflow needs --experimental to run
// we pass the flag here so that we report at every interaction
Expand Down Expand Up @@ -572,7 +571,11 @@ func MainWithErrorCode() int {
globalLogger.Printf("Failed to derive target id, %v", targetIdError)
}
cliAnalytics.GetInstrumentation().SetTargetId(targetId)
cliAnalytics.GetInstrumentation().SetDuration(time.Since(startTime))

if cliAnalytics.GetInstrumentation().GetDuration() == 0 {
cliAnalytics.GetInstrumentation().SetDuration(time.Since(startTime))
}

cliAnalytics.GetInstrumentation().AddExtension("exitcode", exitCode)
if exitCode == 2 {
cliAnalytics.GetInstrumentation().SetStatus(analytics.Failure)
Expand Down
2 changes: 1 addition & 1 deletion cliv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/snyk/cli-extension-sbom v0.0.0-20240523084359-a2830fadb001
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1
github.com/snyk/error-catalog-golang-public v0.0.0-20240527112826-2b77438d25f1
github.com/snyk/go-application-framework v0.0.0-20240531131804-44c710e2a81b
github.com/snyk/go-application-framework v0.0.0-20240603174251-84f6ded78c8f
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65
github.com/snyk/snyk-iac-capture v0.6.5
github.com/snyk/snyk-ls v0.0.0-20240529144555-b14182aa0973
Expand Down
4 changes: 2 additions & 2 deletions cliv2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1 h1:9RKY9NdX5DrJ
github.com/snyk/container-cli v0.0.0-20240322120441-6d9b9482f9b1/go.mod h1:38w+dcAQp9eG3P5t2eNS9eG0reut10AeJjLv5lJ5lpM=
github.com/snyk/error-catalog-golang-public v0.0.0-20240527112826-2b77438d25f1 h1:49X/bTeiWdi+DrkTbTSw5BePpQ6LiucIt++/Z+MB95U=
github.com/snyk/error-catalog-golang-public v0.0.0-20240527112826-2b77438d25f1/go.mod h1:Ytttq7Pw4vOCu9NtRQaOeDU2dhBYUyNBe6kX4+nIIQ4=
github.com/snyk/go-application-framework v0.0.0-20240531131804-44c710e2a81b h1:XUx2t//6sHIi6CAS/8Nz7UBNh5a5kmVntnyYYAsSs7s=
github.com/snyk/go-application-framework v0.0.0-20240531131804-44c710e2a81b/go.mod h1:1T6hnPUgT0lJPO0g3VjwvW0fCKDcgst8bgXs+wCfmW8=
github.com/snyk/go-application-framework v0.0.0-20240603174251-84f6ded78c8f h1:GucVC2OeSXfWVkHxZJ2xFRl2uXWk2YDl0SsF45HZt/Y=
github.com/snyk/go-application-framework v0.0.0-20240603174251-84f6ded78c8f/go.mod h1:1T6hnPUgT0lJPO0g3VjwvW0fCKDcgst8bgXs+wCfmW8=
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65 h1:CEQuYv0Go6MEyRCD3YjLYM2u3Oxkx8GpCpFBd4rUTUk=
github.com/snyk/go-httpauth v0.0.0-20240307114523-1f5ea3f55c65/go.mod h1:88KbbvGYlmLgee4OcQ19yr0bNpXpOr2kciOthaSzCAg=
github.com/snyk/policy-engine v0.30.11 h1:wUy5LMar2vccMbNM62MSBRdjAQAhAbIm7aNXXO+g2tk=
Expand Down

0 comments on commit d33a773

Please sign in to comment.