Skip to content

Commit bc722c8

Browse files
feat(serviceaccount): add analytics support for service account
1 parent 6daa949 commit bc722c8

File tree

18 files changed

+113
-39
lines changed

18 files changed

+113
-39
lines changed

ee/modules/dashboard/impldashboard/module.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ func (module *module) Update(ctx context.Context, orgID valuer.UUID, id valuer.U
215215
return module.pkgDashboardModule.Update(ctx, orgID, id, updatedBy, data, diff)
216216
}
217217

218-
func (module *module) LockUnlock(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedByUserID valuer.UUID, lock bool) error {
219-
return module.pkgDashboardModule.LockUnlock(ctx, orgID, id, updatedByUserID, lock)
218+
func (module *module) LockUnlock(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, lock bool) error {
219+
return module.pkgDashboardModule.LockUnlock(ctx, orgID, id, updatedBy, lock)
220220
}
221221

222222
func (module *module) MustGetTypeables() []authtypes.Typeable {

pkg/modules/dashboard/dashboard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Module interface {
4343

4444
Update(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, data dashboardtypes.UpdatableDashboard, diff int) (*dashboardtypes.Dashboard, error)
4545

46-
LockUnlock(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedByUserID valuer.UUID, lock bool) error
46+
LockUnlock(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, lock bool) error
4747

4848
Delete(ctx context.Context, orgID valuer.UUID, id valuer.UUID) error
4949

pkg/modules/dashboard/impldashboard/handler.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ func (handler *handler) Create(rw http.ResponseWriter, r *http.Request) {
5858
dashboardMigrator.Migrate(ctx, req)
5959
}
6060

61-
// TODO[@vikrantgupta25]: figure out what to do with this author thing
6261
dashboard, err := handler.module.Create(ctx, orgID, claims.Email, valuer.MustNewUUID(claims.GetIdentityID()), req)
6362
if err != nil {
6463
render.Error(rw, err)
@@ -157,7 +156,7 @@ func (handler *handler) LockUnlock(rw http.ResponseWriter, r *http.Request) {
157156
return
158157
}
159158

160-
err = handler.module.LockUnlock(ctx, orgID, dashboardID, valuer.MustNewUUID(claims.UserID), *req.Locked)
159+
err = handler.module.LockUnlock(ctx, orgID, dashboardID, valuer.MustNewEmail(claims.Email).String(), *req.Locked)
161160
if err != nil {
162161
render.Error(rw, err)
163162
return

pkg/modules/dashboard/impldashboard/module.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,13 @@ func (module *module) Update(ctx context.Context, orgID valuer.UUID, id valuer.U
102102
return dashboard, nil
103103
}
104104

105-
func (module *module) LockUnlock(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedByUserID valuer.UUID, lock bool) error {
105+
func (module *module) LockUnlock(ctx context.Context, orgID valuer.UUID, id valuer.UUID, updatedBy string, lock bool) error {
106106
dashboard, err := module.Get(ctx, orgID, id)
107107
if err != nil {
108108
return err
109109
}
110110

111-
user, err := module.userGetter.GetByOrgIDAndID(ctx, orgID, updatedByUserID)
112-
if err != nil {
113-
return err
114-
}
115-
116-
err = dashboard.LockUnlock(lock, user.Role, user.Email.String())
111+
err = dashboard.LockUnlock(lock, updatedBy)
117112
if err != nil {
118113
return err
119114
}

pkg/modules/serviceaccount/implserviceaccount/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (handler *handler) UpdateFactorAPIKey(rw http.ResponseWriter, r *http.Reque
300300
}
301301

302302
factorAPIKey.Update(req.Name, req.ExpiresAt)
303-
err = handler.module.UpdateFactorAPIKey(ctx, serviceAccount.ID, factorAPIKey)
303+
err = handler.module.UpdateFactorAPIKey(ctx, valuer.MustNewUUID(claims.OrgID), serviceAccount.ID, factorAPIKey)
304304
if err != nil {
305305
render.Error(rw, err)
306306
return

pkg/modules/serviceaccount/implserviceaccount/module.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package implserviceaccount
33
import (
44
"context"
55

6+
"github.com/SigNoz/signoz/pkg/analytics"
67
"github.com/SigNoz/signoz/pkg/authz"
78
"github.com/SigNoz/signoz/pkg/emailing"
89
"github.com/SigNoz/signoz/pkg/errors"
@@ -15,15 +16,16 @@ import (
1516
)
1617

1718
type module struct {
18-
store serviceaccounttypes.Store
19-
authz authz.AuthZ
20-
emailing emailing.Emailing
21-
settings factory.ScopedProviderSettings
19+
store serviceaccounttypes.Store
20+
authz authz.AuthZ
21+
emailing emailing.Emailing
22+
analytics analytics.Analytics
23+
settings factory.ScopedProviderSettings
2224
}
2325

24-
func NewModule(store serviceaccounttypes.Store, authz authz.AuthZ, emailing emailing.Emailing, providerSettings factory.ProviderSettings) serviceaccount.Module {
26+
func NewModule(store serviceaccounttypes.Store, authz authz.AuthZ, emailing emailing.Emailing, analytics analytics.Analytics, providerSettings factory.ProviderSettings) serviceaccount.Module {
2527
settings := factory.NewScopedProviderSettings(providerSettings, "github.com/SigNoz/signoz/pkg/modules/serviceaccount/implserviceaccount")
26-
return &module{store: store, authz: authz, emailing: emailing, settings: settings}
28+
return &module{store: store, authz: authz, emailing: emailing, analytics: analytics, settings: settings}
2729
}
2830

2931
func (module *module) Create(ctx context.Context, orgID valuer.UUID, serviceAccount *serviceaccounttypes.ServiceAccount) error {
@@ -58,6 +60,8 @@ func (module *module) Create(ctx context.Context, orgID valuer.UUID, serviceAcco
5860
return err
5961
}
6062

63+
module.analytics.IdentifyUser(ctx, orgID.String(), serviceAccount.ID.String(), serviceAccount.Traits())
64+
module.analytics.TrackUser(ctx, orgID.String(), serviceAccount.ID.String(), "Service Account Created", serviceAccount.Traits())
6165
return nil
6266
}
6367

@@ -76,6 +80,8 @@ func (module *module) GetOrCreate(ctx context.Context, serviceAccount *serviceac
7680
return nil, err
7781
}
7882

83+
module.analytics.IdentifyUser(ctx, serviceAccount.OrgID.String(), serviceAccount.ID.String(), serviceAccount.Traits())
84+
module.analytics.TrackUser(ctx, serviceAccount.OrgID.String(), serviceAccount.ID.String(), "Service Account Created", serviceAccount.Traits())
7985
return serviceAccount, nil
8086
}
8187

@@ -186,6 +192,8 @@ func (module *module) Update(ctx context.Context, orgID valuer.UUID, input *serv
186192
return err
187193
}
188194

195+
module.analytics.IdentifyUser(ctx, orgID.String(), input.ID.String(), input.Traits())
196+
module.analytics.TrackUser(ctx, orgID.String(), input.ID.String(), "Service Account Updated", input.Traits())
189197
return nil
190198
}
191199

@@ -212,6 +220,7 @@ func (module *module) UpdateStatus(ctx context.Context, orgID valuer.UUID, input
212220
}
213221
}
214222

223+
module.analytics.TrackUser(ctx, orgID.String(), input.ID.String(), "Service Account Deleted", map[string]any{})
215224
return nil
216225
}
217226

@@ -274,6 +283,7 @@ func (module *module) CreateFactorAPIKey(ctx context.Context, factorAPIKey *serv
274283
module.settings.Logger().ErrorContext(ctx, "failed to send email", "error", err)
275284
}
276285

286+
module.analytics.TrackUser(ctx, serviceAccount.OrgID, serviceAccount.ID.String(), "API Key created", factorAPIKey.Traits())
277287
return nil
278288
}
279289

@@ -295,8 +305,14 @@ func (module *module) ListFactorAPIKey(ctx context.Context, serviceAccountID val
295305
return serviceaccounttypes.NewFactorAPIKeyFromStorables(storables), nil
296306
}
297307

298-
func (module *module) UpdateFactorAPIKey(ctx context.Context, serviceAccountID valuer.UUID, factorAPIKey *serviceaccounttypes.FactorAPIKey) error {
299-
return module.store.UpdateFactorAPIKey(ctx, serviceAccountID, serviceaccounttypes.NewStorableFactorAPIKey(factorAPIKey))
308+
func (module *module) UpdateFactorAPIKey(ctx context.Context, orgID valuer.UUID, serviceAccountID valuer.UUID, factorAPIKey *serviceaccounttypes.FactorAPIKey) error {
309+
err := module.store.UpdateFactorAPIKey(ctx, serviceAccountID, serviceaccounttypes.NewStorableFactorAPIKey(factorAPIKey))
310+
if err != nil {
311+
return err
312+
}
313+
314+
module.analytics.TrackUser(ctx, orgID.String(), serviceAccountID.String(), "API Key updated", factorAPIKey.Traits())
315+
return nil
300316
}
301317

302318
func (module *module) RevokeFactorAPIKey(ctx context.Context, serviceAccountID valuer.UUID, id valuer.UUID) error {
@@ -324,9 +340,26 @@ func (module *module) RevokeFactorAPIKey(ctx context.Context, serviceAccountID v
324340
module.settings.Logger().ErrorContext(ctx, "failed to send email", "error", err)
325341
}
326342

343+
module.analytics.TrackUser(ctx, serviceAccount.OrgID, serviceAccountID.String(), "API Key revoked", factorAPIKey.Traits())
327344
return nil
328345
}
329346

347+
func (module *module) Collect(ctx context.Context, orgID valuer.UUID) (map[string]any, error) {
348+
stats := make(map[string]any)
349+
350+
count, err := module.store.CountByOrgID(ctx, orgID)
351+
if err == nil {
352+
stats["serviceaccount.count"] = count
353+
}
354+
355+
count, err = module.store.CountFactorAPIKeysByOrgID(ctx, orgID)
356+
if err == nil {
357+
stats["serviceaccount.keys.count"] = count
358+
}
359+
360+
return stats, nil
361+
}
362+
330363
func (module *module) disableServiceAccount(ctx context.Context, orgID valuer.UUID, input *serviceaccounttypes.ServiceAccount) error {
331364
err := module.authz.Revoke(ctx, orgID, input.Roles, authtypes.MustNewSubject(authtypes.TypeableServiceAccount, input.ID.String(), orgID, nil))
332365
if err != nil {

pkg/modules/serviceaccount/implserviceaccount/store.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,23 @@ func (store *store) GetServiceAccountRoles(ctx context.Context, id valuer.UUID)
164164
return storables, nil
165165
}
166166

167+
func (store *store) CountByOrgID(ctx context.Context, orgID valuer.UUID) (int64, error) {
168+
storable := new(serviceaccounttypes.StorableServiceAccount)
169+
170+
count, err := store.
171+
sqlstore.
172+
BunDB().
173+
NewSelect().
174+
Model(storable).
175+
Where("org_id = ?", orgID).
176+
Count(ctx)
177+
if err != nil {
178+
return 0, err
179+
}
180+
181+
return int64(count), nil
182+
}
183+
167184
func (store *store) ListServiceAccountRolesByOrgID(ctx context.Context, orgID valuer.UUID) ([]*serviceaccounttypes.StorableServiceAccountRole, error) {
168185
storables := make([]*serviceaccounttypes.StorableServiceAccountRole, 0)
169186

@@ -283,6 +300,25 @@ func (store *store) ListFactorAPIKeyByOrgID(ctx context.Context, orgID valuer.UU
283300
return storables, nil
284301
}
285302

303+
func (store *store) CountFactorAPIKeysByOrgID(ctx context.Context, orgID valuer.UUID) (int64, error) {
304+
storable := new(serviceaccounttypes.StorableFactorAPIKey)
305+
306+
count, err := store.
307+
sqlstore.
308+
BunDBCtx(ctx).
309+
NewSelect().
310+
Model(storable).
311+
Join("JOIN service_account").
312+
JoinOn("service_account.id = factor_api_key.service_account_id").
313+
Where("service_account.org_id = ?", orgID).
314+
Count(ctx)
315+
if err != nil {
316+
return 0, err
317+
}
318+
319+
return int64(count), nil
320+
}
321+
286322
func (store *store) UpdateFactorAPIKey(ctx context.Context, serviceAccountID valuer.UUID, storable *serviceaccounttypes.StorableFactorAPIKey) error {
287323
_, err := store.
288324
sqlstore.

pkg/modules/serviceaccount/serviceaccount.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"net/http"
66

7+
"github.com/SigNoz/signoz/pkg/statsreporter"
78
"github.com/SigNoz/signoz/pkg/types/serviceaccounttypes"
89
"github.com/SigNoz/signoz/pkg/valuer"
910
)
@@ -41,10 +42,12 @@ type Module interface {
4142
ListFactorAPIKey(context.Context, valuer.UUID) ([]*serviceaccounttypes.FactorAPIKey, error)
4243

4344
// Updates an existing API key for a service account
44-
UpdateFactorAPIKey(context.Context, valuer.UUID, *serviceaccounttypes.FactorAPIKey) error
45+
UpdateFactorAPIKey(context.Context, valuer.UUID, valuer.UUID, *serviceaccounttypes.FactorAPIKey) error
4546

4647
// Revokes an existing API key for a service account
4748
RevokeFactorAPIKey(context.Context, valuer.UUID, valuer.UUID) error
49+
50+
statsreporter.StatsCollector
4851
}
4952

5053
type Handler interface {

pkg/modules/spanpercentile/implspanpercentile/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (h *handler) GetSpanPercentileDetails(w http.ResponseWriter, r *http.Reques
3535
return
3636
}
3737

38-
result, err := h.module.GetSpanPercentile(r.Context(), valuer.MustNewUUID(claims.OrgID), valuer.MustNewUUID(claims.UserID), spanPercentileRequest)
38+
result, err := h.module.GetSpanPercentile(r.Context(), valuer.MustNewUUID(claims.OrgID), spanPercentileRequest)
3939
if err != nil {
4040
render.Error(w, err)
4141
return

pkg/modules/spanpercentile/implspanpercentile/module.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func NewModule(
2828
}
2929
}
3030

31-
func (m *module) GetSpanPercentile(ctx context.Context, orgID valuer.UUID, userID valuer.UUID, req *spanpercentiletypes.SpanPercentileRequest) (*spanpercentiletypes.SpanPercentileResponse, error) {
31+
func (m *module) GetSpanPercentile(ctx context.Context, orgID valuer.UUID, req *spanpercentiletypes.SpanPercentileRequest) (*spanpercentiletypes.SpanPercentileResponse, error) {
3232
ctx = ctxtypes.NewContextWithCommentVals(ctx, map[string]string{
3333
instrumentationtypes.CodeNamespace: "spanpercentile",
3434
instrumentationtypes.CodeFunctionName: "GetSpanPercentile",

0 commit comments

Comments
 (0)