|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "flag" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | + |
| 13 | + dto "github.com/prometheus/client_model/go" |
| 14 | + "github.com/prometheus/common/expfmt" |
| 15 | + "golang.org/x/xerrors" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + metricsFile string |
| 20 | + prometheusDocFile string |
| 21 | + dryRun bool |
| 22 | + |
| 23 | + generatorPrefix = []byte("<!-- Code generated by 'make docs/admin/prometheus.md'. DO NOT EDIT -->") |
| 24 | + generatorSuffix = []byte("<!-- End generated by 'make docs/admin/prometheus.md'. -->") |
| 25 | +) |
| 26 | + |
| 27 | +func main() { |
| 28 | + flag.StringVar(&metricsFile, "metrics-file", "scripts/metricsdocgen/metrics", "Path to Prometheus metrics file") |
| 29 | + flag.StringVar(&prometheusDocFile, "prometheus-doc-file", "docs/admin/prometheus.md", "Path to prometheus doc file") |
| 30 | + flag.BoolVar(&dryRun, "dry-run", false, "Dry run") |
| 31 | + flag.Parse() |
| 32 | + |
| 33 | + metrics, err := readMetrics() |
| 34 | + if err != nil { |
| 35 | + log.Fatal("can't read metrics: ", err) |
| 36 | + } |
| 37 | + |
| 38 | + doc, err := readPrometheusDoc() |
| 39 | + if err != nil { |
| 40 | + log.Fatal("can't read Prometheus doc: ", err) |
| 41 | + } |
| 42 | + |
| 43 | + doc, err = updatePrometheusDoc(doc, metrics) |
| 44 | + if err != nil { |
| 45 | + log.Fatal("can't update Prometheus doc: ", err) |
| 46 | + } |
| 47 | + |
| 48 | + if dryRun { |
| 49 | + log.Println(string(doc)) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + err = writePrometheusDoc(doc) |
| 54 | + if err != nil { |
| 55 | + log.Fatal("can't write updated Prometheus doc: ", err) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func readMetrics() ([]dto.MetricFamily, error) { |
| 60 | + f, err := os.Open(metricsFile) |
| 61 | + if err != nil { |
| 62 | + return nil, xerrors.New("can't open metrics file") |
| 63 | + } |
| 64 | + |
| 65 | + var metrics []dto.MetricFamily |
| 66 | + |
| 67 | + decoder := expfmt.NewDecoder(f, expfmt.FmtProtoText) |
| 68 | + for { |
| 69 | + var m dto.MetricFamily |
| 70 | + err = decoder.Decode(&m) |
| 71 | + if errors.Is(err, io.EOF) { |
| 72 | + break |
| 73 | + } else if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + metrics = append(metrics, m) |
| 77 | + } |
| 78 | + |
| 79 | + sort.Slice(metrics, func(i, j int) bool { |
| 80 | + return sort.StringsAreSorted([]string{*metrics[i].Name, *metrics[j].Name}) |
| 81 | + }) |
| 82 | + return metrics, nil |
| 83 | +} |
| 84 | + |
| 85 | +func readPrometheusDoc() ([]byte, error) { |
| 86 | + doc, err := os.ReadFile(prometheusDocFile) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + return doc, nil |
| 91 | +} |
| 92 | + |
| 93 | +func updatePrometheusDoc(doc []byte, metricFamilies []dto.MetricFamily) ([]byte, error) { |
| 94 | + i := bytes.Index(doc, generatorPrefix) |
| 95 | + if i < 0 { |
| 96 | + return nil, xerrors.New("generator prefix tag not found") |
| 97 | + } |
| 98 | + tableStartIndex := i + len(generatorPrefix) + 1 |
| 99 | + |
| 100 | + j := bytes.Index(doc[tableStartIndex:], generatorSuffix) |
| 101 | + if j < 0 { |
| 102 | + return nil, xerrors.New("generator suffix tag not found") |
| 103 | + } |
| 104 | + tableEndIndex := tableStartIndex + j |
| 105 | + |
| 106 | + var buffer bytes.Buffer |
| 107 | + buffer.Write(doc[:tableStartIndex]) |
| 108 | + buffer.WriteByte('\n') |
| 109 | + |
| 110 | + buffer.WriteString("| Name | Type | Description | Labels |\n") |
| 111 | + buffer.WriteString("| - | - | - | - |\n") |
| 112 | + for _, mf := range metricFamilies { |
| 113 | + buffer.WriteString("| ") |
| 114 | + buffer.Write([]byte("`" + *mf.Name + "`")) |
| 115 | + buffer.WriteString(" | ") |
| 116 | + buffer.Write([]byte(strings.ToLower(mf.Type.String()))) |
| 117 | + buffer.WriteString(" | ") |
| 118 | + if mf.Help != nil { |
| 119 | + buffer.Write([]byte(*mf.Help)) |
| 120 | + } |
| 121 | + buffer.WriteString(" | ") |
| 122 | + |
| 123 | + labels := map[string]struct{}{} |
| 124 | + metrics := mf.GetMetric() |
| 125 | + for _, m := range metrics { |
| 126 | + for _, label := range m.Label { |
| 127 | + labels["`"+*label.Name+"`"] = struct{}{} |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + if len(labels) > 0 { |
| 132 | + buffer.WriteString(strings.Join(sortedKeys(labels), " ")) |
| 133 | + } |
| 134 | + |
| 135 | + buffer.WriteString(" |\n") |
| 136 | + } |
| 137 | + |
| 138 | + buffer.WriteByte('\n') |
| 139 | + buffer.Write(doc[tableEndIndex:]) |
| 140 | + return buffer.Bytes(), nil |
| 141 | +} |
| 142 | + |
| 143 | +func writePrometheusDoc(doc []byte) error { |
| 144 | + // G306: Expect WriteFile permissions to be 0600 or less |
| 145 | + /* #nosec G306 */ |
| 146 | + err := os.WriteFile(prometheusDocFile, doc, 0644) |
| 147 | + if err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +func sortedKeys(m map[string]struct{}) []string { |
| 154 | + var keys []string |
| 155 | + for k := range m { |
| 156 | + keys = append(keys, k) |
| 157 | + } |
| 158 | + sort.Strings(keys) |
| 159 | + return keys |
| 160 | +} |
0 commit comments