|
8 | 8 | "fmt"
|
9 | 9 | "iter"
|
10 | 10 |
|
| 11 | + "go.opentelemetry.io/collector/pdata/pcommon" |
11 | 12 | "go.opentelemetry.io/collector/pdata/ptrace"
|
12 | 13 | "google.golang.org/grpc"
|
13 | 14 |
|
@@ -73,9 +74,114 @@ func (*TraceReader) FindTraces(
|
73 | 74 | panic("not implemented")
|
74 | 75 | }
|
75 | 76 |
|
76 |
| -func (*TraceReader) FindTraceIDs( |
77 |
| - context.Context, |
78 |
| - tracestore.TraceQueryParams, |
| 77 | +func (tr *TraceReader) FindTraceIDs( |
| 78 | + ctx context.Context, |
| 79 | + params tracestore.TraceQueryParams, |
79 | 80 | ) 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 | + } |
81 | 187 | }
|
0 commit comments