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"

    "example.com/logger"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/push"
)

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

}

-- 
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/d07454f9-d43f-4a68-9b3c-b8075088674fn%40googlegroups.com.

Reply via email to