-
Notifications
You must be signed in to change notification settings - Fork 943
docs: Prometheus metrics + generator #5179
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
477fef9
docs: Prometheus metrics
mtojek 887ca4e
Fix
mtojek 663d2e0
Typo
mtojek ce7a623
Typo
mtojek d93b2fd
Typo
mtojek 08edfc5
Fix: link
mtojek a7b903f
Update docs/admin/prometheus.md
mtojek 553c799
Update docs/admin/prometheus.md
mtojek 483f9bc
Update docs/admin/prometheus.md
mtojek 77d9fec
Update docs/admin/prometheus.md
mtojek 2290856
Update docs/admin/prometheus.md
mtojek 8d89937
Rephrase
mtojek 69d97a6
notice
mtojek a29dfae
use ```shell
ghuntley 15d58a3
Generator
mtojek 30fd2d1
gosec
mtojek 7ce111c
fix: lint
mtojek 556b7c1
Merge branch 'main' into 3520-doc-prom-metrics
mtojek 207711d
PR comments
mtojek e112fc4
not needed anymore
mtojek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10000
Loading
Generator
- Loading branch information
commit 15d58a30b63122ce8b6af6a93cb380260773af74
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"flag" | ||
"io" | ||
"log" | ||
"os" | ||
"sort" | ||
"strings" | ||
|
||
dto "github.com/prometheus/client_model/go" | ||
"github.com/prometheus/common/expfmt" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
var ( | ||
metricsFile string | ||
prometheusDocFile string | ||
dryRun bool | ||
|
||
generatorPrefix = []byte("<!-- Code generated by 'make docs/admin/prometheus.md'. DO NOT EDIT -->") | ||
generatorSuffix = []byte("<!-- End generated by 'make docs/admin/prometheus.md'. -->") | ||
) | ||
|
||
func init() { | ||
flag.StringVar(&metricsFile, "metrics-file", "scripts/metricsdocgen/metrics", "Path to Prometheus metrics file") | ||
flag.StringVar(&prometheusDocFile, "prometheus-doc-file", "docs/admin/prometheus.md", "Path to prometheus doc file") | ||
flag.BoolVar(&dryRun, "dry-run", false, "Dry run") | ||
flag.Parse() | ||
} | ||
|
||
func main() { | ||
metrics, err := readMetrics() | ||
if err != nil { | ||
log.Fatal("can't read metrics: ", err) | ||
} | ||
|
||
doc, err := readPrometheusDoc() | ||
if err != nil { | ||
log.Fatal("can't read Prometheus doc: ", err) | ||
} | ||
|
||
doc, err = updatePrometheusDoc(doc, metrics) | ||
if err != nil { | ||
log.Fatal("can't update Prometheus doc: ", err) | ||
} | ||
|
||
if dryRun { | ||
log.Println(string(doc)) | ||
return | ||
} | ||
|
||
err = writePrometheusDoc(doc) | ||
if err != nil { | ||
log.Fatal("can't write updated Prometheus doc: ", err) | ||
} | ||
} | ||
|
||
func readMetrics() ([]dto.MetricFamily, error) { | ||
f, err := os.Open(metricsFile) | ||
if err != nil { | ||
log.Fatalf("can't open metrics file: %s", metricsFile) | ||
} | ||
|
||
var metrics []dto.MetricFamily | ||
|
||
decoder := expfmt.NewDecoder(f, expfmt.FmtProtoText) | ||
for { | ||
var m dto.MetricFamily | ||
err = decoder.Decode(&m) | ||
if errors.Is(err, io.EOF) { | ||
break | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
metrics = append(metrics, m) | ||
} | ||
|
||
sort.Slice(metrics, func(i, j int) bool { | ||
return sort.StringsAreSorted([]string{*metrics[i].Name, *metrics[j].Name}) | ||
}) | ||
return metrics, nil | ||
} | ||
|
||
func readPrometheusDoc() ([]byte, error) { | ||
doc, err := os.ReadFile(prometheusDocFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return doc, nil | ||
} | ||
|
||
func updatePrometheusDoc(doc []byte, metricFamilies []dto.MetricFamily) ([]byte, error) { | ||
i := bytes.Index(doc, generatorPrefix) | ||
if i < 0 { | ||
return nil, xerrors.New("generator prefix tag not found") | ||
} | ||
tableStartIndex := i + len(generatorPrefix) + 1 | ||
|
||
j := bytes.Index(doc[tableStartIndex:], generatorSuffix) | ||
if j < 0 { | ||
return nil, xerrors.New("generator suffix tag not found") | ||
} | ||
tableEndIndex := tableStartIndex + j | ||
|
||
var buffer bytes.Buffer | ||
buffer.Write(doc[:tableStartIndex]) | ||
buffer.WriteByte('\n') | ||
|
||
buffer.WriteString("| Name | Type | Description | Labels |\n") | ||
buffer.WriteString("| - | - | - | - |\n") | ||
for _, mf := range metricFamilies { | ||
buffer.WriteString("| ") | ||
buffer.Write([]byte("`" + *mf.Name + "`")) | ||
buffer.WriteString(" | ") | ||
buffer.Write([]byte(strings.ToLower(mf.Type.String()))) | ||
buffer.WriteString(" | ") | ||
if mf.Help != nil { | ||
buffer.Write([]byte(*mf.Help)) | ||
} | ||
buffer.WriteString(" | ") | ||
|
||
labels := map[string]struct{}{} | ||
metrics := mf.GetMetric() | ||
for _, m := range metrics { | ||
for _, label := range m.Label { | ||
labels["`"+*label.Name+"`"] = struct{}{} | ||
} | ||
} | ||
|
||
if len(labels) > 0 { | ||
buffer.WriteString(strings.Join(sortedKeys(labels), " ")) | ||
} | ||
|
||
buffer.WriteString(" |\n") | ||
} | ||
|
||
buffer.WriteByte('\n') | ||
buffer.Write(doc[tableEndIndex:]) | ||
return buffer.Bytes(), nil | ||
} | ||
|
||
func writePrometheusDoc(doc []byte) error { | ||
err := os.WriteFile(prometheusDocFile, doc, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func sortedKeys(m map[string]struct{}) []string { | ||
var keys []string | ||
for k := range m { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
return keys | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.