forked from mirror/client_golang
Fix things commented on in past code review.
Change-Id: I4dafd098eefa99bc37fdbfebeb4c61a7251ad0be
This commit is contained in:
parent
84dc53148d
commit
3dfae09d30
|
@ -23,7 +23,6 @@ import (
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
|
|
||||||
"code.google.com/p/goprotobuf/proto"
|
"code.google.com/p/goprotobuf/proto"
|
||||||
"github.com/matttproud/golang_protobuf_extensions/ext"
|
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/model"
|
"github.com/prometheus/client_golang/model"
|
||||||
"github.com/prometheus/client_golang/text"
|
"github.com/prometheus/client_golang/text"
|
||||||
|
@ -43,11 +42,11 @@ const (
|
||||||
jsonContentType = "application/json"
|
jsonContentType = "application/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
// encoder is a function that writes a proto.Message to an io.Writer in a
|
// encoder is a function that writes a dto.MetricFamily to an io.Writer in a
|
||||||
// certain encoding. It returns the number of bytes written and any error
|
// certain encoding. It returns the number of bytes written and any error
|
||||||
// encountered. Note that ext.WriteDelimited and text.MetricFamilyToText are
|
// encountered. Note that ext.WriteDelimited and text.MetricFamilyToText are
|
||||||
// encoders.
|
// encoders.
|
||||||
type encoder func(io.Writer, proto.Message) (int, error)
|
type encoder func(io.Writer, *dto.MetricFamily) (int, error)
|
||||||
|
|
||||||
// container represents a top-level registered metric that encompasses its
|
// container represents a top-level registered metric that encompasses its
|
||||||
// static metadata.
|
// static metadata.
|
||||||
|
@ -341,7 +340,7 @@ func (r *registry) Handler() http.HandlerFunc {
|
||||||
switch accept.Params["encoding"] {
|
switch accept.Params["encoding"] {
|
||||||
case "delimited":
|
case "delimited":
|
||||||
header.Set(contentTypeHeader, DelimitedTelemetryContentType)
|
header.Set(contentTypeHeader, DelimitedTelemetryContentType)
|
||||||
enc = ext.WriteDelimited
|
enc = text.WriteProtoDelimited
|
||||||
case "text":
|
case "text":
|
||||||
header.Set(contentTypeHeader, ProtoTextTelemetryContentType)
|
header.Set(contentTypeHeader, ProtoTextTelemetryContentType)
|
||||||
enc = text.WriteProtoText
|
enc = text.WriteProtoText
|
||||||
|
|
|
@ -25,7 +25,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"code.google.com/p/goprotobuf/proto"
|
|
||||||
|
|
||||||
dto "github.com/prometheus/client_model/go"
|
dto "github.com/prometheus/client_model/go"
|
||||||
)
|
)
|
||||||
|
@ -35,33 +34,33 @@ import (
|
||||||
// and any error encountered. This function does not perform checks on the
|
// and any error encountered. This function does not perform checks on the
|
||||||
// content of the metric and label names, i.e. invalid metric or label names
|
// content of the metric and label names, i.e. invalid metric or label names
|
||||||
// will result in invalid text format output.
|
// will result in invalid text format output.
|
||||||
func MetricFamilyToText(out io.Writer, in proto.Message) (int, error) {
|
// This method fulfills the type 'prometheus.encoder'.
|
||||||
mf := in.(*dto.MetricFamily)
|
func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) {
|
||||||
var written int
|
var written int
|
||||||
|
|
||||||
// Fail-fast checks.
|
// Fail-fast checks.
|
||||||
if len(mf.Metric) == 0 {
|
if len(in.Metric) == 0 {
|
||||||
return written, fmt.Errorf("MetricFamily has no metrics: %s", in)
|
return written, fmt.Errorf("MetricFamily has no metrics: %s", in)
|
||||||
}
|
}
|
||||||
name := mf.GetName()
|
name := in.GetName()
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return written, fmt.Errorf("MetricFamily has no name: %s", in)
|
return written, fmt.Errorf("MetricFamily has no name: %s", in)
|
||||||
}
|
}
|
||||||
if mf.Type == nil {
|
if in.Type == nil {
|
||||||
return written, fmt.Errorf("MetricFamily has no type: %s", in)
|
return written, fmt.Errorf("MetricFamily has no type: %s", in)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Comments, first HELP, then TYPE.
|
// Comments, first HELP, then TYPE.
|
||||||
if mf.Help != nil {
|
if in.Help != nil {
|
||||||
n, err := fmt.Fprintf(
|
n, err := fmt.Fprintf(
|
||||||
out, "# HELP %s %s\n",
|
out, "# HELP %s %s\n",
|
||||||
name, strings.Replace(*mf.Help, "\n", `\n`, -1))
|
name, strings.Replace(*in.Help, "\n", `\n`, -1))
|
||||||
written += n
|
written += n
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return written, err
|
return written, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
metricType := mf.GetType()
|
metricType := in.GetType()
|
||||||
n, err := fmt.Fprintf(
|
n, err := fmt.Fprintf(
|
||||||
out, "# TYPE %s %s\n",
|
out, "# TYPE %s %s\n",
|
||||||
name, strings.ToLower(metricType.String()),
|
name, strings.ToLower(metricType.String()),
|
||||||
|
@ -72,7 +71,7 @@ func MetricFamilyToText(out io.Writer, in proto.Message) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally the samples, one line for each.
|
// Finally the samples, one line for each.
|
||||||
for _, metric := range mf.Metric {
|
for _, metric := range in.Metric {
|
||||||
switch metricType {
|
switch metricType {
|
||||||
case dto.MetricType_COUNTER:
|
case dto.MetricType_COUNTER:
|
||||||
if metric.Counter == nil {
|
if metric.Counter == nil {
|
||||||
|
|
|
@ -18,16 +18,26 @@ import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"code.google.com/p/goprotobuf/proto"
|
"code.google.com/p/goprotobuf/proto"
|
||||||
|
"github.com/matttproud/golang_protobuf_extensions/ext"
|
||||||
|
|
||||||
|
dto "github.com/prometheus/client_model/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WriteProtoText writes the proto.Message to the writer in text format and
|
// WriteProtoDelimited writes the MetricFamily to the writer in delimited
|
||||||
|
// protobuf format and returns the number of bytes written and any error
|
||||||
|
// encountered.
|
||||||
|
func WriteProtoDelimited(w io.Writer, p *dto.MetricFamily) (int, error) {
|
||||||
|
return ext.WriteDelimited(w, p)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WriteProtoText writes the MetricFamily to the writer in text format and
|
||||||
// returns the number of bytes written and any error encountered.
|
// returns the number of bytes written and any error encountered.
|
||||||
func WriteProtoText(w io.Writer, p proto.Message) (int, error) {
|
func WriteProtoText(w io.Writer, p *dto.MetricFamily) (int, error) {
|
||||||
return fmt.Fprintf(w, "%s\n", proto.MarshalTextString(p))
|
return fmt.Fprintf(w, "%s\n", proto.MarshalTextString(p))
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteProtoCompactText writes the proto.Message to the writer in compact text
|
// WriteProtoCompactText writes the MetricFamily to the writer in compact text
|
||||||
// format and returns the number of bytes written and any error encountered.
|
// format and returns the number of bytes written and any error encountered.
|
||||||
func WriteProtoCompactText(w io.Writer, p proto.Message) (int, error) {
|
func WriteProtoCompactText(w io.Writer, p *dto.MetricFamily) (int, error) {
|
||||||
return fmt.Fprintf(w, "%s\n", p)
|
return fmt.Fprintf(w, "%s\n", p)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue