penglongli / gin-metrics

gin-gonic/gin metrics for prometheus.
MIT License
247 stars 58 forks source link

Nil pointer dereference when using Summary metric #42

Open manuelpepe opened 1 month ago

manuelpepe commented 1 month ago

When initializing a Summary metric like this:

func AddMetric(m *ginmetrics.Monitor) error {
    metric := &ginmetrics.Metric{
        Type:        ginmetrics.Summary,
        Name:        metricName,
        Description: "description",
        Labels:      []string{"label"},
        Objectives: map[float64]float64{
            0.5:  0.05,  
            0.9:  0.01, 
            0.99: 0.001, 
        },
    }
    return m.AddMetric(metric)
}

I get a nil pointer dereference with this error:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x5aa56c]

goroutine 51 [running]:
github.com/prometheus/client_golang/prometheus.(*Registry).Register.func1()
       /go/pkg/mod/github.com/prometheus/client_golang@v1.16.0/prometheus/registry.go:279 +0x2c
created by github.com/prometheus/client_golang/prometheus.(*Registry).Register
       /go/pkg/mod/github.com/prometheus/client_golang@v1.16.0/prometheus/registry.go:278 +0x124

I've found that the summaryHandler() function is missing the initialization of the metric.vec attribute as the other metric types do.

I'll try the fix and open a PR in a bit.

manuelpepe commented 1 month ago

@penglongli I've also found an old PR (#33) trying to fix the same issue. Please consider merging one of them and creating a new release, or archiving the repository if you're no longer accepting contributions.