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.

7.2 KB
Raw Blame
import { rest } from 'msw';

import commonEnTranslation from '../../public/locales/en/common.json';
import enTranslation from '../../public/locales/en/translation.json';
import {
	notificationChannelsV2,
	notificationChannelV2,
} from './__mockdata__/alerts';
import { alertRulesFixture } from './__mockdata__/alert_rules';
import { triggeredAlertsFixture } from './__mockdata__/triggered_alerts';
import { billingSuccessResponse } from './__mockdata__/billing';
import {
	dashboardSuccessResponse,
	getDashboardById,
} from './__mockdata__/dashboards';
import { licensesSuccessResponse } from './__mockdata__/licenses';
import { queryRangeSuccessResponse } from './__mockdata__/query_range';
import { serviceSuccessResponse } from './__mockdata__/services';
import { topLevelOperationSuccessResponse } from './__mockdata__/top_level_operations';
import { traceDetailResponse } from './__mockdata__/tracedetail';

export const handlers = [
	rest.post('http://localhost/api/v3/query_range', (req, res, ctx) =>
		res(ctx.status(200), ctx.json(queryRangeSuccessResponse)),
	),

	rest.post('http://localhost/api/v4/query_range', (req, res, ctx) =>
		res(ctx.status(200), ctx.json(queryRangeSuccessResponse)),
	),

	rest.post('http://localhost/api/v2/services', (req, res, ctx) =>
		res(
			ctx.status(200),
			ctx.json({ status: 'success', data: serviceSuccessResponse }),
		),
	),

	rest.post(
		'http://localhost/api/v1/service/top_level_operations',
		(req, res, ctx) =>
			res(ctx.status(200), ctx.json(topLevelOperationSuccessResponse)),
	),

	rest.get(
		'http://localhost/api/v3/autocomplete/attribute_keys',
		(req, res, ctx) => {
			const metricName = req.url.searchParams.get('metricName');
			const match = req.url.searchParams.get('match');

			if (metricName === 'signoz_calls_total' && match === 'resource_') {
				return res(
					ctx.status(200),
					ctx.json({ status: 'success', data: ['resource_signoz_collector_id'] }),
				);
			}

			return res(ctx.status(500));
		},
	),

	rest.get(
		'http://localhost/api/v3/autocomplete/attribute_values',
		(req, res, ctx) => {
			// ?metricName=signoz_calls_total&tagKey=resource_signoz_collector_id
			const metricName = req.url.searchParams.get('metricName');
			const tagKey = req.url.searchParams.get('tagKey');

			const attributeKey = req.url.searchParams.get('attributeKey');

			if (attributeKey === 'serviceName') {
				return res(
					ctx.status(200),
					ctx.json({
						status: 'success',
						data: {
							stringAttributeValues: [
								'customer',
								'demo-app',
								'driver',
								'frontend',
								'mysql',
								'redis',
								'route',
								'go-grpc-otel-server',
								'test',
							],
							numberAttributeValues: null,
							boolAttributeValues: null,
						},
					}),
				);
			}

			if (attributeKey === 'name') {
				return res(
					ctx.status(200),
					ctx.json({
						status: 'success',
						data: {
							stringAttributeValues: [
								'HTTP GET',
								'HTTP GET /customer',
								'HTTP GET /dispatch',
								'HTTP GET /route',
							],
							numberAttributeValues: null,
							boolAttributeValues: null,
						},
					}),
				);
			}

			if (
				metricName === 'signoz_calls_total' &&
				tagKey === 'resource_signoz_collector_id'
			) {
				return res(
					ctx.status(200),
					ctx.json({
						status: 'success',
						data: [
							'f38916c2-daf2-4424-bd3e-4907a7e537b6',
							'6d4af7f0-4884-4a37-abd4-6bdbee29fa04',
							'523c44b9-5fe1-46f7-9163-4d2c57ece09b',
							'aa52e8e8-6f88-4056-8fbd-b377394d022c',
							'4d515ba2-065d-4856-b2d8-ddb957c44ddb',
							'fd47a544-1410-4c76-a554-90ef6464da02',
							'bb455f71-3fe1-4761-bbf5-efe2faee18a6',
							'48563680-314e-4117-8a6d-1f0389c95e04',
							'6e866423-7704-4d72-be8b-4695bc36f145',
							'e4886c76-93f5-430f-9076-eef85524312f',
						],
					}),
				);
			}

			return res(ctx.status(500));
		},
	),
	rest.get('http://localhost/api/v2/licenses', (req, res, ctx) =>
		res(ctx.status(200), ctx.json(licensesSuccessResponse)),
	),

	rest.get('http://localhost/api/v1/subscriptions', (req, res, ctx) =>
		res(ctx.status(200), ctx.json(billingSuccessResponse)),
	),

	rest.get('http://localhost/api/v1/dashboards', (_, res, ctx) =>
		res(ctx.status(200), ctx.json(dashboardSuccessResponse)),
	),

	rest.get('http://localhost/api/v1/dashboards/4', (_, res, ctx) =>
		res(ctx.status(200), ctx.json(getDashboardById)),
	),

	rest.post('http://localhost/api/v2/users', (_, res, ctx) =>
		res(
			ctx.status(201),
			ctx.json({
				data: { id: 'user-123' },
			}),
		),
	),

	rest.get(
		'http://localhost/api/v3/autocomplete/aggregate_attributes',
		(req, res, ctx) =>
			res(
				ctx.status(200),
				ctx.json({
					status: 'success',
					data: { attributeKeys: null },
				}),
			),
	),

	rest.post('http://localhost/api/v1/event', (req, res, ctx) =>
		res(
			ctx.status(200),
			ctx.json({
				statusCode: 200,
				error: null,
				payload: 'Event Processed Successfully',
			}),
		),
	),

	rest.get(
		'http://localhost/api/v1/traces/000000000000000071dc9b0a338729b4',
		(req, res, ctx) => res(ctx.status(200), ctx.json(traceDetailResponse)),
	),

	rest.get('http://localhost/api/v2/notification_channels', (req, res, ctx) => {
		const query = req.url.searchParams.get('query')?.toLowerCase() ?? '';
		const kind = req.url.searchParams.get('kind');
		const channels = notificationChannelsV2.filter(
			(channel) =>
				(!query || channel.displayName.toLowerCase().includes(query)) &&
				(!kind || channel.kind === kind),
		);
		return res(
			ctx.status(200),
			ctx.json({
				data: { channels, total: channels.length },
				status: 'success',
			}),
		);
	}),
	rest.get('http://localhost/api/v2/notification_channels/:id', (_, res, ctx) =>
		res(
			ctx.status(200),
			ctx.json({ data: notificationChannelV2, status: 'success' }),
		),
	),
	rest.delete(
		'http://localhost/api/v2/notification_channels/:id',
		(_, res, ctx) => res(ctx.status(204)),
	),
	rest.get('http://localhost/api/v1/alerts', (_, res, ctx) =>
		res(
			ctx.status(200),
			ctx.json({ data: triggeredAlertsFixture, status: 'success' }),
		),
	),
	rest.get('http://localhost/api/v2/rules', (_, res, ctx) =>
		res(
			ctx.status(200),
			ctx.json({ data: alertRulesFixture, status: 'success' }),
		),
	),
	rest.post('http://localhost/api/v2/rules', async (req, res, ctx) => {
		const body = (await req.json()) as { alert?: string };
		return res(
			ctx.status(201),
			ctx.json({
				data: {
					...alertRulesFixture[0],
					id: 'new-rule-id',
					alert: body?.alert ?? 'New Rule',
				},
				status: 'success',
			}),
		);
	}),
	rest.patch('http://localhost/api/v2/rules/:id', (_, res, ctx) =>
		res(ctx.status(200), ctx.json({ status: 'success' })),
	),
	rest.delete('http://localhost/api/v2/rules/:id', (_, res, ctx) =>
		res(ctx.status(200), ctx.json({ status: 'success' })),
	),
	rest.get('http://localhost/locales/en/translation.json', (_, res, ctx) =>
		res(ctx.status(200), ctx.json(enTranslation)),
	),
	rest.get('http://localhost/locales/en/common.json', (_, res, ctx) =>
		res(ctx.status(200), ctx.json(commonEnTranslation)),
	),
	rest.get('http://localhost/locales/en-US/translation.json', (_, res, ctx) =>
		res(ctx.status(200), ctx.json(enTranslation)),
	),
	rest.get('http://localhost/locales/en-US/common.json', (_, res, ctx) =>
		res(ctx.status(200), ctx.json(commonEnTranslation)),
	),
];