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(infra-monitoring): v2 clusters list api (#11132)

Nikhil Mantrica8cc30c3454
diff --git a/docs/api/openapi.yml b/docs/api/openapi.yml
index 2f664b21ff..5a86f2dfb7 100644
--- a/docs/api/openapi.yml
+++ b/docs/api/openapi.yml
@@ -2520,6 +2520,65 @@ components:
enabled:
type: boolean
type: object
+ InframonitoringtypesClusterRecord:
+ properties:
+ clusterCPU:
+ format: double
+ type: number
+ clusterCPUAllocatable:
+ format: double
+ type: number
+ clusterMemory:
+ format: double
+ type: number
+ clusterMemoryAllocatable:
+ format: double
+ type: number
+ clusterName:
+ type: string
+ meta:
+ additionalProperties:
+ type: string
+ nullable: true
+ type: object
+ nodeCountsByReadiness:
+ $ref: '#/components/schemas/InframonitoringtypesNodeCountsByReadiness'
+ podCountsByPhase:
+ $ref: '#/components/schemas/InframonitoringtypesPodCountsByPhase'
+ required:
+ - clusterName
+ - clusterCPU
+ - clusterCPUAllocatable
+ - clusterMemory
+ - clusterMemoryAllocatable
+ - nodeCountsByReadiness
+ - podCountsByPhase
+ - meta
+ type: object
+ InframonitoringtypesClusters:
+ properties:
+ endTimeBeforeRetention:
+ type: boolean
+ records:
+ items:
+ $ref: '#/components/schemas/InframonitoringtypesClusterRecord'
+ nullable: true
+ type: array
+ requiredMetricsCheck:
+ $ref: '#/components/schemas/InframonitoringtypesRequiredMetricsCheck'
+ total:
+ type: integer
+ type:
+ $ref: '#/components/schemas/InframonitoringtypesResponseType'
+ warning:
+ $ref: '#/components/schemas/Querybuildertypesv5QueryWarnData'
+ required:
+ - type
+ - records
+ - total
+ - requiredMetricsCheck
+ - endTimeBeforeRetention
+ type: object
InframonitoringtypesHostFilter:
properties:
expression:
@@ -2824,6 +2883,32 @@ components:
- requiredMetricsCheck
- endTimeBeforeRetention
type: object
+ InframonitoringtypesPostableClusters:
+ properties:
+ end:
+ format: int64
+ type: integer
+ filter:
+ $ref: '#/components/schemas/Querybuildertypesv5Filter'
+ groupBy:
+ items:
+ $ref: '#/components/schemas/Querybuildertypesv5GroupByKey'
+ nullable: true
+ type: array
+ limit:
+ type: integer
+ offset:
+ type: integer
+ orderBy:
+ $ref: '#/components/schemas/Querybuildertypesv5OrderBy'
+ start:
+ format: int64
+ type: integer
+ required:
+ - start
+ - end
+ - limit
+ type: object
InframonitoringtypesPostableHosts:
properties:
end:
@@ -11746,6 +11831,77 @@ paths:
summary: Health check
tags:
- health
+ /api/v2/infra_monitoring/clusters:
+ post:
+ deprecated: false
+ description: 'Returns a paginated list of Kubernetes clusters with key aggregated
+ metrics derived by summing per-node values within the group: CPU usage, CPU
+ allocatable, memory working set, memory allocatable. Each row also reports
+ per-group nodeCountsByReadiness ({ ready, notReady } from each node''s latest
+ k8s.node.condition_ready value) and per-group podCountsByPhase ({ pending,
+ running, succeeded, failed, unknown } from each pod''s latest k8s.pod.phase
+ value). Each cluster includes metadata attributes (k8s.cluster.name). The
+ response type is ''list'' for the default k8s.cluster.name grouping or ''grouped_list''
+ for custom groupBy keys; in both modes every row aggregates nodes and pods
+ in the group. Supports filtering via a filter expression, custom groupBy,
+ ordering by cpu / cpu_allocatable / memory / memory_allocatable, and pagination
+ via offset/limit. Also reports missing required metrics and whether the requested
+ time range falls before the data retention boundary. Numeric metric fields
+ (clusterCPU, clusterCPUAllocatable, clusterMemory, clusterMemoryAllocatable)
+ return -1 as a sentinel when no data is available for that field.'
+ operationId: ListClusters
+ requestBody:
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/InframonitoringtypesPostableClusters'
+ responses:
+ "200":
+ content:
+ application/json:
+ schema:
+ properties:
+ data:
+ $ref: '#/components/schemas/InframonitoringtypesClusters'
+ status:
+ type: string
+ required:
+ - status
+ - data
+ type: object
+ description: OK
+ "400":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Bad Request
+ "401":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Unauthorized
+ "403":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Forbidden
+ "500":
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/RenderErrorResponse'
+ description: Internal Server Error
+ security:
+ - api_key:
+ - VIEWER
+ - tokenizer:
+ - VIEWER
+ summary: List Clusters for Infra Monitoring
+ tags:
+ - inframonitoring
/api/v2/infra_monitoring/hosts:
post:
deprecated: false
diff --git a/frontend/src/api/generated/services/inframonitoring/index.ts b/frontend/src/api/generated/services/inframonitoring/index.ts
index 548b771c07..59e524677f 100644
--- a/frontend/src/api/generated/services/inframonitoring/index.ts
+++ b/frontend/src/api/generated/services/inframonitoring/index.ts
@@ -12,10 +12,12 @@ import type {
} from 'react-query';
import type {
+ InframonitoringtypesPostableClustersDTO,
InframonitoringtypesPostableHostsDTO,
InframonitoringtypesPostableNamespacesDTO,
InframonitoringtypesPostableNodesDTO,
InframonitoringtypesPostablePodsDTO,
+ ListClusters200,
ListHosts200,
ListNamespaces200,
ListNodes200,
@@ -26,6 +28,90 @@ import type {
import { GeneratedAPIInstance } from '../../../generatedAPIInstance';
import type { ErrorType, BodyType } from '../../../generatedAPIInstance';
+/**
+ * Returns a paginated list of Kubernetes clusters with key aggregated metrics derived by summing per-node values within the group: CPU usage, CPU allocatable, memory working set, memory allocatable. Each row also reports per-group nodeCountsByReadiness ({ ready, notReady } from each node's latest k8s.node.condition_ready value) and per-group podCountsByPhase ({ pending, running, succeeded, failed, unknown } from each pod's latest k8s.pod.phase value). Each cluster includes metadata attributes (k8s.cluster.name). The response type is 'list' for the default k8s.cluster.name grouping or 'grouped_list' for custom groupBy keys; in both modes every row aggregates nodes and pods in the group. Supports filtering via a filter expression, custom groupBy, ordering by cpu / cpu_allocatable / memory / memory_allocatable, and pagination via offset/limit. Also reports missing required metrics and whether the requested time range falls before the data retention boundary. Numeric metric fields (clusterCPU, clusterCPUAllocatable, clusterMemory, clusterMemoryAllocatable) return -1 as a sentinel when no data is available for that field.
+ * @summary List Clusters for Infra Monitoring
+ */
+export const listClusters = (
+ inframonitoringtypesPostableClustersDTO: BodyType<InframonitoringtypesPostableClustersDTO>,
+ signal?: AbortSignal,
+) => {
+ return GeneratedAPIInstance<ListClusters200>({
+ url: `/api/v2/infra_monitoring/clusters`,
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ data: inframonitoringtypesPostableClustersDTO,
+ signal,
+ });
+};
+
+export const getListClustersMutationOptions = <
+ TError = ErrorType<RenderErrorResponseDTO>,
+ TContext = unknown,
+>(options?: {
+ mutation?: UseMutationOptions<
+ Awaited<ReturnType<typeof listClusters>>,
+ TError,
+ { data: BodyType<InframonitoringtypesPostableClustersDTO> },
+ TContext
+ >;
+}): UseMutationOptions<
+ Awaited<ReturnType<typeof listClusters>>,
+ TError,
+ { data: BodyType<InframonitoringtypesPostableClustersDTO> },
+ TContext
+> => {
+ const mutationKey = ['listClusters'];
+ const { mutation: mutationOptions } = options
+ ? options.mutation &&
+ 'mutationKey' in options.mutation &&
+ options.mutation.mutationKey
+ ? options
+ : { ...options, mutation: { ...options.mutation, mutationKey } }
+ : { mutation: { mutationKey } };
+
+ const mutationFn: MutationFunction<
+ Awaited<ReturnType<typeof listClusters>>,
+ { data: BodyType<InframonitoringtypesPostableClustersDTO> }
+ > = (props) => {
+ const { data } = props ?? {};
+
+ return listClusters(data);
+ };
+
+ return { mutationFn, ...mutationOptions };
+};
+
+export type ListClustersMutationResult = NonNullable<
+ Awaited<ReturnType<typeof listClusters>>
+>;
+export type ListClustersMutationBody =
+ BodyType<InframonitoringtypesPostableClustersDTO>;
+export type ListClustersMutationError = ErrorType<RenderErrorResponseDTO>;
+
+/**
+ * @summary List Clusters for Infra Monitoring
+ */
+export const useListClusters = <
+ TError = ErrorType<RenderErrorResponseDTO>,
+ TContext = unknown,
+>(options?: {
+ mutation?: UseMutationOptions<
+ Awaited<ReturnType<typeof listClusters>>,
+ TError,
+ { data: BodyType<InframonitoringtypesPostableClustersDTO> },
+ TContext
+ >;
+}): UseMutationResult<
+ Awaited<ReturnType<typeof listClusters>>,
+ TError,
+ { data: BodyType<InframonitoringtypesPostableClustersDTO> },
+ TContext
+> => {
+ const mutationOptions = getListClustersMutationOptions(options);
+
+ return useMutation(mutationOptions);
+};
/**
* Returns a paginated list of hosts with key infrastructure metrics: CPU usage (%), memory usage (%), I/O wait (%), disk usage (%), and 15-minute load average. Each host includes its current status (active/inactive based on metrics reported in the last 10 minutes) and metadata attributes (e.g., os.type). Supports filtering via a filter expression, filtering by host status, custom groupBy to aggregate hosts by any attribute, ordering by any of the five metrics, and pagination via offset/limit. The response type is 'list' for the default host.name grouping or 'grouped_list' for custom groupBy keys. Also reports missing required metrics and whether the requested time range falls before the data retention boundary. Numeric metric fields (cpu, memory, wait, load15, diskUsage) return -1 as a sentinel when no data is available for that field.
* @summary List Hosts for Infra Monitoring
diff --git a/frontend/src/api/generated/services/sigNoz.schemas.ts b/frontend/src/api/generated/services/sigNoz.schemas.ts
index 61eb46781d..956ed717e6 100644
--- a/frontend/src/api/generated/services/sigNoz.schemas.ts
+++ b/frontend/src/api/generated/services/sigNoz.schemas.ts
@@ -4568,6 +4568,66 @@ export interface GlobaltypesTokenizerConfigDTO {
enabled?: boolean;
}
+/**
+ * @nullable
+ */
+export type InframonitoringtypesClusterRecordDTOMeta = {
+ [key: string]: string;
+} | null;
+
+export interface InframonitoringtypesClusterRecordDTO {
+ /**
+ * @type number
+ * @format double
+ */
+ clusterCPU: number;
+ /**
+ * @type number
+ * @format double
+ */
+ clusterCPUAllocatable: number;
+ /**
+ * @type number
+ * @format double
+ */
+ clusterMemory: number;
+ /**
+ * @type number
+ * @format double
+ */
+ clusterMemoryAllocatable: number;
+ /**
+ * @type string
+ */
+ clusterName: string;
+ /**
+ * @type object
+ * @nullable true
+ */
+ meta: InframonitoringtypesClusterRecordDTOMeta;
+ nodeCountsByReadiness: InframonitoringtypesNodeCountsByReadinessDTO;
+ podCountsByPhase: InframonitoringtypesPodCountsByPhaseDTO;
+}
+
+export interface InframonitoringtypesClustersDTO {
+ /**
+ * @type boolean
+ */
+ endTimeBeforeRetention: boolean;
+ /**
+ * @type array
+ * @nullable true
+ */
+ records: InframonitoringtypesClusterRecordDTO[] | null;
+ requiredMetricsCheck: InframonitoringtypesRequiredMetricsCheckDTO;
+ /**
+ * @type integer
+ */
+ total: number;
+ type: InframonitoringtypesResponseTypeDTO;
+ warning?: Querybuildertypesv5QueryWarnDataDTO;
+}
+
export interface InframonitoringtypesHostFilterDTO {
/**
* @type string
@@ -4885,6 +4945,34 @@ export interface InframonitoringtypesPodsDTO {
warning?: Querybuildertypesv5QueryWarnDataDTO;
}
+export interface InframonitoringtypesPostableClustersDTO {
+ /**
+ * @type integer
+ * @format int64
+ */
+ end: number;
+ filter?: Querybuildertypesv5FilterDTO;
+ /**
+ * @type array
+ * @nullable true
+ */
+ groupBy?: Querybuildertypesv5GroupByKeyDTO[] | null;
+ /**
+ * @type integer
+ */
+ limit: number;
+ /**
+ * @type integer
+ */
+ offset?: number;
+ orderBy?: Querybuildertypesv5OrderByDTO;
+ /**
+ * @type integer
+ * @format int64
+ */
+ start: number;
+}
+
export interface InframonitoringtypesPostableHostsDTO {
/**
* @type integer
@@ -9324,6 +9412,14 @@ export type Healthz503 = {
status: string;
};
+export type ListClusters200 = {
+ data: InframonitoringtypesClustersDTO;
+ /**
+ * @type string
+ */
+ status: string;
+};
+
export type ListHosts200 = {
data: InframonitoringtypesHostsDTO;
/**
diff --git a/frontend/src/pages/TraceDetailsV3/TraceFlamegraph/__tests__/computeVisualLayout.test.ts b/frontend/src/pages/TraceDetailsV3/TraceFlamegraph/__tests__/computeVisualLayout.test.ts
index 29d670b34f..53a0eb5ff5 100644
--- a/frontend/src/pages/TraceDetailsV3/TraceFlamegraph/__tests__/computeVisualLayout.test.ts
+++ b/frontend/src/pages/TraceDetailsV3/TraceFlamegraph/__tests__/computeVisualLayout.test.ts
@@ -25,7 +25,7 @@ describe('computeVisualLayout', () => {
it('should handle empty input', () => {
const layout = computeVisualLayout([]);
expect(layout.totalVisualRows).toBe(0);
- expect(layout.visualRows).toEqual([]);
+ expect(layout.visualRows).toStrictEqual([]);
});
it('should handle single root, no children — 1 visual row', () => {
@@ -36,7 +36,7 @@ describe('computeVisualLayout', () => {
});
const layout = computeVisualLayout([[root]]);
expect(layout.totalVisualRows).toBe(1);
- expect(layout.visualRows[0]).toEqual([root]);
+ expect(layout.visualRows[0]).toStrictEqual([root]);
expect(layout.spanToVisualRow['root']).toBe(0);
});
diff --git a/frontend/src/pages/TraceDetailsV3/TraceWaterfall/__tests__/getVisibleSpans.test.ts b/frontend/src/pages/TraceDetailsV3/TraceWaterfall/__tests__/getVisibleSpans.test.ts
index a2aa8eb7c9..efa5a34012 100644
--- a/frontend/src/pages/TraceDetailsV3/TraceWaterfall/__tests__/getVisibleSpans.test.ts
+++ b/frontend/src/pages/TraceDetailsV3/TraceWaterfall/__tests__/getVisibleSpans.test.ts
@@ -44,12 +44,12 @@ function makeSpan(
describe('getVisibleSpans', () => {
it('returns empty array for empty input', () => {
- expect(getVisibleSpans([], new Set())).toEqual([]);
+ expect(getVisibleSpans([], new Set())).toStrictEqual([]);
});
it('returns single root span with no children', () => {
const spans = [makeSpan({ span_id: 'root', level: 0 })];
- expect(getVisibleSpans(spans, new Set())).toEqual(spans);
+ expect(getVisibleSpans(spans, new Set())).toStrictEqual(spans);
});
it('returns all spans for flat tree (all level 0, no children)', () => {
@@ -58,7 +58,7 @@ describe('getVisibleSpans', () => {
makeSpan({ span_id: 'b', level: 0 }),
makeSpan({ span_id: 'c', level: 0 }),
];
- expect(getVisibleSpans(spans, new Set())).toEqual(spans);
+ expect(getVisibleSpans(spans, new Set())).toStrictEqual(spans);
});
it('returns all spans when all parents are expanded', () => {
@@ -68,7 +68,7 @@ describe('getVisibleSpans', () => {
makeSpan({ span_id: 'b', level: 1 }),
];
const uncollapsed = new Set(['root']);
- expect(getVisibleSpans(spans, uncollapsed)).toEqual(spans);
+ expect(getVisibleSpans(spans, uncollapsed)).toStrictEqual(spans);
});
it('hides children when root is collapsed', () => {
@@ -99,7 +99,7 @@ describe('getVisibleSpans', () => {
// root and A expanded, but A is collapsed
const uncollapsed = new Set(['root']); // A not in set → collapsed
const result = getVisibleSpans(spans, uncollapsed);
- expect(result.map((s) => s.span_id)).toEqual(['root', 'A']);
+ expect(result.map((s) => s.span_id)).toStrictEqual(['root', 'A']);
});
it('collapses one subtree while sibling subtree stays visible', () => {
@@ -116,7 +116,7 @@ describe('getVisibleSpans', () => {
// root and childB expanded, childA collapsed
const uncollapsed = new Set(['root', 'childB']);
const result = getVisibleSpans(spans, uncollapsed);
- expect(result.map((s) => s.span_id)).toEqual([
+ expect(result.map((s) => s.span_id)).toStrictEqual([
'root',
'childA', // visible but collapsed
'childB', // visible and expanded
@@ -134,11 +134,11 @@ describe('getVisibleSpans', () => {
// Collapsed
const collapsed = getVisibleSpans(spans, new Set());
- expect(collapsed.map((s) => s.span_id)).toEqual(['root']);
+ expect(collapsed.map((s) => s.span_id)).toStrictEqual(['root']);
// Uncollapsed
const uncollapsed = getVisibleSpans(spans, new Set(['root']));
- expect(uncollapsed.map((s) => s.span_id)).toEqual(['root', 'a', 'b']);
+ expect(uncollapsed.map((s) => s.span_id)).toStrictEqual(['root', 'a', 'b']);
});
it('hides all levels below collapsed span in deeply nested tree', () => {
@@ -154,7 +154,7 @@ describe('getVisibleSpans', () => {
// Expand L0 and L1, collapse L2
const uncollapsed = new Set(['L0', 'L1']);
const result = getVisibleSpans(spans, uncollapsed);
- expect(result.map((s) => s.span_id)).toEqual(['L0', 'L1', 'L2']);
+ expect(result.map((s) => s.span_id)).toStrictEqual(['L0', 'L1', 'L2']);
});
it('shows only root-level spans when all parents are collapsed', () => {
@@ -166,7 +166,7 @@ describe('getVisibleSpans', () => {
];
const uncollapsed = new Set<string>(); // nothing expanded
const result = getVisibleSpans(spans, uncollapsed);
- expect(result.map((s) => s.span_id)).toEqual(['root1', 'root2']);
+ expect(result.map((s) => s.span_id)).toStrictEqual(['root1', 'root2']);
});
it('leaf span in uncollapsed set has no effect', () => {
@@ -177,7 +177,7 @@ describe('getVisibleSpans', () => {
// leaf is in uncollapsed set but has_children=false, should make no difference
const uncollapsed = new Set(['root', 'leaf']);
const result = getVisibleSpans(spans, uncollapsed);
- expect(result).toEqual(spans);
+ expect(result).toStrictEqual(spans);
});
it('handles 10k spans within 50ms', () => {
@@ -218,7 +218,13 @@ describe('getVisibleSpans', () => {
// root and C expanded; A and B collapsed
const uncollapsed = new Set(['root', 'C']);
const result = getVisibleSpans(spans, uncollapsed);
- expect(result.map((s) => s.span_id)).toEqual(['root', 'A', 'B', 'C', 'C1']);
+ expect(result.map((s) => s.span_id)).toStrictEqual([
+ 'root',
+ 'A',
+ 'B',
+ 'C',
+ 'C1',
+ ]);
});
});
@@ -234,7 +240,7 @@ describe('getAncestorSpanIds', () => {
makeSpan({ span_id: 'child', level: 1, parent_span_id: 'root' }),
];
const ancestors = getAncestorSpanIds(spans, 'child');
- expect(ancestors).toEqual(new Set(['root']));
+ expect(ancestors).toStrictEqual(new Set(['root']));
});
it('returns all ancestors for deeply nested span', () => {
@@ -255,7 +261,7 @@ describe('getAncestorSpanIds', () => {
makeSpan({ span_id: 'L3', level: 3, parent_span_id: 'L2' }),
];
const ancestors = getAncestorSpanIds(spans, 'L3');
- expect(ancestors).toEqual(new Set(['L2', 'L1', 'L0']));
+ expect(ancestors).toStrictEqual(new Set(['L2', 'L1', 'L0']));
});
it('returns empty set for unknown span', () => {
diff --git a/pkg/apiserver/signozapiserver/inframonitoring.go b/pkg/apiserver/signozapiserver/inframonitoring.go
index ce2a18f5a6..ed3a444165 100644
--- a/pkg/apiserver/signozapiserver/inframonitoring.go
+++ b/pkg/apiserver/signozapiserver/inframonitoring.go
@@ -86,5 +86,24 @@ func (provider *provider) addInfraMonitoringRoutes(router *mux.Router) error {
return err
}
+ if err := router.Handle("/api/v2/infra_monitoring/clusters", handler.New(
+ provider.authzMiddleware.ViewAccess(provider.infraMonitoringHandler.ListClusters),
+ handler.OpenAPIDef{
+ ID: "ListClusters",
+ Tags: []string{"inframonitoring"},
+ Summary: "List Clusters for Infra Monitoring",
+ Description: "Returns a paginated list of Kubernetes clusters with key aggregated metrics derived by summing per-node values within the group: CPU usage, CPU allocatable, memory working set, memory allocatable. Each row also reports per-group nodeCountsByReadiness ({ ready, notReady } from each node's latest k8s.node.condition_ready value) and per-group podCountsByPhase ({ pending, running, succeeded, failed, unknown } from each pod's latest k8s.pod.phase value). Each cluster includes metadata attributes (k8s.cluster.name). The response type is 'list' for the default k8s.cluster.name grouping or 'grouped_list' for custom groupBy keys; in both modes every row aggregates nodes and pods in the group. Supports filtering via a filter expression, custom groupBy, ordering by cpu / cpu_allocatable / memory / memory_allocatable, and pagination via offset/limit. Also reports missing required metrics and whether the requested time range falls before the data retention boundary. Numeric metric fields (clusterCPU, clusterCPUAllocatable, clusterMemory, clusterMemoryAllocatable) return -1 as a sentinel when no data is available for that field.",
+ Request: new(inframonitoringtypes.PostableClusters),
+ RequestContentType: "application/json",
+ Response: new(inframonitoringtypes.Clusters),
+ ResponseContentType: "application/json",
+ SuccessStatusCode: http.StatusOK,
+ ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized},
+ Deprecated: false,
+ SecuritySchemes: newSecuritySchemes(types.RoleViewer),
+ })).Methods(http.MethodPost).GetError(); err != nil {
+ return err
+ }
+
return nil
}
diff --git a/pkg/modules/inframonitoring/implinframonitoring/clusters.go b/pkg/modules/inframonitoring/implinframonitoring/clusters.go
new file mode 100644
index 0000000000..61bdb32e21
--- /dev/null
+++ b/pkg/modules/inframonitoring/implinframonitoring/clusters.go
@@ -0,0 +1,140 @@
+package implinframonitoring
+
+import (
+ "context"
+ "slices"
+
+ "github.com/SigNoz/signoz/pkg/types/inframonitoringtypes"
+ qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
+ "github.com/SigNoz/signoz/pkg/valuer"
+)
+
+// buildClusterRecords assembles the page records. Node condition counts and
+// pod phase counts come from the respective per-group maps in both modes;
+// every row is a group of nodes+pods, so there's no per-row "current state"
+// concept (analogous to namespaces).
+func buildClusterRecords(
+ resp *qbtypes.QueryRangeResponse,
+ pageGroups []map[string]string,
+ groupBy []qbtypes.GroupByKey,
+ metadataMap map[string]map[string]string,
+ nodeConditionCountsMap map[string]nodeConditionCounts,
+ podPhaseCountsMap map[string]podPhaseCounts,
+) []inframonitoringtypes.ClusterRecord {
+ metricsMap := parseFullQueryResponse(resp, groupBy)
+
+ records := make([]inframonitoringtypes.ClusterRecord, 0, len(pageGroups))
+ for _, labels := range pageGroups {
+ compositeKey := compositeKeyFromLabels(labels, groupBy)
+ clusterName := labels[clusterNameAttrKey]
+
+ record := inframonitoringtypes.ClusterRecord{ // initialize with default values
+ ClusterName: clusterName,
+ ClusterCPU: -1,
+ ClusterCPUAllocatable: -1,
+ ClusterMemory: -1,
+ ClusterMemoryAllocatable: -1,
+ Meta: map[string]string{},
+ }
+
+ if metrics, ok := metricsMap[compositeKey]; ok {
+ if v, exists := metrics["A"]; exists {
+ record.ClusterCPU = v
+ }
+ if v, exists := metrics["B"]; exists {
+ record.ClusterCPUAllocatable = v
+ }
+ if v, exists := metrics["C"]; exists {
+ record.ClusterMemory = v
+ }
+ if v, exists := metrics["D"]; exists {
+ record.ClusterMemoryAllocatable = v
+ }
+ }
+
+ if conditionCountsForGroup, ok := nodeConditionCountsMap[compositeKey]; ok {
+ record.NodeCountsByReadiness = inframonitoringtypes.NodeCountsByReadiness{
+ Ready: conditionCountsForGroup.Ready,
+ NotReady: conditionCountsForGroup.NotReady,
+ }
+ }
+
+ if phaseCountsForGroup, ok := podPhaseCountsMap[compositeKey]; ok {
+ record.PodCountsByPhase = inframonitoringtypes.PodCountsByPhase{
+ Pending: phaseCountsForGroup.Pending,
+ Running: phaseCountsForGroup.Running,
+ Succeeded: phaseCountsForGroup.Succeeded,
+ Failed: phaseCountsForGroup.Failed,
+ Unknown: phaseCountsForGroup.Unknown,
+ }
+ }
+
+ if attrs, ok := metadataMap[compositeKey]; ok {
+ for k, v := range attrs {
+ record.Meta[k] = v
+ }
+ }
+
+ records = append(records, record)
+ }
+ return records
+}
+
+func (m *module) getTopClusterGroups(
+ ctx context.Context,
+ orgID valuer.UUID,
+ req *inframonitoringtypes.PostableClusters,
+ metadataMap map[string]map[string]string,
+) ([]map[string]string, error) {
+ orderByKey := req.OrderBy.Key.Name
+ queryNamesForOrderBy := orderByToClustersQueryNames[orderByKey]
+ rankingQueryName := queryNamesForOrderBy[len(queryNamesForOrderBy)-1]
+
+ topReq := &qbtypes.QueryRangeRequest{
+ Start: uint64(req.Start),
+ End: uint64(req.End),
+ RequestType: qbtypes.RequestTypeScalar,
+ CompositeQuery: qbtypes.CompositeQuery{
+ Queries: make([]qbtypes.QueryEnvelope, 0, len(queryNamesForOrderBy)),
+ },
+ }
+
+ for _, envelope := range m.newClustersTableListQuery().CompositeQuery.Queries {
+ if !slices.Contains(queryNamesForOrderBy, envelope.GetQueryName()) {
+ continue
+ }
+ copied := envelope
+ if copied.Type == qbtypes.QueryTypeBuilder {
+ existingExpr := ""
+ if f := copied.GetFilter(); f != nil {
+ existingExpr = f.Expression
+ }
+ reqFilterExpr := ""
+ if req.Filter != nil {
+ reqFilterExpr = req.Filter.Expression
+ }
+ merged := mergeFilterExpressions(existingExpr, reqFilterExpr)
+ copied.SetFilter(&qbtypes.Filter{Expression: merged})
+ copied.SetGroupBy(req.GroupBy)
+ }
+ topReq.CompositeQuery.Queries = append(topReq.CompositeQuery.Queries, copied)
+ }
+
+ resp, err := m.querier.QueryRange(ctx, orgID, topReq)
+ if err != nil {
+ return nil, err
+ }
+
+ allMetricGroups := parseAndSortGroups(resp, rankingQueryName, req.GroupBy, req.OrderBy.Direction)
+ return paginateWithBackfill(allMetricGroups, metadataMap, req.GroupBy, req.Offset, req.Limit), nil
+}
+
+func (m *module) getClustersTableMetadata(ctx context.Context, req *inframonitoringtypes.PostableClusters) (map[string]map[string]string, error) {
+ var nonGroupByAttrs []string
+ for _, key := range clusterAttrKeysForMetadata {
+ if !isKeyInGroupByAttrs(req.GroupBy, key) {
+ nonGroupByAttrs = append(nonGroupByAttrs, key)
+ }
+ }
+ return m.getMetadata(ctx, clustersTableMetricNamesList, req.GroupBy, nonGroupByAttrs, req.Filter, req.Start, req.End)
+}
diff --git a/pkg/modules/inframonitoring/implinframonitoring/clusters_constants.go b/pkg/modules/inframonitoring/implinframonitoring/clusters_constants.go
new file mode 100644
index 0000000000..6c69e8db85
--- /dev/null
+++ b/pkg/modules/inframonitoring/implinframonitoring/clusters_constants.go
@@ -0,0 +1,140 @@
+package implinframonitoring
+
+import (
+ "github.com/SigNoz/signoz/pkg/types/inframonitoringtypes"
+ "github.com/SigNoz/signoz/pkg/types/metrictypes"
+ qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
+)
+
+// TODO(nikhilmantri0902): change to k8s.cluster.uid after showing the missing
+// data banner. Carried forward from v1 (see k8sClusterUIDAttrKey in
+// pkg/query-service/app/inframetrics/clusters.go).
+const clusterNameAttrKey = "k8s.cluster.name"
+
+var clusterNameGroupByKey = qbtypes.GroupByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: clusterNameAttrKey,
+ FieldContext: telemetrytypes.FieldContextResource,
+ FieldDataType: telemetrytypes.FieldDataTypeString,
+ },
+}
+
+// clustersTableMetricNamesList drives the existence/retention check.
+// Includes k8s.node.condition_ready and k8s.pod.phase so the response
+// short-circuits cleanly when a cluster doesn't ship those metrics — even
+// though they aren't part of the QB composite query (they're queried separately
+// via getPerGroupNodeConditionCounts and getPerGroupPodPhaseCounts).
+var clustersTableMetricNamesList = []string{
+ "k8s.node.cpu.usage",
+ "k8s.node.allocatable_cpu",
+ "k8s.node.memory.working_set",
+ "k8s.node.allocatable_memory",
+ "k8s.node.condition_ready", //TODO(nikhilmantri0902): should these metrics be used to count groups k8s.node.condition_ready and k8s.pod.phase
+ "k8s.pod.phase",
+}
+
+var clusterAttrKeysForMetadata = []string{
+ "k8s.cluster.name",
+}
+
+var orderByToClustersQueryNames = map[string][]string{
+ inframonitoringtypes.ClustersOrderByCPU: {"A"},
+ inframonitoringtypes.ClustersOrderByCPUAllocatable: {"B"},
+ inframonitoringtypes.ClustersOrderByMemory: {"C"},
+ inframonitoringtypes.ClustersOrderByMemoryAllocatable: {"D"},
+}
+
+// newClustersTableListQuery builds the composite QB v5 request for the clusters list.
+// Cluster-scope metrics are derived by summing per-node metrics within the
+// group (default group: k8s.cluster.name). Node condition counts and pod phase
+// counts are derived separately via getPerGroupNodeConditionCounts and
+// getPerGroupPodPhaseCounts respectively (works for both list and grouped_list
+// modes), so neither is included here. Query letters A/B/C/D mirror the v1
+// implementation and the v2 nodes list.
+func (m *module) newClustersTableListQuery() *qbtypes.QueryRangeRequest {
+ queries := []qbtypes.QueryEnvelope{
+ // Query A: CPU usage — sum of node CPU within the group.
+ {
+ Type: qbtypes.QueryTypeBuilder,
+ Spec: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{
+ Name: "A",
+ Signal: telemetrytypes.SignalMetrics,
+ Aggregations: []qbtypes.MetricAggregation{
+ {
+ MetricName: "k8s.node.cpu.usage",
+ TimeAggregation: metrictypes.TimeAggregationAvg,
+ SpaceAggregation: metrictypes.SpaceAggregationSum,
+ ReduceTo: qbtypes.ReduceToAvg,
+ },
+ },
+ GroupBy: []qbtypes.GroupByKey{clusterNameGroupByKey},
+ Disabled: false,
+ },
+ },
+ // Query B: CPU allocatable — sum of node allocatable CPU within the group.
+ // TimeAggregationLatest is the closest v5 equivalent of v1's AnyLast;
+ // allocatable values change rarely so divergence in practice is negligible.
+ {
+ Type: qbtypes.QueryTypeBuilder,
+ Spec: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{
+ Name: "B",
+ Signal: telemetrytypes.SignalMetrics,
+ Aggregations: []qbtypes.MetricAggregation{
+ {
+ MetricName: "k8s.node.allocatable_cpu",
+ TimeAggregation: metrictypes.TimeAggregationLatest,
+ SpaceAggregation: metrictypes.SpaceAggregationSum,
+ ReduceTo: qbtypes.ReduceToAvg,
+ },
+ },
+ GroupBy: []qbtypes.GroupByKey{clusterNameGroupByKey},
+ Disabled: false,
+ },
+ },
+ // Query C: Memory working set — sum of node memory within the group.
+ {
+ Type: qbtypes.QueryTypeBuilder,
+ Spec: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{
+ Name: "C",
+ Signal: telemetrytypes.SignalMetrics,
+ Aggregations: []qbtypes.MetricAggregation{
+ {
+ MetricName: "k8s.node.memory.working_set",
+ TimeAggregation: metrictypes.TimeAggregationAvg,
+ SpaceAggregation: metrictypes.SpaceAggregationSum,
+ ReduceTo: qbtypes.ReduceToAvg,
+ },
+ },
+ GroupBy: []qbtypes.GroupByKey{clusterNameGroupByKey},
+ Disabled: false,
+ },
+ },
+ // Query D: Memory allocatable — sum of node allocatable memory within the group.
+ // Same Latest caveat as Query B.
+ {
+ Type: qbtypes.QueryTypeBuilder,
+ Spec: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{
+ Name: "D",
+ Signal: telemetrytypes.SignalMetrics,
+ Aggregations: []qbtypes.MetricAggregation{
+ {
+ MetricName: "k8s.node.allocatable_memory",
+ TimeAggregation: metrictypes.TimeAggregationLatest,
+ SpaceAggregation: metrictypes.SpaceAggregationSum,
+ ReduceTo: qbtypes.ReduceToAvg,
+ },
+ },
+ GroupBy: []qbtypes.GroupByKey{clusterNameGroupByKey},
+ Disabled: false,
+ },
+ },
+ }
+
+ return &qbtypes.QueryRangeRequest{
+ RequestType: qbtypes.RequestTypeScalar,
+ CompositeQuery: qbtypes.CompositeQuery{
+ Queries: queries,
+ },
+ }
+}
diff --git a/pkg/modules/inframonitoring/implinframonitoring/handler.go b/pkg/modules/inframonitoring/implinframonitoring/handler.go
index f165491d97..c7ea90d150 100644
--- a/pkg/modules/inframonitoring/implinframonitoring/handler.go
+++ b/pkg/modules/inframonitoring/implinframonitoring/handler.go
@@ -117,3 +117,27 @@ func (h *handler) ListNamespaces(rw http.ResponseWriter, req *http.Request) {
render.Success(rw, http.StatusOK, result)
}
+
+func (h *handler) ListClusters(rw http.ResponseWriter, req *http.Request) {
+ claims, err := authtypes.ClaimsFromContext(req.Context())
+ if err != nil {
+ render.Error(rw, err)
+ return
+ }
+
+ orgID := valuer.MustNewUUID(claims.OrgID)
+
+ var parsedReq inframonitoringtypes.PostableClusters
+ if err := binding.JSON.BindBody(req.Body, &parsedReq); err != nil {
+ render.Error(rw, err)
+ return
+ }
+
+ result, err := h.module.ListClusters(req.Context(), orgID, &parsedReq)
+ if err != nil {
+ render.Error(rw, err)
+ return
+ }
+
+ render.Success(rw, http.StatusOK, result)
+}
diff --git a/pkg/modules/inframonitoring/implinframonitoring/module.go b/pkg/modules/inframonitoring/implinframonitoring/module.go
index ed432b12d9..c15469a13a 100644
--- a/pkg/modules/inframonitoring/implinframonitoring/module.go
+++ b/pkg/modules/inframonitoring/implinframonitoring/module.go
@@ -426,3 +426,105 @@ func (m *module) ListNamespaces(ctx context.Context, orgID valuer.UUID, req *inf
return resp, nil
}
+
+func (m *module) ListClusters(ctx context.Context, orgID valuer.UUID, req *inframonitoringtypes.PostableClusters) (*inframonitoringtypes.Clusters, error) {
+ if err := req.Validate(); err != nil {
+ return nil, err
+ }
+
+ resp := &inframonitoringtypes.Clusters{}
+
+ if req.OrderBy == nil {
+ req.OrderBy = &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: inframonitoringtypes.ClustersOrderByCPU,
+ },
+ },
+ Direction: qbtypes.OrderDirectionDesc,
+ }
+ }
+
+ if len(req.GroupBy) == 0 {
+ req.GroupBy = []qbtypes.GroupByKey{clusterNameGroupByKey}
+ resp.Type = inframonitoringtypes.ResponseTypeList
+ } else {
+ resp.Type = inframonitoringtypes.ResponseTypeGroupedList
+ }
+
+ missingMetrics, minFirstReportedUnixMilli, err := m.getMetricsExistenceAndEarliestTime(ctx, clustersTableMetricNamesList)
+ if err != nil {
+ return nil, err
+ }
+ if len(missingMetrics) > 0 {
+ resp.RequiredMetricsCheck = inframonitoringtypes.RequiredMetricsCheck{MissingMetrics: missingMetrics}
+ resp.Records = []inframonitoringtypes.ClusterRecord{}
+ resp.Total = 0
+ return resp, nil
+ }
+ if req.End < int64(minFirstReportedUnixMilli) {
+ resp.EndTimeBeforeRetention = true
+ resp.Records = []inframonitoringtypes.ClusterRecord{}
+ resp.Total = 0
+ return resp, nil
+ }
+ resp.RequiredMetricsCheck = inframonitoringtypes.RequiredMetricsCheck{MissingMetrics: []string{}}
+
+ metadataMap, err := m.getClustersTableMetadata(ctx, req)
+ if err != nil {
+ return nil, err
+ }
+
+ resp.Total = len(metadataMap)
+
+ pageGroups, err := m.getTopClusterGroups(ctx, orgID, req, metadataMap)
+ if err != nil {
+ return nil, err
+ }
+
+ if len(pageGroups) == 0 {
+ resp.Records = []inframonitoringtypes.ClusterRecord{}
+ return resp, nil
+ }
+
+ filterExpr := ""
+ if req.Filter != nil {
+ filterExpr = req.Filter.Expression
+ }
+
+ fullQueryReq := buildFullQueryRequest(req.Start, req.End, filterExpr, req.GroupBy, pageGroups, m.newClustersTableListQuery())
+ queryResp, err := m.querier.QueryRange(ctx, orgID, fullQueryReq)
+ if err != nil {
+ return nil, err
+ }
+
+ // Reuse the nodes condition-counts CTE function via a temp struct — it reads only
+ // Start/End/Filter/GroupBy from PostableNodes. With default groupBy
+ // [k8s.cluster.name], counts are bucketed per cluster; with a custom groupBy,
+ // they aggregate across clusters in that group.
+ nodeConditionCountsMap, err := m.getPerGroupNodeConditionCounts(ctx, &inframonitoringtypes.PostableNodes{
+ Start: req.Start,
+ End: req.End,
+ Filter: req.Filter,
+ GroupBy: req.GroupBy,
+ }, pageGroups)
+ if err != nil {
+ return nil, err
+ }
+
+ // Same pattern for pod phase counts via PostablePods shim.
+ podPhaseCountsMap, err := m.getPerGroupPodPhaseCounts(ctx, &inframonitoringtypes.PostablePods{
+ Start: req.Start,
+ End: req.End,
+ Filter: req.Filter,
+ GroupBy: req.GroupBy,
+ }, pageGroups)
+ if err != nil {
+ return nil, err
+ }
+
+ resp.Records = buildClusterRecords(queryResp, pageGroups, req.GroupBy, metadataMap, nodeConditionCountsMap, podPhaseCountsMap)
+ resp.Warning = queryResp.Warning
+
+ return resp, nil
+}
diff --git a/pkg/modules/inframonitoring/inframonitoring.go b/pkg/modules/inframonitoring/inframonitoring.go
index 9b02f8adfb..b2f5fa0023 100644
--- a/pkg/modules/inframonitoring/inframonitoring.go
+++ b/pkg/modules/inframonitoring/inframonitoring.go
@@ -13,6 +13,7 @@ type Handler interface {
ListPods(http.ResponseWriter, *http.Request)
ListNodes(http.ResponseWriter, *http.Request)
ListNamespaces(http.ResponseWriter, *http.Request)
+ ListClusters(http.ResponseWriter, *http.Request)
}
type Module interface {
@@ -20,4 +21,5 @@ type Module interface {
ListPods(ctx context.Context, orgID valuer.UUID, req *inframonitoringtypes.PostablePods) (*inframonitoringtypes.Pods, error)
ListNodes(ctx context.Context, orgID valuer.UUID, req *inframonitoringtypes.PostableNodes) (*inframonitoringtypes.Nodes, error)
ListNamespaces(ctx context.Context, orgID valuer.UUID, req *inframonitoringtypes.PostableNamespaces) (*inframonitoringtypes.Namespaces, error)
+ ListClusters(ctx context.Context, orgID valuer.UUID, req *inframonitoringtypes.PostableClusters) (*inframonitoringtypes.Clusters, error)
}
diff --git a/pkg/types/inframonitoringtypes/clusters.go b/pkg/types/inframonitoringtypes/clusters.go
new file mode 100644
index 0000000000..bd0f3cc7ad
--- /dev/null
+++ b/pkg/types/inframonitoringtypes/clusters.go
@@ -0,0 +1,105 @@
+package inframonitoringtypes
+
+import (
+ "encoding/json"
+ "slices"
+
+ "github.com/SigNoz/signoz/pkg/errors"
+ qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
+)
+
+type Clusters struct {
+ Type ResponseType `json:"type" required:"true"`
+ Records []ClusterRecord `json:"records" required:"true"`
+ Total int `json:"total" required:"true"`
+ RequiredMetricsCheck RequiredMetricsCheck `json:"requiredMetricsCheck" required:"true"`
+ EndTimeBeforeRetention bool `json:"endTimeBeforeRetention" required:"true"`
+ Warning *qbtypes.QueryWarnData `json:"warning,omitempty"`
+}
+
+type ClusterRecord struct {
+ // TODO(nikhilmantri0902): once the underlying attr key is migrated to
+ // k8s.cluster.uid (see clusterNameAttrKey TODO in implinframonitoring),
+ // surface ClusterUID alongside (or replace) ClusterName.
+ ClusterName string `json:"clusterName" required:"true"`
+ ClusterCPU float64 `json:"clusterCPU" required:"true"`
+ ClusterCPUAllocatable float64 `json:"clusterCPUAllocatable" required:"true"`
+ ClusterMemory float64 `json:"clusterMemory" required:"true"`
+ ClusterMemoryAllocatable float64 `json:"clusterMemoryAllocatable" required:"true"`
+ NodeCountsByReadiness NodeCountsByReadiness `json:"nodeCountsByReadiness" required:"true"`
+ PodCountsByPhase PodCountsByPhase `json:"podCountsByPhase" required:"true"`
+ Meta map[string]string `json:"meta" required:"true"`
+}
+
+// PostableClusters is the request body for the v2 clusters list API.
+type PostableClusters struct {
+ Start int64 `json:"start" required:"true"`
+ End int64 `json:"end" required:"true"`
+ Filter *qbtypes.Filter `json:"filter"`
+ GroupBy []qbtypes.GroupByKey `json:"groupBy"`
+ OrderBy *qbtypes.OrderBy `json:"orderBy"`
+ Offset int `json:"offset"`
+ Limit int `json:"limit" required:"true"`
+}
+
+// Validate ensures PostableClusters contains acceptable values.
+func (req *PostableClusters) Validate() error {
+ if req == nil {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "request is nil")
+ }
+
+ if req.Start <= 0 {
+ return errors.NewInvalidInputf(
+ errors.CodeInvalidInput,
+ "invalid start time %d: start must be greater than 0",
+ req.Start,
+ )
+ }
+
+ if req.End <= 0 {
+ return errors.NewInvalidInputf(
+ errors.CodeInvalidInput,
+ "invalid end time %d: end must be greater than 0",
+ req.End,
+ )
+ }
+
+ if req.Start >= req.End {
+ return errors.NewInvalidInputf(
+ errors.CodeInvalidInput,
+ "invalid time range: start (%d) must be less than end (%d)",
+ req.Start,
+ req.End,
+ )
+ }
+
+ if req.Limit < 1 || req.Limit > 5000 {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "limit must be between 1 and 5000")
+ }
+
+ if req.Offset < 0 {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "offset cannot be negative")
+ }
+
+ if req.OrderBy != nil {
+ if !slices.Contains(ClustersValidOrderByKeys, req.OrderBy.Key.Name) {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "invalid order by key: %s", req.OrderBy.Key.Name)
+ }
+ if req.OrderBy.Direction != qbtypes.OrderDirectionAsc && req.OrderBy.Direction != qbtypes.OrderDirectionDesc {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "invalid order by direction: %s", req.OrderBy.Direction)
+ }
+ }
+
+ return nil
+}
+
+// UnmarshalJSON validates input immediately after decoding.
+func (req *PostableClusters) UnmarshalJSON(data []byte) error {
+ type raw PostableClusters
+ var decoded raw
+ if err := json.Unmarshal(data, &decoded); err != nil {
+ return err
+ }
+ *req = PostableClusters(decoded)
+ return req.Validate()
+}
diff --git a/pkg/types/inframonitoringtypes/clusters_constants.go b/pkg/types/inframonitoringtypes/clusters_constants.go
new file mode 100644
index 0000000000..c7881c8645
--- /dev/null
+++ b/pkg/types/inframonitoringtypes/clusters_constants.go
@@ -0,0 +1,15 @@
+package inframonitoringtypes
+
+const (
+ ClustersOrderByCPU = "cpu"
+ ClustersOrderByCPUAllocatable = "cpu_allocatable"
+ ClustersOrderByMemory = "memory"
+ ClustersOrderByMemoryAllocatable = "memory_allocatable"
+)
+
+var ClustersValidOrderByKeys = []string{
+ ClustersOrderByCPU,
+ ClustersOrderByCPUAllocatable,
+ ClustersOrderByMemory,
+ ClustersOrderByMemoryAllocatable,
+}
diff --git a/pkg/types/inframonitoringtypes/clusters_test.go b/pkg/types/inframonitoringtypes/clusters_test.go
new file mode 100644
index 0000000000..96155bda10
--- /dev/null
+++ b/pkg/types/inframonitoringtypes/clusters_test.go
@@ -0,0 +1,291 @@
+package inframonitoringtypes
+
+import (
+ "testing"
+
+ "github.com/SigNoz/signoz/pkg/errors"
+ qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
+ "github.com/SigNoz/signoz/pkg/valuer"
+ "github.com/stretchr/testify/require"
+)
+
+func TestPostableClusters_Validate(t *testing.T) {
+ tests := []struct {
+ name string
+ req *PostableClusters
+ wantErr bool
+ }{
+ {
+ name: "valid request",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ },
+ wantErr: false,
+ },
+ {
+ name: "nil request",
+ req: nil,
+ wantErr: true,
+ },
+ {
+ name: "start time zero",
+ req: &PostableClusters{
+ Start: 0,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ },
+ wantErr: true,
+ },
+ {
+ name: "start time negative",
+ req: &PostableClusters{
+ Start: -1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ },
+ wantErr: true,
+ },
+ {
+ name: "end time zero",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 0,
+ Limit: 100,
+ Offset: 0,
+ },
+ wantErr: true,
+ },
+ {
+ name: "start time greater than end time",
+ req: &PostableClusters{
+ Start: 2000,
+ End: 1000,
+ Limit: 100,
+ Offset: 0,
+ },
+ wantErr: true,
+ },
+ {
+ name: "start time equal to end time",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 1000,
+ Limit: 100,
+ Offset: 0,
+ },
+ wantErr: true,
+ },
+ {
+ name: "limit zero",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 0,
+ Offset: 0,
+ },
+ wantErr: true,
+ },
+ {
+ name: "limit negative",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: -10,
+ Offset: 0,
+ },
+ wantErr: true,
+ },
+ {
+ name: "limit exceeds max",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 5001,
+ Offset: 0,
+ },
+ wantErr: true,
+ },
+ {
+ name: "offset negative",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: -5,
+ },
+ wantErr: true,
+ },
+ {
+ name: "orderBy nil is valid",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ },
+ wantErr: false,
+ },
+ {
+ name: "orderBy with valid key cpu and direction asc",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ OrderBy: &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: ClustersOrderByCPU,
+ },
+ },
+ Direction: qbtypes.OrderDirectionAsc,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "orderBy with valid key cpu_allocatable and direction desc",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ OrderBy: &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: ClustersOrderByCPUAllocatable,
+ },
+ },
+ Direction: qbtypes.OrderDirectionDesc,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "orderBy with valid key memory and direction desc",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ OrderBy: &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: ClustersOrderByMemory,
+ },
+ },
+ Direction: qbtypes.OrderDirectionDesc,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "orderBy with valid key memory_allocatable and direction asc",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ OrderBy: &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: ClustersOrderByMemoryAllocatable,
+ },
+ },
+ Direction: qbtypes.OrderDirectionAsc,
+ },
+ },
+ wantErr: false,
+ },
+ {
+ name: "orderBy with condition key is rejected",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ OrderBy: &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: "condition",
+ },
+ },
+ Direction: qbtypes.OrderDirectionDesc,
+ },
+ },
+ wantErr: true,
+ },
+ {
+ name: "orderBy with pod_phase key is rejected",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ OrderBy: &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: "pod_phase",
+ },
+ },
+ Direction: qbtypes.OrderDirectionDesc,
+ },
+ },
+ wantErr: true,
+ },
+ {
+ name: "orderBy with invalid key",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ OrderBy: &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: "unknown",
+ },
+ },
+ Direction: qbtypes.OrderDirectionDesc,
+ },
+ },
+ wantErr: true,
+ },
+ {
+ name: "orderBy with valid key but invalid direction",
+ req: &PostableClusters{
+ Start: 1000,
+ End: 2000,
+ Limit: 100,
+ Offset: 0,
+ OrderBy: &qbtypes.OrderBy{
+ Key: qbtypes.OrderByKey{
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: ClustersOrderByMemory,
+ },
+ },
+ Direction: qbtypes.OrderDirection{String: valuer.NewString("invalid")},
+ },
+ },
+ wantErr: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ err := tt.req.Validate()
+ if tt.wantErr {
+ require.Error(t, err)
+ require.True(t, errors.Ast(err, errors.TypeInvalidInput), "expected error to be of type InvalidInput")
+ } else {
+ require.NoError(t, err)
+ }
+ })
+ }
+}