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.

feat: return all spans for flamegraph under a limit (#10757)

Nikhil Soni23e3c75d245f
diff --git a/pkg/query-service/app/clickhouseReader/reader.go b/pkg/query-service/app/clickhouseReader/reader.go
index 5bbacaac72..f11a163226 100644
--- a/pkg/query-service/app/clickhouseReader/reader.go
+++ b/pkg/query-service/app/clickhouseReader/reader.go
@@ -18,6 +18,7 @@ import (
"github.com/uptrace/bun"
"github.com/SigNoz/signoz/pkg/prometheus"
+ "github.com/SigNoz/signoz/pkg/query-service/utils/timestamp"
"github.com/SigNoz/signoz/pkg/sqlstore"
"github.com/SigNoz/signoz/pkg/telemetrystore"
"github.com/SigNoz/signoz/pkg/types"
@@ -1257,7 +1258,7 @@ func (r *ClickHouseReader) GetFlamegraphSpansForTrace(ctx context.Context, orgID
}
}
- selectedSpans = tracedetail.GetSelectedSpansForFlamegraph(traceRoots, spanIdToSpanNodeMap)
+ selectedSpans = tracedetail.GetAllSpansForFlamegraph(traceRoots, spanIdToSpanNodeMap)
traceCache := model.GetFlamegraphSpansForTraceCache{
StartTime: startTime,
EndTime: endTime,
@@ -1274,12 +1275,25 @@ func (r *ClickHouseReader) GetFlamegraphSpansForTrace(ctx context.Context, orgID
}
processingPostCache := time.Now()
- selectedSpansForRequest := tracedetail.GetSelectedSpansForFlamegraphForRequest(req.SelectedSpanID, selectedSpans, startTime, endTime)
- r.logger.Info("getFlamegraphSpansForTrace: processing post cache", "duration", time.Since(processingPostCache), "traceID", traceID)
+ selectedSpansForRequest := selectedSpans
+ clientLimit := min(req.Limit, tracedetail.MaxLimitWithoutSampling)
+ totalSpanCount := tracedetail.GetTotalSpanCount(selectedSpans)
+ if totalSpanCount > uint64(clientLimit) {
+ // using trace start and end time if boundary ts are set to zero (or not set)
+ boundaryStart := max(timestamp.MilliToNano(req.BoundaryStartTS), startTime)
+ boundaryEnd := timestamp.MilliToNano(req.BoundaryEndTS)
+ if boundaryEnd == 0 {
+ boundaryEnd = endTime
+ }
+
+ selectedSpansForRequest = tracedetail.GetSelectedSpansForFlamegraphForRequest(req.SelectedSpanID, selectedSpans, boundaryStart, boundaryEnd)
+ }
+ r.logger.Debug("getFlamegraphSpansForTrace: processing post cache", "duration", time.Since(processingPostCache), "traceID", traceID, "totalSpans", totalSpanCount, "limit", clientLimit)
trace.Spans = selectedSpansForRequest
trace.StartTimestampMillis = startTime / 1000000
trace.EndTimestampMillis = endTime / 1000000
+ trace.HasMore = totalSpanCount > uint64(clientLimit)
return trace, nil
}
diff --git a/pkg/query-service/app/traces/tracedetail/flamegraph.go b/pkg/query-service/app/traces/tracedetail/flamegraph.go
index 02bfa91913..745123364d 100644
--- a/pkg/query-service/app/traces/tracedetail/flamegraph.go
+++ b/pkg/query-service/app/traces/tracedetail/flamegraph.go
@@ -7,9 +7,12 @@ import (
)
var (
- SPAN_LIMIT_PER_REQUEST_FOR_FLAMEGRAPH float64 = 50
- SPAN_LIMIT_PER_LEVEL int = 100
- TIMESTAMP_SAMPLING_BUCKET_COUNT int = 50
+ flamegraphSpanLevelLimit float64 = 50
+ flamegraphSpanLimitPerLevel int = 100
+ flamegraphSamplingBucketCount int = 50
+ flamegraphTopLatencySpanCount int = 5
+
+ MaxLimitWithoutSampling uint = 120_000
)
func ContainsFlamegraphSpan(slice []*model.FlamegraphSpan, item *model.FlamegraphSpan) bool {
@@ -52,7 +55,8 @@ func FindIndexForSelectedSpan(spans [][]*model.FlamegraphSpan, selectedSpanId st
return selectedSpanLevel
}
-func GetSelectedSpansForFlamegraph(traceRoots []*model.FlamegraphSpan, spanIdToSpanNodeMap map[string]*model.FlamegraphSpan) [][]*model.FlamegraphSpan {
+// GetAllSpansForFlamegraph groups all spans as per their level
+func GetAllSpansForFlamegraph(traceRoots []*model.FlamegraphSpan, spanIdToSpanNodeMap map[string]*model.FlamegraphSpan) [][]*model.FlamegraphSpan {
var traceIdLevelledFlamegraph = map[string]map[int64][]*model.FlamegraphSpan{}
selectedSpans := [][]*model.FlamegraphSpan{}
@@ -100,7 +104,7 @@ func getLatencyAndTimestampBucketedSpans(spans []*model.FlamegraphSpan, selected
})
// pick the top 5 latency spans
- for idx := range 5 {
+ for idx := range flamegraphTopLatencySpanCount {
sampledSpans = append(sampledSpans, spans[idx])
}
@@ -117,17 +121,17 @@ func getLatencyAndTimestampBucketedSpans(spans []*model.FlamegraphSpan, selected
}
}
- bucketSize := (endTime - startTime) / uint64(TIMESTAMP_SAMPLING_BUCKET_COUNT)
+ bucketSize := (endTime - startTime) / uint64(flamegraphSamplingBucketCount)
if bucketSize == 0 {
bucketSize = 1
}
- bucketedSpans := make([][]*model.FlamegraphSpan, 50)
+ bucketedSpans := make([][]*model.FlamegraphSpan, flamegraphSamplingBucketCount)
for _, span := range spans {
if span.TimeUnixNano >= startTime && span.TimeUnixNano <= endTime {
bucketIndex := int((span.TimeUnixNano - startTime) / bucketSize)
- if bucketIndex >= 0 && bucketIndex < 50 {
+ if bucketIndex >= 0 && bucketIndex < flamegraphSamplingBucketCount {
bucketedSpans[bucketIndex] = append(bucketedSpans[bucketIndex], span)
}
}
@@ -156,8 +160,8 @@ func GetSelectedSpansForFlamegraphForRequest(selectedSpanID string, selectedSpan
selectedIndex = FindIndexForSelectedSpan(selectedSpans, selectedSpanID)
}
- lowerLimit := selectedIndex - int(SPAN_LIMIT_PER_REQUEST_FOR_FLAMEGRAPH*0.4)
- upperLimit := selectedIndex + int(SPAN_LIMIT_PER_REQUEST_FOR_FLAMEGRAPH*0.6)
+ lowerLimit := selectedIndex - int(flamegraphSpanLevelLimit*0.4)
+ upperLimit := selectedIndex + int(flamegraphSpanLevelLimit*0.6)
if lowerLimit < 0 {
upperLimit = upperLimit - lowerLimit
@@ -174,7 +178,7 @@ func GetSelectedSpansForFlamegraphForRequest(selectedSpanID string, selectedSpan
}
for i := lowerLimit; i < upperLimit; i++ {
- if len(selectedSpans[i]) > SPAN_LIMIT_PER_LEVEL {
+ if len(selectedSpans[i]) > flamegraphSpanLimitPerLevel {
_spans := getLatencyAndTimestampBucketedSpans(selectedSpans[i], selectedSpanID, i == selectedIndex, startTime, endTime)
selectedSpansForRequest = append(selectedSpansForRequest, _spans)
} else {
@@ -184,3 +188,12 @@ func GetSelectedSpansForFlamegraphForRequest(selectedSpanID string, selectedSpan
return selectedSpansForRequest
}
+
+func GetTotalSpanCount(spans [][]*model.FlamegraphSpan) uint64 {
+ levelCount := len(spans)
+ spanCount := uint64(0)
+ for i := range levelCount {
+ spanCount += uint64(len(spans[i]))
+ }
+ return spanCount
+}
diff --git a/pkg/query-service/model/queryParams.go b/pkg/query-service/model/queryParams.go
index b8a9059ec3..56faa8032f 100644
--- a/pkg/query-service/model/queryParams.go
+++ b/pkg/query-service/model/queryParams.go
@@ -337,7 +337,10 @@ type GetWaterfallSpansForTraceWithMetadataParams struct {
}
type GetFlamegraphSpansForTraceParams struct {
- SelectedSpanID string `json:"selectedSpanId"`
+ SelectedSpanID string `json:"selectedSpanId"`
+ Limit uint `json:"limit"`
+ BoundaryStartTS uint64 `json:"boundaryStartTsMilli"`
+ BoundaryEndTS uint64 `json:"boundarEndTsMilli"`
}
type SpanFilterParams struct {
diff --git a/pkg/query-service/model/response.go b/pkg/query-service/model/response.go
index ede438dc3d..9e83bca7a1 100644
--- a/pkg/query-service/model/response.go
+++ b/pkg/query-service/model/response.go
@@ -337,6 +337,7 @@ type GetFlamegraphSpansForTraceResponse struct {
EndTimestampMillis uint64 `json:"endTimestampMillis"`
DurationNano uint64 `json:"durationNano"`
Spans [][]*FlamegraphSpan `json:"spans"`
+ HasMore bool `json:"hasMore"`
}
type OtelSpanRef struct {
diff --git a/pkg/query-service/utils/timestamp/timestamp.go b/pkg/query-service/utils/timestamp/timestamp.go
index 6bc2b64ee8..71e8c6829d 100644
--- a/pkg/query-service/utils/timestamp/timestamp.go
+++ b/pkg/query-service/utils/timestamp/timestamp.go
@@ -11,3 +11,7 @@ func FromTime(t time.Time) int64 {
func Time(ts int64) time.Time {
return time.Unix(ts/1000, (ts%1000)*int64(time.Millisecond))
}
+
+func MilliToNano(milliTS uint64) uint64 {
+ return milliTS * 1000_000
+}