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.

fix: handle series with different types of labels (#11112)

Nityananda Gohainb445b22bd619
diff --git a/pkg/querier/postprocess.go b/pkg/querier/postprocess.go
index 60d4e161bf..21ef254d86 100644
--- a/pkg/querier/postprocess.go
+++ b/pkg/querier/postprocess.go
@@ -628,19 +628,29 @@ func convertTimeSeriesDataToScalar(tsData *qbtypes.TimeSeriesData, queryName str
return &qbtypes.ScalarData{QueryName: queryName}
}
- columns := []*qbtypes.ColumnDescriptor{}
-
- // Add group columns from first series
- if len(tsData.Aggregations[0].Series) > 0 {
- for _, label := range tsData.Aggregations[0].Series[0].Labels {
- columns = append(columns, &qbtypes.ColumnDescriptor{
- TelemetryFieldKey: label.Key,
- QueryName: queryName,
- Type: qbtypes.ColumnTypeGroup,
- })
+ // Series can have ragged label sets; build the column schema from the
+ // union of all label keys (first-seen order) and fill rows by key lookup.
+ keyOrder := []telemetrytypes.TelemetryFieldKey{}
+ keyIndex := map[string]int{}
+ for _, series := range tsData.Aggregations[0].Series {
+ for _, label := range series.Labels {
+ if _, ok := keyIndex[label.Key.Name]; ok {
+ continue
+ }
+ keyIndex[label.Key.Name] = len(keyOrder)
+ keyOrder = append(keyOrder, label.Key)
}
}
+ columns := make([]*qbtypes.ColumnDescriptor, 0, len(keyOrder)+len(tsData.Aggregations))
+ for _, key := range keyOrder {
+ columns = append(columns, &qbtypes.ColumnDescriptor{
+ TelemetryFieldKey: key,
+ QueryName: queryName,
+ Type: qbtypes.ColumnTypeGroup,
+ })
+ }
+
// Add aggregation columns
for _, agg := range tsData.Aggregations {
name := agg.Alias
@@ -656,18 +666,18 @@ func convertTimeSeriesDataToScalar(tsData *qbtypes.TimeSeriesData, queryName str
})
}
- // Build rows
+ // Build rows.
+ groupColCount := len(keyOrder)
data := [][]any{}
for seriesIdx, series := range tsData.Aggregations[0].Series {
row := make([]any, len(columns))
- // Add group values
- for i, label := range series.Labels {
- row[i] = label.Value
+ // Place each label under its key's column (by lookup, not index).
+ for _, label := range series.Labels {
+ row[keyIndex[label.Key.Name]] = label.Value
}
// Add aggregation values (last value)
- groupColCount := len(series.Labels)
for aggIdx, agg := range tsData.Aggregations {
if seriesIdx < len(agg.Series) && len(agg.Series[seriesIdx].Values) > 0 {
lastValue := agg.Series[seriesIdx].Values[len(agg.Series[seriesIdx].Values)-1].Value
diff --git a/pkg/querier/postprocess_test.go b/pkg/querier/postprocess_test.go
new file mode 100644
index 0000000000..7113503bfb
--- /dev/null
+++ b/pkg/querier/postprocess_test.go
@@ -0,0 +1,53 @@
+package querier
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
+ "github.com/SigNoz/signoz/pkg/types/telemetrytypes"
+)
+
+// Multiple series with different number of labels, shouldn't panic and should align labels correctly.
+func TestConvertTimeSeriesDataToScalar_RaggedLabels(t *testing.T) {
+ label := func(name string, value any) *qbtypes.Label {
+ return &qbtypes.Label{
+ Key: telemetrytypes.TelemetryFieldKey{Name: name},
+ Value: value,
+ }
+ }
+ series := func(labels []*qbtypes.Label, value float64) *qbtypes.TimeSeries {
+ return &qbtypes.TimeSeries{
+ Labels: labels,
+ Values: []*qbtypes.TimeSeriesValue{{Timestamp: 1, Value: value}},
+ }
+ }
+
+ tsData := &qbtypes.TimeSeriesData{
+ QueryName: "A",
+ Aggregations: []*qbtypes.AggregationBucket{{
+ Index: 0,
+ Series: []*qbtypes.TimeSeries{
+ series([]*qbtypes.Label{label("label_1", "orphan-0")}, 20),
+ series([]*qbtypes.Label{label("label_1", "box-0"), label("label_2", "rpc-0")}, 10),
+ },
+ }},
+ }
+
+ var sd *qbtypes.ScalarData
+ require.NotPanics(t, func() {
+ sd = convertTimeSeriesDataToScalar(tsData, "A")
+ })
+
+ require.NotNil(t, sd)
+ require.Len(t, sd.Columns, 3)
+ assert.Equal(t, "label_1", sd.Columns[0].Name)
+ assert.Equal(t, "label_2", sd.Columns[1].Name)
+ assert.Equal(t, "__result_0", sd.Columns[2].Name)
+
+ require.Len(t, sd.Data, 2)
+ assert.Equal(t, []any{"orphan-0", nil, 20.0}, sd.Data[0])
+ assert.Equal(t, []any{"box-0", "rpc-0", 10.0}, sd.Data[1])
+}