|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright Authors of Cilium |
| 3 | + |
| 4 | +package metrics |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + "net" |
| 10 | + "regexp" |
| 11 | + "syscall" |
| 12 | + |
| 13 | + "github.com/cilium/hive/cell" |
| 14 | + "github.com/prometheus/client_golang/prometheus" |
| 15 | + "github.com/prometheus/client_golang/prometheus/collectors" |
| 16 | + "golang.org/x/sys/unix" |
| 17 | + |
| 18 | + "github.com/cilium/cilium/pkg/metrics" |
| 19 | + "github.com/cilium/cilium/pkg/metrics/metric" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + sdpNamespace = "standalone_dns_proxy" |
| 24 | + |
| 25 | + // Error labels for CiliumAgentConnection metric. |
| 26 | + LabelErrorClientCreation = "client_creation" // gRPC client creation failed |
| 27 | + LabelErrorOpenPolicyStream = "open_policy_stream" // opening the policy stream failed |
| 28 | + |
| 29 | + // Error labels for RetrieveDNSRules metric. |
| 30 | + LabelErrorPolicyStreamSend = "policy_stream_send" // sending ACK on the policy stream failed |
| 31 | + LabelErrorPolicyStreamRecv = "policy_stream_recv" // receiving from the policy stream failed |
| 32 | + |
| 33 | + // Error labels for FQDNMappingSync metric. |
| 34 | + LabelErrorMappingSyncConnection = "mapping_sync_connection" // FQDN mapping sync failed due to connection error |
| 35 | + LabelErrorMappingSyncRequest = "mapping_sync_request" // FQDN mapping sync request failed |
| 36 | +) |
| 37 | + |
| 38 | +// goCustomCollectorsRX tracks enabled go runtime metrics. |
| 39 | +var goCustomCollectorsRX = regexp.MustCompile(`^/sched/latencies:seconds`) |
| 40 | + |
| 41 | +// Metrics contains all metrics for the standalone DNS proxy. |
| 42 | +type Metrics struct { |
| 43 | + // CiliumAgentConnection tracks total errors while connecting to cilium-agent. |
| 44 | + CiliumAgentConnection metric.Vec[metric.Counter] |
| 45 | + |
| 46 | + // FQDNMappingSync tracks errors while syncing FQDN mappings. |
| 47 | + FQDNMappingSync metric.Vec[metric.Counter] |
| 48 | + |
| 49 | + // RetrieveDNSRules tracks errors while retrieving DNS rules. |
| 50 | + RetrieveDNSRules metric.Vec[metric.Counter] |
| 51 | + |
| 52 | + // ProxyBootstrapError tracks errors that occurred during proxy bootstrap. |
| 53 | + ProxyBootstrapError metric.Counter |
| 54 | +} |
| 55 | + |
| 56 | +// NewMetrics creates the SDP metrics. |
| 57 | +func NewMetrics() *Metrics { |
| 58 | + return &Metrics{ |
| 59 | + CiliumAgentConnection: metric.NewCounterVec(metric.CounterOpts{ |
| 60 | + Namespace: sdpNamespace, |
| 61 | + Name: "cilium_agent_connection_errors", |
| 62 | + Help: "Total errors while connecting to cilium-agent", |
| 63 | + }, []string{metrics.LabelError}), |
| 64 | + FQDNMappingSync: metric.NewCounterVec(metric.CounterOpts{ |
| 65 | + Namespace: sdpNamespace, |
| 66 | + Name: "fqdn_mapping_sync_errors", |
| 67 | + Help: "Total errors while syncing FQDN mappings", |
| 68 | + }, []string{metrics.LabelError}), |
| 69 | + RetrieveDNSRules: metric.NewCounterVec(metric.CounterOpts{ |
| 70 | + Namespace: sdpNamespace, |
| 71 | + Name: "retrieve_dns_rules_errors", |
| 72 | + Help: "Total errors while retrieving DNS rules", |
| 73 | + }, []string{metrics.LabelError}), |
| 74 | + ProxyBootstrapError: metric.NewCounter(metric.CounterOpts{ |
| 75 | + Namespace: sdpNamespace, |
| 76 | + Name: "proxy_bootstrap_errors", |
| 77 | + Help: "Number of errors occurred during proxy bootstrap", |
| 78 | + }), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +type initParams struct { |
| 83 | + cell.In |
| 84 | + |
| 85 | + Logger *slog.Logger |
| 86 | + Registry *metrics.Registry |
| 87 | + |
| 88 | + Metrics []metric.WithMetadata `group:"hive-metrics"` |
| 89 | +} |
| 90 | + |
| 91 | +func initializeMetrics(p initParams) { |
| 92 | + p.Registry.MustRegister(collectors.NewGoCollector( |
| 93 | + collectors.WithGoCollectorRuntimeMetrics( |
| 94 | + collectors.GoRuntimeMetricsRule{Matcher: goCustomCollectorsRX}, |
| 95 | + ), |
| 96 | + )) |
| 97 | + |
| 98 | + p.Registry.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{ |
| 99 | + Namespace: sdpNamespace, |
| 100 | + })) |
| 101 | + |
| 102 | + for _, m := range p.Metrics { |
| 103 | + p.Registry.MustRegister(m.(prometheus.Collector)) |
| 104 | + } |
| 105 | + |
| 106 | + p.Registry.AddServerRuntimeHooks("sdp-prometheus-server", nil, net.ListenConfig{ |
| 107 | + Control: setsockoptReusePort, |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +// setsockoptReusePort sets SO_REUSEPORT on the socket to allow the new SDP pod |
| 112 | +// to bind the metrics port while the old pod is still terminating during a |
| 113 | +// surge-based rolling update. |
| 114 | +func setsockoptReusePort(network, address string, c syscall.RawConn) error { |
| 115 | + var soerr error |
| 116 | + if err := c.Control(func(su uintptr) { |
| 117 | + s := int(su) |
| 118 | + if err := unix.SetsockoptInt(s, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil { |
| 119 | + soerr = fmt.Errorf("failed to setsockopt(SO_REUSEPORT): %w", err) |
| 120 | + } |
| 121 | + }); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + return soerr |
| 125 | +} |
0 commit comments