From 55677351425b9a1900337be2508e71c4f16726df Mon Sep 17 00:00:00 2001 From: James King Date: Fri, 15 Dec 2017 10:58:20 -0500 Subject: [PATCH 1/3] Update to work with gateway release 0.4.0 --- prometheus/prometheus.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index 5474092..2457142 100644 --- a/prometheus/prometheus.go +++ b/prometheus/prometheus.go @@ -151,10 +151,9 @@ func sendMetrics(config map[string]ctypes.ConfigValue, promUrl *url.URL, client buf := new(bytes.Buffer) for _, m := range metrics { name, tags, value, ts := mangleMetric(m) - buf.WriteString(prometheusString(name, tags, value, ts)) + buf.WriteString(prometheusString(name, tags, value)) buf.WriteByte('\n') } - req, err := http.NewRequest("PUT", promUrl.String(), bytes.NewReader(buf.Bytes())) req.Header.Set("Content-Type", "text/plain; version=0.0.4") res, err := client.Conn.Do(req) @@ -169,16 +168,15 @@ func sendMetrics(config map[string]ctypes.ConfigValue, promUrl *url.URL, client } } -func prometheusString(name string, tags map[string]string, value string, ts int64) string { +func prometheusString(name string, tags map[string]string, value string) string { tmp1 := []string{} for k, v := range tags { tmp1 = append(tmp1, fmt.Sprintf("%s=\"%s\"", k, v)) } - return fmt.Sprintf("%s{%s} %s %d", + return fmt.Sprintf("%s{%s} %s", name, strings.Join(tmp1, ","), value, - ts, ) } From f10a85fac77502961d97dcf544833cfaba571ef2 Mon Sep 17 00:00:00 2001 From: James King Date: Sun, 17 Dec 2017 10:06:44 -0500 Subject: [PATCH 2/3] Fixed references to ts that I missed in previous commit --- prometheus/prometheus.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index 2457142..044d776 100644 --- a/prometheus/prometheus.go +++ b/prometheus/prometheus.go @@ -150,7 +150,7 @@ func sendMetrics(config map[string]ctypes.ConfigValue, promUrl *url.URL, client logger := getLogger(config) buf := new(bytes.Buffer) for _, m := range metrics { - name, tags, value, ts := mangleMetric(m) + name, tags, value := mangleMetric(m) buf.WriteString(prometheusString(name, tags, value)) buf.WriteByte('\n') } @@ -180,7 +180,7 @@ func prometheusString(name string, tags map[string]string, value string) string ) } -func mangleMetric(m plugin.MetricType) (name string, tags map[string]string, value string, ts int64) { +func mangleMetric(m plugin.MetricType) (name string, tags map[string]string, value string) { tags = make(map[string]string) ns := m.Namespace().Strings() isDynamic, indexes := m.Namespace().IsDynamic() @@ -225,7 +225,6 @@ func mangleMetric(m plugin.MetricType) (name string, tags map[string]string, val name = strings.Join(ns, "_") value = fmt.Sprint(m.Data()) - ts = m.Timestamp().Unix() * 1000 return } From 7750ba7a12c42036c63d2a4fb9ce954aa3b1bea6 Mon Sep 17 00:00:00 2001 From: James King Date: Tue, 2 Jan 2018 13:47:38 -0500 Subject: [PATCH 3/3] Added Job Name Updates * The Push Gateway uses the Job Name as the key to remove stale data via their API. Added an optional configuration parameter to allow control of this value. --- prometheus/prometheus.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index 044d776..e48f88a 100644 --- a/prometheus/prometheus.go +++ b/prometheus/prometheus.go @@ -109,6 +109,13 @@ func (p *prometheusPublisher) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) { r4.Description = "Prometheus debug" config.Add(r4) + r5, err := cpolicy.NewStringRule("jobName", false) + if err != nil { + panic(err) + } + r5.Description = "Prometheus Job Name - defaults to unused" + config.Add(r5) + cp.Add([]string{""}, config) return cp, nil } @@ -155,16 +162,20 @@ func sendMetrics(config map[string]ctypes.ConfigValue, promUrl *url.URL, client buf.WriteByte('\n') } req, err := http.NewRequest("PUT", promUrl.String(), bytes.NewReader(buf.Bytes())) + if err != nil { + logger.Errorf("Error creating request: %v", err) + return + } req.Header.Set("Content-Type", "text/plain; version=0.0.4") res, err := client.Conn.Do(req) if err != nil { - logger.Error("Error sending data to Prometheus: %v", err) + logger.Errorf("Error sending data to Prometheus: %v", err) return } defer res.Body.Close() _, err = ioutil.ReadAll(res.Body) if err != nil { - logger.Error("Error getting Prometheus response: %v", err) + logger.Errorf("Error getting Prometheus response: %v", err) } } @@ -233,8 +244,11 @@ func prometheusUrl(config map[string]ctypes.ConfigValue) (*url.URL, error) { if config["https"].(ctypes.ConfigValueBool).Value { prefix = "https" } - - u, err := url.Parse(fmt.Sprintf("%s://%s:%d/metrics/job/unused", prefix, config["host"].(ctypes.ConfigValueStr).Value, config["port"].(ctypes.ConfigValueInt).Value)) + var jobName = "unused" + if _, ok := config["job"]; ok { + jobName = config["job"].(ctypes.ConfigValueStr).Value + } + u, err := url.Parse(fmt.Sprintf("%s://%s:%d/metrics/job/%s", prefix, config["host"].(ctypes.ConfigValueStr).Value, config["port"].(ctypes.ConfigValueInt).Value, jobName)) if err != nil { return nil, err }