Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -150,39 +157,41 @@ 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)
buf.WriteString(prometheusString(name, tags, value, ts))
name, tags, value := mangleMetric(m)
buf.WriteString(prometheusString(name, tags, value))
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)
}
}

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,
)
}

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()
Expand Down Expand Up @@ -227,7 +236,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
}

Expand All @@ -236,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
}
Expand Down