当前位置: 首页 > 工具软件 > github-client > 使用案例 >

使用github.com/prometheus/client_golang/prometheus包的注意事项

夏侯和韵
2023-12-01

1.panic: duplicate metrics collector registration attempted

重复注册。

错误代码如下

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

但是这样是可以的(value不一样)

promauto.NewCounter(prometheus.CounterOpts{

        Name:        "name",

        ConstLabels: prometheus.Labels{"k1""v1"},

    })

    promauto.NewCounter(prometheus.CounterOpts{

        Name:        "name",

        ConstLabels: prometheus.Labels{"k1""v2"},

    })

2.panic: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "name", help: "", constLabels: {k2="v2"}, variableLabels: []} has different label names or a different help string

name一样但是labels的key不一样也会导致报错。

特别注意,label的key必须是完全一样

错误示例:

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k2""v2"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1""k2""v2"},

})

当name一样,help不一样也会导致报错

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    Help:        "help",

    ConstLabels: prometheus.Labels{"k1""v1"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    Help:        "help1",

    ConstLabels: prometheus.Labels{"k1""v2"},

})

正确示例:

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1""k2""v2"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1""k2""v22"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v1""k2""v2"},

})

promauto.NewCounter(prometheus.CounterOpts{

    Name:        "name",

    ConstLabels: prometheus.Labels{"k1""v11""k2""v22"},

})

 类似资料: