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
import type {
DashboardtypesAIBuilderQuerySpecDTO,
DashboardtypesBuilderQuerySpecDTO,
DashboardtypesQueryDTO,
Querybuildertypesv5CompositeQueryDTO,
Querybuildertypesv5QueryEnvelopeDTO,
} from 'api/generated/services/sigNoz.schemas';
import {
DashboardtypesQueryPluginVariantGithubComSigNozSignozPkgTypesDashboardtypesAIBuilderQuerySpecDTOKind as AIBuilderQueryPluginKind,
DashboardtypesQueryPluginVariantGithubComSigNozSignozPkgTypesDashboardtypesBuilderQuerySpecDTOKind as BuilderQueryPluginKind,
DashboardtypesQueryPluginVariantGithubComSigNozSignozPkgTypesQuerybuildertypesQuerybuildertypesv5CompositeQueryDTOKind as CompositeQueryPluginKind,
Querybuildertypesv5QueryEnvelopeClickHouseSQLDTOType,
Querybuildertypesv5QueryEnvelopePromQLDTOType,
Querybuildertypesv5RequestTypeDTO,
} from 'api/generated/services/sigNoz.schemas';
import { initialQueriesMap, PANEL_TYPES } from 'constants/queryBuilder';
import { mapCompositeQueryFromQuery } from 'lib/newQueryBuilder/queryBuilderMappers/mapCompositeQueryFromQuery';
import { mapQueryDataFromApi } from 'lib/newQueryBuilder/queryBuilderMappers/mapQueryDataFromApi';
import type { ICompositeMetricQuery } from 'types/api/alerts/compositeQuery';
import type { Query } from 'types/api/queryBuilder/queryBuilderData';
import type { QueryEnvelope } from 'types/api/v5/queryRange';
import { EQueryType } from 'types/common/dashboard';
import { DataSource } from 'types/common/queryBuilder';
import {
isAIBuilderEnvelope,
isBuilderEnvelope,
} from 'lib/query/builderEnvelope';
import { toQueryEnvelopes } from './buildQueryRangeRequest';
/**
* Adapters between the V2 perses query shape and the V1 `Query` the shared query
* builder uses. Both directions pivot through the V5 query-envelope list to reuse
* the existing V1↔V5 mappers.
*
* Two nominally-distinct envelope families meet here: the generated
* `Querybuildertypesv5QueryEnvelopeDTO` (enum `type`, undiscriminated `spec`) carried
* by the panel spec + request builder, and the hand-written `QueryEnvelope` (typed
* `spec`) the V1 mappers consume. They describe the same wire shape, so converting
* between them is a structural cast — localized to the two `*Envelopes` helpers below.
*/
const toMapperEnvelopes = (
envelopes: Querybuildertypesv5QueryEnvelopeDTO[],
): QueryEnvelope[] => envelopes as unknown as QueryEnvelope[];
const toGeneratedEnvelopes = (
envelopes: QueryEnvelope[],
): Querybuildertypesv5QueryEnvelopeDTO[] =>
envelopes as unknown as Querybuildertypesv5QueryEnvelopeDTO[];
/**
* Clears the V1 explorer's `pageSize`/`offset` before conversion — the shared mapper folds
* `pageSize` into the V5 `limit`, which usePanelQuery would read as a user cap and hide the
* pager. Dropped here, `limit` reflects only a real user limit and List panels page by default.
*/
const withoutExplorerPaging = (query: Query): Query => ({
...query,
builder: {
...query.builder,
queryData: query.builder.queryData.map((data) => ({
...data,
pageSize: undefined,
offset: undefined,
})),
},
});
export function deriveQueryType(
envelopes: Querybuildertypesv5QueryEnvelopeDTO[],
): EQueryType {
if (
envelopes.some(
(e) => e.type === Querybuildertypesv5QueryEnvelopePromQLDTOType.promql,
)
) {
return EQueryType.PROM;
}
if (
envelopes.some(
(e) =>
e.type ===
Querybuildertypesv5QueryEnvelopeClickHouseSQLDTOType.clickhouse_sql,
)
) {
return EQueryType.CLICKHOUSE;
}
return EQueryType.QUERY_BUILDER;
}
/**
* Maps a legacy panel type to the V5 `requestType`. Lives on this side of the V1 boundary
* because only the V1 pivot still speaks `PANEL_TYPES` — V2 panels read `requestType` off
* their kind's declared query capabilities instead. BAR/HISTOGRAM bin client-side from a raw
* time series, so they request `time_series` (V1 parity).
*/
export function panelTypeToRequestType(
panelType: PANEL_TYPES,
): Querybuildertypesv5RequestTypeDTO {
switch (panelType) {
case PANEL_TYPES.TIME_SERIES:
case PANEL_TYPES.BAR:
case PANEL_TYPES.AREA:
case PANEL_TYPES.HISTOGRAM:
return Querybuildertypesv5RequestTypeDTO.time_series;
case PANEL_TYPES.TABLE:
case PANEL_TYPES.PIE:
case PANEL_TYPES.VALUE:
case PANEL_TYPES.SCATTER:
return Querybuildertypesv5RequestTypeDTO.scalar;
case PANEL_TYPES.LIST:
return Querybuildertypesv5RequestTypeDTO.raw;
case PANEL_TYPES.HEATMAP:
return Querybuildertypesv5RequestTypeDTO.heatmap;
case PANEL_TYPES.TRACE:
return Querybuildertypesv5RequestTypeDTO.trace;
default:
return Querybuildertypesv5RequestTypeDTO.time_series;
}
}
/**
* V5 query-envelope list → V1 `Query`, via `mapQueryDataFromApi`. An empty list opens
* on a fresh metrics builder query. Used by `fromPerses` and by the envelopes a
* `/substitute_vars` round-trip returns with dashboard variables resolved.
*/
export function envelopesToQuery(
envelopes: Querybuildertypesv5QueryEnvelopeDTO[],
panelType: PANEL_TYPES,
): Query {
if (envelopes.length === 0) {
return initialQueriesMap[DataSource.METRICS];
}
const composite: ICompositeMetricQuery = {
queryType: deriveQueryType(envelopes),
panelType,
builderQueries: {},
chQueries: {},
promQueries: {},
unit: undefined,
queries: toMapperEnvelopes(envelopes),
};
return mapQueryDataFromApi(composite);
}
/**
* Perses panel queries → V1 `Query` (to seed the query builder), via the V5 envelope
* list + `mapQueryDataFromApi`. An empty panel opens on a fresh metrics builder query.
*/
export function fromPerses(
queries: DashboardtypesQueryDTO[],
panelType: PANEL_TYPES,
): Query {
return envelopesToQuery(toQueryEnvelopes(queries), panelType);
}
/**
* V1 `Query` → perses panel queries (to write the builder result back to the editor
* draft). Wrapped in a single `signoz/CompositeQuery` to satisfy the
* `panel.queries.length === 1` invariant. Exception: List rejects `signoz/CompositeQuery`
* backend-side, so it emits its one builder query as the bare plugin matching the query —
* a bare plugin carries no envelope `type`, so the kind is what keeps an AI list AI.
*/
export function toPerses(
query: Query,
panelType: PANEL_TYPES,
): DashboardtypesQueryDTO[] {
// List panels page server-side via usePanelQuery, so drop the V1 explorer's paging
// fields before conversion — otherwise the shared mapper folds them into `limit`.
const source =
panelType === PANEL_TYPES.LIST ? withoutExplorerPaging(query) : query;
const composite = mapCompositeQueryFromQuery(source, panelType);
const envelopes = toGeneratedEnvelopes(composite.queries ?? []);
if (panelType === PANEL_TYPES.LIST) {
const builder = envelopes.find(isBuilderEnvelope);
if (!builder) {
return [];
}
const plugin = isAIBuilderEnvelope(builder)
? {
kind: AIBuilderQueryPluginKind['signoz/AIBuilderQuery'],
spec: builder.spec as unknown as DashboardtypesAIBuilderQuerySpecDTO,
}
: {
kind: BuilderQueryPluginKind['signoz/BuilderQuery'],
spec: builder.spec as DashboardtypesBuilderQuerySpecDTO,
};
return [
{
kind: panelTypeToRequestType(panelType),
spec: { plugin },
},
];
}
const compositeSpec: Querybuildertypesv5CompositeQueryDTO = {
queries: envelopes,
};
return [
{
kind: panelTypeToRequestType(panelType),
spec: {
plugin: {
kind: CompositeQueryPluginKind['signoz/CompositeQuery'],
spec: compositeSpec,
},
},
},
];
}