Skip to content

Commit

Permalink
Add statistics to query_range and instant_query API. (#1615)
Browse files Browse the repository at this point in the history
* Refactor stats package to use proto.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Merge statistics in the frontend.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Improve engine logging.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Fixes GRPC by rolling back

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Add a test to verify stats in the LogQL engine.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Set circle ci build image to 0.9.1 which contains protobuf regression

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Fix drone CI.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Change timing data from nanoseconds to seconds like Prometheus.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Improve naming of stats

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Move stats property under data property like Prometheus.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Adjust queryrange frontend to use the new stats property

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Cleanup unused structs.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Add documentation for stats API.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Format proto file.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Adds missing godoc on logql.Result

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
cyriltovena authored Feb 4, 2020
1 parent 1773900 commit 93eb4ad
Show file tree
Hide file tree
Showing 32 changed files with 3,595 additions and 917 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ workflows:
# https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/
.defaults: &defaults
docker:
- image: grafana/loki-build-image:0.9.0
- image: grafana/loki-build-image:0.9.1
working_directory: /src/loki

jobs:
Expand Down
10 changes: 5 additions & 5 deletions .drone/drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ workspace:

steps:
- name: test
image: grafana/loki-build-image:0.9.0
image: grafana/loki-build-image:0.9.1
commands:
- make BUILD_IN_CONTAINER=false test
depends_on:
- clone

- name: lint
image: grafana/loki-build-image:0.9.0
image: grafana/loki-build-image:0.9.1
commands:
- make BUILD_IN_CONTAINER=false lint
depends_on:
- clone

- name: check-generated-files
image: grafana/loki-build-image:0.9.0
image: grafana/loki-build-image:0.9.1
commands:
- make BUILD_IN_CONTAINER=false check-generated-files
depends_on:
- clone

- name: check-mod
image: grafana/loki-build-image:0.9.0
image: grafana/loki-build-image:0.9.1
commands:
- make BUILD_IN_CONTAINER=false check-mod
depends_on:
Expand Down Expand Up @@ -562,7 +562,7 @@ platform:

steps:
- name: trigger
image: grafana/loki-build-image:0.9.0
image: grafana/loki-build-image:0.9.1
commands:
- ./tools/deploy.sh
environment:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ IMAGE_NAMES := $(foreach dir,$(DOCKER_IMAGE_DIRS),$(patsubst %,$(IMAGE_PREFIX)%,
# make BUILD_IN_CONTAINER=false target
# or you can override this with an environment variable
BUILD_IN_CONTAINER ?= true
BUILD_IMAGE_VERSION := 0.9.0
BUILD_IMAGE_VERSION := 0.9.1

# Docker image info
IMAGE_PREFIX ?= grafana
Expand Down
2 changes: 1 addition & 1 deletion cmd/docker-driver/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BUILD_IMAGE=grafana/loki-build-image:0.9.0
ARG BUILD_IMAGE=grafana/loki-build-image:0.9.1
# Directories in this file are referenced from the root of the project not this folder
# This file is intended to be called from the root like so:
# docker build -t grafana/loki -f cmd/loki/Dockerfile .
Expand Down
2 changes: 1 addition & 1 deletion cmd/loki-canary/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BUILD_IMAGE=grafana/loki-build-image:0.9.0
ARG BUILD_IMAGE=grafana/loki-build-image:0.9.1
# Directories in this file are referenced from the root of the project not this folder
# This file is intended to be called from the root like so:
# docker build -t grafana/promtail -f cmd/promtail/Dockerfile .
Expand Down
2 changes: 1 addition & 1 deletion cmd/loki/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BUILD_IMAGE=grafana/loki-build-image:0.9.0
ARG BUILD_IMAGE=grafana/loki-build-image:0.9.1
# Directories in this file are referenced from the root of the project not this folder
# This file is intended to be called from the root like so:
# docker build -t grafana/loki -f cmd/loki/Dockerfile .
Expand Down
3 changes: 1 addition & 2 deletions cmd/promtail/Dockerfile.cross
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BUILD_IMAGE=grafana/loki-build-image:0.9.0
ARG BUILD_IMAGE=grafana/loki-build-image:0.9.1
# Directories in this file are referenced from the root of the project not this folder
# This file is intended to be called from the root like so:
# docker build -t grafana/promtail -f cmd/promtail/Dockerfile .
Expand All @@ -23,4 +23,3 @@ COPY --from=build /src/loki/cmd/promtail/promtail /usr/bin/promtail
COPY cmd/promtail/promtail-local-config.yaml /etc/promtail/local-config.yaml
COPY cmd/promtail/promtail-docker-config.yaml /etc/promtail/docker-config.yaml
ENTRYPOINT ["/usr/bin/promtail"]

94 changes: 82 additions & 12 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ These endpoints are exposed by all components:
- [`GET /ready`](#get-ready)
- [`GET /metrics`](#get-metrics)

These endpoints are exposed by just the querier:
These endpoints are exposed by the querier and the frontend:

- [`GET /loki/api/v1/query`](#get-lokiapiv1query)
- [`GET /loki/api/v1/query_range`](#get-lokiapiv1query_range)
Expand Down Expand Up @@ -86,7 +86,7 @@ query parameters support the following values:
- `time`: The evaluation time for the query as a nanosecond Unix epoch. Defaults to now.
- `direction`: Determines the sort order of logs. Supported values are `forward` or `backward`. Defaults to `backward.`

In microservices mode, `/loki/api/v1/query` is exposed by the querier.
In microservices mode, `/loki/api/v1/query` is exposed by the querier and the frontend.

Response:

Expand All @@ -95,7 +95,8 @@ Response:
"status": "success",
"data": {
"resultType": "vector" | "streams",
"result": [<vector value>] | [<stream value>]
"result": [<vector value>] | [<stream value>].
"stats" : [<statistics>]
}
}
```
Expand Down Expand Up @@ -131,6 +132,8 @@ And `<stream value>` is:
}
```

See [statistics](#Statistics) for information about the statistics returned by Loki.

### Examples

```bash
Expand Down Expand Up @@ -165,7 +168,10 @@ $ curl -G -s "http://localhost:3100/loki/api/v1/query" --data-urlencode 'query=
"37.69"
]
}
]
],
"stats": {
...
}
}
}
```
Expand All @@ -192,9 +198,12 @@ $ curl -G -s "http://localhost:3100/loki/api/v1/query" --data-urlencode 'query=
"1568234269716526880",
"bar"
]
]
],
}
]
],
"stats": {
...
}
}
}
```
Expand All @@ -216,7 +225,7 @@ find log streams for particular labels. Because the index store is spread out by
time, the time span covered by `start` and `end`, if large, may cause additional
load against the index server and result in a slow query.

In microservices mode, `/loki/api/v1/query_range` is exposed by the querier.
In microservices mode, `/loki/api/v1/query_range` is exposed by the querier and the frontend.

Response:

Expand All @@ -226,6 +235,7 @@ Response:
"data": {
"resultType": "matrix" | "streams",
"result": [<matrix value>] | [<stream value>]
"stats" : [<statistics>]
}
}
```
Expand Down Expand Up @@ -261,6 +271,8 @@ And `<stream value>` is:
}
```

See [statistics](#Statistics) for information about the statistics returned by Loki.

### Examples

```bash
Expand Down Expand Up @@ -308,7 +320,10 @@ $ curl -G -s "http://localhost:3100/loki/api/v1/query_range" --data-urlencode '
]
]
}
]
],
"stats": {
...
}
}
}
```
Expand Down Expand Up @@ -337,7 +352,10 @@ $ curl -G -s "http://localhost:3100/loki/api/v1/query_range" --data-urlencode '
]
]
}
]
],
"stats": {
...
}
}
}
```
Expand Down Expand Up @@ -564,7 +582,7 @@ support the following values:
- `direction`: Determines the sort order of logs. Supported values are `forward` or `backward`. Defaults to `backward.`
- `regexp`: a regex to filter the returned results

In microservices mode, `/api/prom/query` is exposed by the querier.
In microservices mode, `/api/prom/query` is exposed by the querier and the frontend.

Note that the larger the time span between `start` and `end` will cause
additional load on Loki and the index store, resulting in slower queries.
Expand All @@ -585,10 +603,13 @@ Response:
],
},
...
]
],
"stats": [<statistics>]
}
```

See [statistics](#Statistics) for information about the statistics returned by Loki.

### Examples

```bash
Expand All @@ -608,7 +629,10 @@ $ curl -G -s "http://localhost:3100/api/prom/query" --data-urlencode '{foo="bar
}
]
}
]
],
"stats": {
...
}
}
```

Expand Down Expand Up @@ -822,3 +846,49 @@ $ curl -s "http://localhost:3100/loki/api/v1/series" --data-urlencode 'match={co
]
}
```

## Statistics

Query endpoints such as `/api/prom/query`, `/loki/api/v1/query` and `/loki/api/v1/query_range` return a set of statistics about the query execution. Those statistics allow users to understand the amount of data processed and at which speed.

The example belows show all possible statistics returned with their respective description.

```json
{
"status": "success",
"data": {
"resultType": "streams",
"result": [],
"stats": {
"ingester" : {
"compressedBytes": 0, // Total bytes of compressed chunks (blocks) processed by ingesters
"decompressedBytes": 0, // Total bytes decompressed and processed by ingesters
"decompressedLines": 0, // Total lines decompressed and processed by ingesters
"headChunkBytes": 0, // Total bytes read from ingesters head chunks
"headChunkLines": 0, // Total lines read from ingesters head chunks
"totalBatches": 0, // Total batches sent by ingesters
"totalChunksMatched": 0, // Total chunks matched by ingesters
"totalDuplicates": 0, // Total of duplicates found by ingesters
"totalLinesSent": 0, // Total lines sent by ingesters
"totalReached": 0 // Amount of ingesters reached.
},
"store": {
"compressedBytes": 0, // Total bytes of compressed chunks (blocks) processed by the store
"decompressedBytes": 0, // Total bytes decompressed and processed by the store
"decompressedLines": 0, // Total lines decompressed and processed by the store
"chunksDownloadTime": 0, // Total time spent downloading chunks in seconds (float)
"totalChunksRef": 0, // Total chunks found in the index for the current query
"totalChunksDownloaded": 0, // Total of chunks downloaded
"totalDuplicates": 0 // Total of duplicates removed from replication
},
"summary": {
"bytesProcessedPerSeconds": 0, // Total of bytes processed per seconds
"execTime": 0, // Total execution time in seconds (float)
"linesProcessedPerSeconds": 0, // Total lines processed per second
"totalBytesProcessed":0, // Total amount of bytes processed overall for this request
"totalLinesProcessed":0 // Total amount of lines processed overall for this request
}
}
}
}
```
4 changes: 2 additions & 2 deletions loki-build-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ COPY --from=drone /go/bin/drone /usr/bin/drone
# It's possible this can be revisited in newer versions of Go if the behavior around GOPATH vs GO111MODULES changes
RUN GO111MODULE=on go get \
github.com/golang/protobuf/protoc-gen-go@v1.3.0 \
github.com/gogo/protobuf/protoc-gen-gogoslick@v1.3.0 \
github.com/gogo/protobuf/gogoproto@v1.3.0 \
github.com/gogo/protobuf/protoc-gen-gogoslick@v1.2.1 \
github.com/gogo/protobuf/gogoproto@v1.2.1 \
github.com/go-delve/delve/cmd/dlv \
# Due to the lack of a proper release tag, we use the commit hash of
# https://github.com/golang/tools/releases v0.1.7
Expand Down
10 changes: 10 additions & 0 deletions pkg/iter/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ type EntryIterator interface {
Close() error
}

type noOpIterator struct{}

var NoopIterator = noOpIterator{}

func (noOpIterator) Next() bool { return false }
func (noOpIterator) Error() error { return nil }
func (noOpIterator) Labels() string { return "" }
func (noOpIterator) Entry() logproto.Entry { return logproto.Entry{} }
func (noOpIterator) Close() error { return nil }

// streamIterator iterates over entries in a stream.
type streamIterator struct {
i int
Expand Down
12 changes: 8 additions & 4 deletions pkg/loghttp/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"unsafe"

"github.com/grafana/loki/pkg/logproto"
"github.com/grafana/loki/pkg/logql/stats"
json "github.com/json-iterator/go"
"github.com/prometheus/common/model"
)
Expand Down Expand Up @@ -55,8 +56,9 @@ type ResultValue interface {

//QueryResponseData represents the http json response to a label query
type QueryResponseData struct {
ResultType ResultType `json:"resultType"`
Result ResultValue `json:"result"`
ResultType ResultType `json:"resultType"`
Result ResultValue `json:"result"`
Statistics stats.Result `json:"stats"`
}

// Type implements the promql.Value interface
Expand Down Expand Up @@ -98,8 +100,9 @@ type Entry struct {
// UnmarshalJSON implements the json.Unmarshaler interface.
func (q *QueryResponseData) UnmarshalJSON(data []byte) error {
unmarshal := struct {
Type ResultType `json:"resultType"`
Result json.RawMessage `json:"result"`
Type ResultType `json:"resultType"`
Result json.RawMessage `json:"result"`
Statistics stats.Result `json:"stats"`
}{}

err := json.Unmarshal(data, &unmarshal)
Expand Down Expand Up @@ -133,6 +136,7 @@ func (q *QueryResponseData) UnmarshalJSON(data []byte) error {

q.ResultType = unmarshal.Type
q.Result = value
q.Statistics = unmarshal.Statistics

return nil
}
Expand Down
Loading

0 comments on commit 93eb4ad

Please sign in to comment.