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.

fix: prevent creation of funnels with duplicate names (#8633)

Ankit Nayan3c53ba308fbd
diff --git a/pkg/modules/tracefunnel/impltracefunnel/module.go b/pkg/modules/tracefunnel/impltracefunnel/module.go
index b53ffb42c6..21232ae855 100644
--- a/pkg/modules/tracefunnel/impltracefunnel/module.go
+++ b/pkg/modules/tracefunnel/impltracefunnel/module.go
@@ -52,8 +52,10 @@ func (module *module) Create(ctx context.Context, timestamp int64, name string,
funnel.CreatedBy = funnel.CreatedByUser.Identifiable.ID.String()
}
- if err := module.store.Create(ctx, funnel); err != nil {
- return nil, fmt.Errorf("failed to create funnel: %v", err)
+ err := module.store.Create(ctx, funnel)
+
+ if err != nil {
+ return nil, err
}
return funnel, nil
diff --git a/pkg/modules/tracefunnel/impltracefunnel/store.go b/pkg/modules/tracefunnel/impltracefunnel/store.go
index f0c2b119b5..0bb48e788e 100644
--- a/pkg/modules/tracefunnel/impltracefunnel/store.go
+++ b/pkg/modules/tracefunnel/impltracefunnel/store.go
@@ -19,29 +19,40 @@ func NewStore(sqlstore sqlstore.SQLStore) traceFunnels.FunnelStore {
}
func (store *store) Create(ctx context.Context, funnel *traceFunnels.StorableFunnel) error {
+ tx, err := store.sqlstore.BunDB().BeginTx(ctx, nil)
+ if err != nil {
+ return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to start transaction")
+ }
+ defer func() {
+ if err != nil {
+ _ = tx.Rollback()
+ }
+ }()
+
// Check if a funnel with the same name already exists in the organization
- exists, err := store.
- sqlstore.
- BunDB().
+ exists, err := tx.
NewSelect().
Model(new(traceFunnels.StorableFunnel)).
Where("name = ? AND org_id = ?", funnel.Name, funnel.OrgID.String()).
Exists(ctx)
if err != nil {
- return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to check for existing funnelr")
+ return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to check for existing funnel")
}
if exists {
- return store.sqlstore.WrapAlreadyExistsErrf(nil, traceFunnels.ErrFunnelAlreadyExists, "a funnel with name '%s' already exists in this organization", funnel.Name)
+ return errors.Newf(errors.TypeAlreadyExists, errors.CodeAlreadyExists, "a funnel with name '%s' already exists in this organization", funnel.Name)
}
- _, err = store.
- sqlstore.
- BunDB().
+ _, err = tx.
NewInsert().
Model(funnel).
Exec(ctx)
if err != nil {
- return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to create funnels")
+ return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to create funnel")
+ }
+
+ err = tx.Commit()
+ if err != nil {
+ return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to commit transaction")
}
return nil
@@ -66,18 +77,45 @@ func (store *store) Get(ctx context.Context, uuid valuer.UUID, orgID valuer.UUID
// Update updates an existing funnel
func (store *store) Update(ctx context.Context, funnel *traceFunnels.StorableFunnel) error {
+ tx, err := store.sqlstore.BunDB().BeginTx(ctx, nil)
+ if err != nil {
+ return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to start transaction")
+ }
+ defer func() {
+ if err != nil {
+ _ = tx.Rollback()
+ }
+ }()
+
+ // Check if a funnel with the same name already exists in the organization (excluding current funnel)
+ exists, err := tx.
+ NewSelect().
+ Model(new(traceFunnels.StorableFunnel)).
+ Where("name = ? AND org_id = ? AND id != ?", funnel.Name, funnel.OrgID.String(), funnel.ID.String()).
+ Exists(ctx)
+ if err != nil {
+ return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to check for existing funnel")
+ }
+ if exists {
+ return errors.Newf(errors.TypeAlreadyExists, errors.CodeAlreadyExists, "a funnel with name '%s' already exists in this organization", funnel.Name)
+ }
+
funnel.UpdatedAt = time.Now()
- _, err := store.
- sqlstore.
- BunDB().
+ _, err = tx.
NewUpdate().
Model(funnel).
WherePK().
Exec(ctx)
if err != nil {
- return store.sqlstore.WrapAlreadyExistsErrf(err, traceFunnels.ErrFunnelAlreadyExists, "a funnel with name '%s' already exists in this organization", funnel.Name)
+ return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to update funnel")
+ }
+
+ err = tx.Commit()
+ if err != nil {
+ return errors.Wrapf(err, errors.TypeInternal, errors.CodeInternal, "failed to commit transaction")
}
+
return nil
}
diff --git a/pkg/modules/tracefunnel/impltracefunnel/store_duplicate_names_test.go b/pkg/modules/tracefunnel/impltracefunnel/store_duplicate_names_test.go
new file mode 100644
index 0000000000..6be8c47a0f
--- /dev/null
+++ b/pkg/modules/tracefunnel/impltracefunnel/store_duplicate_names_test.go
@@ -0,0 +1,96 @@
+package impltracefunnel
+
+import (
+ "context"
+ "fmt"
+ "testing"
+
+ "github.com/SigNoz/signoz/pkg/errors"
+ traceFunnels "github.com/SigNoz/signoz/pkg/types/tracefunneltypes"
+ "github.com/SigNoz/signoz/pkg/valuer"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/mock"
+)
+
+// Test that Create method properly validates duplicate names
+func TestModule_Create_DuplicateNameValidation(t *testing.T) {
+ mockStore := new(MockStore)
+ module := NewModule(mockStore)
+
+ ctx := context.Background()
+ timestamp := int64(1234567890)
+ name := "Duplicate Funnel"
+ userID := valuer.GenerateUUID()
+ orgID := valuer.GenerateUUID()
+
+ // Mock store to return "already exists" error
+ expectedErr := errors.Wrapf(nil, errors.TypeAlreadyExists, traceFunnels.ErrFunnelAlreadyExists, "a funnel with name '%s' already exists in this organization", name)
+ mockStore.On("Create", ctx, mock.MatchedBy(func(f *traceFunnels.StorableFunnel) bool {
+ return f.Name == name && f.OrgID == orgID
+ })).Return(expectedErr)
+
+ funnel, err := module.Create(ctx, timestamp, name, userID, orgID)
+ assert.Error(t, err)
+ assert.Nil(t, funnel)
+ assert.Contains(t, err.Error(), fmt.Sprintf("a funnel with name '%s' already exists in this organization", name))
+
+ mockStore.AssertExpectations(t)
+}
+
+// Test that Update method properly validates duplicate names
+func TestModule_Update_DuplicateNameValidation(t *testing.T) {
+ mockStore := new(MockStore)
+ module := NewModule(mockStore)
+
+ ctx := context.Background()
+ userID := valuer.GenerateUUID()
+ orgID := valuer.GenerateUUID()
+ funnelName := "Duplicate Name"
+
+ funnel := &traceFunnels.StorableFunnel{
+ Name: funnelName,
+ OrgID: orgID,
+ }
+ funnel.ID = valuer.GenerateUUID()
+
+ // Mock store to return "already exists" error
+ expectedErr := errors.Wrapf(nil, errors.TypeAlreadyExists, traceFunnels.ErrFunnelAlreadyExists, "a funnel with name '%s' already exists in this organization", funnelName)
+ mockStore.On("Update", ctx, funnel).Return(expectedErr)
+
+ err := module.Update(ctx, funnel, userID)
+ assert.Error(t, err)
+ assert.Contains(t, err.Error(), fmt.Sprintf("a funnel with name '%s' already exists in this organization", funnelName))
+ assert.Equal(t, userID.String(), funnel.UpdatedBy) // Should still set UpdatedBy
+
+ mockStore.AssertExpectations(t)
+}
+
+// MockStore for testing
+type MockStore struct {
+ mock.Mock
+}
+
+func (m *MockStore) Create(ctx context.Context, funnel *traceFunnels.StorableFunnel) error {
+ args := m.Called(ctx, funnel)
+ return args.Error(0)
+}
+
+func (m *MockStore) Get(ctx context.Context, uuid valuer.UUID, orgID valuer.UUID) (*traceFunnels.StorableFunnel, error) {
+ args := m.Called(ctx, uuid, orgID)
+ return args.Get(0).(*traceFunnels.StorableFunnel), args.Error(1)
+}
+
+func (m *MockStore) List(ctx context.Context, orgID valuer.UUID) ([]*traceFunnels.StorableFunnel, error) {
+ args := m.Called(ctx, orgID)
+ return args.Get(0).([]*traceFunnels.StorableFunnel), args.Error(1)
+}
+
+func (m *MockStore) Update(ctx context.Context, funnel *traceFunnels.StorableFunnel) error {
+ args := m.Called(ctx, funnel)
+ return args.Error(0)
+}
+
+func (m *MockStore) Delete(ctx context.Context, uuid valuer.UUID, orgID valuer.UUID) error {
+ args := m.Called(ctx, uuid, orgID)
+ return args.Error(0)
+}
\ No newline at end of file