Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal API - Visibility System improvements #1315

Merged
merged 4 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/eyalbe4/jfrog-client-go v1.28.1-0.20241220200217-c4b9ef90c453
replace github.com/jfrog/jfrog-client-go => github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241222151331-7cc07c031216

// 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 @@ -51,6 +51,8 @@ github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcej
github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241222151331-7cc07c031216 h1:YcDm/SI+4tNdV3SUJgXWmsG8/d/itVKhxRzttJI5Y3o=
github.com/eyalbe4/jfrog-client-go v1.28.1-0.20241222151331-7cc07c031216/go.mod h1:2ySOMva54L3EYYIlCBYBTcTgqfrrQ19gtpA/MWfA/ec=
github.com/forPelevin/gomoji v1.2.0 h1:9k4WVSSkE1ARO/BWywxgEUBvR/jMnao6EZzrql5nxJ8=
github.com/forPelevin/gomoji v1.2.0/go.mod h1:8+Z3KNGkdslmeGZBC3tCrwMrcPy5GRzAD+gL9NAwMXg=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
Expand Down Expand Up @@ -93,8 +95,6 @@ github.com/jfrog/build-info-go v1.10.7 h1:10NVHYg0193gJpQft+S4WQfvYMtj5jlwwhJRvk
github.com/jfrog/build-info-go v1.10.7/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.48.4 h1:uXvBr2ebFKpBRUhWgC9TSSJe32IbSYGlbDp9tDzBcaY=
github.com/jfrog/jfrog-client-go v1.48.4/go.mod h1:2ySOMva54L3EYYIlCBYBTcTgqfrrQ19gtpA/MWfA/ec=
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
32 changes: 5 additions & 27 deletions utils/usage/visibility_system_manager.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package usage

import (
"encoding/json"
"os"

"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"
"github.com/jfrog/jfrog-client-go/jfconnect/services"
)

type VisibilitySystemManager struct {
Expand All @@ -19,24 +19,8 @@ func NewVisibilitySystemManager(serverDetails *config.ServerDetails) *Visibility
}
}

type labels struct {
ProductID string `json:"product_id"`
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"`
}

type visibilityMetric struct {
Value int `json:"value"`
MetricsName string `json:"metrics_name"`
Labels labels `json:"labels"`
}

func (vsm *VisibilitySystemManager) createMetric(commandName string) ([]byte, error) {
metricLabels := labels{
func (vsm *VisibilitySystemManager) createMetric(commandName string) services.VisibilityMetric {
metricLabels := services.Labels{
ProductID: coreutils.GetCliUserAgentName(),
FeatureID: commandName,
OIDCUsed: os.Getenv("JFROG_CLI_USAGE_OIDC_USED"),
Expand All @@ -46,23 +30,17 @@ func (vsm *VisibilitySystemManager) createMetric(commandName string) ([]byte, er
GhTokenForCodeScanningAlertsProvided: os.Getenv("JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED"),
}

metric := visibilityMetric{
return services.VisibilityMetric{
Value: 1,
MetricsName: "jfcli_commands_count",
Labels: metricLabels,
}

return json.Marshal(metric)
}

func (vsm *VisibilitySystemManager) SendUsage(commandName string) error {
manager, err := utils.CreateJfConnectServiceManager(vsm.serverDetails)
if err != nil {
return err
}
metric, err := vsm.createMetric(commandName)
if err != nil {
return err
}
return manager.PostMetric(metric)
return manager.PostMetric(vsm.createMetric(commandName))
}
6 changes: 4 additions & 2 deletions utils/usage/visibility_system_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package usage

import (
"encoding/json"
"testing"

"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
Expand Down Expand Up @@ -29,7 +30,8 @@ func TestCreateMetric(t *testing.T) {
}()

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

// Define the expected JSON structure
Expand All @@ -48,5 +50,5 @@ func TestCreateMetric(t *testing.T) {
}`

// Compare the generated JSON to the expected JSON
assert.JSONEq(t, expectedJSON, string(metric))
assert.JSONEq(t, expectedJSON, string(metricJSON))
}
Loading