-
Notifications
You must be signed in to change notification settings - Fork 343
feat: implement gauge and counter support for OpenMetrics 2.0 #894
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
Open
dashpole
wants to merge
2
commits into
prometheus:main
Choose a base branch
from
dashpole:om2_1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,291 @@ | ||
| // Copyright The Prometheus Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package expfmt | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "math" | ||
| "strconv" | ||
|
|
||
| dto "github.com/prometheus/client_model/go" | ||
| ) | ||
|
|
||
| // MetricFamilyToOpenMetrics20 converts a MetricFamily proto message into the | ||
| // OpenMetrics text format version 2.0.0 and writes the resulting lines to 'out'. | ||
| // It returns the number of bytes written and any error encountered. | ||
| // | ||
| // NOTE: This method implements OpenMetrics 2.0-rc.0 which is experimental. | ||
| // Breaking changes might happen in the future. This implementation is still a | ||
| // work-in-progress, and does not yet support all features of the format. | ||
| func MetricFamilyToOpenMetrics20(out io.Writer, in *dto.MetricFamily, options ...EncoderOption) (written int, err error) { | ||
| _ = options | ||
| name := in.GetName() | ||
| if name == "" { | ||
| return 0, fmt.Errorf("MetricFamily has no name: %s", in) | ||
| } | ||
|
|
||
| // Try the interface upgrade. If it doesn't work, we'll use a | ||
| // bufio.Writer from the sync.Pool. | ||
| w, ok := out.(enhancedWriter) | ||
| if !ok { | ||
| b := bufPool.Get().(*bufio.Writer) | ||
| b.Reset(out) | ||
| w = b | ||
| defer func() { | ||
| bErr := b.Flush() | ||
| if err == nil { | ||
| err = bErr | ||
| } | ||
| bufPool.Put(b) | ||
| }() | ||
| } | ||
|
|
||
| var ( | ||
| n int | ||
| metricType = in.GetType() | ||
| ) | ||
|
|
||
| // Comments, first HELP, then TYPE. | ||
| if in.Help != nil { | ||
| n, err = w.WriteString("# HELP ") | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| n, err = writeName(w, name) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| err = w.WriteByte(' ') | ||
| written++ | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| n, err = writeEscapedString(w, *in.Help, true) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| err = w.WriteByte('\n') | ||
| written++ | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| } | ||
| n, err = w.WriteString("# TYPE ") | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| n, err = writeName(w, name) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| switch metricType { | ||
| case dto.MetricType_COUNTER: | ||
| n, err = w.WriteString(" counter\n") | ||
| case dto.MetricType_GAUGE: | ||
| n, err = w.WriteString(" gauge\n") | ||
| case dto.MetricType_SUMMARY: | ||
| n, err = w.WriteString(" summary\n") | ||
| case dto.MetricType_UNTYPED: | ||
| n, err = w.WriteString(" unknown\n") | ||
| case dto.MetricType_HISTOGRAM: | ||
| n, err = w.WriteString(" histogram\n") | ||
| case dto.MetricType_GAUGE_HISTOGRAM: | ||
| n, err = w.WriteString(" gaugehistogram\n") | ||
| default: | ||
dashpole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // TODO: Support Info and StateSet once they are supported in the | ||
| // Prometheus protobuf format. | ||
| return written, fmt.Errorf("unknown metric type %s", metricType.String()) | ||
| } | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| if in.Unit != nil { | ||
| n, err = w.WriteString("# UNIT ") | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| n, err = writeName(w, name) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
|
|
||
| err = w.WriteByte(' ') | ||
| written++ | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| n, err = writeEscapedString(w, *in.Unit, true) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| err = w.WriteByte('\n') | ||
| written++ | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| } | ||
|
|
||
| // Finally the samples, one line for each. | ||
| for _, metric := range in.Metric { | ||
| switch metricType { | ||
| case dto.MetricType_COUNTER: | ||
| if metric.Counter == nil { | ||
| return written, fmt.Errorf("expected counter in metric %s %s", name, metric) | ||
| } | ||
| n, err = writeOpenMetrics20Sample(w, name, metric, metric.Counter.GetValue(), 0, false, metric.Counter.Exemplar) | ||
| case dto.MetricType_GAUGE: | ||
| if metric.Gauge == nil { | ||
| return written, fmt.Errorf("expected gauge in metric %s %s", name, metric) | ||
| } | ||
| n, err = writeOpenMetrics20Sample(w, name, metric, metric.Gauge.GetValue(), 0, false, nil) | ||
| case dto.MetricType_UNTYPED: | ||
| if metric.Untyped == nil { | ||
| return written, fmt.Errorf("expected untyped in metric %s %s", name, metric) | ||
| } | ||
| n, err = writeOpenMetrics20Sample(w, name, metric, metric.Untyped.GetValue(), 0, false, nil) | ||
| case dto.MetricType_SUMMARY: | ||
| if metric.Summary == nil { | ||
| return written, fmt.Errorf("expected summary in metric %s %s", name, metric) | ||
| } | ||
| n, err = writeCompositeSummary(w, name, metric) | ||
| case dto.MetricType_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM: | ||
| if metric.Histogram == nil { | ||
| return written, fmt.Errorf("expected histogram in metric %s %s", name, metric) | ||
| } | ||
| n, err = writeCompositeHistogram(w, name, metric, metricType == dto.MetricType_GAUGE_HISTOGRAM) | ||
dashpole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| default: | ||
| return written, fmt.Errorf("unexpected type in metric %s %s", name, metric) | ||
| } | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| } | ||
| return written, nil | ||
| } | ||
|
|
||
| // writeOpenMetrics20Sample writes a single sample for simple types (Counter, Gauge, Untyped). | ||
| func writeOpenMetrics20Sample(w enhancedWriter, name string, metric *dto.Metric, floatValue float64, intValue uint64, useIntValue bool, exemplar *dto.Exemplar) (int, error) { | ||
| written := 0 | ||
| n, err := writeOpenMetricsNameAndLabelPairs(w, name, metric.Label, "", 0) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| err = w.WriteByte(' ') | ||
| written++ | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
|
|
||
| if useIntValue { | ||
| n, err = writeUint(w, intValue) | ||
| } else { | ||
| n, err = writeFloat(w, floatValue) | ||
| } | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
|
|
||
| if metric.TimestampMs != nil { | ||
| err = w.WriteByte(' ') | ||
| written++ | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| n, err = writeOpenMetrics20Timestamp(w, float64(*metric.TimestampMs)/1000) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| } | ||
|
|
||
| // Start Timestamp for Counter | ||
| if metric.Counter != nil && metric.Counter.CreatedTimestamp != nil { | ||
| n, err = w.WriteString(" st@") | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| ts := metric.Counter.CreatedTimestamp | ||
| n, err = writeOpenMetrics20Timestamp(w, float64(ts.GetSeconds())+float64(ts.GetNanos())/1e9) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| } | ||
|
|
||
| if exemplar != nil && len(exemplar.Label) > 0 { | ||
| n, err = writeExemplar(w, exemplar) | ||
| written += n | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| } | ||
|
|
||
| err = w.WriteByte('\n') | ||
| written++ | ||
| if err != nil { | ||
| return written, err | ||
| } | ||
| return written, nil | ||
| } | ||
|
|
||
| // writeOpenMetrics20Timestamp writes a float64 as a timestamp without scientific notation. | ||
| func writeOpenMetrics20Timestamp(w enhancedWriter, f float64) (int, error) { | ||
| switch { | ||
| case math.IsNaN(f): | ||
| return w.WriteString("NaN") | ||
| case math.IsInf(f, +1): | ||
| return w.WriteString("+Inf") | ||
| case math.IsInf(f, -1): | ||
| return w.WriteString("-Inf") | ||
| default: | ||
| bp := numBufPool.Get().(*[]byte) | ||
| *bp = strconv.AppendFloat((*bp)[:0], f, 'f', -1, 64) | ||
| written, err := w.Write(*bp) | ||
| numBufPool.Put(bp) | ||
| return written, err | ||
| } | ||
| } | ||
|
|
||
| // Stubs for Summary and Histogram | ||
|
|
||
| func writeCompositeSummary(w enhancedWriter, name string, metric *dto.Metric) (int, error) { | ||
| _ = w | ||
dashpole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ = name | ||
| _ = metric | ||
| return 0, errors.New("summary not implemented yet") | ||
| } | ||
|
|
||
| func writeCompositeHistogram(w enhancedWriter, name string, metric *dto.Metric, isGauge bool) (int, error) { | ||
| _ = w | ||
| _ = name | ||
| _ = metric | ||
| _ = isGauge | ||
| return 0, errors.New("histogram not implemented yet") | ||
| } | ||
Oops, something went wrong.
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.