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++
        }
    }

}
On Tuesday, October 8, 2024 at 4:44:12 PM UTC+5:45 Bjoern Rabenstein wrote:

> 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] [email protected]
>

-- 
You received this message because you are subscribed to the Google Groups 
"Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/prometheus-users/f1b970aa-0995-4f49-970c-224a974c00e2n%40googlegroups.com.

Reply via email to