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.

chore(dot-metrics): added log line to check for those queries who are still using normalized metrics (#8518)

aniketio-ctrld712dc1f28ce
diff --git a/pkg/query-service/app/clickhouseReader/reader.go b/pkg/query-service/app/clickhouseReader/reader.go
index 31e766a9ef..9750d60437 100644
--- a/pkg/query-service/app/clickhouseReader/reader.go
+++ b/pkg/query-service/app/clickhouseReader/reader.go
@@ -6054,3 +6054,90 @@ func (r *ClickHouseReader) SearchTraces(ctx context.Context, params *model.Searc
return &searchSpansResult, nil
}
+
+func (r *ClickHouseReader) GetNormalizedStatus(
+ ctx context.Context,
+ orgID valuer.UUID,
+ metricNames []string,
+) (map[string]bool, error) {
+
+ if len(metricNames) == 0 {
+ return map[string]bool{}, nil
+ }
+
+ result := make(map[string]bool, len(metricNames))
+ buildKey := func(name string) string {
+ return constants.NormalizedMetricsMapCacheKey + ":" + name
+ }
+
+ uncached := make([]string, 0, len(metricNames))
+ for _, m := range metricNames {
+ var status model.MetricsNormalizedMap
+ if err := r.cache.Get(ctx, orgID, buildKey(m), &status, true); err == nil {
+ result[m] = status.IsUnNormalized
+ } else {
+ uncached = append(uncached, m)
+ }
+ }
+ if len(uncached) == 0 {
+ return result, nil
+ }
+
+ placeholders := "'" + strings.Join(uncached, "', '") + "'"
+
+ q := fmt.Sprintf(
+ `SELECT metric_name, toUInt8(__normalized)
+ FROM %s.%s
+ WHERE metric_name IN (%s)
+ GROUP BY metric_name, __normalized`,
+ signozMetricDBName, signozTSTableNameV41Day, placeholders,
+ )
+
+ rows, err := r.db.Query(ctx, q)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+
+ // tmp[m] collects the set {0,1} for a metric name, truth table
+ tmp := make(map[string]map[uint8]struct{}, len(uncached))
+
+ for rows.Next() {
+ var (
+ name string
+ normalized uint8
+ )
+ if err := rows.Scan(&name, &normalized); err != nil {
+ return nil, err
+ }
+ if _, ok := tmp[name]; !ok {
+ tmp[name] = make(map[uint8]struct{}, 2)
+ }
+ tmp[name][normalized] = struct{}{}
+ }
+ if err := rows.Err(); err != nil {
+ return nil, err
+ }
+
+ for _, m := range uncached {
+ set := tmp[m]
+ switch {
+ case len(set) == 0:
+ return nil, fmt.Errorf("metric %q not found in ClickHouse", m)
+
+ case len(set) == 2:
+ result[m] = true
+
+ default:
+ _, hasUnnorm := set[0]
+ result[m] = hasUnnorm
+ }
+ status := model.MetricsNormalizedMap{
+ MetricName: m,
+ IsUnNormalized: result[m],
+ }
+ _ = r.cache.Set(ctx, orgID, buildKey(m), &status, 0)
+ }
+
+ return result, nil
+}
diff --git a/pkg/query-service/app/querier/v2/helper.go b/pkg/query-service/app/querier/v2/helper.go
index 7312fc46da..b4d5d4c2cf 100644
--- a/pkg/query-service/app/querier/v2/helper.go
+++ b/pkg/query-service/app/querier/v2/helper.go
@@ -3,6 +3,7 @@ package v2
import (
"context"
"fmt"
+ "github.com/prometheus/prometheus/promql/parser"
"strings"
"sync"
@@ -276,3 +277,59 @@ func (q *querier) runBuilderQuery(
Series: resultSeries,
}
}
+
+// ValidateMetricNames function is used to print all those queries who are still using old normalized metrics and not new metrics.
+func (q *querier) ValidateMetricNames(ctx context.Context, query *v3.CompositeQuery, orgID valuer.UUID) {
+ var metricNames []string
+ switch query.QueryType {
+ case v3.QueryTypePromQL:
+ for _, query := range query.PromQueries {
+ expr, err := parser.ParseExpr(query.Query)
+ if err != nil {
+ zap.L().Debug("error parsing promQL expression", zap.String("query", query.Query), zap.Error(err))
+ continue
+ }
+ parser.Inspect(expr, func(node parser.Node, path []parser.Node) error {
+ if vs, ok := node.(*parser.VectorSelector); ok {
+ for _, m := range vs.LabelMatchers {
+ if m.Name == "__name__" {
+ metricNames = append(metricNames, m.Value)
+ }
+ }
+ }
+ return nil
+ })
+ }
+ metrics, err := q.reader.GetNormalizedStatus(ctx, orgID, metricNames)
+ if err != nil {
+ zap.L().Debug("error getting corresponding normalized metrics", zap.Error(err))
+ return
+ }
+ for metricName, metricPresent := range metrics {
+ if metricPresent {
+ continue
+ } else {
+ zap.L().Warn("using normalized metric name", zap.String("metrics", metricName))
+ continue
+ }
+ }
+ case v3.QueryTypeBuilder:
+ for _, query := range query.BuilderQueries {
+ metricName := query.AggregateAttribute.Key
+ metricNames = append(metricNames, metricName)
+ }
+ metrics, err := q.reader.GetNormalizedStatus(ctx, orgID, metricNames)
+ if err != nil {
+ zap.L().Debug("error getting corresponding normalized metrics", zap.Error(err))
+ return
+ }
+ for metricName, metricPresent := range metrics {
+ if metricPresent {
+ continue
+ } else {
+ zap.L().Warn("using normalized metric name", zap.String("metrics", metricName))
+ continue
+ }
+ }
+ }
+}
diff --git a/pkg/query-service/app/querier/v2/querier.go b/pkg/query-service/app/querier/v2/querier.go
index 322b25be91..19090fca06 100644
--- a/pkg/query-service/app/querier/v2/querier.go
+++ b/pkg/query-service/app/querier/v2/querier.go
@@ -506,6 +506,9 @@ func (q *querier) QueryRange(ctx context.Context, orgID valuer.UUID, params *v3.
var results []*v3.Result
var err error
var errQueriesByName map[string]error
+ if !q.testingMode && q.reader != nil {
+ q.ValidateMetricNames(ctx, params.CompositeQuery, orgID)
+ }
if params.CompositeQuery != nil {
switch params.CompositeQuery.QueryType {
case v3.QueryTypeBuilder:
diff --git a/pkg/query-service/constants/constants.go b/pkg/query-service/constants/constants.go
index 0b78419267..07aae5112c 100644
--- a/pkg/query-service/constants/constants.go
+++ b/pkg/query-service/constants/constants.go
@@ -3,6 +3,7 @@ package constants
import (
"maps"
"os"
+ "regexp"
"strconv"
"time"
@@ -33,6 +34,12 @@ var InviteEmailTemplate = GetOrDefaultEnv("INVITE_EMAIL_TEMPLATE", "/root/templa
var MetricsExplorerClickhouseThreads = GetOrDefaultEnvInt("METRICS_EXPLORER_CLICKHOUSE_THREADS", 8)
var UpdatedMetricsMetadataCachePrefix = GetOrDefaultEnv("METRICS_UPDATED_METADATA_CACHE_KEY", "UPDATED_METRICS_METADATA")
+const NormalizedMetricsMapCacheKey = "NORMALIZED_METRICS_MAP_CACHE_KEY"
+const NormalizedMetricsMapQueryThreads = 10
+
+var NormalizedMetricsMapRegex = regexp.MustCompile(`[^a-zA-Z0-9]`)
+var NormalizedMetricsMapQuantileRegex = regexp.MustCompile(`(?i)([._-]?quantile.*)$`)
+
// TODO(srikanthccv): remove after backfilling is done
func UseMetricsPreAggregation() bool {
return GetOrDefaultEnv("USE_METRICS_PRE_AGGREGATION", "true") == "true"
diff --git a/pkg/query-service/interfaces/interface.go b/pkg/query-service/interfaces/interface.go
index d0a05fe979..b378c94a02 100644
--- a/pkg/query-service/interfaces/interface.go
+++ b/pkg/query-service/interfaces/interface.go
@@ -130,6 +130,7 @@ type Reader interface {
GetUpdatedMetricsMetadata(ctx context.Context, orgID valuer.UUID, metricNames ...string) (map[string]*model.UpdateMetricsMetadata, *model.ApiError)
CheckForLabelsInMetric(ctx context.Context, metricName string, labels []string) (bool, *model.ApiError)
+ GetNormalizedStatus(ctx context.Context, orgID valuer.UUID, metricNames []string) (map[string]bool, error)
}
type Querier interface {
diff --git a/pkg/query-service/model/normalizedMetrics.go b/pkg/query-service/model/normalizedMetrics.go
new file mode 100644
index 0000000000..13672c651a
--- /dev/null
+++ b/pkg/query-service/model/normalizedMetrics.go
@@ -0,0 +1,15 @@
+package model
+
+import "encoding/json"
+
+type MetricsNormalizedMap struct {
+ MetricName string `json:"metricName"`
+ IsUnNormalized bool `json:"isUnNormalized"`
+}
+
+func (c *MetricsNormalizedMap) MarshalBinary() (data []byte, err error) {
+ return json.Marshal(c)
+}
+func (c *MetricsNormalizedMap) UnmarshalBinary(data []byte) error {
+ return json.Unmarshal(data, c)
+}