Skip to content

Commit

Permalink
grpcurl-query-ingesters improvements (#8598)
Browse files Browse the repository at this point in the history
* Make download-chunks-from-ingesters.sh work when executed from any directory, and extract output directory to a variable

* Convert tabs to spaces

* Simplify `SCRIPT_DIR` definition

* Parse all responses in the file.

Signed-off-by: Peter Štibraný <pstibrany@gmail.com>

* Add note about manually modifying `mimir.proto`

---------

Signed-off-by: Peter Štibraný <pstibrany@gmail.com>
Co-authored-by: Peter Štibraný <pstibrany@gmail.com>
  • Loading branch information
charleskorn and pstibrany authored Jul 8, 2024
1 parent 4603e4c commit 003d6e8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
5 changes: 3 additions & 2 deletions tools/grpcurl-query-ingesters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ A simple hacky script + tool to download chunks from ingesters and dump their co

# How to use it

1. Edit `pkg/ingester/client/ingester.proto` and change the `import "github.com/grafana/mimir/pkg/mimirpb/mimir.proto"` statement to `import "pkg/mimirpb/mimir.proto"`
1. Edit `download-chunks-from-ingesters-query.json` with the label matchers and time range to query.
2. Edit `download-chunks-from-ingesters.sh` with the configuration about the Kubernetes namespace and Mimir tenant to query.
3. Once you've got the dump (1 file per ingester), run the go tool in this directory to print the dump content of 1+ files.
1. Edit `download-chunks-from-ingesters.sh` with the configuration about the Kubernetes namespace and Mimir tenant to query.
1. Once you've got the dump (1 file per ingester), run the go tool in this directory to print the dump content of 1+ files.
21 changes: 14 additions & 7 deletions tools/grpcurl-query-ingesters/download-chunks-from-ingesters.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ K8S_NAMESPACE=""
MIMIR_TENANT_ID=""
# End of configuration.

mkdir -p chunks-dump/
SCRIPT_DIR=$(realpath "$(dirname "${0}")")
OUTPUT_DIR="chunks-dump"

mkdir -p "$OUTPUT_DIR"

# Get list of pods.
PODS=$(kubectl --context "$K8S_CONTEXT" -n "$K8S_NAMESPACE" get pods --no-headers | grep ingester | awk '{print $1}')
Expand All @@ -22,14 +25,18 @@ for POD in $PODS; do
# Wait some time
sleep 5

cat query.json | grpcurl \
# HACK
# If you get an error resolving the reference to "github.com/grafana/mimir/pkg/mimirpb/mimir.proto" in
# pkg/ingester/client/ingester.proto, you need to manually modify the import statement to be just
# "pkg/mimirpb/mimir.proto".
cat "$SCRIPT_DIR/download-chunks-from-ingesters-query.json" | grpcurl \
-d @ \
-H "X-Scope-OrgID: $MIMIR_TENANT_ID" \
-proto pkg/ingester/client/ingester.proto \
-import-path . \
-import-path ./vendor \
-plaintext \
localhost:9095 "cortex.Ingester/QueryStream" > "chunks-dump/$POD"
-proto pkg/ingester/client/ingester.proto \
-import-path "$SCRIPT_DIR" \
-import-path "$SCRIPT_DIR/vendor" \
-plaintext \
localhost:9095 "cortex.Ingester/QueryStream" > "$OUTPUT_DIR/$POD"

kill $KUBECTL_PID
wait $KUBECTL_PID
Expand Down
28 changes: 20 additions & 8 deletions tools/grpcurl-query-ingesters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"os"
"time"

Expand All @@ -23,31 +25,41 @@ func main() {
}

for _, arg := range args {
res, err := parseFile(arg)
resps, err := parseFile(arg)
if err != nil {
fmt.Println("Failed to parse file:", err.Error())
os.Exit(1)
}

dumpResponse(res)
for _, res := range resps {
dumpResponse(res)
}
}
}

func parseFile(file string) (QueryStreamResponse, error) {
res := QueryStreamResponse{}
func parseFile(file string) ([]QueryStreamResponse, error) {
resps := []QueryStreamResponse{}

fileData, err := os.ReadFile(file)
if err != nil {
return res, err
return nil, err
}

// Decode file.
decoder := json.NewDecoder(bytes.NewReader(fileData))
if err := decoder.Decode(&res); err != nil {
return res, err
for {
res := QueryStreamResponse{}
if err := decoder.Decode(&res); err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, err
}

resps = append(resps, res)
}

return res, nil
return resps, nil
}

func dumpResponse(res QueryStreamResponse) {
Expand Down

0 comments on commit 003d6e8

Please sign in to comment.