Skip to content

Commit

Permalink
Visibility - update commands count metric to correspond with client c…
Browse files Browse the repository at this point in the history
…hanges (#1330)
  • Loading branch information
omerzi authored Jan 21, 2025
1 parent 134977b commit adb9013
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 57 deletions.
5 changes: 3 additions & 2 deletions common/commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
coreusage "github.com/jfrog/jfrog-cli-core/v2/utils/usage"
usageReporter "github.com/jfrog/jfrog-cli-core/v2/utils/usage"
"github.com/jfrog/jfrog-cli-core/v2/utils/usage/visibility"
rtClient "github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/usage"
"github.com/jfrog/jfrog-client-go/utils/log"
Expand Down Expand Up @@ -92,7 +92,8 @@ func reportUsage(command Command, channel chan<- bool) {
}

func reportUsageToVisibilitySystem(command Command, serverDetails *config.ServerDetails) {
if err := coreusage.NewVisibilitySystemManager(serverDetails).SendUsage(command.CommandName()); err != nil {
commandsCountMetric := visibility.NewCommandsCountMetric(command.CommandName())
if err := visibility.NewVisibilitySystemManager(serverDetails).SendUsage(commandsCountMetric); err != nil {
log.Debug("Visibility System Usage reporting:", err.Error())
}
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
)

replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20250119123447-32f2fc161629
replace github.com/jfrog/jfrog-client-go => github.com/jfrog/jfrog-client-go v1.28.1-0.20250121093504-a3e981c875d7

// replace github.com/jfrog/build-info-go => github.com/jfrog/build-info-go v1.8.9-0.20241121100855-e7a75ceee2bd

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ github.com/jfrog/build-info-go v1.10.8 h1:8D4wtvKzLS1hzfDWtfH4OliZLtLCgL62tXCnGW
github.com/jfrog/build-info-go v1.10.8/go.mod h1:JcISnovFXKx3wWf3p1fcMmlPdt6adxScXvoJN4WXqIE=
github.com/jfrog/gofrog v1.7.6 h1:QmfAiRzVyaI7JYGsB7cxfAJePAZTzFz0gRWZSE27c6s=
github.com/jfrog/gofrog v1.7.6/go.mod h1:ntr1txqNOZtHplmaNd7rS4f8jpA5Apx8em70oYEe7+4=
github.com/jfrog/jfrog-client-go v1.28.1-0.20250119123447-32f2fc161629 h1:53aHwuDQrxEBBCNfS88kckKjjJmnjg5F6QnFF+UjgvI=
github.com/jfrog/jfrog-client-go v1.28.1-0.20250119123447-32f2fc161629/go.mod h1:ohIfKpMBCQsE9kunrKQ1wvoExpqsPLaluRFO186B5EM=
github.com/jfrog/jfrog-client-go v1.28.1-0.20250121093504-a3e981c875d7 h1:w0thxklC2enMUFn1lWoOBiuidzIvJnDJNczQRmRE0wQ=
github.com/jfrog/jfrog-client-go v1.28.1-0.20250121093504-a3e981c875d7/go.mod h1:ohIfKpMBCQsE9kunrKQ1wvoExpqsPLaluRFO186B5EM=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
Expand Down
35 changes: 35 additions & 0 deletions utils/usage/visibility/commands_count_metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package visibility

import (
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-client-go/jfconnect/services"
"os"
)

type commandsCountLabels struct {
ProductID string `json:"product_id"`
ProductVersion string `json:"product_version"`
FeatureID string `json:"feature_id"`
OIDCUsed string `json:"oidc_used"`
JobID string `json:"job_id"`
RunID string `json:"run_id"`
GitRepo string `json:"git_repo"`
GhTokenForCodeScanningAlertsProvided string `json:"gh_token_for_code_scanning_alerts_provided"`
}

func NewCommandsCountMetric(commandName string) services.VisibilityMetric {
return services.VisibilityMetric{
Value: 1,
Name: "jfcli_commands_count",
Labels: &commandsCountLabels{
ProductID: coreutils.GetCliUserAgentName(),
ProductVersion: coreutils.GetCliUserAgentVersion(),
FeatureID: commandName,
OIDCUsed: os.Getenv("JFROG_CLI_USAGE_OIDC_USED"),
JobID: os.Getenv("JFROG_CLI_USAGE_JOB_ID"),
RunID: os.Getenv("JFROG_CLI_USAGE_RUN_ID"),
GitRepo: os.Getenv("JFROG_CLI_USAGE_GIT_REPO"),
GhTokenForCodeScanningAlertsProvided: os.Getenv("JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED"),
},
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package usage
package visibility

import (
"encoding/json"
"testing"

"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
testsutils "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCreateMetric(t *testing.T) {
func TestCreateCommandsCountMetric(t *testing.T) {
// Set environment variables for the test using SetEnvWithCallbackAndAssert
envVars := map[string]string{
"JFROG_CLI_USAGE_OIDC_USED": "TRUE",
Expand All @@ -30,7 +29,7 @@ func TestCreateMetric(t *testing.T) {
}()

commandName := "testCommand"
metric := NewVisibilitySystemManager(nil).createMetric(commandName)
metric := NewCommandsCountMetric(commandName)
metricJSON, err := json.Marshal(metric)
assert.NoError(t, err)

Expand Down
25 changes: 25 additions & 0 deletions utils/usage/visibility/visibility_system_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package visibility

import (
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-client-go/jfconnect/services"
)

type VisibilitySystemManager struct {
serverDetails *config.ServerDetails
}

func NewVisibilitySystemManager(serverDetails *config.ServerDetails) *VisibilitySystemManager {
return &VisibilitySystemManager{
serverDetails: serverDetails,
}
}

func (vsm *VisibilitySystemManager) SendUsage(metric services.VisibilityMetric) error {
manager, err := utils.CreateJfConnectServiceManager(vsm.serverDetails, 0, 0)
if err != nil {
return err
}
return manager.PostVisibilityMetric(metric)
}
47 changes: 0 additions & 47 deletions utils/usage/visibility_system_manager.go

This file was deleted.

0 comments on commit adb9013

Please sign in to comment.