Files
signoz/pkg/telemetrytraces/trace_time_range.go
Nityananda Gohain 3a82085eb4 chore: enrich clickhouse log_comment (#10446)
* chore: enchance clickhouse log_comment

* chore: enrich all clickhouse queries

* fix: remove is_meatadata

* fix: use the new func

* fix: use semconv keys

* fix: add more details

* fix: minor changes

* fix: address comments

* fix: address cursor comments

* fix: minor changes

* fix: addressed comments

* fix: address comments and move to module functions where possible

---------

Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
2026-03-06 12:57:15 +00:00

73 lines
1.9 KiB
Go

package telemetrytraces
import (
"context"
"fmt"
"strings"
"github.com/SigNoz/signoz/pkg/telemetrystore"
"github.com/SigNoz/signoz/pkg/types/ctxtypes"
"github.com/SigNoz/signoz/pkg/types/instrumentationtypes"
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
)
type TraceTimeRangeFinder struct {
telemetryStore telemetrystore.TelemetryStore
}
func NewTraceTimeRangeFinder(telemetryStore telemetrystore.TelemetryStore) *TraceTimeRangeFinder {
return &TraceTimeRangeFinder{
telemetryStore: telemetryStore,
}
}
func (f *TraceTimeRangeFinder) GetTraceTimeRange(ctx context.Context, traceID string) (startNano, endNano int64, ok bool) {
traceIDs := []string{traceID}
return f.GetTraceTimeRangeMulti(ctx, traceIDs)
}
func (f *TraceTimeRangeFinder) GetTraceTimeRangeMulti(ctx context.Context, traceIDs []string) (startNano, endNano int64, ok bool) {
ctx = ctxtypes.NewContextWithCommentVals(ctx, map[string]string{
instrumentationtypes.TelemetrySignal: telemetrytypes.SignalTraces.StringValue(),
instrumentationtypes.CodeNamespace: "trace-time-range",
instrumentationtypes.CodeFunctionName: "GetTraceTimeRangeMulti",
})
if len(traceIDs) == 0 {
return 0, 0, false
}
cleanedIDs := make([]string, len(traceIDs))
for i, id := range traceIDs {
cleanedIDs[i] = strings.Trim(id, "'\"")
}
placeholders := make([]string, len(cleanedIDs))
args := make([]any, len(cleanedIDs))
for i, id := range cleanedIDs {
placeholders[i] = "?"
args[i] = id
}
query := fmt.Sprintf(`
SELECT
toUnixTimestamp64Nano(min(start)),
toUnixTimestamp64Nano(max(end))
FROM %s.%s
WHERE trace_id IN (%s)
`, DBName, TraceSummaryTableName, strings.Join(placeholders, ", "))
row := f.telemetryStore.ClickhouseDB().QueryRow(ctx, query, args...)
err := row.Scan(&startNano, &endNano)
if err != nil {
return 0, 0, false
}
if startNano > 1_000_000_000 {
startNano -= 1_000_000_000
}
endNano += 1_000_000_000
return startNano, endNano, true
}