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.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix: empty patterns in pricing are rejected (#12995)
Nityananda Gohainec05bfe7550c
diff --git a/pkg/types/llmpricingruletypes/pricing.go b/pkg/types/llmpricingruletypes/pricing.goindex 5792fb8f71..6b722de84b 100644--- a/pkg/types/llmpricingruletypes/pricing.go+++ b/pkg/types/llmpricingruletypes/pricing.go@@ -208,6 +208,35 @@ func NewGettableUnmappedModels(items []*UnmappedModel) *GettableUnmappedModels {}}+func (u *UpdatableLLMPricingRule) UnmarshalJSON(data []byte) error {+ type Alias UpdatableLLMPricingRule++ var temp Alias+ if err := json.Unmarshal(data, &temp); err != nil {+ return err+ }++ *u = UpdatableLLMPricingRule(temp)+ return u.Validate()+}++// Validate mirrors the collector's pattern check: at least one pattern, none+// empty, all valid path.Match globs.+func (u *UpdatableLLMPricingRule) Validate() error {+ if len(u.ModelPattern) == 0 {+ return errors.Newf(errors.TypeInvalidInput, ErrCodePricingRuleInvalidInput, "model %q: modelPattern must contain at least one pattern", u.Model)+ }+ for _, p := range u.ModelPattern {+ if p == "" {+ return errors.Newf(errors.TypeInvalidInput, ErrCodePricingRuleInvalidInput, "model %q: modelPattern must not contain an empty pattern", u.Model)+ }+ if _, err := path.Match(p, ""); err != nil {+ return errors.Newf(errors.TypeInvalidInput, ErrCodePricingRuleInvalidInput, "model %q: modelPattern %q is not a valid glob", u.Model, p)+ }+ }+ return nil+}+func NewLLMPricingRuleFromUpdatable(u *UpdatableLLMPricingRule, orgID valuer.UUID, userEmail string, now time.Time) *LLMPricingRule {id := valuer.GenerateUUID()if u.ID != nil {diff --git a/pkg/types/llmpricingruletypes/processorconfig_test.go b/pkg/types/llmpricingruletypes/processorconfig_test.goindex 21a11aa7c9..9722b77be9 100644--- a/pkg/types/llmpricingruletypes/processorconfig_test.go+++ b/pkg/types/llmpricingruletypes/processorconfig_test.go@@ -1,6 +1,7 @@package llmpricingruletypesimport (+ "encoding/json""os""path/filepath""testing"@@ -126,3 +127,34 @@ func TestGenerateCollectorConfig_EmptyInputPassthrough(t *testing.T) {assert.Equal(t, in, out)}}++func TestUpdatableLLMPricingRuleUnmarshalJSON(t *testing.T) {+ tests := []struct {+ name string+ pattern string+ wantErr bool+ }{+ {name: "valid", pattern: `["gpt-4o*", "gpt-4o"]`},+ {name: "missing", pattern: ``, wantErr: true},+ {name: "null", pattern: `null`, wantErr: true},+ {name: "empty_list", pattern: `[]`, wantErr: true},+ {name: "empty_entry", pattern: `["gpt-4o*", ""]`, wantErr: true},+ {name: "bad_glob", pattern: `["gpt-["]`, wantErr: true},+ }++ for _, tc := range tests {+ t.Run(tc.name, func(t *testing.T) {+ body := `{"modelName": "gpt-4o"}`+ if tc.pattern != "" {+ body = `{"modelName": "gpt-4o", "modelPattern": ` + tc.pattern + `}`+ }+ var req UpdatableLLMPricingRules+ err := json.Unmarshal([]byte(`{"rules": [`+body+`]}`), &req)+ if tc.wantErr {+ assert.Error(t, err)+ } else {+ assert.NoError(t, err)+ }+ })+ }+}diff --git a/tests/integration/tests/llmpricingrules/01_upsert.py b/tests/integration/tests/llmpricingrules/01_upsert.pyindex c9f61ac5e9..3b50049502 100644--- a/tests/integration/tests/llmpricingrules/01_upsert.py+++ b/tests/integration/tests/llmpricingrules/01_upsert.py@@ -130,3 +130,17 @@ def test_bulk_sync(assert all(r["pricing"]["input"] == 5 for r in stored)delete_all_llm_pricing_rules(signoz, token)+++def test_rejects_rule_without_pattern(+ signoz: types.SigNoz,+ create_user_admin: types.Operation, # pylint: disable=unused-argument+ get_token: Callable[[str, str], str],+):+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)+ delete_all_llm_pricing_rules(signoz, token)++ rules = zeus_rules(10)+ rules[1]["modelPattern"] = []+ assert upsert_llm_pricing_rules(signoz, token, rules).status_code == HTTPStatus.BAD_REQUEST+ assert list_llm_pricing_rules(signoz, token) == []