github.com/cilium/cilium@v1.16.2/pkg/hubble/metrics/icmp/handler.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Hubble 3 4 package icmp 5 6 import ( 7 "context" 8 9 "github.com/google/gopacket/layers" 10 "github.com/prometheus/client_golang/prometheus" 11 12 flowpb "github.com/cilium/cilium/api/v1/flow" 13 "github.com/cilium/cilium/pkg/hubble/metrics/api" 14 ) 15 16 type icmpHandler struct { 17 icmp *prometheus.CounterVec 18 context *api.ContextOptions 19 } 20 21 func (h *icmpHandler) Init(registry *prometheus.Registry, options api.Options) error { 22 c, err := api.ParseContextOptions(options) 23 if err != nil { 24 return err 25 } 26 h.context = c 27 28 labels := []string{"family", "type"} 29 labels = append(labels, h.context.GetLabelNames()...) 30 31 h.icmp = prometheus.NewCounterVec(prometheus.CounterOpts{ 32 Namespace: api.DefaultPrometheusNamespace, 33 Name: "icmp_total", 34 Help: "Number of ICMP messages", 35 }, labels) 36 37 registry.MustRegister(h.icmp) 38 return nil 39 } 40 41 func (h *icmpHandler) Status() string { 42 return h.context.Status() 43 } 44 45 func (h *icmpHandler) Context() *api.ContextOptions { 46 return h.context 47 } 48 49 func (h *icmpHandler) ListMetricVec() []*prometheus.MetricVec { 50 return []*prometheus.MetricVec{h.icmp.MetricVec} 51 } 52 53 func (h *icmpHandler) ProcessFlow(ctx context.Context, flow *flowpb.Flow) error { 54 l4 := flow.GetL4() 55 if l4 == nil { 56 return nil 57 } 58 59 labelValues, err := h.context.GetLabelValues(flow) 60 if err != nil { 61 return err 62 } 63 64 if icmp := l4.GetICMPv4(); icmp != nil { 65 labels := []string{"IPv4", layers.CreateICMPv4TypeCode(uint8(icmp.Type), uint8(icmp.Code)).String()} 66 labels = append(labels, labelValues...) 67 h.icmp.WithLabelValues(labels...).Inc() 68 } 69 70 if icmp := l4.GetICMPv6(); icmp != nil { 71 labels := []string{"IPv4", layers.CreateICMPv6TypeCode(uint8(icmp.Type), uint8(icmp.Code)).String()} 72 labels = append(labels, labelValues...) 73 h.icmp.WithLabelValues(labels...).Inc() 74 } 75 76 return nil 77 }