|
| 1 | +package exporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "sort" |
| 7 | + "strings" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "github.com/alexandredaubois/ember/internal/model" |
| 11 | +) |
| 12 | + |
| 13 | +type StateHolder struct { |
| 14 | + mu sync.RWMutex |
| 15 | + state model.State |
| 16 | +} |
| 17 | + |
| 18 | +func (h *StateHolder) Store(s model.State) { |
| 19 | + h.mu.Lock() |
| 20 | + h.state = s |
| 21 | + h.mu.Unlock() |
| 22 | +} |
| 23 | + |
| 24 | +func (h *StateHolder) Load() model.State { |
| 25 | + h.mu.RLock() |
| 26 | + defer h.mu.RUnlock() |
| 27 | + return h.state |
| 28 | +} |
| 29 | + |
| 30 | +const prometheusContentType = "text/plain; version=0.0.4; charset=utf-8" |
| 31 | + |
| 32 | +func Handler(holder *StateHolder) http.HandlerFunc { |
| 33 | + return func(w http.ResponseWriter, r *http.Request) { |
| 34 | + s := holder.Load() |
| 35 | + if s.Current == nil { |
| 36 | + http.Error(w, "no data yet", http.StatusServiceUnavailable) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + w.Header().Set("Content-Type", prometheusContentType) |
| 41 | + |
| 42 | + writeThreadMetrics(w, &s) |
| 43 | + writeThreadMemory(w, &s) |
| 44 | + writeWorkerMetrics(w, &s) |
| 45 | + writePercentiles(w, &s) |
| 46 | + writeProcessMetrics(w, &s) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func writeThreadMetrics(w http.ResponseWriter, s *model.State) { |
| 51 | + total := len(s.Current.Threads.ThreadDebugStates) |
| 52 | + other := total - s.Derived.TotalBusy - s.Derived.TotalIdle |
| 53 | + if other < 0 { |
| 54 | + other = 0 |
| 55 | + } |
| 56 | + |
| 57 | + fmt.Fprintln(w, "# HELP frankenphp_threads_total Number of FrankenPHP threads by state") |
| 58 | + fmt.Fprintln(w, "# TYPE frankenphp_threads_total gauge") |
| 59 | + fmt.Fprintf(w, "frankenphp_threads_total{state=\"busy\"} %d\n", s.Derived.TotalBusy) |
| 60 | + fmt.Fprintf(w, "frankenphp_threads_total{state=\"idle\"} %d\n", s.Derived.TotalIdle) |
| 61 | + fmt.Fprintf(w, "frankenphp_threads_total{state=\"other\"} %d\n", other) |
| 62 | +} |
| 63 | + |
| 64 | +func writeThreadMemory(w http.ResponseWriter, s *model.State) { |
| 65 | + hasMemory := false |
| 66 | + for _, t := range s.Current.Threads.ThreadDebugStates { |
| 67 | + if t.MemoryUsage > 0 { |
| 68 | + hasMemory = true |
| 69 | + break |
| 70 | + } |
| 71 | + } |
| 72 | + if !hasMemory { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + fmt.Fprintln(w, "# HELP frankenphp_thread_memory_bytes Memory usage per FrankenPHP thread") |
| 77 | + fmt.Fprintln(w, "# TYPE frankenphp_thread_memory_bytes gauge") |
| 78 | + for _, t := range s.Current.Threads.ThreadDebugStates { |
| 79 | + if t.MemoryUsage > 0 { |
| 80 | + fmt.Fprintf(w, "frankenphp_thread_memory_bytes{index=\"%d\"} %d\n", t.Index, t.MemoryUsage) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func writeWorkerMetrics(out http.ResponseWriter, s *model.State) { |
| 86 | + if len(s.Current.Metrics.Workers) == 0 { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + names := sortedWorkerNames(s.Current.Metrics.Workers) |
| 91 | + |
| 92 | + fmt.Fprintln(out, "# HELP frankenphp_worker_crashes_total Total worker crashes") |
| 93 | + fmt.Fprintln(out, "# TYPE frankenphp_worker_crashes_total counter") |
| 94 | + for _, name := range names { |
| 95 | + wm := s.Current.Metrics.Workers[name] |
| 96 | + fmt.Fprintf(out, "frankenphp_worker_crashes_total{worker=\"%s\"} %g\n", escapeLabelValue(name), wm.Crashes) |
| 97 | + } |
| 98 | + |
| 99 | + fmt.Fprintln(out, "# HELP frankenphp_worker_restarts_total Total worker restarts") |
| 100 | + fmt.Fprintln(out, "# TYPE frankenphp_worker_restarts_total counter") |
| 101 | + for _, name := range names { |
| 102 | + wm := s.Current.Metrics.Workers[name] |
| 103 | + fmt.Fprintf(out, "frankenphp_worker_restarts_total{worker=\"%s\"} %g\n", escapeLabelValue(name), wm.Restarts) |
| 104 | + } |
| 105 | + |
| 106 | + fmt.Fprintln(out, "# HELP frankenphp_worker_queue_depth Requests in queue per worker") |
| 107 | + fmt.Fprintln(out, "# TYPE frankenphp_worker_queue_depth gauge") |
| 108 | + for _, name := range names { |
| 109 | + wm := s.Current.Metrics.Workers[name] |
| 110 | + fmt.Fprintf(out, "frankenphp_worker_queue_depth{worker=\"%s\"} %g\n", escapeLabelValue(name), wm.QueueDepth) |
| 111 | + } |
| 112 | + |
| 113 | + fmt.Fprintln(out, "# HELP frankenphp_worker_requests_total Total requests processed per worker") |
| 114 | + fmt.Fprintln(out, "# TYPE frankenphp_worker_requests_total counter") |
| 115 | + for _, name := range names { |
| 116 | + wm := s.Current.Metrics.Workers[name] |
| 117 | + fmt.Fprintf(out, "frankenphp_worker_requests_total{worker=\"%s\"} %g\n", escapeLabelValue(name), wm.RequestCount) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func writePercentiles(w http.ResponseWriter, s *model.State) { |
| 122 | + if !s.Derived.HasPercentiles { |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + fmt.Fprintln(w, "# HELP frankenphp_request_duration_milliseconds Request duration percentiles") |
| 127 | + fmt.Fprintln(w, "# TYPE frankenphp_request_duration_milliseconds gauge") |
| 128 | + fmt.Fprintf(w, "frankenphp_request_duration_milliseconds{quantile=\"0.5\"} %.2f\n", s.Derived.P50) |
| 129 | + fmt.Fprintf(w, "frankenphp_request_duration_milliseconds{quantile=\"0.95\"} %.2f\n", s.Derived.P95) |
| 130 | + fmt.Fprintf(w, "frankenphp_request_duration_milliseconds{quantile=\"0.99\"} %.2f\n", s.Derived.P99) |
| 131 | +} |
| 132 | + |
| 133 | +func writeProcessMetrics(w http.ResponseWriter, s *model.State) { |
| 134 | + fmt.Fprintln(w, "# HELP process_cpu_percent CPU usage of the monitored process") |
| 135 | + fmt.Fprintln(w, "# TYPE process_cpu_percent gauge") |
| 136 | + fmt.Fprintf(w, "process_cpu_percent %.2f\n", s.Current.Process.CPUPercent) |
| 137 | + |
| 138 | + fmt.Fprintln(w, "# HELP process_rss_bytes Resident set size of the monitored process") |
| 139 | + fmt.Fprintln(w, "# TYPE process_rss_bytes gauge") |
| 140 | + fmt.Fprintf(w, "process_rss_bytes %d\n", s.Current.Process.RSS) |
| 141 | +} |
| 142 | + |
| 143 | +func escapeLabelValue(s string) string { |
| 144 | + s = strings.ReplaceAll(s, `\`, `\\`) |
| 145 | + s = strings.ReplaceAll(s, `"`, `\"`) |
| 146 | + s = strings.ReplaceAll(s, "\n", `\n`) |
| 147 | + return s |
| 148 | +} |
| 149 | + |
| 150 | +func sortedWorkerNames[V any](m map[string]V) []string { |
| 151 | + names := make([]string, 0, len(m)) |
| 152 | + for name := range m { |
| 153 | + names = append(names, name) |
| 154 | + } |
| 155 | + sort.Strings(names) |
| 156 | + return names |
| 157 | +} |
0 commit comments