grafana / loki

Like Prometheus, but for logs.
https://grafana.com/loki
GNU Affero General Public License v3.0
23.39k stars 3.39k forks source link

/api/prom/push produce error 400 with 'unexpected IDENTIFIER' #1727

Closed mailtosmyk closed 4 years ago

mailtosmyk commented 4 years ago

Describe the bug When trying to submit stream thru api endpoint "/api/prom/push" loki return error code 400 with not helpful message "parse error at line 1, col 1: syntax error: unexpected IDENTIFIER"

To Reproduce 1 clone grafana/loki from https://github.com/grafana/loki 2 Run docker-compose up 3 Using chrome plugin ARC create POST request with based on documentation from https://github.com/grafana/loki/blob/v1.3.0/docs/api.md#get-lokiapiv1labels:

URL: http://localhost:3100/api/prom/push
Body content: application/json
body: {"streams":[{"labels":"cat","entries":[{"ts":"2018-12-18T08:28:06.801064-04:00","line":"fizzbuzz"}]}]}

CURL output: c:\test\loki\production\loki-mixin>curl -H "Content-Type: application/json" -X POST "http://localhost:3100/api/prom/push" -d "{\"streams\": [{ \"labels\": \"cats\", \"entries\": [{ \"ts\": \"2018-12-18T08:28:06.801064-04:00\", \"line\": \"fizzbuzz\" }] }]}" parse error at line 1, col 1: syntax error: unexpected IDENTIFIER

4 Loki server return "parse error at line 1, col 1: syntax error: unexpected IDENTIFIER"

Expected behavior HTTP status code 2XX or better explanation what is missing/incorrect

Environment:

cyriltovena commented 4 years ago

The error should be better, the label field is incorrect. See https://github.com/grafana/loki/blob/master/docs/api.md#examples-8

Also I recommend you to use the v1 API see the link above.

talham7391 commented 4 years ago

I can take a look at this!

cyriltovena commented 4 years ago

Sure I'll feed you with more info on how to on Monday!

Pixelstudio commented 4 years ago

Hi, Iv encoured the same error using the following json, im using the new api : loki/api/v1/push

{"streams": [{ "labels": "{foo=\"bar\"}", "entries": [{ "ts": "2018-12-18T08:28:06.801064-04:00", "line": "fizzbuzz" }] }]}

The error is : parse error at line 1, col 2: syntax error: unexpected }, expecting IDENTIFIER

cyriltovena commented 4 years ago

I’m sorry you’re encountering this again, the v1 api has a different label format.

See https://github.com/grafana/loki/blob/master/docs/api.md#examples-4

Basically now it’s a map before it was a string.

cyriltovena commented 4 years ago

@talham7391 It's happening here: https://github.com/grafana/loki/blob/master/pkg/distributor/validator.go#L60

I would change add more context to the error. Something like

return httpgrpc.Errorf(http.StatusBadRequest, fmt.Errorf("error parsing labels: %v", err.Error()))
talham7391 commented 4 years ago

Okay great, will push a PR soon!