Skip to content

Report bad samples as 400s (ingester) #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions cmd/cortex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,26 @@ func setupDistributor(
prometheus.MustRegister(distributor)

prefix := "/api/prom"
http.Handle(prefix+"/push", instrument(logSuccess, cortex.AppenderHandler(distributor)))
http.Handle(prefix+"/push", instrument(logSuccess, cortex.AppenderHandler(distributor, handleDistributorError)))

// TODO: Move querier to separate binary.
setupQuerier(distributor, chunkStore, prefix, logSuccess)
}

func handleDistributorError(w http.ResponseWriter, err error) {
switch e := err.(type) {
case cortex.IngesterError:
switch {
case 400 <= e.StatusCode && e.StatusCode < 500:
log.Warnf("append err: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
log.Errorf("append err: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}

// setupQuerier sets up a complete querying pipeline:
//
// PromQL -> MergeQuerier -> Distributor -> IngesterQuerier -> Ingester
Expand Down Expand Up @@ -256,12 +270,23 @@ func setupIngester(
}
prometheus.MustRegister(ingester)

http.Handle("/push", instrument(logSuccess, cortex.AppenderHandler(ingester)))
http.Handle("/push", instrument(logSuccess, cortex.AppenderHandler(ingester, handleIngesterError)))
http.Handle("/query", instrument(logSuccess, cortex.QueryHandler(ingester)))
http.Handle("/label_values", instrument(logSuccess, cortex.LabelValuesHandler(ingester)))
return ingester
}

func handleIngesterError(w http.ResponseWriter, err error) {
switch err {
case ingester.ErrOutOfOrderSample, ingester.ErrDuplicateSampleForTimestamp:
log.Warnf("append err: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
default:
log.Errorf("append err: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

// instrument instruments a handler.
func instrument(logSuccess bool, handler http.Handler) http.Handler {
return middleware.Merge(
Expand Down
19 changes: 18 additions & 1 deletion ingester_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ type IngesterClient struct {
timeout time.Duration
}

// IngesterError is an error we got from an ingester.
type IngesterError struct {
StatusCode int
err error
}

func errStatusCode(code int, status string) IngesterError {
return IngesterError{
StatusCode: code,
err: fmt.Errorf("server returned HTTP status %s", status),
}
}

func (i IngesterError) Error() string {
return i.err.Error()
}

// NewIngesterClient makes a new IngesterClient. This client is careful to
// propagate the user ID from Distributor -> Ingester.
func NewIngesterClient(address string, timeout time.Duration) (*IngesterClient, error) {
Expand Down Expand Up @@ -185,7 +202,7 @@ func (c *IngesterClient) doRequest(ctx context.Context, endpoint string, req pro
}
defer httpResp.Body.Close()
if httpResp.StatusCode/100 != 2 {
return fmt.Errorf("server returned HTTP status %s", httpResp.Status)
return errStatusCode(httpResp.StatusCode, httpResp.Status)
}
if resp == nil {
return nil
Expand Down
6 changes: 2 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func getSamples(req *remote.WriteRequest) []*model.Sample {
}

// AppenderHandler returns a http.Handler that accepts proto encoded samples.
func AppenderHandler(appender SampleAppender) http.Handler {
func AppenderHandler(appender SampleAppender, errorHandler func(http.ResponseWriter, error)) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get(userIDHeaderName)
if userID == "" {
Expand All @@ -122,9 +122,7 @@ func AppenderHandler(appender SampleAppender) http.Handler {

err = appender.Append(ctx, getSamples(&req))
if err != nil {
log.Errorf("append err: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
errorHandler(w, err)
}
})
}
Expand Down