|
| 1 | +// Copyright The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package expfmt |
| 15 | + |
| 16 | +import ( |
| 17 | + "bufio" |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "io" |
| 21 | + "math" |
| 22 | + "strconv" |
| 23 | + |
| 24 | + dto "github.com/prometheus/client_model/go" |
| 25 | +) |
| 26 | + |
| 27 | +// MetricFamilyToOpenMetrics20 converts a MetricFamily proto message into the |
| 28 | +// OpenMetrics text format version 2.0.0 and writes the resulting lines to 'out'. |
| 29 | +// It returns the number of bytes written and any error encountered. |
| 30 | +func MetricFamilyToOpenMetrics20(out io.Writer, in *dto.MetricFamily, options ...EncoderOption) (written int, err error) { |
| 31 | + _ = options |
| 32 | + name := in.GetName() |
| 33 | + if name == "" { |
| 34 | + return 0, fmt.Errorf("MetricFamily has no name: %s", in) |
| 35 | + } |
| 36 | + |
| 37 | + // Try the interface upgrade. If it doesn't work, we'll use a |
| 38 | + // bufio.Writer from the sync.Pool. |
| 39 | + w, ok := out.(enhancedWriter) |
| 40 | + if !ok { |
| 41 | + b := bufPool.Get().(*bufio.Writer) |
| 42 | + b.Reset(out) |
| 43 | + w = b |
| 44 | + defer func() { |
| 45 | + bErr := b.Flush() |
| 46 | + if err == nil { |
| 47 | + err = bErr |
| 48 | + } |
| 49 | + bufPool.Put(b) |
| 50 | + }() |
| 51 | + } |
| 52 | + |
| 53 | + var ( |
| 54 | + n int |
| 55 | + metricType = in.GetType() |
| 56 | + ) |
| 57 | + |
| 58 | + // Comments, first HELP, then TYPE. |
| 59 | + if in.Help != nil { |
| 60 | + n, err = w.WriteString("# HELP ") |
| 61 | + written += n |
| 62 | + if err != nil { |
| 63 | + return written, err |
| 64 | + } |
| 65 | + n, err = writeName(w, name) |
| 66 | + written += n |
| 67 | + if err != nil { |
| 68 | + return written, err |
| 69 | + } |
| 70 | + err = w.WriteByte(' ') |
| 71 | + written++ |
| 72 | + if err != nil { |
| 73 | + return written, err |
| 74 | + } |
| 75 | + n, err = writeEscapedString(w, *in.Help, true) |
| 76 | + written += n |
| 77 | + if err != nil { |
| 78 | + return written, err |
| 79 | + } |
| 80 | + err = w.WriteByte('\n') |
| 81 | + written++ |
| 82 | + if err != nil { |
| 83 | + return written, err |
| 84 | + } |
| 85 | + } |
| 86 | + n, err = w.WriteString("# TYPE ") |
| 87 | + written += n |
| 88 | + if err != nil { |
| 89 | + return written, err |
| 90 | + } |
| 91 | + n, err = writeName(w, name) |
| 92 | + written += n |
| 93 | + if err != nil { |
| 94 | + return written, err |
| 95 | + } |
| 96 | + switch metricType { |
| 97 | + case dto.MetricType_COUNTER: |
| 98 | + n, err = w.WriteString(" counter\n") |
| 99 | + case dto.MetricType_GAUGE: |
| 100 | + n, err = w.WriteString(" gauge\n") |
| 101 | + case dto.MetricType_SUMMARY: |
| 102 | + n, err = w.WriteString(" summary\n") |
| 103 | + case dto.MetricType_UNTYPED: |
| 104 | + n, err = w.WriteString(" unknown\n") |
| 105 | + case dto.MetricType_HISTOGRAM: |
| 106 | + n, err = w.WriteString(" histogram\n") |
| 107 | + case dto.MetricType_GAUGE_HISTOGRAM: |
| 108 | + n, err = w.WriteString(" gaugehistogram\n") |
| 109 | + default: |
| 110 | + return written, fmt.Errorf("unknown metric type %s", metricType.String()) |
| 111 | + } |
| 112 | + written += n |
| 113 | + if err != nil { |
| 114 | + return written, err |
| 115 | + } |
| 116 | + if in.Unit != nil { |
| 117 | + n, err = w.WriteString("# UNIT ") |
| 118 | + written += n |
| 119 | + if err != nil { |
| 120 | + return written, err |
| 121 | + } |
| 122 | + n, err = writeName(w, name) |
| 123 | + written += n |
| 124 | + if err != nil { |
| 125 | + return written, err |
| 126 | + } |
| 127 | + |
| 128 | + err = w.WriteByte(' ') |
| 129 | + written++ |
| 130 | + if err != nil { |
| 131 | + return written, err |
| 132 | + } |
| 133 | + n, err = writeEscapedString(w, *in.Unit, true) |
| 134 | + written += n |
| 135 | + if err != nil { |
| 136 | + return written, err |
| 137 | + } |
| 138 | + err = w.WriteByte('\n') |
| 139 | + written++ |
| 140 | + if err != nil { |
| 141 | + return written, err |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Finally the samples, one line for each. |
| 146 | + for _, metric := range in.Metric { |
| 147 | + switch metricType { |
| 148 | + case dto.MetricType_COUNTER: |
| 149 | + if metric.Counter == nil { |
| 150 | + return written, fmt.Errorf("expected counter in metric %s %s", name, metric) |
| 151 | + } |
| 152 | + n, err = writeOpenMetrics20Sample(w, name, metric, metric.Counter.GetValue(), 0, false, metric.Counter.Exemplar) |
| 153 | + case dto.MetricType_GAUGE: |
| 154 | + if metric.Gauge == nil { |
| 155 | + return written, fmt.Errorf("expected gauge in metric %s %s", name, metric) |
| 156 | + } |
| 157 | + n, err = writeOpenMetrics20Sample(w, name, metric, metric.Gauge.GetValue(), 0, false, nil) |
| 158 | + case dto.MetricType_UNTYPED: |
| 159 | + if metric.Untyped == nil { |
| 160 | + return written, fmt.Errorf("expected untyped in metric %s %s", name, metric) |
| 161 | + } |
| 162 | + n, err = writeOpenMetrics20Sample(w, name, metric, metric.Untyped.GetValue(), 0, false, nil) |
| 163 | + case dto.MetricType_SUMMARY: |
| 164 | + if metric.Summary == nil { |
| 165 | + return written, fmt.Errorf("expected summary in metric %s %s", name, metric) |
| 166 | + } |
| 167 | + n, err = writeCompositeSummary(w, name, metric) |
| 168 | + case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM: |
| 169 | + if metric.Histogram == nil { |
| 170 | + return written, fmt.Errorf("expected histogram in metric %s %s", name, metric) |
| 171 | + } |
| 172 | + n, err = writeCompositeHistogram(w, name, metric, metricType == dto.MetricType_GAUGE_HISTOGRAM) |
| 173 | + default: |
| 174 | + return written, fmt.Errorf("unexpected type in metric %s %s", name, metric) |
| 175 | + } |
| 176 | + written += n |
| 177 | + if err != nil { |
| 178 | + return written, err |
| 179 | + } |
| 180 | + } |
| 181 | + return written, nil |
| 182 | +} |
| 183 | + |
| 184 | +// writeOpenMetrics20Sample writes a single sample for simple types (Counter, Gauge, Untyped). |
| 185 | +func writeOpenMetrics20Sample(w enhancedWriter, name string, metric *dto.Metric, floatValue float64, intValue uint64, useIntValue bool, exemplar *dto.Exemplar) (int, error) { |
| 186 | + written := 0 |
| 187 | + n, err := writeOpenMetricsNameAndLabelPairs(w, name, metric.Label, "", 0) |
| 188 | + written += n |
| 189 | + if err != nil { |
| 190 | + return written, err |
| 191 | + } |
| 192 | + err = w.WriteByte(' ') |
| 193 | + written++ |
| 194 | + if err != nil { |
| 195 | + return written, err |
| 196 | + } |
| 197 | + |
| 198 | + if useIntValue { |
| 199 | + n, err = writeUint(w, intValue) |
| 200 | + } else { |
| 201 | + n, err = writeFloat(w, floatValue) |
| 202 | + } |
| 203 | + written += n |
| 204 | + if err != nil { |
| 205 | + return written, err |
| 206 | + } |
| 207 | + |
| 208 | + if metric.TimestampMs != nil { |
| 209 | + err = w.WriteByte(' ') |
| 210 | + written++ |
| 211 | + if err != nil { |
| 212 | + return written, err |
| 213 | + } |
| 214 | + n, err = writeOpenMetrics20Timestamp(w, float64(*metric.TimestampMs)/1000) |
| 215 | + written += n |
| 216 | + if err != nil { |
| 217 | + return written, err |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + // Start Timestamp for Counter |
| 222 | + if metric.Counter != nil && metric.Counter.CreatedTimestamp != nil { |
| 223 | + n, err = w.WriteString(" st@") |
| 224 | + written += n |
| 225 | + if err != nil { |
| 226 | + return written, err |
| 227 | + } |
| 228 | + ts := metric.Counter.CreatedTimestamp |
| 229 | + n, err = writeOpenMetrics20Timestamp(w, float64(ts.GetSeconds())+float64(ts.GetNanos())/1e9) |
| 230 | + written += n |
| 231 | + if err != nil { |
| 232 | + return written, err |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + if exemplar != nil && len(exemplar.Label) > 0 { |
| 237 | + n, err = writeExemplar(w, exemplar) |
| 238 | + written += n |
| 239 | + if err != nil { |
| 240 | + return written, err |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + err = w.WriteByte('\n') |
| 245 | + written++ |
| 246 | + if err != nil { |
| 247 | + return written, err |
| 248 | + } |
| 249 | + return written, nil |
| 250 | +} |
| 251 | + |
| 252 | +// writeOpenMetrics20Timestamp writes a float64 as a timestamp without scientific notation. |
| 253 | +func writeOpenMetrics20Timestamp(w enhancedWriter, f float64) (int, error) { |
| 254 | + switch { |
| 255 | + case math.IsNaN(f): |
| 256 | + return w.WriteString("NaN") |
| 257 | + case math.IsInf(f, +1): |
| 258 | + return w.WriteString("+Inf") |
| 259 | + case math.IsInf(f, -1): |
| 260 | + return w.WriteString("-Inf") |
| 261 | + default: |
| 262 | + bp := numBufPool.Get().(*[]byte) |
| 263 | + *bp = strconv.AppendFloat((*bp)[:0], f, 'f', -1, 64) |
| 264 | + written, err := w.Write(*bp) |
| 265 | + numBufPool.Put(bp) |
| 266 | + return written, err |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +// Stubs for Summary and Histogram |
| 271 | + |
| 272 | +func writeCompositeSummary(w enhancedWriter, name string, metric *dto.Metric) (int, error) { |
| 273 | + _ = w |
| 274 | + _ = name |
| 275 | + _ = metric |
| 276 | + return 0, errors.New("summary not implemented yet") |
| 277 | +} |
| 278 | + |
| 279 | +func writeCompositeHistogram(w enhancedWriter, name string, metric *dto.Metric, isGauge bool) (int, error) { |
| 280 | + _ = w |
| 281 | + _ = name |
| 282 | + _ = metric |
| 283 | + _ = isGauge |
| 284 | + return 0, errors.New("histogram not implemented yet") |
| 285 | +} |
0 commit comments