Skip to content

Commit

Permalink
Merge pull request #6 from axieinfinity/feature/prometheus-default-la…
Browse files Browse the repository at this point in the history
…bels

Now every collectors will be initialized with default label
  • Loading branch information
ssscrom committed Aug 26, 2022
2 parents affff8f + 4a6d05f commit 1c37a22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
1 change: 1 addition & 0 deletions adapters/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Prometheus struct {
PushJob string `default:"sm-bridge-v2" envconfig:"PUSH_JOB"`
PushInterval int `default:"15" envconfig:"PUSH_INTERVAl"`
TurnOn bool `envconfig:"PROMETHEUS_TURN_ON"`
InstanceName string `envconfig:"PUSH_LABEL_INSTANCE_NAME"`
}

func New() (*Config, error) {
Expand Down
33 changes: 25 additions & 8 deletions adapters/prometheus/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Pusher struct {
histograms map[string]prometheus.Histogram
registry *prometheus.Registry
pusher *push.Pusher
labels map[string]string
}

func (p *Pusher) AddCounter(name string, description string) *Pusher {
Expand All @@ -25,8 +26,9 @@ func (p *Pusher) AddCounter(name string, description string) *Pusher {
}

counter := prometheus.NewCounter(prometheus.CounterOpts{
Name: name,
Help: description,
Name: name,
Help: description,
ConstLabels: p.labels,
})
p.counters[name] = counter
p.pusher.Collector(counter)
Expand All @@ -37,7 +39,9 @@ func (p *Pusher) AddCounterWithLable(name string, description string, labels map
if _, ok := p.counters[name]; ok {
return p
}

for k, v := range p.labels {
labels[k] = v
}
counter := prometheus.NewCounter(prometheus.CounterOpts{
Name: name,
Help: description,
Expand All @@ -59,9 +63,11 @@ func (p *Pusher) AddGauge(name string, description string) *Pusher {
if _, ok := p.gauges[name]; ok {
return p
}

gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: name,
Help: description,
Name: name,
Help: description,
ConstLabels: p.labels,
})
p.gauges[name] = gauge
p.pusher.Collector(gauge)
Expand All @@ -72,6 +78,11 @@ func (p *Pusher) AddGaugeWithLabel(name string, description string, labels map[s
if _, ok := p.gauges[name]; ok {
return p
}

for k, v := range p.labels {
labels[k] = v
}

gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: name,
Help: description,
Expand Down Expand Up @@ -104,8 +115,9 @@ func (p *Pusher) AddHistogram(name string, description string) *Pusher {
}

histogram := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: name,
Help: description,
Name: name,
Help: description,
ConstLabels: p.labels,
})
p.histograms[name] = histogram
p.pusher.Collector(histogram)
Expand All @@ -116,7 +128,9 @@ func (p *Pusher) AddHistogramWithLabels(name string, description string, labels
if _, ok := p.histograms[name]; ok {
return p
}

for k, v := range p.labels {
labels[k] = v
}
histogram := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: name,
Help: description,
Expand Down Expand Up @@ -162,5 +176,8 @@ func NewPusher() *Pusher {
counters: make(map[string]prometheus.Counter),
gauges: make(map[string]prometheus.Gauge),
histograms: make(map[string]prometheus.Histogram),
labels: map[string]string{
"instance": adapters.AppConfig.Prometheus.InstanceName,
},
}
}
7 changes: 4 additions & 3 deletions job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package internal
import (
"context"
"fmt"
"math/big"
"sync/atomic"
"time"

"github.com/axieinfinity/bridge-core/metrics"
"github.com/axieinfinity/bridge-core/models"
"github.com/axieinfinity/bridge-core/stores"
"github.com/axieinfinity/bridge-core/utils"
"github.com/ethereum/go-ethereum/common/hexutil"
"math/big"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
Expand Down

0 comments on commit 1c37a22

Please sign in to comment.