Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/mcuadros/go-gin-prometheus"
ginprometheus "github.com/mcuadros/go-gin-prometheus"

"github.com/gin-gonic/gin"
)
Expand All @@ -28,9 +28,29 @@ func main() {
// histogram, histogram_vec, summary, summary_vec
}
p := ginprometheus.NewPrometheus("gin", customMetrics)
// add data to custom metrics
m := p.MetricsList["1234"].MetricCollector.(prometheus.Counter)
m.Inc()
*/

p := ginprometheus.NewPrometheus("gin")
customMetrics := []*ginprometheus.Metric{
{
ID: "1234", // optional string
Name: "test_metric", // required string
Description: "Counter test metric", // required string
Type: "counter", // required string
},
{
ID: "1235", // Identifier
Name: "test_metric_2", // Metric Name
Description: "Summary test metric", // Help Description
Type: "summary", // type associated with prometheus collector
},
// Type Options:
// counter, counter_vec, gauge, gauge_vec,
// histogram, histogram_vec, summary, summary_vec
}
p := ginprometheus.NewPrometheus("gin", customMetrics)

p.Use(r)
r.GET("/", func(c *gin.Context) {
Expand Down
26 changes: 20 additions & 6 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"strconv"
"sync"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -94,13 +95,14 @@ type Prometheus struct {
listenAddress string
Ppg PrometheusPushGateway

MetricsList []*Metric
MetricsList map[string]*Metric
MetricsPath string

ReqCntURLLabelMappingFn RequestCounterURLLabelMappingFn

// gin.Context string to use as a prometheus URL label
URLLabelFromContext string
sync.RWMutex
}

// PrometheusPushGateway contains the configuration for pushing to a Prometheus pushgateway (optional)
Expand All @@ -123,17 +125,20 @@ type PrometheusPushGateway struct {

// NewPrometheus generates a new set of metrics with a certain subsystem name
func NewPrometheus(subsystem string, customMetricsList ...[]*Metric) *Prometheus {

var metricsList []*Metric
metricsList := make(map[string]*Metric)

if len(customMetricsList) > 1 {
panic("Too many args. NewPrometheus( string, <optional []*Metric> ).")
} else if len(customMetricsList) == 1 {
metricsList = customMetricsList[0]
if len(customMetricsList[0]) > 0 {
for _, metric := range customMetricsList[0] {
metricsList[metric.ID] = metric
}
}
}

for _, metric := range standardMetrics {
metricsList = append(metricsList, metric)
metricsList[metric.ID] = metric
}

p := &Prometheus{
Expand Down Expand Up @@ -219,6 +224,12 @@ func (p *Prometheus) getMetrics() []byte {
return body
}

func (p *Prometheus) GetMetric(id string) *Metric {
p.RLock()
defer p.RUnlock()
return p.MetricsList[id]
}

func (p *Prometheus) getPushGatewayURL() string {
h, _ := os.Hostname()
if p.Ppg.Job == "" {
Expand All @@ -229,6 +240,10 @@ func (p *Prometheus) getPushGatewayURL() string {

func (p *Prometheus) sendMetricsToPushGateway(metrics []byte) {
req, err := http.NewRequest("POST", p.getPushGatewayURL(), bytes.NewBuffer(metrics))
if err != nil {
log.WithError(err).Errorln("Error New post request")
return
}
client := &http.Client{}
if _, err = client.Do(req); err != nil {
log.WithError(err).Errorln("Error sending to push gateway")
Expand Down Expand Up @@ -321,7 +336,6 @@ func NewMetric(m *Metric, subsystem string) prometheus.Collector {
}

func (p *Prometheus) registerMetrics(subsystem string) {

for _, metricDef := range p.MetricsList {
metric := NewMetric(metricDef, subsystem)
if err := prometheus.Register(metric); err != nil {
Expand Down