diff --git a/chain/chain.go b/chain/chain.go index 90c3ba6..1183110 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -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 { diff --git a/chain/route.go b/chain/route.go index 1be04da..b20cb47 100644 --- a/chain/route.go +++ b/chain/route.go @@ -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] diff --git a/metrics/metrics.go b/metrics/metrics.go index 85d5ee0..089adaa 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -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, + }) +}