Skip to content

Commit b55edb3

Browse files
Arta AsadiArta Asadi
Arta Asadi
authored and
Arta Asadi
committed
fix: update usage tracker send request
1 parent 3cb9910 commit b55edb3

File tree

9 files changed

+91
-39
lines changed

9 files changed

+91
-39
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ require (
3434
github.com/lib/pq v1.10.9
3535
github.com/nats-io/nats.go v1.38.0
3636
github.com/open-policy-agent/opa v0.69.0
37-
github.com/opengovern/og-util v1.15.48
37+
github.com/opengovern/og-util v1.15.51
3838
github.com/opensearch-project/opensearch-go/v2 v2.3.0
3939
github.com/ory/dockertest/v3 v3.10.0
4040
github.com/patrickmn/go-cache v2.1.0+incompatible

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,12 @@ github.com/opencontainers/runc v1.2.6 h1:P7Hqg40bsMvQGCS4S7DJYhUZOISMLJOB2iGX5CO
986986
github.com/opencontainers/runc v1.2.6/go.mod h1:dOQeFo29xZKBNeRBI0B19mJtfHv68YgCTh1X+YphA+4=
987987
github.com/opengovern/og-util v1.15.48 h1:w9St+tfVphxAhdZUZtnsfMGabili7xBUGvQ/OskDVkQ=
988988
github.com/opengovern/og-util v1.15.48/go.mod h1:THqKUUaoAemc4x31PzZEWIzhp+MnJNf9413wgfZyHyc=
989+
github.com/opengovern/og-util v1.15.49 h1:IZbqjV5MfXM/3SVchZoJQRIFXaOVZjecBARaNQ3wZ5s=
990+
github.com/opengovern/og-util v1.15.49/go.mod h1:THqKUUaoAemc4x31PzZEWIzhp+MnJNf9413wgfZyHyc=
991+
github.com/opengovern/og-util v1.15.50-0.20250505101939-eda5a95e3b6f h1:lREXwaCh7avGIAVNyk2+6ZB4AdtNOZjt8BWI4HgQbYU=
992+
github.com/opengovern/og-util v1.15.50-0.20250505101939-eda5a95e3b6f/go.mod h1:THqKUUaoAemc4x31PzZEWIzhp+MnJNf9413wgfZyHyc=
993+
github.com/opengovern/og-util v1.15.51 h1:x6lx327j2rQdnMDBwsSBgXQVwPhXe8RK4n0fG8zGesg=
994+
github.com/opengovern/og-util v1.15.51/go.mod h1:THqKUUaoAemc4x31PzZEWIzhp+MnJNf9413wgfZyHyc=
989995
github.com/opensearch-project/opensearch-go/v2 v2.3.0 h1:nQIEMr+A92CkhHrZgUhcfsrZjibvB3APXf2a1VwCmMQ=
990996
github.com/opensearch-project/opensearch-go/v2 v2.3.0/go.mod h1:8LDr9FCgUTVoT+5ESjc2+iaZuldqE+23Iq0r1XeNue8=
991997
github.com/ory/dockertest/v3 v3.10.0 h1:4K3z2VMe8Woe++invjaTB7VRyQXQy5UY+loujO4aNE4=

jobs/checkup-job/job.go

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7+
"os"
78
"time"
89

910
authAPI "github.com/opengovern/og-util/pkg/api"
@@ -20,6 +21,10 @@ import (
2021
"go.uber.org/zap"
2122
)
2223

24+
var (
25+
UsageTrackerEndpoint = os.Getenv("USAGE_TRACKER_ENDPOINT")
26+
)
27+
2328
type Job struct {
2429
JobID uint
2530
ExecutedAt int64
@@ -62,34 +67,32 @@ func (j Job) Do(integrationClient client.IntegrationServiceClient, authClient au
6267

6368
// Healthcheck
6469
logger.Info("starting healthcheck")
65-
70+
6671
counter := 0
6772
integrations, err := integrationClient.ListIntegrations(&httpclient.Context{
6873
UserRole: authAPI.EditorRole,
6974
}, nil)
7075

71-
72-
7376
if err != nil {
7477
time.Sleep(3 * time.Minute)
75-
integrations, err = integrationClient.ListIntegrations(&httpclient.Context{
76-
UserRole: authAPI.EditorRole,
77-
}, nil)
78-
for {
79-
if err != nil {
80-
counter++
81-
if counter < 10 {
82-
logger.Warn("Waiting for status to be GREEN or YELLOW. Sleeping for 10 seconds...")
83-
time.Sleep(4 * time.Minute)
84-
continue
85-
}
86-
87-
logger.Error("failed to check integration healthcheck", zap.Error(err))
88-
fail(fmt.Errorf("failed to check integration healthcheck: %w", err))
89-
}
90-
break
91-
}
92-
78+
integrations, err = integrationClient.ListIntegrations(&httpclient.Context{
79+
UserRole: authAPI.EditorRole,
80+
}, nil)
81+
for {
82+
if err != nil {
83+
counter++
84+
if counter < 10 {
85+
logger.Warn("Waiting for status to be GREEN or YELLOW. Sleeping for 10 seconds...")
86+
time.Sleep(4 * time.Minute)
87+
continue
88+
}
89+
90+
logger.Error("failed to check integration healthcheck", zap.Error(err))
91+
fail(fmt.Errorf("failed to check integration healthcheck: %w", err))
92+
}
93+
break
94+
}
95+
9396
} else {
9497
for _, integrationObj := range integrations.Integrations {
9598
if integrationObj.LastCheck != nil && integrationObj.LastCheck.Add(8*time.Hour).After(time.Now()) {
@@ -127,49 +130,58 @@ func (j *Job) SendTelemetry(ctx context.Context, logger *zap.Logger, workerConfi
127130

128131
httpCtx := httpclient.Context{Ctx: ctx, UserRole: authAPI.AdminRole}
129132

130-
req := shared_entities.CspmUsageRequest{
131-
GatherTimestamp: now,
132-
Hostname: workerConfig.TelemetryHostname,
133-
IntegrationTypeCount: make(map[string]int),
134-
}
133+
var plugins []shared_entities.UsageTrackerPluginInfo
135134

136-
integrations, err := integrationClient.ListIntegrations(&httpCtx, nil)
135+
pluginsResponse, err := integrationClient.ListPlugins(&httpCtx)
137136
if err != nil {
138137
logger.Error("failed to list sources", zap.Error(err))
139138
return
140139
}
141-
for _, integration := range integrations.Integrations {
142-
if _, ok := req.IntegrationTypeCount[integration.IntegrationType.String()]; !ok {
143-
req.IntegrationTypeCount[integration.IntegrationType.String()] = 0
144-
}
145-
req.IntegrationTypeCount[integration.IntegrationType.String()] += 1
140+
for _, p := range pluginsResponse.Items {
141+
plugins = append(plugins, shared_entities.UsageTrackerPluginInfo{
142+
Name: p.Name,
143+
Version: p.Version,
144+
IntegrationCount: p.Count.Total,
145+
})
146146
}
147147

148148
users, err := authClient.ListUsers(&httpCtx)
149149
if err != nil {
150150
logger.Error("failed to list users", zap.Error(err))
151151
return
152152
}
153-
req.NumberOfUsers = int64(len(users))
153+
keys, err := authClient.ListApiKeys(&httpCtx)
154+
if err != nil {
155+
logger.Error("failed to list api keys", zap.Error(err))
156+
return
157+
}
154158

155159
about, err := coreClient.GetAbout(&httpCtx)
156160
if err != nil {
157161
logger.Error("failed to get about", zap.Error(err))
158162
return
159163
}
160-
req.InstallId = about.InstallID
161164

162-
url := fmt.Sprintf("%s/api/v1/information/usage", "https://hub.clearcompass.so")
165+
req := shared_entities.UsageTrackerRequest{
166+
InstanceID: about.InstallID,
167+
Time: now,
168+
Version: about.AppVersion,
169+
Hostname: workerConfig.TelemetryHostname,
170+
IsSsoConfigured: false,
171+
UserCount: int64(len(users)),
172+
ApiKeyCount: int64(len(keys)),
173+
}
174+
163175
reqBytes, err := json.Marshal(req)
164176
if err != nil {
165177
logger.Error("failed to marshal telemetry request", zap.Error(err))
166178
return
167179
}
168180
var resp any
169-
if statusCode, err := httpclient.DoRequest(httpCtx.Ctx, http.MethodPost, url, httpCtx.ToHeaders(), reqBytes, &resp); err != nil {
170-
logger.Error("failed to send telemetry", zap.Error(err), zap.Int("status_code", statusCode), zap.String("url", url), zap.Any("req", req), zap.Any("resp", resp))
181+
if statusCode, err := httpclient.DoRequest(httpCtx.Ctx, http.MethodPost, UsageTrackerEndpoint, httpCtx.ToHeaders(), reqBytes, &resp); err != nil {
182+
logger.Error("failed to send telemetry", zap.Error(err), zap.Int("status_code", statusCode), zap.String("url", UsageTrackerEndpoint), zap.Any("req", req), zap.Any("resp", resp))
171183
return
172184
}
173185

174-
logger.Info("sent telemetry", zap.String("url", url))
186+
logger.Info("sent telemetry", zap.String("url", UsageTrackerEndpoint))
175187
}

services/auth/client/auth.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
type AuthServiceClient interface {
1313
ListUsers(ctx *httpclient.Context) ([]api.GetUsersResponse, error)
14+
ListApiKeys(ctx *httpclient.Context) ([]api.APIKeyResponse, error)
1415
GetConnectors(ctx *httpclient.Context) ([]api.GetConnectorsResponse, error)
1516
}
1617

@@ -35,6 +36,19 @@ func (s *authClient) ListUsers(ctx *httpclient.Context) ([]api.GetUsersResponse,
3536
return users, nil
3637
}
3738

39+
func (s *authClient) ListApiKeys(ctx *httpclient.Context) ([]api.APIKeyResponse, error) {
40+
url := fmt.Sprintf("%s/api/v1/keys", s.baseURL)
41+
42+
var keys []api.APIKeyResponse
43+
if statusCode, err := httpclient.DoRequest(ctx.Ctx, http.MethodGet, url, ctx.ToHeaders(), nil, &keys); err != nil {
44+
if 400 <= statusCode && statusCode < 500 {
45+
return nil, echo.NewHTTPError(statusCode, err.Error())
46+
}
47+
return nil, err
48+
}
49+
return keys, nil
50+
}
51+
3852
func (s *authClient) GetConnectors(ctx *httpclient.Context) ([]api.GetConnectorsResponse, error) {
3953
url := fmt.Sprintf("%s/api/v1/connectors", s.baseURL)
4054

services/integration/api/integration-types/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ func (a *API) ListPlugins(c echo.Context) error {
781781
URL: plugin.URL,
782782
Tier: plugin.Tier,
783783
Description: plugin.Description,
784+
Version: plugin.Version,
784785
Icon: plugin.Icon,
785786
Availability: plugin.Availability,
786787
SourceCode: plugin.SourceCode,
@@ -885,6 +886,7 @@ func (a *API) GetPlugin(c echo.Context) error {
885886
URL: plugin.URL,
886887
Tier: plugin.Tier,
887888
Description: plugin.Description,
889+
Version: plugin.Version,
888890
Icon: plugin.Icon,
889891
Availability: plugin.Availability,
890892
SourceCode: plugin.SourceCode,

services/integration/api/models/integration_plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type IntegrationPlugin struct {
1616
Name string `json:"name"`
1717
Tier string `json:"tier"`
1818
Description string `json:"description"`
19+
Version string `json:"version"`
1920
Icon string `json:"icon"`
2021
Availability string `json:"availability"`
2122
SourceCode string `json:"source_code"`

services/integration/client/integration.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type IntegrationServiceClient interface {
2929
GetPluginsTables(ctx *httpclient.Context) ([]models.PluginTables, error)
3030
GetIntegrationTypeResourceType(ctx *httpclient.Context, integrationType string, resourceType string) (*models.ResourceTypeConfiguration, error)
3131
ListIntegrationTypeResourceTypes(ctx *httpclient.Context, integrationType string) (*models.ListIntegrationTypeResourceTypesResponse, error)
32+
ListPlugins(ctx *httpclient.Context) (*models.IntegrationPluginListResponse, error)
3233
}
3334

3435
type integrationClient struct {
@@ -293,3 +294,17 @@ func (c *integrationClient) GetIntegrationTypeResourceType(ctx *httpclient.Conte
293294

294295
return &response, nil
295296
}
297+
298+
func (c *integrationClient) ListPlugins(ctx *httpclient.Context) (*models.IntegrationPluginListResponse, error) {
299+
url := fmt.Sprintf("%s/api/v1/integrations-types/plugin", c.baseURL)
300+
var response models.IntegrationPluginListResponse
301+
302+
if statusCode, err := httpclient.DoRequest(ctx.Ctx, http.MethodGet, url, ctx.ToHeaders(), nil, &response); err != nil {
303+
if 400 <= statusCode && statusCode < 500 {
304+
return nil, echo.NewHTTPError(statusCode, err.Error())
305+
}
306+
return nil, err
307+
}
308+
309+
return &response, nil
310+
}

services/integration/models/integration_plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type IntegrationPlugin struct {
5656
Name string
5757
Tier string
5858
Description string
59+
Version string
5960
Icon string
6061
Availability string
6162
SourceCode string

services/integration/utils/load-plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func ExtractIntegrationBinaries(logger *zap.Logger, plugin platformspec.PluginSp
164164
IntegrationType: plugin.IntegrationType,
165165
Name: plugin.Name,
166166
Description: plugin.Metadata.Description,
167+
Version: plugin.Version,
167168
Icon: plugin.Metadata.Icon,
168169
PackageType: plugin.Type,
169170
InstallState: installState,

0 commit comments

Comments
 (0)