From 3dfae09d308a71d632bc121dd7f3758010b9e340 Mon Sep 17 00:00:00 2001 From: Bjoern Rabenstein Date: Tue, 29 Apr 2014 12:44:31 +0200 Subject: [PATCH] Fix things commented on in past code review. Change-Id: I4dafd098eefa99bc37fdbfebeb4c61a7251ad0be --- prometheus/registry.go | 7 +++---- text/create.go | 19 +++++++++---------- text/proto.go | 18 ++++++++++++++---- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/prometheus/registry.go b/prometheus/registry.go index e128b77..3a0705b 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -23,7 +23,6 @@ import ( dto "github.com/prometheus/client_model/go" "code.google.com/p/goprotobuf/proto" - "github.com/matttproud/golang_protobuf_extensions/ext" "github.com/prometheus/client_golang/model" "github.com/prometheus/client_golang/text" @@ -43,11 +42,11 @@ const ( 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 // encountered. Note that ext.WriteDelimited and text.MetricFamilyToText are // 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 // static metadata. @@ -341,7 +340,7 @@ func (r *registry) Handler() http.HandlerFunc { switch accept.Params["encoding"] { case "delimited": header.Set(contentTypeHeader, DelimitedTelemetryContentType) - enc = ext.WriteDelimited + enc = text.WriteProtoDelimited case "text": header.Set(contentTypeHeader, ProtoTextTelemetryContentType) enc = text.WriteProtoText diff --git a/text/create.go b/text/create.go index 3f28871..7f2376a 100644 --- a/text/create.go +++ b/text/create.go @@ -25,7 +25,6 @@ import ( "fmt" "io" "strings" - "code.google.com/p/goprotobuf/proto" dto "github.com/prometheus/client_model/go" ) @@ -35,33 +34,33 @@ import ( // 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 // will result in invalid text format output. -func MetricFamilyToText(out io.Writer, in proto.Message) (int, error) { - mf := in.(*dto.MetricFamily) +// This method fulfills the type 'prometheus.encoder'. +func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (int, error) { var written int // Fail-fast checks. - if len(mf.Metric) == 0 { + if len(in.Metric) == 0 { return written, fmt.Errorf("MetricFamily has no metrics: %s", in) } - name := mf.GetName() + name := in.GetName() if name == "" { 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) } // Comments, first HELP, then TYPE. - if mf.Help != nil { + if in.Help != nil { n, err := fmt.Fprintf( out, "# HELP %s %s\n", - name, strings.Replace(*mf.Help, "\n", `\n`, -1)) + name, strings.Replace(*in.Help, "\n", `\n`, -1)) written += n if err != nil { return written, err } } - metricType := mf.GetType() + metricType := in.GetType() n, err := fmt.Fprintf( out, "# TYPE %s %s\n", 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. - for _, metric := range mf.Metric { + for _, metric := range in.Metric { switch metricType { case dto.MetricType_COUNTER: if metric.Counter == nil { diff --git a/text/proto.go b/text/proto.go index 3edd9d0..8cbfd41 100644 --- a/text/proto.go +++ b/text/proto.go @@ -18,16 +18,26 @@ import ( "io" "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. -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)) } -// 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. -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) }