metrics: add chain error counter

This commit is contained in:
ginuerzh 2022-03-16 22:18:26 +08:00
parent 6b2ccaad38
commit 26d322379d
3 changed files with 39 additions and 2 deletions

View File

@ -5,9 +5,17 @@ type Chainer interface {
}
type Chain struct {
name string
groups []*NodeGroup
}
func NewChain(name string, groups ...*NodeGroup) *Chain {
return &Chain{
name: name,
groups: groups,
}
}
func (c *Chain) AddNodeGroup(group *NodeGroup) {
c.groups = append(c.groups, group)
}
@ -17,7 +25,9 @@ func (c *Chain) Route(network, address string) (r *Route) {
return
}
r = &Route{}
r = &Route{
chain: c,
}
for _, group := range c.groups {
node := group.Next()
if node == nil {

View File

@ -11,6 +11,7 @@ import (
"github.com/go-gost/core/common/util/udp"
"github.com/go-gost/core/connector"
"github.com/go-gost/core/logger"
"github.com/go-gost/core/metrics"
)
var (
@ -18,8 +19,9 @@ var (
)
type Route struct {
nodes []*Node
chain *Chain
ifceName string
nodes []*Node
logger logger.Logger
}
@ -75,6 +77,12 @@ func (r *Route) connect(ctx context.Context) (conn net.Conn, err error) {
return nil, ErrEmptyRoute
}
defer func() {
if err != nil && r.chain != nil {
metrics.ChainErrors(r.chain.name).Inc()
}
}()
network := "ip"
node := r.nodes[0]

View File

@ -39,6 +39,7 @@ type Metrics struct {
inputBytes *prometheus.CounterVec
outputBytes *prometheus.CounterVec
handlerErrors *prometheus.CounterVec
chainErrors *prometheus.CounterVec
}
func NewMetrics() *Metrics {
@ -93,6 +94,12 @@ func NewMetrics() *Metrics {
Help: "Total service handler errors",
},
[]string{"host", "service"}),
chainErrors: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "gost_chain_errors_total",
Help: "Total chain errors",
},
[]string{"host", "chain"}),
}
prometheus.MustRegister(m.services)
prometheus.MustRegister(m.requests)
@ -101,6 +108,7 @@ func NewMetrics() *Metrics {
prometheus.MustRegister(m.inputBytes)
prometheus.MustRegister(m.outputBytes)
prometheus.MustRegister(m.handlerErrors)
prometheus.MustRegister(m.chainErrors)
return m
}
@ -180,3 +188,14 @@ func HandlerErrors(service string) Counter {
"service": service,
})
}
func ChainErrors(chain string) Counter {
if metrics == nil || metrics.chainErrors == nil {
return nilCounter
}
return metrics.chainErrors.
With(prometheus.Labels{
"host": metrics.host,
"chain": chain,
})
}