| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package k8s_apiserver |
| 4 | |
| 5 | import ( |
| 6 | mtx "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/oldmetrix" |
| 7 | ) |
| 8 | |
| 9 | func newMetrics() *metrics { |
| 10 | var mx metrics |
| 11 | mx.Request.ByVerb = make(map[string]mtx.Gauge) |
| 12 | mx.Request.ByCode = make(map[string]mtx.Gauge) |
| 13 | mx.Request.ByResource = make(map[string]mtx.Gauge) |
| 14 | mx.RESTClient.ByCode = make(map[string]mtx.Gauge) |
| 15 | mx.RESTClient.ByMethod = make(map[string]mtx.Gauge) |
| 16 | mx.Workqueue.Controllers = make(map[string]*workqueueMetrics) |
| 17 | mx.Etcd.ObjectCounts = make(map[string]mtx.Gauge) |
| 18 | mx.Admission.Controllers = make(map[string]*admissionControllerMetrics) |
| 19 | mx.Admission.Webhooks = make(map[string]*admissionWebhookMetrics) |
| 20 | return &mx |
| 21 | } |
| 22 | |
| 23 | type metrics struct { |
| 24 | Request requestMetrics `stm:"request"` |
| 25 | Inflight inflightMetrics `stm:"inflight"` |
| 26 | RESTClient restClientMetrics `stm:"rest_client"` |
| 27 | Admission admissionMetrics `stm:"admission"` |
| 28 | Etcd etcdMetrics `stm:"etcd"` |
| 29 | Workqueue workqueueGroup `stm:"workqueue"` |
| 30 | Process processMetrics `stm:"process"` |
| 31 | Audit auditMetrics `stm:"audit"` |
| 32 | Auth authMetrics `stm:"auth"` |
| 33 | } |
| 34 | |
| 35 | // Request metrics: apiserver_request_total, apiserver_request_duration_seconds |
| 36 | type requestMetrics struct { |
| 37 | Total mtx.Gauge `stm:"total"` |
| 38 | ByVerb map[string]mtx.Gauge `stm:"by_verb"` |
| 39 | ByCode map[string]mtx.Gauge `stm:"by_code"` |
| 40 | ByResource map[string]mtx.Gauge `stm:"by_resource"` |
| 41 | Dropped mtx.Gauge `stm:"dropped_total"` |
| 42 | // Latency percentiles (from histogram) |
| 43 | Latency struct { |
| 44 | P50 mtx.Gauge `stm:"p50"` |
| 45 | P90 mtx.Gauge `stm:"p90"` |
| 46 | P99 mtx.Gauge `stm:"p99"` |
| 47 | } `stm:"latency"` |
| 48 | // Response sizes (from histogram) |
| 49 | ResponseSize struct { |
| 50 | P50 mtx.Gauge `stm:"p50"` |
| 51 | P90 mtx.Gauge `stm:"p90"` |
| 52 | P99 mtx.Gauge `stm:"p99"` |
| 53 | } `stm:"response_size"` |
| 54 | } |
| 55 | |
| 56 | // Inflight metrics: apiserver_current_inflight_requests, apiserver_longrunning_gauge |
| 57 | type inflightMetrics struct { |
| 58 | Mutating mtx.Gauge `stm:"mutating"` |
| 59 | ReadOnly mtx.Gauge `stm:"readonly"` |
| 60 | Longrunning mtx.Gauge `stm:"longrunning"` |
| 61 | } |
| 62 | |
| 63 | // REST client metrics: rest_client_requests_total, rest_client_request_duration_seconds |
| 64 | type restClientMetrics struct { |
| 65 | ByCode map[string]mtx.Gauge `stm:"by_code"` |
| 66 | ByMethod map[string]mtx.Gauge `stm:"by_method"` |
| 67 | Latency struct { |
| 68 | P50 mtx.Gauge `stm:"p50"` |
| 69 | P90 mtx.Gauge `stm:"p90"` |
| 70 | P99 mtx.Gauge `stm:"p99"` |
| 71 | } `stm:"latency"` |
| 72 | } |
| 73 | |
| 74 | // Admission metrics |
| 75 | type admissionMetrics struct { |
| 76 | Controllers map[string]*admissionControllerMetrics `stm:"controller"` |
| 77 | Webhooks map[string]*admissionWebhookMetrics `stm:"webhook"` |
| 78 | StepLatency struct { |
| 79 | Validate mtx.Gauge `stm:"validate"` |
| 80 | Admit mtx.Gauge `stm:"admit"` |
| 81 | } `stm:"step_latency"` |
| 82 | } |
| 83 | |
| 84 | type admissionControllerMetrics struct { |
| 85 | // Histogram bucket counts (non-cumulative) for heatmap visualization |
| 86 | Bucket5ms mtx.Gauge `stm:"bucket_5ms"` |
| 87 | Bucket25ms mtx.Gauge `stm:"bucket_25ms"` |
| 88 | Bucket100ms mtx.Gauge `stm:"bucket_100ms"` |
| 89 | Bucket500ms mtx.Gauge `stm:"bucket_500ms"` |
| 90 | Bucket1s mtx.Gauge `stm:"bucket_1s"` |
| 91 | Bucket2500ms mtx.Gauge `stm:"bucket_2500ms"` |
| 92 | BucketInf mtx.Gauge `stm:"bucket_inf"` |
| 93 | } |
| 94 | |
| 95 | type admissionWebhookMetrics struct { |
| 96 | // Histogram bucket counts (non-cumulative) for heatmap visualization |
| 97 | Bucket5ms mtx.Gauge `stm:"bucket_5ms"` |
| 98 | Bucket25ms mtx.Gauge `stm:"bucket_25ms"` |
| 99 | Bucket100ms mtx.Gauge `stm:"bucket_100ms"` |
| 100 | Bucket500ms mtx.Gauge `stm:"bucket_500ms"` |
| 101 | Bucket1s mtx.Gauge `stm:"bucket_1s"` |
| 102 | Bucket2500ms mtx.Gauge `stm:"bucket_2500ms"` |
| 103 | BucketInf mtx.Gauge `stm:"bucket_inf"` |
| 104 | } |
| 105 | |
| 106 | // Etcd metrics |
| 107 | type etcdMetrics struct { |
| 108 | ObjectCounts map[string]mtx.Gauge `stm:"objects"` |
| 109 | } |
| 110 | |
| 111 | // Workqueue metrics (for each controller) |
| 112 | type workqueueGroup struct { |
| 113 | Controllers map[string]*workqueueMetrics `stm:""` |
| 114 | } |
| 115 | |
| 116 | type workqueueMetrics struct { |
| 117 | Depth mtx.Gauge `stm:"depth"` |
| 118 | Adds mtx.Gauge `stm:"adds_total"` |
| 119 | Retries mtx.Gauge `stm:"retries_total"` |
| 120 | LatencyP50 mtx.Gauge `stm:"latency_p50"` |
| 121 | LatencyP90 mtx.Gauge `stm:"latency_p90"` |
| 122 | LatencyP99 mtx.Gauge `stm:"latency_p99"` |
| 123 | DurationP50 mtx.Gauge `stm:"duration_p50"` |
| 124 | DurationP90 mtx.Gauge `stm:"duration_p90"` |
| 125 | DurationP99 mtx.Gauge `stm:"duration_p99"` |
| 126 | } |
| 127 | |
| 128 | // Process metrics: go runtime and process |
| 129 | type processMetrics struct { |
| 130 | Goroutines mtx.Gauge `stm:"goroutines"` |
| 131 | Threads mtx.Gauge `stm:"threads"` |
| 132 | CPUSeconds mtx.Gauge `stm:"cpu_seconds_total"` |
| 133 | ResidentMemory mtx.Gauge `stm:"resident_memory_bytes"` |
| 134 | VirtualMemory mtx.Gauge `stm:"virtual_memory_bytes"` |
| 135 | OpenFDs mtx.Gauge `stm:"open_fds"` |
| 136 | MaxFDs mtx.Gauge `stm:"max_fds"` |
| 137 | HeapAlloc mtx.Gauge `stm:"heap_alloc_bytes"` |
| 138 | HeapInuse mtx.Gauge `stm:"heap_inuse_bytes"` |
| 139 | StackInuse mtx.Gauge `stm:"stack_inuse_bytes"` |
| 140 | GCDurationMin mtx.Gauge `stm:"gc_duration_min"` |
| 141 | GCDurationP25 mtx.Gauge `stm:"gc_duration_p25"` |
| 142 | GCDurationP50 mtx.Gauge `stm:"gc_duration_p50"` |
| 143 | GCDurationP75 mtx.Gauge `stm:"gc_duration_p75"` |
| 144 | GCDurationMax mtx.Gauge `stm:"gc_duration_max"` |
| 145 | } |
| 146 | |
| 147 | // Audit metrics |
| 148 | type auditMetrics struct { |
| 149 | EventsTotal mtx.Gauge `stm:"events_total"` |
| 150 | RejectedTotal mtx.Gauge `stm:"rejected_total"` |
| 151 | } |
| 152 | |
| 153 | // Auth metrics |
| 154 | type authMetrics struct { |
| 155 | AuthenticatedRequests mtx.Gauge `stm:"authenticated_requests_total"` |
| 156 | CertExpirationSeconds mtx.Gauge `stm:"client_cert_expiration_seconds"` |
| 157 | } |