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
chore(metrics-explorer): return 404 for non-existent metrics (#10386)
Nikhil Mantribf13b26a37ad
diff --git a/docs/api/openapi.yml b/docs/api/openapi.ymlindex 2f66818a3c..0f050106db 100644--- a/docs/api/openapi.yml+++ b/docs/api/openapi.yml@@ -5532,6 +5532,12 @@ paths:schema:$ref: '#/components/schemas/RenderErrorResponse'description: Forbidden+ "404":+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/RenderErrorResponse'+ description: Not Found"500":content:application/json:@@ -5601,6 +5607,12 @@ paths:schema:$ref: '#/components/schemas/RenderErrorResponse'description: Forbidden+ "404":+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/RenderErrorResponse'+ description: Not Found"500":content:application/json:@@ -5659,6 +5671,12 @@ paths:schema:$ref: '#/components/schemas/RenderErrorResponse'description: Forbidden+ "404":+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/RenderErrorResponse'+ description: Not Found"500":content:application/json:@@ -5718,6 +5736,12 @@ paths:schema:$ref: '#/components/schemas/RenderErrorResponse'description: Forbidden+ "404":+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/RenderErrorResponse'+ description: Not Found"500":content:application/json:diff --git a/pkg/apiserver/signozapiserver/metricsexplorer.go b/pkg/apiserver/signozapiserver/metricsexplorer.goindex 12edf35f6f..9b36d871e5 100644--- a/pkg/apiserver/signozapiserver/metricsexplorer.go+++ b/pkg/apiserver/signozapiserver/metricsexplorer.go@@ -81,7 +81,7 @@ func (provider *provider) addMetricsExplorerRoutes(router *mux.Router) error {Response: new(metricsexplorertypes.MetricAttributesResponse),ResponseContentType: "application/json",SuccessStatusCode: http.StatusOK,- ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusInternalServerError},+ ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusNotFound, http.StatusInternalServerError},Deprecated: false,SecuritySchemes: newSecuritySchemes(types.RoleViewer),})).Methods(http.MethodGet).GetError(); err != nil {@@ -138,7 +138,7 @@ func (provider *provider) addMetricsExplorerRoutes(router *mux.Router) error {Response: new(metricsexplorertypes.MetricHighlightsResponse),ResponseContentType: "application/json",SuccessStatusCode: http.StatusOK,- ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusInternalServerError},+ ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusNotFound, http.StatusInternalServerError},Deprecated: false,SecuritySchemes: newSecuritySchemes(types.RoleViewer),})).Methods(http.MethodGet).GetError(); err != nil {@@ -157,7 +157,7 @@ func (provider *provider) addMetricsExplorerRoutes(router *mux.Router) error {Response: new(metricsexplorertypes.MetricAlertsResponse),ResponseContentType: "application/json",SuccessStatusCode: http.StatusOK,- ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusInternalServerError},+ ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusNotFound, http.StatusInternalServerError},Deprecated: false,SecuritySchemes: newSecuritySchemes(types.RoleViewer),})).Methods(http.MethodGet).GetError(); err != nil {@@ -176,7 +176,7 @@ func (provider *provider) addMetricsExplorerRoutes(router *mux.Router) error {Response: new(metricsexplorertypes.MetricDashboardsResponse),ResponseContentType: "application/json",SuccessStatusCode: http.StatusOK,- ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusInternalServerError},+ ErrorStatusCodes: []int{http.StatusBadRequest, http.StatusUnauthorized, http.StatusNotFound, http.StatusInternalServerError},Deprecated: false,SecuritySchemes: newSecuritySchemes(types.RoleViewer),})).Methods(http.MethodGet).GetError(); err != nil {diff --git a/pkg/modules/metricsexplorer/implmetricsexplorer/handler.go b/pkg/modules/metricsexplorer/implmetricsexplorer/handler.goindex a101f912ee..92c748525c 100644--- a/pkg/modules/metricsexplorer/implmetricsexplorer/handler.go+++ b/pkg/modules/metricsexplorer/implmetricsexplorer/handler.go@@ -1,6 +1,7 @@package implmetricsexplorerimport (+ "context""net/http""github.com/SigNoz/signoz/pkg/errors"@@ -187,6 +188,12 @@ func (h *handler) GetMetricAlerts(rw http.ResponseWriter, req *http.Request) {}orgID := valuer.MustNewUUID(claims.OrgID)++ if err := h.checkMetricExists(req.Context(), orgID, metricName); err != nil {+ render.Error(rw, err)+ return+ }+out, err := h.module.GetMetricAlerts(req.Context(), orgID, metricName)if err != nil {render.Error(rw, err)@@ -209,6 +216,12 @@ func (h *handler) GetMetricDashboards(rw http.ResponseWriter, req *http.Request)}orgID := valuer.MustNewUUID(claims.OrgID)++ if err := h.checkMetricExists(req.Context(), orgID, metricName); err != nil {+ render.Error(rw, err)+ return+ }+out, err := h.module.GetMetricDashboards(req.Context(), orgID, metricName)if err != nil {render.Error(rw, err)@@ -231,6 +244,12 @@ func (h *handler) GetMetricHighlights(rw http.ResponseWriter, req *http.Request)}orgID := valuer.MustNewUUID(claims.OrgID)++ if err := h.checkMetricExists(req.Context(), orgID, metricName); err != nil {+ render.Error(rw, err)+ return+ }+highlights, err := h.module.GetMetricHighlights(req.Context(), orgID, metricName)if err != nil {render.Error(rw, err)@@ -266,6 +285,12 @@ func (h *handler) GetMetricAttributes(rw http.ResponseWriter, req *http.Request)}orgID := valuer.MustNewUUID(claims.OrgID)++ if err := h.checkMetricExists(req.Context(), orgID, metricName); err != nil {+ render.Error(rw, err)+ return+ }+out, err := h.module.GetMetricAttributes(req.Context(), orgID, &in)if err != nil {render.Error(rw, err)@@ -274,3 +299,14 @@ func (h *handler) GetMetricAttributes(rw http.ResponseWriter, req *http.Request)render.Success(rw, http.StatusOK, out)}++func (h *handler) checkMetricExists(ctx context.Context, orgID valuer.UUID, metricName string) error {+ exists, err := h.module.CheckMetricExists(ctx, orgID, metricName)+ if err != nil {+ return err+ }+ if !exists {+ return errors.NewNotFoundf(errors.CodeNotFound, "metric not found: %q", metricName)+ }+ return nil+}diff --git a/pkg/modules/metricsexplorer/implmetricsexplorer/module.go b/pkg/modules/metricsexplorer/implmetricsexplorer/module.goindex 0624813149..801b4ad64e 100644--- a/pkg/modules/metricsexplorer/implmetricsexplorer/module.go+++ b/pkg/modules/metricsexplorer/implmetricsexplorer/module.go@@ -404,6 +404,26 @@ func (m *module) GetMetricAttributes(ctx context.Context, orgID valuer.UUID, req}, nil}+func (m *module) CheckMetricExists(ctx context.Context, orgID valuer.UUID, metricName string) (bool, error) {+ sb := sqlbuilder.NewSelectBuilder()+ sb.Select("count(*) > 0 as metricExists")+ sb.From(fmt.Sprintf("%s.%s", telemetrymetrics.DBName, telemetrymetrics.AttributesMetadataTableName))+ sb.Where(sb.E("metric_name", metricName))++ query, args := sb.BuildWithFlavor(sqlbuilder.ClickHouse)++ db := m.telemetryStore.ClickhouseDB()+ var exists bool+ valueCtx := ctxtypes.SetClickhouseMaxThreads(ctx, m.config.TelemetryStore.Threads)++ err := db.QueryRow(valueCtx, query, args...).Scan(&exists)+ if err != nil {+ return false, errors.WrapInternalf(err, errors.CodeInternal, "failed to check if metric exists")+ }++ return exists, nil+}+func (m *module) fetchMetadataFromCache(ctx context.Context, orgID valuer.UUID, metricNames []string) (map[string]*metricsexplorertypes.MetricMetadata, []string) {hits := make(map[string]*metricsexplorertypes.MetricMetadata)misses := make([]string, 0)diff --git a/pkg/modules/metricsexplorer/metricsexplorer.go b/pkg/modules/metricsexplorer/metricsexplorer.goindex 02ef6d3d24..aa591622eb 100644--- a/pkg/modules/metricsexplorer/metricsexplorer.go+++ b/pkg/modules/metricsexplorer/metricsexplorer.go@@ -23,6 +23,7 @@ type Handler interface {// Module represents the metrics module interface.type Module interface {+ CheckMetricExists(ctx context.Context, orgID valuer.UUID, metricName string) (bool, error)ListMetrics(ctx context.Context, orgID valuer.UUID, params *metricsexplorertypes.ListMetricsParams) (*metricsexplorertypes.ListMetricsResponse, error)GetStats(ctx context.Context, orgID valuer.UUID, req *metricsexplorertypes.StatsRequest) (*metricsexplorertypes.StatsResponse, error)GetTreemap(ctx context.Context, orgID valuer.UUID, req *metricsexplorertypes.TreemapRequest) (*metricsexplorertypes.TreemapResponse, error)