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
feat(infra-monitoring): v2 namespaces list api (#11131)
Nikhil Mantrif0b533b198c3
diff --git a/docs/api/openapi.yml b/docs/api/openapi.ymlindex 2c65900de5..2f664b21ff 100644--- a/docs/api/openapi.yml+++ b/docs/api/openapi.yml@@ -2599,6 +2599,54 @@ components:- requiredMetricsCheck- endTimeBeforeRetentiontype: object+ InframonitoringtypesNamespaceRecord:+ properties:+ meta:+ additionalProperties:+ type: string+ nullable: true+ type: object+ namespaceCPU:+ format: double+ type: number+ namespaceMemory:+ format: double+ type: number+ namespaceName:+ type: string+ podCountsByPhase:+ $ref: '#/components/schemas/InframonitoringtypesPodCountsByPhase'+ required:+ - namespaceName+ - namespaceCPU+ - namespaceMemory+ - podCountsByPhase+ - meta+ type: object+ InframonitoringtypesNamespaces:+ properties:+ endTimeBeforeRetention:+ type: boolean+ records:+ items:+ $ref: '#/components/schemas/InframonitoringtypesNamespaceRecord'+ 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: objectInframonitoringtypesNodeCondition:enum:- ready@@ -2802,6 +2850,32 @@ components:- end- limittype: object+ InframonitoringtypesPostableNamespaces:+ 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: objectInframonitoringtypesPostableNodes:properties:end:@@ -11740,6 +11814,74 @@ paths:summary: List Hosts for Infra Monitoringtags:- inframonitoring+ /api/v2/infra_monitoring/namespaces:+ post:+ deprecated: false+ description: 'Returns a paginated list of Kubernetes namespaces with key aggregated+ pod metrics: CPU usage and memory working set (summed across pods in the group),+ plus per-group podCountsByPhase ({ pending, running, succeeded, failed, unknown+ } from each pod''s latest k8s.pod.phase value in the window). Each namespace+ includes metadata attributes (k8s.namespace.name, k8s.cluster.name). The response+ type is ''list'' for the default k8s.namespace.name grouping or ''grouped_list''+ for custom groupBy keys; in both modes every row aggregates pods in the group.+ Supports filtering via a filter expression, custom groupBy, ordering by cpu+ / memory, 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 (namespaceCPU, namespaceMemory) return -1 as a sentinel+ when no data is available for that field.'+ operationId: ListNamespaces+ requestBody:+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/InframonitoringtypesPostableNamespaces'+ responses:+ "200":+ content:+ application/json:+ schema:+ properties:+ data:+ $ref: '#/components/schemas/InframonitoringtypesNamespaces'+ 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 Namespaces for Infra Monitoring+ tags:+ - inframonitoring/api/v2/infra_monitoring/nodes:post:deprecated: falsediff --git a/frontend/src/api/generated/services/inframonitoring/index.ts b/frontend/src/api/generated/services/inframonitoring/index.tsindex 149125ed3f..548b771c07 100644--- a/frontend/src/api/generated/services/inframonitoring/index.ts+++ b/frontend/src/api/generated/services/inframonitoring/index.ts@@ -13,9 +13,11 @@ import type {import type {InframonitoringtypesPostableHostsDTO,+ InframonitoringtypesPostableNamespacesDTO,InframonitoringtypesPostableNodesDTO,InframonitoringtypesPostablePodsDTO,ListHosts200,+ ListNamespaces200,ListNodes200,ListPods200,RenderErrorResponseDTO,@@ -108,6 +110,90 @@ export const useListHosts = <return useMutation(mutationOptions);};+/**+ * Returns a paginated list of Kubernetes namespaces with key aggregated pod metrics: CPU usage and memory working set (summed across pods in the group), plus per-group podCountsByPhase ({ pending, running, succeeded, failed, unknown } from each pod's latest k8s.pod.phase value in the window). Each namespace includes metadata attributes (k8s.namespace.name, k8s.cluster.name). The response type is 'list' for the default k8s.namespace.name grouping or 'grouped_list' for custom groupBy keys; in both modes every row aggregates pods in the group. Supports filtering via a filter expression, custom groupBy, ordering by cpu / memory, 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 (namespaceCPU, namespaceMemory) return -1 as a sentinel when no data is available for that field.+ * @summary List Namespaces for Infra Monitoring+ */+export const listNamespaces = (+ inframonitoringtypesPostableNamespacesDTO: BodyType<InframonitoringtypesPostableNamespacesDTO>,+ signal?: AbortSignal,+) => {+ return GeneratedAPIInstance<ListNamespaces200>({+ url: `/api/v2/infra_monitoring/namespaces`,+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ data: inframonitoringtypesPostableNamespacesDTO,+ signal,+ });+};++export const getListNamespacesMutationOptions = <+ TError = ErrorType<RenderErrorResponseDTO>,+ TContext = unknown,+>(options?: {+ mutation?: UseMutationOptions<+ Awaited<ReturnType<typeof listNamespaces>>,+ TError,+ { data: BodyType<InframonitoringtypesPostableNamespacesDTO> },+ TContext+ >;+}): UseMutationOptions<+ Awaited<ReturnType<typeof listNamespaces>>,+ TError,+ { data: BodyType<InframonitoringtypesPostableNamespacesDTO> },+ TContext+> => {+ const mutationKey = ['listNamespaces'];+ 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 listNamespaces>>,+ { data: BodyType<InframonitoringtypesPostableNamespacesDTO> }+ > = (props) => {+ const { data } = props ?? {};++ return listNamespaces(data);+ };++ return { mutationFn, ...mutationOptions };+};++export type ListNamespacesMutationResult = NonNullable<+ Awaited<ReturnType<typeof listNamespaces>>+>;+export type ListNamespacesMutationBody =+ BodyType<InframonitoringtypesPostableNamespacesDTO>;+export type ListNamespacesMutationError = ErrorType<RenderErrorResponseDTO>;++/**+ * @summary List Namespaces for Infra Monitoring+ */+export const useListNamespaces = <+ TError = ErrorType<RenderErrorResponseDTO>,+ TContext = unknown,+>(options?: {+ mutation?: UseMutationOptions<+ Awaited<ReturnType<typeof listNamespaces>>,+ TError,+ { data: BodyType<InframonitoringtypesPostableNamespacesDTO> },+ TContext+ >;+}): UseMutationResult<+ Awaited<ReturnType<typeof listNamespaces>>,+ TError,+ { data: BodyType<InframonitoringtypesPostableNamespacesDTO> },+ TContext+> => {+ const mutationOptions = getListNamespacesMutationOptions(options);++ return useMutation(mutationOptions);+};/*** Returns a paginated list of Kubernetes nodes with key metrics: CPU usage, CPU allocatable, memory working set, memory allocatable, per-group nodeCountsByReadiness ({ ready, notReady } from each node's latest k8s.node.condition_ready in the window) and per-group podCountsByPhase ({ pending, running, succeeded, failed, unknown } for pods scheduled on the listed nodes). Each node includes metadata attributes (k8s.node.uid, k8s.cluster.name). The response type is 'list' for the default k8s.node.name grouping (each row is one node with its current condition string: ready / not_ready / no_data) or 'grouped_list' for custom groupBy keys (each row aggregates nodes in the group; condition stays no_data). 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 (nodeCPU, nodeCPUAllocatable, nodeMemory, nodeMemoryAllocatable) return -1 as a sentinel when no data is available for that field.* @summary List Nodes for Infra Monitoringdiff --git a/frontend/src/api/generated/services/sigNoz.schemas.ts b/frontend/src/api/generated/services/sigNoz.schemas.tsindex fcf375683b..61eb46781d 100644--- a/frontend/src/api/generated/services/sigNoz.schemas.ts+++ b/frontend/src/api/generated/services/sigNoz.schemas.ts@@ -4653,6 +4653,55 @@ export interface InframonitoringtypesHostsDTO {warning?: Querybuildertypesv5QueryWarnDataDTO;}+/**+ * @nullable+ */+export type InframonitoringtypesNamespaceRecordDTOMeta = {+ [key: string]: string;+} | null;++export interface InframonitoringtypesNamespaceRecordDTO {+ /**+ * @type object+ * @nullable true+ */+ meta: InframonitoringtypesNamespaceRecordDTOMeta;+ /**+ * @type number+ * @format double+ */+ namespaceCPU: number;+ /**+ * @type number+ * @format double+ */+ namespaceMemory: number;+ /**+ * @type string+ */+ namespaceName: string;+ podCountsByPhase: InframonitoringtypesPodCountsByPhaseDTO;+}++export interface InframonitoringtypesNamespacesDTO {+ /**+ * @type boolean+ */+ endTimeBeforeRetention: boolean;+ /**+ * @type array+ * @nullable true+ */+ records: InframonitoringtypesNamespaceRecordDTO[] | null;+ requiredMetricsCheck: InframonitoringtypesRequiredMetricsCheckDTO;+ /**+ * @type integer+ */+ total: number;+ type: InframonitoringtypesResponseTypeDTO;+ warning?: Querybuildertypesv5QueryWarnDataDTO;+}+export enum InframonitoringtypesNodeConditionDTO {ready = 'ready',not_ready = 'not_ready',@@ -4864,6 +4913,34 @@ export interface InframonitoringtypesPostableHostsDTO {start: number;}+export interface InframonitoringtypesPostableNamespacesDTO {+ /**+ * @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 InframonitoringtypesPostableNodesDTO {/*** @type integer@@ -9255,6 +9332,14 @@ export type ListHosts200 = {status: string;};+export type ListNamespaces200 = {+ data: InframonitoringtypesNamespacesDTO;+ /**+ * @type string+ */+ status: string;+};+export type ListNodes200 = {data: InframonitoringtypesNodesDTO;/**diff --git a/frontend/src/components/TanStackTableView/__tests__/useTableParams.test.tsx b/frontend/src/components/TanStackTableView/__tests__/useTableParams.test.tsxindex 4d8f1526ee..572bf87f4e 100644--- a/frontend/src/components/TanStackTableView/__tests__/useTableParams.test.tsx+++ b/frontend/src/components/TanStackTableView/__tests__/useTableParams.test.tsx@@ -398,7 +398,7 @@ describe('useTableParams (selective URL mode — partial config object)', () =>.filter(Boolean).pop();expect(lastExpanded).toBeDefined();- expect(JSON.parse(lastExpanded!)).toEqual(+ expect(JSON.parse(lastExpanded!)).toStrictEqual(expect.arrayContaining(['row-1', 'row-2']),);diff --git a/pkg/apiserver/signozapiserver/inframonitoring.go b/pkg/apiserver/signozapiserver/inframonitoring.goindex 59845b2cc6..ce2a18f5a6 100644--- a/pkg/apiserver/signozapiserver/inframonitoring.go+++ b/pkg/apiserver/signozapiserver/inframonitoring.go@@ -67,5 +67,24 @@ func (provider *provider) addInfraMonitoringRoutes(router *mux.Router) error {return err}+ if err := router.Handle("/api/v2/infra_monitoring/namespaces", handler.New(+ provider.authzMiddleware.ViewAccess(provider.infraMonitoringHandler.ListNamespaces),+ handler.OpenAPIDef{+ ID: "ListNamespaces",+ Tags: []string{"inframonitoring"},+ Summary: "List Namespaces for Infra Monitoring",+ Description: "Returns a paginated list of Kubernetes namespaces with key aggregated pod metrics: CPU usage and memory working set (summed across pods in the group), plus per-group podCountsByPhase ({ pending, running, succeeded, failed, unknown } from each pod's latest k8s.pod.phase value in the window). Each namespace includes metadata attributes (k8s.namespace.name, k8s.cluster.name). The response type is 'list' for the default k8s.namespace.name grouping or 'grouped_list' for custom groupBy keys; in both modes every row aggregates pods in the group. Supports filtering via a filter expression, custom groupBy, ordering by cpu / memory, 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 (namespaceCPU, namespaceMemory) return -1 as a sentinel when no data is available for that field.",+ Request: new(inframonitoringtypes.PostableNamespaces),+ RequestContentType: "application/json",+ Response: new(inframonitoringtypes.Namespaces),+ 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/handler.go b/pkg/modules/inframonitoring/implinframonitoring/handler.goindex 6c8780fb6c..f165491d97 100644--- a/pkg/modules/inframonitoring/implinframonitoring/handler.go+++ b/pkg/modules/inframonitoring/implinframonitoring/handler.go@@ -93,3 +93,27 @@ func (h *handler) ListNodes(rw http.ResponseWriter, req *http.Request) {render.Success(rw, http.StatusOK, result)}++func (h *handler) ListNamespaces(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.PostableNamespaces+ if err := binding.JSON.BindBody(req.Body, &parsedReq); err != nil {+ render.Error(rw, err)+ return+ }++ result, err := h.module.ListNamespaces(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.goindex 2167b99fee..ed432b12d9 100644--- a/pkg/modules/inframonitoring/implinframonitoring/module.go+++ b/pkg/modules/inframonitoring/implinframonitoring/module.go@@ -337,3 +337,92 @@ func (m *module) ListNodes(ctx context.Context, orgID valuer.UUID, req *inframonreturn resp, nil}++func (m *module) ListNamespaces(ctx context.Context, orgID valuer.UUID, req *inframonitoringtypes.PostableNamespaces) (*inframonitoringtypes.Namespaces, error) {+ if err := req.Validate(); err != nil {+ return nil, err+ }++ resp := &inframonitoringtypes.Namespaces{}++ if req.OrderBy == nil {+ req.OrderBy = &qbtypes.OrderBy{+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: inframonitoringtypes.NamespacesOrderByCPU,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ }+ }++ if len(req.GroupBy) == 0 {+ req.GroupBy = []qbtypes.GroupByKey{namespaceNameGroupByKey}+ resp.Type = inframonitoringtypes.ResponseTypeList+ } else {+ resp.Type = inframonitoringtypes.ResponseTypeGroupedList+ }++ missingMetrics, minFirstReportedUnixMilli, err := m.getMetricsExistenceAndEarliestTime(ctx, namespacesTableMetricNamesList)+ if err != nil {+ return nil, err+ }+ if len(missingMetrics) > 0 {+ resp.RequiredMetricsCheck = inframonitoringtypes.RequiredMetricsCheck{MissingMetrics: missingMetrics}+ resp.Records = []inframonitoringtypes.NamespaceRecord{}+ resp.Total = 0+ return resp, nil+ }+ if req.End < int64(minFirstReportedUnixMilli) {+ resp.EndTimeBeforeRetention = true+ resp.Records = []inframonitoringtypes.NamespaceRecord{}+ resp.Total = 0+ return resp, nil+ }+ resp.RequiredMetricsCheck = inframonitoringtypes.RequiredMetricsCheck{MissingMetrics: []string{}}++ metadataMap, err := m.getNamespacesTableMetadata(ctx, req)+ if err != nil {+ return nil, err+ }++ resp.Total = len(metadataMap)++ pageGroups, err := m.getTopNamespaceGroups(ctx, orgID, req, metadataMap)+ if err != nil {+ return nil, err+ }++ if len(pageGroups) == 0 {+ resp.Records = []inframonitoringtypes.NamespaceRecord{}+ return resp, nil+ }++ filterExpr := ""+ if req.Filter != nil {+ filterExpr = req.Filter.Expression+ }++ fullQueryReq := buildFullQueryRequest(req.Start, req.End, filterExpr, req.GroupBy, pageGroups, m.newNamespacesTableListQuery())+ queryResp, err := m.querier.QueryRange(ctx, orgID, fullQueryReq)+ if err != nil {+ return nil, err+ }++ // Reuse the pods phase-counts CTE function via a temp struct — it reads only+ // Start/End/Filter/GroupBy from PostablePods.+ phaseCounts, 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 = buildNamespaceRecords(queryResp, pageGroups, req.GroupBy, metadataMap, phaseCounts)+ resp.Warning = queryResp.Warning++ return resp, nil+}diff --git a/pkg/modules/inframonitoring/implinframonitoring/namespaces.go b/pkg/modules/inframonitoring/implinframonitoring/namespaces.gonew file mode 100644index 0000000000..72e41bb320--- /dev/null+++ b/pkg/modules/inframonitoring/implinframonitoring/namespaces.go@@ -0,0 +1,123 @@+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"+)++// buildNamespaceRecords assembles the page records. Pod phase counts come from+// phaseCounts in both modes; every row is a group of pods, so there's no+// per-row "current phase" concept (unlike pods/nodes list mode).+func buildNamespaceRecords(+ resp *qbtypes.QueryRangeResponse,+ pageGroups []map[string]string,+ groupBy []qbtypes.GroupByKey,+ metadataMap map[string]map[string]string,+ phaseCounts map[string]podPhaseCounts,+) []inframonitoringtypes.NamespaceRecord {+ metricsMap := parseFullQueryResponse(resp, groupBy)++ records := make([]inframonitoringtypes.NamespaceRecord, 0, len(pageGroups))+ for _, labels := range pageGroups {+ compositeKey := compositeKeyFromLabels(labels, groupBy)+ namespaceName := labels[namespaceNameAttrKey]++ record := inframonitoringtypes.NamespaceRecord{ // initialize with default values+ NamespaceName: namespaceName,+ NamespaceCPU: -1,+ NamespaceMemory: -1,+ Meta: map[string]string{},+ }++ if metrics, ok := metricsMap[compositeKey]; ok {+ if v, exists := metrics["A"]; exists {+ record.NamespaceCPU = v+ }+ if v, exists := metrics["D"]; exists {+ record.NamespaceMemory = v+ }+ }++ if phaseCountsForGroup, ok := phaseCounts[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) getTopNamespaceGroups(+ ctx context.Context,+ orgID valuer.UUID,+ req *inframonitoringtypes.PostableNamespaces,+ metadataMap map[string]map[string]string,+) ([]map[string]string, error) {+ orderByKey := req.OrderBy.Key.Name+ queryNamesForOrderBy := orderByToNamespacesQueryNames[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.newNamespacesTableListQuery().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) getNamespacesTableMetadata(ctx context.Context, req *inframonitoringtypes.PostableNamespaces) (map[string]map[string]string, error) {+ var nonGroupByAttrs []string+ for _, key := range namespaceAttrKeysForMetadata {+ if !isKeyInGroupByAttrs(req.GroupBy, key) {+ nonGroupByAttrs = append(nonGroupByAttrs, key)+ }+ }+ return m.getMetadata(ctx, namespacesTableMetricNamesList, req.GroupBy, nonGroupByAttrs, req.Filter, req.Start, req.End)+}diff --git a/pkg/modules/inframonitoring/implinframonitoring/namespaces_constants.go b/pkg/modules/inframonitoring/implinframonitoring/namespaces_constants.gonew file mode 100644index 0000000000..d386ef01b8--- /dev/null+++ b/pkg/modules/inframonitoring/implinframonitoring/namespaces_constants.go@@ -0,0 +1,92 @@+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"+)++const (+ namespaceNameAttrKey = "k8s.namespace.name"+)++var namespaceNameGroupByKey = qbtypes.GroupByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: namespaceNameAttrKey,+ FieldContext: telemetrytypes.FieldContextResource,+ FieldDataType: telemetrytypes.FieldDataTypeString,+ },+}++// namespacesTableMetricNamesList drives the existence/retention check.+// Includes k8s.pod.phase so the response short-circuits cleanly when a+// cluster doesn't ship the metric — even though phase isn't part of the+// QB composite query (it's queried separately via getPerGroupPodPhaseCounts).+var namespacesTableMetricNamesList = []string{+ "k8s.pod.cpu.usage",+ "k8s.pod.memory.working_set",+ "k8s.pod.phase",+}++var namespaceAttrKeysForMetadata = []string{+ "k8s.namespace.name",+ "k8s.cluster.name",+}++var orderByToNamespacesQueryNames = map[string][]string{+ inframonitoringtypes.NamespacesOrderByCPU: {"A"},+ inframonitoringtypes.NamespacesOrderByMemory: {"D"},+}++// newNamespacesTableListQuery builds the composite QB v5 request for the namespaces list.+// Pod phase counts are derived separately via getPerGroupPodPhaseCounts (works for both+// list and grouped_list modes), so no phase query is included here.+// Query letters A and D are kept aligned with the v1 implementation.+func (m *module) newNamespacesTableListQuery() *qbtypes.QueryRangeRequest {+ queries := []qbtypes.QueryEnvelope{+ // Query A: CPU usage — sum of pod CPU within the group.+ {+ Type: qbtypes.QueryTypeBuilder,+ Spec: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{+ Name: "A",+ Signal: telemetrytypes.SignalMetrics,+ Aggregations: []qbtypes.MetricAggregation{+ {+ MetricName: "k8s.pod.cpu.usage",+ TimeAggregation: metrictypes.TimeAggregationAvg,+ SpaceAggregation: metrictypes.SpaceAggregationSum,+ ReduceTo: qbtypes.ReduceToAvg,+ },+ },+ GroupBy: []qbtypes.GroupByKey{namespaceNameGroupByKey},+ Disabled: false,+ },+ },+ // Query D: Memory working set — sum of pod memory within the group.+ {+ Type: qbtypes.QueryTypeBuilder,+ Spec: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{+ Name: "D",+ Signal: telemetrytypes.SignalMetrics,+ Aggregations: []qbtypes.MetricAggregation{+ {+ MetricName: "k8s.pod.memory.working_set",+ TimeAggregation: metrictypes.TimeAggregationAvg,+ SpaceAggregation: metrictypes.SpaceAggregationSum,+ ReduceTo: qbtypes.ReduceToAvg,+ },+ },+ GroupBy: []qbtypes.GroupByKey{namespaceNameGroupByKey},+ Disabled: false,+ },+ },+ }++ return &qbtypes.QueryRangeRequest{+ RequestType: qbtypes.RequestTypeScalar,+ CompositeQuery: qbtypes.CompositeQuery{+ Queries: queries,+ },+ }+}diff --git a/pkg/modules/inframonitoring/inframonitoring.go b/pkg/modules/inframonitoring/inframonitoring.goindex b41e8e60ca..9b02f8adfb 100644--- a/pkg/modules/inframonitoring/inframonitoring.go+++ b/pkg/modules/inframonitoring/inframonitoring.go@@ -12,10 +12,12 @@ type Handler interface {ListHosts(http.ResponseWriter, *http.Request)ListPods(http.ResponseWriter, *http.Request)ListNodes(http.ResponseWriter, *http.Request)+ ListNamespaces(http.ResponseWriter, *http.Request)}type Module interface {ListHosts(ctx context.Context, orgID valuer.UUID, req *inframonitoringtypes.PostableHosts) (*inframonitoringtypes.Hosts, error)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)}diff --git a/pkg/types/inframonitoringtypes/namespaces.go b/pkg/types/inframonitoringtypes/namespaces.gonew file mode 100644index 0000000000..fad585efe2--- /dev/null+++ b/pkg/types/inframonitoringtypes/namespaces.go@@ -0,0 +1,99 @@+package inframonitoringtypes++import (+ "encoding/json"+ "slices"++ "github.com/SigNoz/signoz/pkg/errors"+ qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"+)++type Namespaces struct {+ Type ResponseType `json:"type" required:"true"`+ Records []NamespaceRecord `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 NamespaceRecord struct {+ NamespaceName string `json:"namespaceName" required:"true"`+ NamespaceCPU float64 `json:"namespaceCPU" required:"true"`+ NamespaceMemory float64 `json:"namespaceMemory" required:"true"`+ PodCountsByPhase PodCountsByPhase `json:"podCountsByPhase" required:"true"`+ Meta map[string]string `json:"meta" required:"true"`+}++// PostableNamespaces is the request body for the v2 namespaces list API.+type PostableNamespaces 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 PostableNamespaces contains acceptable values.+func (req *PostableNamespaces) 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(NamespacesValidOrderByKeys, 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 *PostableNamespaces) UnmarshalJSON(data []byte) error {+ type raw PostableNamespaces+ var decoded raw+ if err := json.Unmarshal(data, &decoded); err != nil {+ return err+ }+ *req = PostableNamespaces(decoded)+ return req.Validate()+}diff --git a/pkg/types/inframonitoringtypes/namespaces_constants.go b/pkg/types/inframonitoringtypes/namespaces_constants.gonew file mode 100644index 0000000000..8b18b82e7f--- /dev/null+++ b/pkg/types/inframonitoringtypes/namespaces_constants.go@@ -0,0 +1,11 @@+package inframonitoringtypes++const (+ NamespacesOrderByCPU = "cpu"+ NamespacesOrderByMemory = "memory"+)++var NamespacesValidOrderByKeys = []string{+ NamespacesOrderByCPU,+ NamespacesOrderByMemory,+}diff --git a/pkg/types/inframonitoringtypes/namespaces_test.go b/pkg/types/inframonitoringtypes/namespaces_test.gonew file mode 100644index 0000000000..58f6a1b46f--- /dev/null+++ b/pkg/types/inframonitoringtypes/namespaces_test.go@@ -0,0 +1,237 @@+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 TestPostableNamespaces_Validate(t *testing.T) {+ tests := []struct {+ name string+ req *PostableNamespaces+ wantErr bool+ }{+ {+ name: "valid request",+ req: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: 100,+ Offset: 0,+ },+ wantErr: false,+ },+ {+ name: "nil request",+ req: nil,+ wantErr: true,+ },+ {+ name: "start time zero",+ req: &PostableNamespaces{+ Start: 0,+ End: 2000,+ Limit: 100,+ Offset: 0,+ },+ wantErr: true,+ },+ {+ name: "start time negative",+ req: &PostableNamespaces{+ Start: -1000,+ End: 2000,+ Limit: 100,+ Offset: 0,+ },+ wantErr: true,+ },+ {+ name: "end time zero",+ req: &PostableNamespaces{+ Start: 1000,+ End: 0,+ Limit: 100,+ Offset: 0,+ },+ wantErr: true,+ },+ {+ name: "start time greater than end time",+ req: &PostableNamespaces{+ Start: 2000,+ End: 1000,+ Limit: 100,+ Offset: 0,+ },+ wantErr: true,+ },+ {+ name: "start time equal to end time",+ req: &PostableNamespaces{+ Start: 1000,+ End: 1000,+ Limit: 100,+ Offset: 0,+ },+ wantErr: true,+ },+ {+ name: "limit zero",+ req: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: 0,+ Offset: 0,+ },+ wantErr: true,+ },+ {+ name: "limit negative",+ req: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: -10,+ Offset: 0,+ },+ wantErr: true,+ },+ {+ name: "limit exceeds max",+ req: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: 5001,+ Offset: 0,+ },+ wantErr: true,+ },+ {+ name: "offset negative",+ req: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: 100,+ Offset: -5,+ },+ wantErr: true,+ },+ {+ name: "orderBy nil is valid",+ req: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: 100,+ Offset: 0,+ },+ wantErr: false,+ },+ {+ name: "orderBy with valid key cpu and direction asc",+ req: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: 100,+ Offset: 0,+ OrderBy: &qbtypes.OrderBy{+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: NamespacesOrderByCPU,+ },+ },+ Direction: qbtypes.OrderDirectionAsc,+ },+ },+ wantErr: false,+ },+ {+ name: "orderBy with valid key memory and direction desc",+ req: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: 100,+ Offset: 0,+ OrderBy: &qbtypes.OrderBy{+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: NamespacesOrderByMemory,+ },+ },+ Direction: qbtypes.OrderDirectionDesc,+ },+ },+ wantErr: false,+ },+ {+ name: "orderBy with pod_phase key is rejected",+ req: &PostableNamespaces{+ 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: &PostableNamespaces{+ 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: &PostableNamespaces{+ Start: 1000,+ End: 2000,+ Limit: 100,+ Offset: 0,+ OrderBy: &qbtypes.OrderBy{+ Key: qbtypes.OrderByKey{+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{+ Name: NamespacesOrderByMemory,+ },+ },+ 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)+ }+ })+ }+}