Skip to content

Commit

Permalink
Support rendering docs with a higher heading level
Browse files Browse the repository at this point in the history
This is useful when the metrics reference is embedded in another markdown
document.

Requested here:
cilium/tetragon#2164 (comment)
cilium/tetragon#2164 (comment)

Signed-off-by: Anna Kapuscinska <[email protected]>
  • Loading branch information
lambdanis committed Mar 1, 2024
1 parent 7f0ee42 commit 7b8bde9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/metricsmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ type Config struct {
LabelOverrides []LabelOverrides
InitMetrics func(target string, reg *prometheus.Registry, log *slog.Logger) error
AutogeneratedComment bool
HeadingLevel int // must be between 0 and 4
}
6 changes: 5 additions & 1 deletion pkg/metricsmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func Generate(reg *prometheus.Registry, w io.Writer, config *Config) error {

for _, metric := range metricsFamilies {
// Include the metric name and help text.
io.WriteString(w, fmt.Sprintf("## `%s`\n\n", metric.GetName()))
h := "##"
for i := 0; i < config.HeadingLevel; i++ {
h += "#"
}
io.WriteString(w, fmt.Sprintf("%s `%s`\n\n", h, metric.GetName()))
io.WriteString(w, fmt.Sprintf("%s\n\n", metric.GetHelp()))
// The rest is generating a list of label names and values

Expand Down
6 changes: 5 additions & 1 deletion pkg/metricsmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func NewCmd(vp *viper.Viper, log *slog.Logger, config *Config) *cobra.Command {
b.WriteString("<!-- This file is autogenerated via https://github.com/isovalent/metricstool -->\n\n")
}
// Document title
b.WriteString(fmt.Sprintf("# %s Metrics\n\n", config.Targets[target]))
h := "#"
for i := 0; i < config.HeadingLevel; i++ {
h += "#"
}
b.WriteString(fmt.Sprintf("%s %s Metrics\n\n", h, config.Targets[target]))
// Generate metrics reference
err = Generate(reg, &b, config)
if err != nil {
Expand Down

0 comments on commit 7b8bde9

Please sign in to comment.