Skip to content

Commit

Permalink
prometheus job comes from metric point tag "prom_job"
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Porter <portertech@gmail.com>
  • Loading branch information
portertech committed Jul 31, 2020
1 parent 9aaf047 commit a58a4ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
10 changes: 7 additions & 3 deletions example_event.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"name": "curl_timings.time_total",
"tags": [
{
"name": "instance",
"name": "prom_instance",
"value": "sensu-go-sandbox"
}
],
Expand All @@ -72,11 +72,15 @@
"name": "curl_requests.count",
"tags": [
{
"name": "instance",
"name": "prom_job",
"value": "sensu-go-sandbox"
},
{
"name": "prometheus_type",
"name": "prom_instance",
"value": "sensu-go-sandbox"
},
{
"name": "prom_type",
"value": "counter"
}
],
Expand Down
51 changes: 22 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ var (
Usage: "The Prometheus Pushgateway metrics API URL.",
Value: &plugin.URL,
},
&sensu.PluginConfigOption{
Path: "job",
Env: "PUSHGATEWAY_JOB",
Argument: "job",
Shorthand: "j",
Default: "",
Usage: "The Prometheus Pushgateway metrics job name (required).",
Value: &plugin.Job,
},
}
)

Expand All @@ -56,32 +47,35 @@ func main() {
}

func checkArgs(_ *types.Event) error {
if len(plugin.Job) == 0 {
return fmt.Errorf("--job or PUSHGATEWAY_JOB environment variable is required")
}
return nil
}

func transformMetrics(event *types.Event) (string, string) {
func transformMetrics(event *types.Event) (string, string, string) {
job := ""
inst := ""
info := map[string]string{}
prom := map[string]string{}
for _, m := range event.Metrics.Points {
mt := "untyped"
lt := ""
for _, t := range m.Tags {
if inst == "" && t.Name == "instance" {
inst = t.Value
continue
}
if t.Name == "prometheus_type" {
switch t.Name {
case "prom_job":
if job == "" {
job = t.Value
}
case "prom_instance":
if inst == "" {
inst = t.Value
}
case "prom_type":
mt = t.Value
continue
}
if lt != "" {
lt = lt + ","
default:
if lt != "" {
lt = lt + ","
}
lt = lt + fmt.Sprintf("%s=\"%s\"", t.Name, t.Value)
}
lt = lt + fmt.Sprintf("%s=\"%s\"", t.Name, t.Value)
}
l := strings.Replace(m.Name, ".", "_", -1)
if lt != "" {
Expand All @@ -100,11 +94,11 @@ func transformMetrics(event *types.Event) (string, string) {
m = fmt.Sprintf("# TYPE %s %s\n", n, t) + prom[n] + m
}
log.Println(m)
return inst, m
return job, inst, m
}

func postMetrics(i string, m string) error {
url := fmt.Sprintf("%s/job/%s/instance/%s", plugin.URL, plugin.Job, i)
func postMetrics(j string, i string, m string) error {
url := fmt.Sprintf("%s/job/%s/instance/%s", plugin.URL, j, i)
resp, err := http.Post(url, "text/plain", bytes.NewBuffer([]byte(m)))
if err != nil {
return err
Expand All @@ -120,8 +114,7 @@ func postMetrics(i string, m string) error {

func executeHandler(event *types.Event) error {
log.Println("executing handler with --url", plugin.URL)
log.Println("executing handler with --job", plugin.Job)
i, m := transformMetrics(event)
err := postMetrics(i, m)
j, i, m := transformMetrics(event)
err := postMetrics(j, i, m)
return err
}

0 comments on commit a58a4ea

Please sign in to comment.