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(ruletypes): publish OpenAPI 3 discriminator on RuleThresholdData and EvaluationEnvelope (#11180)

Pandey9e118ded1fed
diff --git a/docs/api/openapi.yml b/docs/api/openapi.yml
index 18bd9105c6..aebfb3b1d3 100644
--- a/docs/api/openapi.yml
+++ b/docs/api/openapi.yml
@@ -4463,18 +4463,19 @@ components:
$ref: '#/components/schemas/RuletypesEvaluationKind'
spec:
$ref: '#/components/schemas/RuletypesCumulativeWindow'
+ required:
+ - kind
+ - spec
type: object
RuletypesEvaluationEnvelope:
+ discriminator:
+ mapping:
+ cumulative: '#/components/schemas/RuletypesEvaluationCumulative'
+ rolling: '#/components/schemas/RuletypesEvaluationRolling'
+ propertyName: kind
oneOf:
- $ref: '#/components/schemas/RuletypesEvaluationRolling'
- $ref: '#/components/schemas/RuletypesEvaluationCumulative'
- properties:
- kind:
- $ref: '#/components/schemas/RuletypesEvaluationKind'
- spec: {}
- required:
- - kind
- - spec
type: object
RuletypesEvaluationKind:
enum:
@@ -4487,6 +4488,9 @@ components:
$ref: '#/components/schemas/RuletypesEvaluationKind'
spec:
$ref: '#/components/schemas/RuletypesRollingWindow'
+ required:
+ - kind
+ - spec
type: object
RuletypesGettableTestRule:
properties:
@@ -4794,15 +4798,12 @@ components:
- compositeQuery
type: object
RuletypesRuleThresholdData:
+ discriminator:
+ mapping:
+ basic: '#/components/schemas/RuletypesThresholdBasic'
+ propertyName: kind
oneOf:
- $ref: '#/components/schemas/RuletypesThresholdBasic'
- properties:
- kind:
- $ref: '#/components/schemas/RuletypesThresholdKind'
- spec: {}
- required:
- - kind
- - spec
type: object
RuletypesRuleType:
enum:
@@ -4844,6 +4845,9 @@ components:
$ref: '#/components/schemas/RuletypesThresholdKind'
spec:
$ref: '#/components/schemas/RuletypesBasicRuleThresholds'
+ required:
+ - kind
+ - spec
type: object
RuletypesThresholdKind:
enum:
diff --git a/frontend/src/api/generated/services/sigNoz.schemas.ts b/frontend/src/api/generated/services/sigNoz.schemas.ts
index 5bc89ad60e..239fca24c6 100644
--- a/frontend/src/api/generated/services/sigNoz.schemas.ts
+++ b/frontend/src/api/generated/services/sigNoz.schemas.ts
@@ -6676,28 +6676,36 @@ export interface RuletypesCumulativeWindowDTO {
timezone: string;
}
+export enum RuletypesEvaluationCumulativeDTOKind {
+ cumulative = 'cumulative',
+}
export interface RuletypesEvaluationCumulativeDTO {
- kind?: RuletypesEvaluationKindDTO;
- spec?: RuletypesCumulativeWindowDTO;
+ /**
+ * @type string
+ * @enum cumulative
+ */
+ kind: RuletypesEvaluationCumulativeDTOKind;
+ spec: RuletypesCumulativeWindowDTO;
}
export type RuletypesEvaluationEnvelopeDTO =
- | (RuletypesEvaluationRollingDTO & {
- kind: RuletypesEvaluationKindDTO;
- spec: unknown;
- })
- | (RuletypesEvaluationCumulativeDTO & {
- kind: RuletypesEvaluationKindDTO;
- spec: unknown;
- });
+ | RuletypesEvaluationRollingDTO
+ | RuletypesEvaluationCumulativeDTO;
export enum RuletypesEvaluationKindDTO {
rolling = 'rolling',
cumulative = 'cumulative',
}
+export enum RuletypesEvaluationRollingDTOKind {
+ rolling = 'rolling',
+}
export interface RuletypesEvaluationRollingDTO {
- kind?: RuletypesEvaluationKindDTO;
- spec?: RuletypesRollingWindowDTO;
+ /**
+ * @type string
+ * @enum rolling
+ */
+ kind: RuletypesEvaluationRollingDTOKind;
+ spec: RuletypesRollingWindowDTO;
}
export interface RuletypesGettableTestRuleDTO {
@@ -7052,10 +7060,7 @@ export interface RuletypesRuleConditionDTO {
thresholds?: RuletypesRuleThresholdDataDTO;
}
-export type RuletypesRuleThresholdDataDTO = RuletypesThresholdBasicDTO & {
- kind: RuletypesThresholdKindDTO;
- spec: unknown;
-};
+export type RuletypesRuleThresholdDataDTO = RuletypesThresholdBasicDTO;
export enum RuletypesRuleTypeDTO {
threshold_rule = 'threshold_rule',
@@ -7091,9 +7096,16 @@ export enum RuletypesSeasonalityDTO {
daily = 'daily',
weekly = 'weekly',
}
+export enum RuletypesThresholdBasicDTOKind {
+ basic = 'basic',
+}
export interface RuletypesThresholdBasicDTO {
- kind?: RuletypesThresholdKindDTO;
- spec?: RuletypesBasicRuleThresholdsDTO;
+ /**
+ * @type string
+ * @enum basic
+ */
+ kind: RuletypesThresholdBasicDTOKind;
+ spec: RuletypesBasicRuleThresholdsDTO;
}
export enum RuletypesThresholdKindDTO {
diff --git a/pkg/signoz/openapi.go b/pkg/signoz/openapi.go
index 6460d703c4..641964944d 100644
--- a/pkg/signoz/openapi.go
+++ b/pkg/signoz/openapi.go
@@ -44,6 +44,8 @@ import (
"gopkg.in/yaml.v2"
)
+const signozDiscriminatorKey string = "x-signoz-discriminator"
+
type OpenAPI struct {
apiserver apiserver.APIServer
reflector *openapi3.Reflector
@@ -142,6 +144,8 @@ func (openapi *OpenAPI) CreateAndWrite(path string) error {
return err
}
+ attachDiscriminators(openapi.reflector.Spec)
+
// The library's MarshalYAML does a JSON round-trip that converts all numbers
// to float64, causing large integers (e.g. epoch millisecond timestamps) to
// render in scientific notation (1.6409952e+12).
@@ -199,3 +203,59 @@ func convertJSONNumbers(v interface{}) {
}
}
}
+
+// attachDiscriminators promotes x-signoz-discriminator extensions
+// into openapi3 Discriminator fields. Malformed markers are dropped.
+func attachDiscriminators(spec *openapi3.Spec) {
+ if spec.Components == nil || spec.Components.Schemas == nil {
+ return
+ }
+
+ for name, entry := range spec.Components.Schemas.MapOfSchemaOrRefValues {
+ if entry.Schema == nil {
+ continue
+ }
+
+ raw, ok := entry.Schema.MapOfAnything[signozDiscriminatorKey]
+ if !ok {
+ continue
+ }
+
+ marker, ok := raw.(map[string]any)
+ if !ok {
+ continue
+ }
+
+ propertyName, ok := marker["propertyName"].(string)
+ if !ok || propertyName == "" {
+ continue
+ }
+
+ disc := openapi3.Discriminator{PropertyName: propertyName}
+ if rawMapping, ok := marker["mapping"]; ok {
+ if mapping, ok := rawMapping.(map[string]string); ok {
+ disc.Mapping = mapping
+ } else if mapping, ok := rawMapping.(map[string]any); ok {
+ converted := make(map[string]string, len(mapping))
+ for k, v := range mapping {
+ if s, ok := v.(string); ok {
+ converted[k] = s
+ }
+ }
+ disc.Mapping = converted
+ }
+ }
+
+ entry.Schema.Discriminator = &disc
+ delete(entry.Schema.MapOfAnything, signozDiscriminatorKey)
+
+ // The parent's reflected `properties` / `required` duplicate
+ // what the oneOf variants already declare, and orval intersects
+ // the two — turning a clean discriminated union DTO into a
+ // noisy union of intersections. Drop them here.
+ entry.Schema.Properties = nil
+ entry.Schema.Required = nil
+
+ spec.Components.Schemas.MapOfSchemaOrRefValues[name] = entry
+ }
+}
diff --git a/pkg/types/ruletypes/evaluation.go b/pkg/types/ruletypes/evaluation.go
index 883fb9a5a3..646edd07c7 100644
--- a/pkg/types/ruletypes/evaluation.go
+++ b/pkg/types/ruletypes/evaluation.go
@@ -250,17 +250,20 @@ type EvaluationEnvelope struct {
// evaluationRolling is the OpenAPI schema for an EvaluationEnvelope with kind=rolling.
type evaluationRolling struct {
- Kind EvaluationKind `json:"kind" description:"The kind of evaluation."`
- Spec RollingWindow `json:"spec" description:"The rolling window evaluation specification."`
+ Kind EvaluationKind `json:"kind" description:"The kind of evaluation." required:"true"`
+ Spec RollingWindow `json:"spec" description:"The rolling window evaluation specification." required:"true"`
}
// evaluationCumulative is the OpenAPI schema for an EvaluationEnvelope with kind=cumulative.
type evaluationCumulative struct {
- Kind EvaluationKind `json:"kind" description:"The kind of evaluation."`
- Spec CumulativeWindow `json:"spec" description:"The cumulative window evaluation specification."`
+ Kind EvaluationKind `json:"kind" description:"The kind of evaluation." required:"true"`
+ Spec CumulativeWindow `json:"spec" description:"The cumulative window evaluation specification." required:"true"`
}
-var _ jsonschema.OneOfExposer = EvaluationEnvelope{}
+var (
+ _ jsonschema.OneOfExposer = EvaluationEnvelope{}
+ _ jsonschema.Preparer = EvaluationEnvelope{}
+)
// JSONSchemaOneOf returns the oneOf variants for the EvaluationEnvelope discriminated union.
// Each variant represents a different evaluation kind with its corresponding spec schema.
@@ -271,6 +274,22 @@ func (EvaluationEnvelope) JSONSchemaOneOf() []any {
}
}
+func (EvaluationEnvelope) PrepareJSONSchema(schema *jsonschema.Schema) error {
+ if schema.ExtraProperties == nil {
+ schema.ExtraProperties = map[string]any{}
+ }
+
+ schema.ExtraProperties["x-signoz-discriminator"] = map[string]any{
+ "propertyName": "kind",
+ "mapping": map[string]string{
+ "rolling": "#/components/schemas/RuletypesEvaluationRolling",
+ "cumulative": "#/components/schemas/RuletypesEvaluationCumulative",
+ },
+ }
+
+ return nil
+}
+
func (e *EvaluationEnvelope) UnmarshalJSON(data []byte) error {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
diff --git a/pkg/types/ruletypes/threshold.go b/pkg/types/ruletypes/threshold.go
index 9a2b96bfee..71db7d5a66 100644
--- a/pkg/types/ruletypes/threshold.go
+++ b/pkg/types/ruletypes/threshold.go
@@ -36,11 +36,14 @@ type RuleThresholdData struct {
// thresholdBasic is the OpenAPI schema for a RuleThresholdData with kind=basic.
type thresholdBasic struct {
- Kind ThresholdKind `json:"kind" description:"The kind of threshold."`
- Spec BasicRuleThresholds `json:"spec" description:"The basic threshold specification (array of thresholds)."`
+ Kind ThresholdKind `json:"kind" description:"The kind of threshold." required:"true"`
+ Spec BasicRuleThresholds `json:"spec" description:"The basic threshold specification (array of thresholds)." required:"true"`
}
-var _ jsonschema.OneOfExposer = RuleThresholdData{}
+var (
+ _ jsonschema.OneOfExposer = RuleThresholdData{}
+ _ jsonschema.Preparer = RuleThresholdData{}
+)
// JSONSchemaOneOf returns the oneOf variants for the RuleThresholdData discriminated union.
// Each variant represents a different threshold kind with its corresponding spec schema.
@@ -50,6 +53,24 @@ func (RuleThresholdData) JSONSchemaOneOf() []any {
}
}
+// PrepareJSONSchema marks the schema with x-signoz-discriminator;
+// signoz.attachDiscriminators promotes it to a real OpenAPI 3
+// discriminator after reflection.
+func (RuleThresholdData) PrepareJSONSchema(schema *jsonschema.Schema) error {
+ if schema.ExtraProperties == nil {
+ schema.ExtraProperties = map[string]any{}
+ }
+
+ schema.ExtraProperties["x-signoz-discriminator"] = map[string]any{
+ "propertyName": "kind",
+ "mapping": map[string]string{
+ "basic": "#/components/schemas/RuletypesThresholdBasic",
+ },
+ }
+
+ return nil
+}
+
func (r *RuleThresholdData) UnmarshalJSON(data []byte) error {
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {