Skip to content

Commit

Permalink
Make prow/metrics compatible with prometheus/client_golang v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaroaleman committed Oct 23, 2019
1 parent 5b31707 commit 7090579
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90
github.com/prometheus/common v0.4.1
github.com/prometheus/procfs v0.0.5 // indirect
github.com/rogpeppe/go-internal v1.3.2 // indirect
github.com/satori/go.uuid v0.0.0-20160713180306-0aa62d5ddceb
Expand Down
8 changes: 6 additions & 2 deletions prow/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["metrics.go"],
srcs = [
"metrics.go",
"push.go",
],
importpath = "k8s.io/test-infra/prow/metrics",
visibility = ["//visibility:public"],
deps = [
"//prow/config:go_default_library",
"//prow/interrupts:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promhttp:go_default_library",
"@com_github_prometheus_client_golang//prometheus/push:go_default_library",
"@com_github_prometheus_common//expfmt:go_default_library",
"@com_github_prometheus_common//model:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)
Expand Down
3 changes: 1 addition & 2 deletions prow/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus/push"
"github.com/sirupsen/logrus"

"k8s.io/test-infra/prow/config"
Expand Down Expand Up @@ -68,7 +67,7 @@ func serveMetrics(reg *prometheus.Registry) {
// metrics to the provided endpoint.
func pushMetrics(component, endpoint string, interval time.Duration) {
interrupts.TickLiteral(func() {
if err := push.FromGatherer(component, push.HostnameGroupingKey(), endpoint, prometheus.DefaultGatherer); err != nil {
if err := fromGatherer(component, hostnameGroupingKey(), endpoint, prometheus.DefaultGatherer); err != nil {
logrus.WithField("component", component).WithError(err).Error("Failed to push metrics.")
}
}, interval)
Expand Down
110 changes: 110 additions & 0 deletions prow/metrics/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright 2019 The Kubernetes 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.
*/

// This file is a copy from upstream where it was removed in
// https://github.com/prometheus/client_golang/pull/600
package metrics

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
)

const contentTypeHeader = "Content-Type"

func fromGatherer(job string, grouping map[string]string, url string, g prometheus.Gatherer) error {
return push(job, grouping, url, g, "PUT")
}

func push(job string, grouping map[string]string, pushURL string, g prometheus.Gatherer, method string) error {
if !strings.Contains(pushURL, "://") {
pushURL = "http://" + pushURL
}
if strings.HasSuffix(pushURL, "/") {
pushURL = pushURL[:len(pushURL)-1]
}

if strings.Contains(job, "/") {
return fmt.Errorf("job contains '/': %s", job)
}
urlComponents := []string{url.QueryEscape(job)}
for ln, lv := range grouping {
if !model.LabelName(ln).IsValid() {
return fmt.Errorf("grouping label has invalid name: %s", ln)
}
if strings.Contains(lv, "/") {
return fmt.Errorf("value of grouping label %s contains '/': %s", ln, lv)
}
urlComponents = append(urlComponents, ln, lv)
}
pushURL = fmt.Sprintf("%s/metrics/job/%s", pushURL, strings.Join(urlComponents, "/"))

mfs, err := g.Gather()
if err != nil {
return err
}
buf := &bytes.Buffer{}
enc := expfmt.NewEncoder(buf, expfmt.FmtProtoDelim)
// Check for pre-existing grouping labels:
for _, mf := range mfs {
for _, m := range mf.GetMetric() {
for _, l := range m.GetLabel() {
if l.GetName() == "job" {
return fmt.Errorf("pushed metric %s (%s) already contains a job label", mf.GetName(), m)
}
if _, ok := grouping[l.GetName()]; ok {
return fmt.Errorf(
"pushed metric %s (%s) already contains grouping label %s",
mf.GetName(), m, l.GetName(),
)
}
}
}
enc.Encode(mf)
}
req, err := http.NewRequest(method, pushURL, buf)
if err != nil {
return err
}
req.Header.Set(contentTypeHeader, string(expfmt.FmtProtoDelim))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 202 {
body, _ := ioutil.ReadAll(resp.Body) // Ignore any further error as this is for an error message only.
return fmt.Errorf("unexpected status code %d while pushing to %s: %s", resp.StatusCode, pushURL, body)
}
return nil
}

func hostnameGroupingKey() map[string]string {
hostname, err := os.Hostname()
if err != nil {
return map[string]string{"instance": "unknown"}
}
return map[string]string{"instance": hostname}
}

0 comments on commit 7090579

Please sign in to comment.