MCPcopy Index your code
hub / github.com/cortexproject/cortex / InstantQueryHandler

Method InstantQueryHandler

pkg/api/queryapi/query_api.go:154–225  ·  view source on GitHub ↗
(r *http.Request)

Source from the content-addressed store, hash-verified

152}
153
154func (q *QueryAPI) InstantQueryHandler(r *http.Request) (result apiFuncResult) {
155 // TODO(Sungjin1212): Change to emit basic error (not gRPC)
156 ts, err := util.ParseTimeParam(r, "time", q.now().Unix())
157 if err != nil {
158 return invalidParamError(err, "time")
159 }
160
161 ctx := r.Context()
162 if to := r.FormValue("timeout"); to != "" {
163 var cancel context.CancelFunc
164 timeout, err := util.ParseDurationMs(to)
165 if err != nil {
166 return invalidParamError(err, "timeout")
167 }
168
169 ctx, cancel = context.WithDeadline(ctx, q.now().Add(convertMsToDuration(timeout)))
170 defer cancel()
171 }
172
173 opts, err := extractQueryOpts(r)
174 if err != nil {
175 return apiFuncResult{nil, &apiError{errorBadData, err}, nil, nil}
176 }
177
178 ctx = engine.AddEngineTypeToContext(ctx, r)
179 ctx = querier.AddBlockStoreTypeToContext(ctx, r.Header.Get(querier.BlockStoreTypeHeader))
180
181 var qry promql.Query
182 tsTime := convertMsToTime(ts)
183
184 byteLP := []byte(r.PostFormValue("plan"))
185 if len(byteLP) != 0 {
186 logicalPlan, err := distributed_execution.Unmarshal(byteLP)
187 if err != nil {
188 return apiFuncResult{nil, &apiError{errorInternal, fmt.Errorf("invalid logical plan: %v", err)}, nil, nil}
189 }
190 qry, err = q.queryEngine.MakeInstantQueryFromPlan(ctx, q.queryable, opts, logicalPlan, tsTime, r.FormValue("query"))
191 if err != nil {
192 return apiFuncResult{nil, &apiError{errorInternal, fmt.Errorf("failed to create instant query from logical plan: %v", err)}, nil, nil}
193 }
194 } else { // if there is logical plan field is empty, fall back
195 qry, err = q.queryEngine.NewInstantQuery(ctx, q.queryable, opts, r.FormValue("query"), tsTime)
196 if err != nil {
197 return invalidParamError(httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error()), "query")
198 }
199 }
200
201 // From now on, we must only return with a finalizer in the result (to
202 // be called by the caller) or call qry.Close ourselves (which is
203 // required in the case of a panic).
204 defer func() {
205 if result.finalizer == nil {
206 qry.Close()
207 }
208 }()
209
210 ctx = httputil.ContextFromRequest(ctx, r)
211

Callers

nothing calls this directly

Calls 15

ParseTimeParamFunction · 0.92
ParseDurationMsFunction · 0.92
AddEngineTypeToContextFunction · 0.92
UnmarshalFunction · 0.92
invalidParamErrorFunction · 0.85
convertMsToDurationFunction · 0.85
extractQueryOptsFunction · 0.85
convertMsToTimeFunction · 0.85
returnAPIErrorFunction · 0.85
NewInstantQueryMethod · 0.80
TypeMethod · 0.80

Tested by

no test coverage detected