ParseSearchRequestWithDefault takes an http.Request and decodes query params to create a tempopb.SearchRequest using the provided default value for SpansPerSpanSet when not specified in the request
(r *http.Request, defaultSpansPerSpanSet uint32)
| 154 | // ParseSearchRequestWithDefault takes an http.Request and decodes query params to create a tempopb.SearchRequest |
| 155 | // using the provided default value for SpansPerSpanSet when not specified in the request |
| 156 | func ParseSearchRequestWithDefault(r *http.Request, defaultSpansPerSpanSet uint32) (*tempopb.SearchRequest, error) { |
| 157 | req := &tempopb.SearchRequest{ |
| 158 | Tags: map[string]string{}, |
| 159 | SpansPerSpanSet: defaultSpansPerSpanSet, |
| 160 | } |
| 161 | |
| 162 | vals := r.URL.Query() |
| 163 | |
| 164 | if s, ok := extractQueryParam(vals, urlParamStart); ok { |
| 165 | start, err := strconv.ParseUint(s, 10, 32) |
| 166 | if err != nil { |
| 167 | return nil, fmt.Errorf("invalid start: %w", err) |
| 168 | } |
| 169 | req.Start = uint32(start) |
| 170 | } |
| 171 | |
| 172 | if s, ok := extractQueryParam(vals, urlParamEnd); ok { |
| 173 | end, err := strconv.ParseUint(s, 10, 32) |
| 174 | if err != nil { |
| 175 | return nil, fmt.Errorf("invalid end: %w", err) |
| 176 | } |
| 177 | req.End = uint32(end) |
| 178 | } |
| 179 | |
| 180 | query, queryFound := extractQueryParam(vals, urlParamQuery) |
| 181 | if queryFound { |
| 182 | req.Query = query |
| 183 | } |
| 184 | |
| 185 | encodedTags, tagsFound := extractQueryParam(vals, urlParamTags) |
| 186 | if tagsFound { |
| 187 | // tags and traceQL API are mutually exclusive |
| 188 | if queryFound { |
| 189 | return nil, fmt.Errorf("invalid request: can't specify tags and q in the same query") |
| 190 | } |
| 191 | |
| 192 | decoder := logfmt.NewDecoder(strings.NewReader(encodedTags)) |
| 193 | |
| 194 | for decoder.ScanRecord() { |
| 195 | for decoder.ScanKeyval() { |
| 196 | key := string(decoder.Key()) |
| 197 | if _, ok := req.Tags[key]; ok { |
| 198 | return nil, fmt.Errorf("invalid tags: tag %s has been set twice", key) |
| 199 | } |
| 200 | req.Tags[key] = string(decoder.Value()) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if err := decoder.Err(); err != nil { |
| 205 | var syntaxErr *logfmt.SyntaxError |
| 206 | if ok := errors.As(err, &syntaxErr); ok { |
| 207 | return nil, fmt.Errorf("invalid tags: %s at pos %d", syntaxErr.Msg, syntaxErr.Pos) |
| 208 | } |
| 209 | return nil, fmt.Errorf("invalid tags: %w", err) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // if we don't have a query or tags, and we don't see start or end treat this like an old style search |
no test coverage detected