Skip to content
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

Improves generate-sample-config script #236

Merged
merged 8 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions pkg/cli/cli_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ func getInstallPrefix() string {

return filepath.Clean(filepath.Join(cellarPath, "../"))
}

func resolvePath(path string) string {
if res, err := filepath.EvalSymlinks(path); err == nil {
return res
}
return path
}
74 changes: 74 additions & 0 deletions scripts/generate-sample-config/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!-- generate-sample-config:server:md -->
| Name | Default Value | Usage |
| :- | :- | :- |
| analytics-opt-out | false | "disables analytics" |
| log-level | info | "log level: debug|info|warn|error" |
| badger-log-level | error | "log level: debug|info|warn|error" |
| storage-path | /var/lib/pyroscope | "directory where pyroscope stores profiling data" |
| api-bind-addr | :4040 | "port for the HTTP server used for data ingestion and web UI" |
| base-url | | "base URL for when the server is behind a reverse proxy with a different path" |
| cache-dimension-size | 1000 | "max number of elements in LRU cache for dimensions" |
| cache-dictionary-size | 1000 | "max number of elements in LRU cache for dictionaries" |
| cache-segment-size | 1000 | "max number of elements in LRU cache for segments" |
| cache-tree-size | 1000 | "max number of elements in LRU cache for trees" |
| badger-no-truncate | false | "indicates whether value log files should be truncated to delete corrupt data, if any" |
| max-nodes-serialization | 2048 | "max number of nodes used when saving profiles to disk" |
| max-nodes-render | 8192 | "max number of nodes used to display data on the frontend" |
| hide-applications | | "please don't use, this will soon be deprecated" |
| out-of-space-threshold | 512.00 MB | "Threshold value to consider out of space in bytes" |
| sample-rate | 100 | "sample rate for the profiler in Hz. 100 means reading 100 times per second" |
<!-- /generate-sample-config -->

<!-- generate-sample-config:server:yaml -->
```yaml
---
# disables analytics
analytics-opt-out: "false"

# log level: debug|info|warn|error
log-level: "info"

# log level: debug|info|warn|error
badger-log-level: "error"

# directory where pyroscope stores profiling data
storage-path: "/var/lib/pyroscope"

# port for the HTTP server used for data ingestion and web UI
api-bind-addr: ":4040"

# base URL for when the server is behind a reverse proxy with a different path
base-url: ""

# max number of elements in LRU cache for dimensions
cache-dimension-size: "1000"

# max number of elements in LRU cache for dictionaries
cache-dictionary-size: "1000"

# max number of elements in LRU cache for segments
cache-segment-size: "1000"

# max number of elements in LRU cache for trees
cache-tree-size: "1000"

# indicates whether value log files should be truncated to delete corrupt data, if any
badger-no-truncate: "false"

# max number of nodes used when saving profiles to disk
max-nodes-serialization: "2048"

# max number of nodes used to display data on the frontend
max-nodes-render: "8192"

# please don't use, this will soon be deprecated
hide-applications: ""

# Threshold value to consider out of space in bytes
out-of-space-threshold: "512.00 MB"

# sample rate for the profiler in Hz. 100 means reading 100 times per second
sample-rate: "100"

```
<!-- /generate-sample-config -->
101 changes: 91 additions & 10 deletions scripts/generate-sample-config/main.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,123 @@
package main

import (
"bytes"
"flag"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"

"github.com/pyroscope-io/pyroscope/pkg/cli"
"github.com/pyroscope-io/pyroscope/pkg/config"
"github.com/pyroscope-io/pyroscope/pkg/util/slices"
"github.com/sirupsen/logrus"
)

// to run this program:
// go run scripts/generate-sample-config/main.go -format yaml
// go run scripts/generate-sample-config/main.go -format md
// or:
// go run scripts/generate-sample-config/main.go -directory ../pyroscope.io/docs

var cfg config.Config

func main() {
var format string
var (
format string
subcommand string
directory string
)
flag.StringVar(&format, "format", "yaml", "yaml or md")
flag.StringVar(&subcommand, "subcommand", "server", "server, agent, exec...")
flag.StringVar(&directory, "directory", "", "directory to scan and perform auto replacements")
flag.Parse()

serverFlagSet := flag.NewFlagSet("pyroscope server", flag.ExitOnError)
cfg := config.Config{Server: config.Server{}}
cli.PopulateFlagSet(&cfg.Server, serverFlagSet)
if directory != "" {
err := filepath.Walk(directory, func(path string, f os.FileInfo, err error) error {
if slices.StringContains([]string{".mdx", ".md"}, filepath.Ext(path)) {
// log.Println(""path)
processFile(path)
}
return nil
})
if err != nil {
panic(err)
}
} else {
writeConfigDocs(os.Stdout, subcommand, format)
}
}

func processFile(path string) {
log.Printf("processing %s", path)
content, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}

// r := regexp.MustCompile("<!-- generate-sample-config:.+?:.+? -->.+?<!-- \\/generate-sample-config -->")
r := regexp.MustCompile("(?s)<!--\\s*generate-sample-config:.+?:.+?\\s*-->.*?<!--\\s*/generate-sample-config\\s*-->")
r2 := regexp.MustCompile("generate-sample-config:(.+?):(.+?)\\s*-")
newContent := r.ReplaceAllFunc(content, func(b []byte) []byte {
submatches := r2.FindSubmatch(b)
buf := bytes.Buffer{}

subcommand := string(submatches[1])
format := string(submatches[2])

fmt.Fprintf(&buf, "<!-- generate-sample-config:%s:%s -->\n", subcommand, format)
if format == "yaml" {
fmt.Fprintf(&buf, "```yaml\n")
}
writeConfigDocs(&buf, subcommand, format)
if format == "yaml" {
fmt.Fprintf(&buf, "```\n")
}
fmt.Fprintf(&buf, "<!-- /generate-sample-config -->")
return buf.Bytes()
})

if bytes.Equal(content, newContent) {
log.Println("no changes")
} else {
ioutil.WriteFile(path, newContent, fs.FileMode(0))
}

}
func writeConfigDocs(w io.Writer, subcommand, format string) {
var val interface{}
if subcommand == "agent" {
val = &cfg.Agent
} else if subcommand == "server" {
val = &cfg.Server
} else if subcommand == "convert" {
val = &cfg.Convert
} else if subcommand == "exec" {
val = &cfg.Exec
}

sf := cli.NewSortedFlags(&cfg.Server, serverFlagSet)
flagSet := flag.NewFlagSet("pyroscope "+subcommand, flag.ExitOnError)

cli.PopulateFlagSet(val, flagSet)
sf := cli.NewSortedFlags(val, flagSet)
if format == "yaml" {
fmt.Println("---")
fmt.Fprintln(w, "---")
sf.VisitAll(func(f *flag.Flag) {
if f.Name != "config" {
fmt.Printf("# %s\n%s: %q\n\n", f.Usage, f.Name, f.DefValue)
fmt.Fprintf(w, "# %s\n%s: %q\n\n", f.Usage, f.Name, f.DefValue)
}
})
} else if format == "md" {
fmt.Printf("| %s | %s | %s |\n", "Name", "Default Value", "Usage")
fmt.Printf("| %s | %s | %s |\n", ":-", ":-", ":-")
fmt.Fprintf(w, "| %s | %s | %s |\n", "Name", "Default Value", "Usage")
fmt.Fprintf(w, "| %s | %s | %s |\n", ":-", ":-", ":-")
sf.VisitAll(func(f *flag.Flag) {
if f.Name != "config" {
fmt.Printf("| %s | %s | %q |\n", f.Name, f.DefValue, f.Usage)
fmt.Fprintf(w, "| %s | %s | %q |\n", f.Name, f.DefValue, f.Usage)
}
})
} else {
Expand Down