mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-25 05:34:05 -08:00
fix: apply suggested changes
Signed-off-by: François Gouteroux <francois.gouteroux@gmail.com>
This commit is contained in:
parent
3eaa7eb538
commit
58d38c4c56
|
@ -102,7 +102,7 @@ func PushMetrics(url *url.URL, roundTripper http.RoundTripper, headers map[strin
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAndPushMetrics(client *remote.Client, data []byte, labels map[string]string) bool {
|
func parseAndPushMetrics(client *remote.Client, data []byte, labels map[string]string) bool {
|
||||||
metricsData, err := fmtutil.ParseMetricsTextAndFormat(bytes.NewReader(data), labels)
|
metricsData, err := fmtutil.MetricTextToWriteRequest(bytes.NewReader(data), labels)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
fmt.Fprintln(os.Stderr, " FAILED:", err)
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -44,8 +44,18 @@ var MetricMetadataTypeValue = map[string]int32{
|
||||||
"STATESET": 7,
|
"STATESET": 7,
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateWriteRequest convert metric family to a writerequest.
|
// MetricTextToWriteRequest consumes an io.Reader and return the data in write request format.
|
||||||
func CreateWriteRequest(mf map[string]*dto.MetricFamily, extraLabels map[string]string) (*prompb.WriteRequest, error) {
|
func MetricTextToWriteRequest(input io.Reader, labels map[string]string) (*prompb.WriteRequest, error) {
|
||||||
|
var parser expfmt.TextParser
|
||||||
|
mf, err := parser.TextToMetricFamilies(input)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return MetricFamiliesToWriteRequest(mf, labels)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MetricFamiliesToWriteRequest convert metric family to a writerequest.
|
||||||
|
func MetricFamiliesToWriteRequest(mf map[string]*dto.MetricFamily, extraLabels map[string]string) (*prompb.WriteRequest, error) {
|
||||||
wr := &prompb.WriteRequest{}
|
wr := &prompb.WriteRequest{}
|
||||||
|
|
||||||
// build metric list
|
// build metric list
|
||||||
|
@ -191,22 +201,3 @@ func makeLabelsMap(m *dto.Metric, metricName string, extraLabels map[string]stri
|
||||||
|
|
||||||
return labels
|
return labels
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseMetricsTextReader consumes an io.Reader and returns the MetricFamily.
|
|
||||||
func ParseMetricsTextReader(input io.Reader) (map[string]*dto.MetricFamily, error) {
|
|
||||||
var parser expfmt.TextParser
|
|
||||||
mf, err := parser.TextToMetricFamilies(input)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return mf, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseMetricsTextAndFormat return the data in the expected prometheus metrics write request format.
|
|
||||||
func ParseMetricsTextAndFormat(input io.Reader, labels map[string]string) (*prompb.WriteRequest, error) {
|
|
||||||
mf, err := ParseMetricsTextReader(input)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return CreateWriteRequest(mf, labels)
|
|
||||||
}
|
|
||||||
|
|
|
@ -201,13 +201,13 @@ func TestParseAndPushMetricsTextAndFormat(t *testing.T) {
|
||||||
`))
|
`))
|
||||||
labels := map[string]string{"job": "promtool"}
|
labels := map[string]string{"job": "promtool"}
|
||||||
|
|
||||||
expected, err := ParseMetricsTextAndFormat(input, labels)
|
expected, err := MetricTextToWriteRequest(input, labels)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
require.Equal(t, writeRequestFixture, expected)
|
require.Equal(t, writeRequestFixture, expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMetricsTextAndFormatErrorParsingFloatValue(t *testing.T) {
|
func TestMetricTextToWriteRequestErrorParsingFloatValue(t *testing.T) {
|
||||||
input := bytes.NewReader([]byte(`
|
input := bytes.NewReader([]byte(`
|
||||||
# HELP http_requests_total The total number of HTTP requests.
|
# HELP http_requests_total The total number of HTTP requests.
|
||||||
# TYPE http_requests_total counter
|
# TYPE http_requests_total counter
|
||||||
|
@ -216,11 +216,11 @@ func TestParseMetricsTextAndFormatErrorParsingFloatValue(t *testing.T) {
|
||||||
`))
|
`))
|
||||||
labels := map[string]string{"job": "promtool"}
|
labels := map[string]string{"job": "promtool"}
|
||||||
|
|
||||||
_, err := ParseMetricsTextAndFormat(input, labels)
|
_, err := MetricTextToWriteRequest(input, labels)
|
||||||
require.Equal(t, err.Error(), "text format parsing error in line 4: expected float as value, got \"1027Error\"")
|
require.Equal(t, err.Error(), "text format parsing error in line 4: expected float as value, got \"1027Error\"")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseMetricsTextAndFormatErrorParsingMetricType(t *testing.T) {
|
func TestMetricTextToWriteRequestErrorParsingMetricType(t *testing.T) {
|
||||||
input := bytes.NewReader([]byte(`
|
input := bytes.NewReader([]byte(`
|
||||||
# HELP node_info node info summary.
|
# HELP node_info node info summary.
|
||||||
# TYPE node_info info
|
# TYPE node_info info
|
||||||
|
@ -228,6 +228,6 @@ func TestParseMetricsTextAndFormatErrorParsingMetricType(t *testing.T) {
|
||||||
`))
|
`))
|
||||||
labels := map[string]string{"job": "promtool"}
|
labels := map[string]string{"job": "promtool"}
|
||||||
|
|
||||||
_, err := ParseMetricsTextAndFormat(input, labels)
|
_, err := MetricTextToWriteRequest(input, labels)
|
||||||
require.Equal(t, err.Error(), "text format parsing error in line 3: unknown metric type \"info\"")
|
require.Equal(t, err.Error(), "text format parsing error in line 3: unknown metric type \"info\"")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue