Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

feat(authz): add stats reporting for public dashboards (#9605)

Vikrant Guptae9fa68e1f39e
diff --git a/pkg/modules/dashboard/impldashboard/module.go b/pkg/modules/dashboard/impldashboard/module.go
index 40850c965f..71079286ba 100644
--- a/pkg/modules/dashboard/impldashboard/module.go
+++ b/pkg/modules/dashboard/impldashboard/module.go
@@ -2,6 +2,7 @@ package impldashboard
import (
"context"
+ "maps"
"strings"
"github.com/SigNoz/signoz/pkg/analytics"
@@ -58,6 +59,14 @@ func (module *module) Create(ctx context.Context, orgID valuer.UUID, createdBy s
}
func (module *module) CreatePublic(ctx context.Context, orgID valuer.UUID, publicDashboard *dashboardtypes.PublicDashboard) error {
+ storablePublicDashboard, err := module.store.GetPublic(ctx, publicDashboard.DashboardID.StringValue())
+ if err != nil && !errors.Ast(err, errors.TypeNotFound) {
+ return err
+ }
+ if storablePublicDashboard != nil {
+ return errors.Newf(errors.TypeAlreadyExists, dashboardtypes.ErrCodePublicDashboardAlreadyExists, "dashboard with id %s is already public", storablePublicDashboard.DashboardID)
+ }
+
role, err := module.role.GetOrCreate(ctx, roletypes.NewRole(roletypes.AnonymousUserRoleName, roletypes.AnonymousUserRoleDescription, roletypes.RoleTypeManaged.StringValue(), orgID))
if err != nil {
return err
@@ -203,19 +212,14 @@ func (module *module) Delete(ctx context.Context, orgID valuer.UUID, id valuer.U
}
err = module.store.RunInTx(ctx, func(ctx context.Context) error {
- err := module.store.Delete(ctx, orgID, id)
- if err != nil {
+ err := module.DeletePublic(ctx, orgID, id)
+ if err != nil && !errors.Ast(err, errors.TypeNotFound) {
return err
}
- err = module.store.DeletePublic(ctx, id.StringValue())
+ err = module.store.Delete(ctx, orgID, id)
if err != nil {
- // do not do anything if no public config exists.
- if !errors.Ast(err, errors.TypeNotFound) {
- return err
- }
-
- return nil
+ return err
}
return nil
@@ -343,7 +347,15 @@ func (module *module) Collect(ctx context.Context, orgID valuer.UUID) (map[strin
return nil, err
}
- return dashboardtypes.NewStatsFromStorableDashboards(dashboards), nil
+ publicDashboards, err := module.store.ListPublic(ctx, orgID)
+ if err != nil {
+ return nil, err
+ }
+
+ stats := make(map[string]any)
+ maps.Copy(stats, dashboardtypes.NewStatsFromStorableDashboards(dashboards))
+ maps.Copy(stats, dashboardtypes.NewStatsFromStorablePublicDashboards(publicDashboards))
+ return stats, nil
}
func (module *module) MustGetTypeables() []authtypes.Typeable {
diff --git a/pkg/modules/dashboard/impldashboard/store.go b/pkg/modules/dashboard/impldashboard/store.go
index 028c3ee5ae..960aa2e9be 100644
--- a/pkg/modules/dashboard/impldashboard/store.go
+++ b/pkg/modules/dashboard/impldashboard/store.go
@@ -132,6 +132,24 @@ func (store *store) List(ctx context.Context, orgID valuer.UUID) ([]*dashboardty
return storableDashboards, nil
}
+func (store *store) ListPublic(ctx context.Context, orgID valuer.UUID) ([]*dashboardtypes.StorablePublicDashboard, error) {
+ storable := make([]*dashboardtypes.StorablePublicDashboard, 0)
+ err := store.
+ sqlstore.
+ BunDB().
+ NewSelect().
+ Model(&storable).
+ Join("JOIN dashboard").
+ JoinOn("public_dashboard.dashboard_id = dashboard.id").
+ Where("dashboard.org_id = ?", orgID).
+ Scan(ctx)
+ if err != nil {
+ return nil, err
+ }
+
+ return storable, nil
+}
+
func (store *store) Update(ctx context.Context, orgID valuer.UUID, storableDashboard *dashboardtypes.StorableDashboard) error {
_, err := store.
sqlstore.
diff --git a/pkg/types/dashboardtypes/public_dashboard.go b/pkg/types/dashboardtypes/public_dashboard.go
index 0f726541df..ec8c9aa8a0 100644
--- a/pkg/types/dashboardtypes/public_dashboard.go
+++ b/pkg/types/dashboardtypes/public_dashboard.go
@@ -17,7 +17,7 @@ var (
)
type StorablePublicDashboard struct {
- bun.BaseModel `bun:"table:public_dashboard"`
+ bun.BaseModel `bun:"table:public_dashboard,alias:public_dashboard"`
types.Identifiable
types.TimeAuditable
@@ -263,3 +263,10 @@ func (typ *UpdatablePublicDashboard) UnmarshalJSON(data []byte) error {
*typ = UpdatablePublicDashboard(temp)
return nil
}
+
+func NewStatsFromStorablePublicDashboards(publicDashboards []*StorablePublicDashboard) map[string]any {
+ stats := make(map[string]any)
+
+ stats["public_dashboard.count"] = int64(len(publicDashboards))
+ return stats
+}
diff --git a/pkg/types/dashboardtypes/store.go b/pkg/types/dashboardtypes/store.go
index 27e5845a02..ce2dee0baf 100644
--- a/pkg/types/dashboardtypes/store.go
+++ b/pkg/types/dashboardtypes/store.go
@@ -21,6 +21,8 @@ type Store interface {
List(context.Context, valuer.UUID) ([]*StorableDashboard, error)
+ ListPublic(context.Context, valuer.UUID) ([]*StorablePublicDashboard, error)
+
Update(context.Context, valuer.UUID, *StorableDashboard) error
UpdatePublic(context.Context, *StorablePublicDashboard) error