github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/scripts/decode-resp/main.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "flag" 6 "fmt" 7 "os" 8 "path/filepath" 9 ) 10 11 func usage() { 12 f, w := flag.CommandLine, flag.CommandLine.Output() 13 fmt.Fprintf(w, `Decode response from pyroscope, for debugging 14 15 Usage: decode-resp -file <response.json> 16 where <response.json> is the body response from /render API 17 example: http://localhost:4040/render?from=now-1h&until=now&name=pyroscope.server.alloc_objects{}&max-nodes=1024&format=json 18 19 Flags: 20 `) 21 f.PrintDefaults() 22 } 23 24 func main() { 25 var inFile string 26 var outFile string 27 28 flag.StringVar(&inFile, "file", "", "path to response file (required)") 29 flag.StringVar(&outFile, "out", "", "name of output file (default to NAME.out.EXT)") 30 flag.Usage = usage 31 flag.Parse() 32 33 if inFile == "" { 34 usage() 35 os.Exit(2) 36 } 37 if outFile == "" { 38 dir, base := filepath.Dir(inFile), filepath.Base(inFile) 39 ext := filepath.Ext(base) 40 name := base[:len(base)-len(ext)] 41 outFile = filepath.Join(dir, name+".out"+ext) 42 } 43 44 // read file 45 inData, err := os.ReadFile(inFile) 46 must(err) 47 var input Input 48 must(json.Unmarshal(inData, &input)) 49 50 // decode 51 output := decodeLevels(&input) 52 53 // write file 54 outData, err := json.MarshalIndent(output, "", " ") 55 must(err) 56 must(os.WriteFile(outFile, outData, 0644)) 57 58 fmt.Fprintf(os.Stderr, "decoded to %v\n", outFile) 59 } 60 61 func must(err error) { 62 if err != nil { 63 panic(err) 64 } 65 }