TUN-1212: Expose tunnel_id in metrics

This commit is contained in:
Adam Chalmers 2018-11-26 11:32:27 -06:00
parent 10d547f528
commit 192ae35728
3 changed files with 62 additions and 4 deletions

View File

@ -52,6 +52,7 @@ type TunnelMetrics struct {
oldServerLocations map[string]string oldServerLocations map[string]string
muxerMetrics *muxerMetrics muxerMetrics *muxerMetrics
tunnelsHA tunnelsForHA
} }
func newMuxerMetrics() *muxerMetrics { func newMuxerMetrics() *muxerMetrics {
@ -355,6 +356,7 @@ func NewTunnelMetrics() *TunnelMetrics {
serverLocations: serverLocations, serverLocations: serverLocations,
oldServerLocations: make(map[string]string), oldServerLocations: make(map[string]string),
muxerMetrics: newMuxerMetrics(), muxerMetrics: newMuxerMetrics(),
tunnelsHA: NewTunnelsForHA(),
} }
} }
@ -375,7 +377,7 @@ func (t *TunnelMetrics) incrementRequests(connectionID string) {
var concurrentRequests uint64 var concurrentRequests uint64
var ok bool var ok bool
if concurrentRequests, ok = t.concurrentRequests[connectionID]; ok { if concurrentRequests, ok = t.concurrentRequests[connectionID]; ok {
t.concurrentRequests[connectionID] += 1 t.concurrentRequests[connectionID]++
concurrentRequests++ concurrentRequests++
} else { } else {
t.concurrentRequests[connectionID] = 1 t.concurrentRequests[connectionID] = 1
@ -395,7 +397,7 @@ func (t *TunnelMetrics) incrementRequests(connectionID string) {
func (t *TunnelMetrics) decrementConcurrentRequests(connectionID string) { func (t *TunnelMetrics) decrementConcurrentRequests(connectionID string) {
t.concurrentRequestsLock.Lock() t.concurrentRequestsLock.Lock()
if _, ok := t.concurrentRequests[connectionID]; ok { if _, ok := t.concurrentRequests[connectionID]; ok {
t.concurrentRequests[connectionID] -= 1 t.concurrentRequests[connectionID]--
} }
t.concurrentRequestsLock.Unlock() t.concurrentRequestsLock.Unlock()

View File

@ -292,7 +292,13 @@ func IsRPCStreamResponse(headers []h2mux.Header) bool {
return true return true
} }
func RegisterTunnel(ctx context.Context, muxer *h2mux.Muxer, config *TunnelConfig, connectionID uint8, originLocalIP string) error { func RegisterTunnel(
ctx context.Context,
muxer *h2mux.Muxer,
config *TunnelConfig,
connectionID uint8,
originLocalIP string,
) error {
config.Logger.Debug("initiating RPC stream to register") config.Logger.Debug("initiating RPC stream to register")
stream, err := muxer.OpenStream([]h2mux.Header{ stream, err := muxer.OpenStream([]h2mux.Header{
{Name: ":method", Value: "RPC"}, {Name: ":method", Value: "RPC"},
@ -346,7 +352,8 @@ func RegisterTunnel(ctx context.Context, muxer *h2mux.Muxer, config *TunnelConfi
} }
if registration.TunnelID != "" { if registration.TunnelID != "" {
config.Logger.Info("Tunnel ID: " + registration.TunnelID) config.Metrics.tunnelsHA.AddTunnelID(connectionID, registration.TunnelID)
config.Logger.Infof("Each HA connection's tunnel IDs: %v", config.Metrics.tunnelsHA.String())
} }
// Print out the user's trial zone URL in a nice box (if they requested and got one) // Print out the user's trial zone URL in a nice box (if they requested and got one)

49
origin/tunnelsforha.go Normal file
View File

@ -0,0 +1,49 @@
package origin
import (
"fmt"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
// tunnelsForHA maps this cloudflared instance's HA connections to the tunnel IDs they serve.
type tunnelsForHA struct {
sync.Mutex
metrics *prometheus.GaugeVec
entries map[uint8]string
}
// NewTunnelsForHA initializes the Prometheus metrics etc for a tunnelsForHA.
func NewTunnelsForHA() tunnelsForHA {
metrics := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "tunnel_ids",
Help: "The ID of all tunnels (and their corresponding HA connection ID) running in this instance of cloudflared.",
},
[]string{"tunnel_id", "ha_conn_id"},
)
prometheus.MustRegister(metrics)
return tunnelsForHA{
metrics: metrics,
entries: make(map[uint8]string),
}
}
// Track a new tunnel ID, removing the disconnected tunnel (if any) and update metrics.
func (t *tunnelsForHA) AddTunnelID(haConn uint8, tunnelID string) {
t.Lock()
defer t.Unlock()
if oldTunnelID, ok := t.entries[haConn]; ok {
t.metrics.WithLabelValues(oldTunnelID).Dec()
}
t.entries[haConn] = tunnelID
t.metrics.WithLabelValues(tunnelID, fmt.Sprintf("%v", haConn)).Inc()
}
func (t *tunnelsForHA) String() string {
t.Lock()
defer t.Unlock()
return fmt.Sprintf("%v", t.entries)
}