MCPcopy
hub / github.com/grafana/tempo / generateFakeSearchResponse

Function generateFakeSearchResponse

pkg/traceql/fake_data.go:14–83  ·  view source on GitHub ↗

generateFakeSearchResponse creates a fake SearchResponse with a chance defined by given probability. It must be used only for testing purposes.

(probability float64)

Source from the content-addressed store, hash-verified

12// generateFakeSearchResponse creates a fake SearchResponse with a chance defined by given probability.
13// It must be used only for testing purposes.
14func generateFakeSearchResponse(probability float64) *tempopb.SearchResponse {
15 if probability <= 0 {
16 return &tempopb.SearchResponse{}
17 }
18 if probability < 1 && rand.Float64() > probability { //nolint:gosec // G404
19 return &tempopb.SearchResponse{}
20 }
21
22 numTraces := 1 + rand.Intn(3) //nolint:gosec // G404
23 traces := make([]*tempopb.TraceSearchMetadata, numTraces)
24
25 for i := range numTraces {
26 traceID := fmt.Sprintf("%016x%016x", rand.Int63(), rand.Int63()) //nolint:gosec // G404
27 startTime := time.Now().Add(-time.Duration(rand.Intn(3600)) * time.Second).UnixNano() //nolint:gosec // G404
28 duration := uint32(100 + rand.Intn(900)) //nolint:gosec // G404
29
30 numSpans := 1 + rand.Intn(5) //nolint:gosec // G404
31 spans := make([]*tempopb.Span, numSpans)
32
33 for k := range numSpans {
34 spanID := fmt.Sprintf("%016x", rand.Int63()) //nolint:gosec // G404
35 spanStartTime := uint64(startTime) + uint64(rand.Intn(int(duration)))*1000000 //nolint:gosec // G404
36 spanDuration := uint64(rand.Intn(100)) * 1000000 //nolint:gosec // G404
37
38 numAttrs := 1 + rand.Intn(3) //nolint:gosec // G404
39 attrs := make([]*v1.KeyValue, numAttrs)
40 for l := range attrs {
41 attrs[l] = &v1.KeyValue{
42 Key: fmt.Sprintf("attr-%d", l),
43 Value: &v1.AnyValue{
44 Value: &v1.AnyValue_StringValue{
45 StringValue: fmt.Sprintf("value-%d", rand.Intn(10)), //nolint:gosec // G404
46 },
47 },
48 }
49 }
50
51 spans[k] = &tempopb.Span{
52 SpanID: spanID,
53 Name: fmt.Sprintf("span-%d", k),
54 StartTimeUnixNano: spanStartTime,
55 DurationNanos: spanDuration,
56 Attributes: attrs,
57 }
58 }
59
60 spanSet := &tempopb.SpanSet{
61 Spans: spans,
62 Matched: uint32(numSpans),
63 }
64
65 traces[i] = &tempopb.TraceSearchMetadata{
66 TraceID: traceID,
67 RootServiceName: fmt.Sprintf("service-%d", rand.Intn(5)), //nolint:gosec // G404
68 RootTraceName: fmt.Sprintf("operation-%d", rand.Intn(10)), //nolint:gosec // G404
69 StartTimeUnixNano: uint64(startTime),
70 DurationMs: duration,
71 SpanSet: spanSet,

Calls 3

DurationMethod · 0.80
AddMethod · 0.65
NowMethod · 0.65