@@ -44,6 +44,13 @@ var targetUPMetric = prometheus.NewGaugeVec(prometheus.GaugeOpts{
4444 Help : "Has value 1 if the gNMI connection to the target is established; otherwise, 0." ,
4545}, []string {"name" })
4646
47+ var targetConnStateMetric = prometheus .NewGaugeVec (prometheus.GaugeOpts {
48+ Namespace : "gnmic" ,
49+ Subsystem : "target" ,
50+ Name : "connection_state" ,
51+ Help : "The current gRPC connection state to the target. The value can be one of the following: 0(INVALID_STATE), 1 (IDLE), 2 (CONNECTING), 3 (READY), 4 (TRANSIENT_FAILURE), or 5 (SHUTDOWN)." ,
52+ }, []string {"name" })
53+
4754// cluster
4855var clusterNumberOfLockedTargets = prometheus .NewGauge (prometheus.GaugeOpts {
4956 Namespace : "gnmic" ,
@@ -63,9 +70,14 @@ func (a *App) registerTargetMetrics() {
6370 if err != nil {
6471 a .Logger .Printf ("failed to register target metric: %v" , err )
6572 }
73+ err = a .reg .Register (targetConnStateMetric )
74+ if err != nil {
75+ a .Logger .Printf ("failed to register target connection state metric: %v" , err )
76+ }
6677 a .configLock .RLock ()
6778 for _ , t := range a .Config .Targets {
6879 targetUPMetric .WithLabelValues (t .Name ).Set (0 )
80+ targetConnStateMetric .WithLabelValues (t .Name ).Set (0 )
6981 }
7082 a .configLock .RUnlock ()
7183 go func () {
@@ -90,25 +102,42 @@ func (a *App) registerTargetMetrics() {
90102 }
91103 }
92104 targetUPMetric .Reset ()
105+ targetConnStateMetric .Reset ()
93106 a .configLock .RLock ()
94107 for _ , tc := range a .Config .Targets {
95108 a .operLock .RLock ()
96109 t , ok := a .Targets [tc .Name ]
97110 a .operLock .RUnlock ()
98111 if ok {
99112 switch t .ConnState () {
100- case "IDLE" , "READY" :
113+ case "IDLE" :
114+ targetUPMetric .WithLabelValues (tc .Name ).Set (1 )
115+ targetConnStateMetric .WithLabelValues (tc .Name ).Set (1 )
116+ case "CONNECTING" :
117+ targetUPMetric .WithLabelValues (tc .Name ).Set (0 )
118+ targetConnStateMetric .WithLabelValues (tc .Name ).Set (2 )
119+ case "READY" :
101120 targetUPMetric .WithLabelValues (tc .Name ).Set (1 )
121+ targetConnStateMetric .WithLabelValues (tc .Name ).Set (3 )
122+ case "TRANSIENT_FAILURE" :
123+ targetUPMetric .WithLabelValues (tc .Name ).Set (0 )
124+ targetConnStateMetric .WithLabelValues (tc .Name ).Set (4 )
125+ case "SHUTDOWN" :
126+ targetUPMetric .WithLabelValues (tc .Name ).Set (0 )
127+ targetConnStateMetric .WithLabelValues (tc .Name ).Set (5 )
102128 default :
103129 targetUPMetric .WithLabelValues (tc .Name ).Set (0 )
130+ targetConnStateMetric .WithLabelValues (tc .Name ).Set (0 )
104131 }
105132 } else {
106133 if a .isLeader {
107134 if ownTargets [tc .Name ] == a .Config .Clustering .InstanceName {
108135 targetUPMetric .WithLabelValues (tc .Name ).Set (0 )
136+ targetConnStateMetric .WithLabelValues (tc .Name ).Set (0 )
109137 }
110138 } else {
111139 targetUPMetric .WithLabelValues (tc .Name ).Set (0 )
140+ targetConnStateMetric .WithLabelValues (tc .Name ).Set (0 )
112141 }
113142 }
114143 }
0 commit comments