Skip to content

Commit 663e450

Browse files
authored
[grpc][v2] Implement FindTraceIDs Call in gRPC reader for remote storage api v2 (#6858)
<!-- !! Please DELETE this comment before posting. We appreciate your contribution to the Jaeger project! 👋🎉 --> ## Which problem is this PR solving? - Towards #6789 ## Description of the changes - This PR implements the GetOperations call in the gRPC v2 API client - In the process, we also had to add a helper to convert a `pcommon.Map` to the proto representation `[]*KeyValue`. ## How was this change tested? - Added unit tests ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality - [x] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `npm run lint` and `npm run test` --------- Signed-off-by: Mahad Zaryab <[email protected]>
1 parent dca4c86 commit 663e450

File tree

3 files changed

+393
-14
lines changed

3 files changed

+393
-14
lines changed

internal/storage/v2/api/tracestore/reader.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ type GetTraceParams struct {
8383
type TraceQueryParams struct {
8484
ServiceName string
8585
OperationName string
86-
Attributes pcommon.Map
87-
StartTimeMin time.Time
88-
StartTimeMax time.Time
89-
DurationMin time.Duration
90-
DurationMax time.Duration
91-
SearchDepth int
86+
// Attributes must initialized with pcommon.NewMap() before use.
87+
Attributes pcommon.Map
88+
StartTimeMin time.Time
89+
StartTimeMax time.Time
90+
DurationMin time.Duration
91+
DurationMax time.Duration
92+
SearchDepth int
9293
}
9394

9495
// FoundTraceID is a wrapper around trace ID returned from FindTraceIDs

internal/storage/v2/grpc/tracereader.go

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"iter"
1010

11+
"go.opentelemetry.io/collector/pdata/pcommon"
1112
"go.opentelemetry.io/collector/pdata/ptrace"
1213
"google.golang.org/grpc"
1314

@@ -73,9 +74,114 @@ func (*TraceReader) FindTraces(
7374
panic("not implemented")
7475
}
7576

76-
func (*TraceReader) FindTraceIDs(
77-
context.Context,
78-
tracestore.TraceQueryParams,
77+
func (tr *TraceReader) FindTraceIDs(
78+
ctx context.Context,
79+
params tracestore.TraceQueryParams,
7980
) iter.Seq2[[]tracestore.FoundTraceID, error] {
80-
panic("not implemented")
81+
return func(yield func([]tracestore.FoundTraceID, error) bool) {
82+
resp, err := tr.client.FindTraceIDs(ctx, &storage.FindTracesRequest{
83+
Query: toProtoQueryParameters(params),
84+
})
85+
if err != nil {
86+
yield(nil, fmt.Errorf("failed to execute FindTraceIDs: %w", err))
87+
return
88+
}
89+
foundTraceIDs := make([]tracestore.FoundTraceID, len(resp.TraceIds))
90+
for i, foundTraceID := range resp.TraceIds {
91+
var sizedTraceID [16]byte
92+
copy(sizedTraceID[:], foundTraceID.TraceId)
93+
94+
foundTraceIDs[i] = tracestore.FoundTraceID{
95+
TraceID: pcommon.TraceID(sizedTraceID),
96+
Start: foundTraceID.Start,
97+
End: foundTraceID.End,
98+
}
99+
}
100+
yield(foundTraceIDs, nil)
101+
}
102+
}
103+
104+
func toProtoQueryParameters(t tracestore.TraceQueryParams) *storage.TraceQueryParameters {
105+
return &storage.TraceQueryParameters{
106+
ServiceName: t.ServiceName,
107+
OperationName: t.OperationName,
108+
Attributes: convertMapToKeyValueList(t.Attributes),
109+
StartTimeMin: t.StartTimeMin,
110+
StartTimeMax: t.StartTimeMax,
111+
DurationMin: t.DurationMin,
112+
DurationMax: t.DurationMax,
113+
SearchDepth: int32(t.SearchDepth), //nolint: gosec // G115
114+
}
115+
}
116+
117+
func convertMapToKeyValueList(m pcommon.Map) []*storage.KeyValue {
118+
keyValues := make([]*storage.KeyValue, 0, m.Len())
119+
m.Range(func(k string, v pcommon.Value) bool {
120+
keyValues = append(keyValues, &storage.KeyValue{
121+
Key: k,
122+
Value: convertValueToAnyValue(v),
123+
})
124+
return true
125+
})
126+
return keyValues
127+
}
128+
129+
func convertValueToAnyValue(v pcommon.Value) *storage.AnyValue {
130+
switch v.Type() {
131+
case pcommon.ValueTypeStr:
132+
return &storage.AnyValue{
133+
Value: &storage.AnyValue_StringValue{
134+
StringValue: v.Str(),
135+
},
136+
}
137+
case pcommon.ValueTypeBool:
138+
return &storage.AnyValue{
139+
Value: &storage.AnyValue_BoolValue{
140+
BoolValue: v.Bool(),
141+
},
142+
}
143+
case pcommon.ValueTypeInt:
144+
return &storage.AnyValue{
145+
Value: &storage.AnyValue_IntValue{
146+
IntValue: v.Int(),
147+
},
148+
}
149+
case pcommon.ValueTypeDouble:
150+
return &storage.AnyValue{
151+
Value: &storage.AnyValue_DoubleValue{
152+
DoubleValue: v.Double(),
153+
},
154+
}
155+
case pcommon.ValueTypeBytes:
156+
return &storage.AnyValue{
157+
Value: &storage.AnyValue_BytesValue{
158+
BytesValue: v.Bytes().AsRaw(),
159+
},
160+
}
161+
case pcommon.ValueTypeSlice:
162+
arr := v.Slice()
163+
arrayValues := make([]*storage.AnyValue, 0, arr.Len())
164+
for i := 0; i < arr.Len(); i++ {
165+
arrayValues = append(arrayValues, convertValueToAnyValue(arr.At(i)))
166+
}
167+
return &storage.AnyValue{
168+
Value: &storage.AnyValue_ArrayValue{
169+
ArrayValue: &storage.ArrayValue{
170+
Values: arrayValues,
171+
},
172+
},
173+
}
174+
case pcommon.ValueTypeMap:
175+
kvList := &storage.KeyValueList{}
176+
v.Map().Range(func(k string, val pcommon.Value) bool {
177+
kvList.Values = append(kvList.Values, &storage.KeyValue{
178+
Key: k,
179+
Value: convertValueToAnyValue(val),
180+
})
181+
return true
182+
})
183+
return &storage.AnyValue{Value: &storage.AnyValue_KvlistValue{KvlistValue: kvList}}
184+
default:
185+
return nil
186+
}
81187
}

0 commit comments

Comments
 (0)