Posting to pushgateway using prometheus go client

37 views
Skip to first unread message

Anurupa Dhamala

unread,
Oct 5, 2024, 8:21:28 AM10/5/24
to Prometheus Users
I am trying to push my metrics to a pushgateway in a go routine. But I keep getting 
```collected metric "packet_received" { label:{name:"client_name" value:"machine1"} counter:{value:1 created_timestamp:{seconds:1728120095 nanos:654777418}}} was collected before with the same name and label values``` this error

I dont understand how to push the metrics. Here is how I have implemented my code 

package main

import (
    "context"
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "time"

)

var prometheusMetrics *logger.PrometheusMetrics

func main() {
    prom := "http://172.17.0.1:9091"
    go initPrometheus(prom)

    quit := make(chan os.Signal, 1)
    signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
    <-quit
}

func initPrometheus(url string) {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    i := 0

    pusher := push.New(url, "verulink_test").Grouping("instance", "test")

    prometheusMetrics = logger.NewPrometheusMetrics()

    ticker := time.NewTicker(5 * time.Second)
    defer ticker.Stop()

    for {
        select {
        case <-ctx.Done():
            fmt.Println("Stopping metric push...")
            return
        case <-ticker.C:
            prometheusMetrics.AddInPackets("machine1", i)
            gatherer := prometheus.Gatherers{
                prometheusMetrics.Registry,
            }
            fmt.Println("Attempting to push metrics...")
            if err := pusher.Gatherer(gatherer).Push(); err != nil {
                fmt.Printf("Error pushing metrics to Pushgateway: %s\n", err)
            } else {
                fmt.Println("Metrics pushed successfully.")
            }
            i++
        }
    }

}

Bjoern Rabenstein

unread,
Oct 8, 2024, 6:59:12 AM10/8/24
to Anurupa Dhamala, Prometheus Users
On 05.10.24 02:24, Anurupa Dhamala wrote:
> I am trying to push my metrics to a pushgateway in a go routine. But I keep
> getting
> ```collected metric "packet_received" { label:{name:"client_name"
> value:"machine1"} counter:{value:1 created_timestamp:{seconds:1728120095
> nanos:654777418}}} was collected before with the same name and label
> values``` this error

Where exactly is this error message coming from?

I would guess it is created before the push is even attempted. It
probably happens during metrics collection (which happens before
metrics are pushed). Such a message is created if there are two
metrics with the same name and label set. Presumably, there is a bug
in the `logger` package.


--
Björn Rabenstein
[PGP-ID] 0x851C3DA17D748D03
[email] bjo...@rabenste.in

Anurupa Dhamala

unread,
Oct 8, 2024, 11:06:57 AM10/8/24
to Prometheus Users
The error log was " was collected before with the same name and label values  " but I did manage to solve this. 
I had been creating a new gatherer inside the for loop, and every new gatherer is appended. So while pushing there were multiple gatherer with same metrics name and label set. I solved it by pushing the gatherer to pusher object outside of the for loop. 

func initPrometheus(url string) {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    i := 0

    pusher := push.New(url, "test").Grouping("instance", "test")
    prometheusMetrics = logger.NewPrometheusMetrics()
    pusher.Gatherer(prometheusMetrics.Registry)

    ticker := time.NewTicker(5 * time.Second)
    defer ticker.Stop()

    for {
        select {
        case <-ctx.Done():
            fmt.Println("Stopping metric push...")
            return
        case <-ticker.C:
            prometheusMetrics.AddInPackets("machine1", i)
            fmt.Println("Attempting to push metrics...")
            if err := pusher.Push(); err != nil {
                fmt.Printf("Error pushing metrics to Pushgateway: %s\n", err)
            } else {
                fmt.Println("Metrics pushed successfully.")
            }
            i++
        }
    }

}
Reply all
Reply to author
Forward
0 new messages