MCPcopy Index your code
hub / github.com/coder/coder / updatePrometheusDoc

Function updatePrometheusDoc

scripts/metricsdocgen/main.go:141–189  ·  view source on GitHub ↗
(doc []byte, metricFamilies []*dto.MetricFamily)

Source from the content-addressed store, hash-verified

139}
140
141func updatePrometheusDoc(doc []byte, metricFamilies []*dto.MetricFamily) ([]byte, error) {
142 i := bytes.Index(doc, generatorPrefix)
143 if i < 0 {
144 return nil, xerrors.New("generator prefix tag not found")
145 }
146 tableStartIndex := i + len(generatorPrefix) + 1
147
148 j := bytes.Index(doc[tableStartIndex:], generatorSuffix)
149 if j < 0 {
150 return nil, xerrors.New("generator suffix tag not found")
151 }
152 tableEndIndex := tableStartIndex + j
153
154 var buffer bytes.Buffer
155 _, _ = buffer.Write(doc[:tableStartIndex])
156 _ = buffer.WriteByte('\n')
157
158 _, _ = buffer.WriteString("| Name | Type | Description | Labels |\n")
159 _, _ = buffer.WriteString("| - | - | - | - |\n")
160 for _, mf := range metricFamilies {
161 _, _ = buffer.WriteString("| ")
162 _, _ = buffer.Write([]byte("`" + *mf.Name + "`"))
163 _, _ = buffer.WriteString(" | ")
164 _, _ = buffer.Write([]byte(strings.ToLower(mf.Type.String())))
165 _, _ = buffer.WriteString(" | ")
166 if mf.Help != nil {
167 _, _ = buffer.Write([]byte(*mf.Help))
168 }
169 _, _ = buffer.WriteString(" | ")
170
171 labels := map[string]struct{}{}
172 metrics := mf.GetMetric()
173 for _, m := range metrics {
174 for _, label := range m.Label {
175 labels["`"+*label.Name+"`"] = struct{}{}
176 }
177 }
178
179 if len(labels) > 0 {
180 _, _ = buffer.WriteString(strings.Join(maps.SortedKeys(labels), " "))
181 }
182
183 _, _ = buffer.WriteString(" |\n")
184 }
185
186 _ = buffer.WriteByte('\n')
187 _, _ = buffer.Write(doc[tableEndIndex:])
188 return buffer.Bytes(), nil
189}
190
191func writePrometheusDoc(doc []byte) error {
192 return atomicwrite.File(prometheusDocFile, doc)

Callers 1

mainFunction · 0.85

Calls 6

SortedKeysFunction · 0.92
WriteStringMethod · 0.80
NewMethod · 0.65
WriteMethod · 0.65
StringMethod · 0.45
BytesMethod · 0.45

Tested by

no test coverage detected