|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/IBM/alchemy-logging/src/go/alog" |
| 11 | + "github.com/prometheus/client_golang/prometheus" |
| 12 | + dto "github.com/prometheus/client_model/go" |
| 13 | +) |
| 14 | + |
| 15 | +// allowedMetrics is a list of metrics that are allowed to be exposed |
| 16 | +var allowedMetrics = map[string]bool{ |
| 17 | + "trawler_version_info": true, |
| 18 | + "health_status": true, |
| 19 | + "datapower_version_info": true, |
| 20 | + "apiconnect_gatewaycluster_status": true, |
| 21 | +} |
| 22 | + |
| 23 | +func RemoteGatewayMetricsHandler(w http.ResponseWriter, r *http.Request) { |
| 24 | + w.Header().Set("Content-Type", "application/json") |
| 25 | + w.WriteHeader(http.StatusOK) |
| 26 | + fmt.Fprint(w, getMetrics()) |
| 27 | +} |
| 28 | + |
| 29 | +// Set up a timer to report hourly |
| 30 | +func RemoteGatewayReport(ClientId, Url string) { |
| 31 | + log.Log(alog.INFO, "Starting hourly reporting for remote gateway") |
| 32 | + ticker := time.NewTicker(1 * time.Hour) |
| 33 | + // Start the datapower loop |
| 34 | + for range ticker.C { |
| 35 | + PostMetrics(ClientId, Url) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Send metrics for remote gateway back to IBM APIC SaaS |
| 40 | +func PostMetrics(ClientId, Url string) { |
| 41 | + |
| 42 | + client := &http.Client{} |
| 43 | + |
| 44 | + client.Transport = &http.Transport{} |
| 45 | + log.Log(alog.DEBUG, "Calling %s", Url) |
| 46 | + |
| 47 | + req, err := http.NewRequest("POST", Url, strings.NewReader(getMetrics())) |
| 48 | + if err != nil { |
| 49 | + log.Log(alog.ERROR, err.Error()) |
| 50 | + } |
| 51 | + req.Header.Add("Accept", "application/json") |
| 52 | + req.Header.Add("Content-Type", "application/json") |
| 53 | + req.Header.Add("User-Agent", fmt.Sprintf("Trawler/%s", Version)) |
| 54 | + req.Header.Add("X-IBM-Client-ID", ClientId) |
| 55 | + |
| 56 | + response, err := client.Do(req) |
| 57 | + if err != nil { |
| 58 | + log.Log(alog.ERROR, err.Error()) |
| 59 | + } |
| 60 | + if response.StatusCode != 200 { |
| 61 | + log.Log(alog.ERROR, "unexpected status - got %s, expected 200", response.Status) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// MetricData represents the structured data for a metric |
| 66 | +type MetricData map[string]interface{} |
| 67 | + |
| 68 | +// StatusMetricData represents the structured data for a status metric |
| 69 | +type StatusMetricData map[string]interface{} |
| 70 | + |
| 71 | +type StatusOutput struct { |
| 72 | + Name string `json:"name"` |
| 73 | + Status string `json:"status"` |
| 74 | + Namespace string `json:"namespace"` |
| 75 | + Version string `json:"version"` |
| 76 | +} |
| 77 | +type TrawlerDetails struct { |
| 78 | + Version string `json:"version"` |
| 79 | + BuildTime string `json:"buildtime"` |
| 80 | +} |
| 81 | + |
| 82 | +type Payload struct { |
| 83 | + Subsystems []StatusOutput `json:"subsystems"` |
| 84 | + Trawler TrawlerDetails `json:"trawler"` |
| 85 | +} |
| 86 | + |
| 87 | +// getMetrics gathers and formats Prometheus metrics for remote gateway consumption |
| 88 | +func getMetrics() string { |
| 89 | + metrics, err := prometheus.DefaultGatherer.Gather() |
| 90 | + if err != nil { |
| 91 | + // Log the error properly instead of just printing |
| 92 | + fmt.Println(err) |
| 93 | + return "{}" |
| 94 | + } |
| 95 | + |
| 96 | + // Process metrics and build the compact representation |
| 97 | + gatewayMetrics := processMetrics(metrics) |
| 98 | + |
| 99 | + response := Payload{ |
| 100 | + Trawler: TrawlerDetails{Version: Version, BuildTime: BuildTime}, |
| 101 | + Subsystems: gatewayMetrics, |
| 102 | + } |
| 103 | + jsonData, _ := json.MarshalIndent(response, "", " ") |
| 104 | + return string(jsonData) |
| 105 | +} |
| 106 | + |
| 107 | +// processMetrics processes the gathered metrics and separates them into status and non-status metrics |
| 108 | +func processMetrics(metrics []*dto.MetricFamily) []StatusOutput { |
| 109 | + // Map to store status metrics by name and namespace |
| 110 | + var gatewayMetrics []StatusOutput |
| 111 | + |
| 112 | + for _, metricFamily := range metrics { |
| 113 | + metricName := metricFamily.GetName() |
| 114 | + |
| 115 | + // Skip metrics that are not in the allowlist |
| 116 | + if !allowedMetrics[metricName] { |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + for _, metric := range metricFamily.GetMetric() { |
| 121 | + // Extract labels from the metric |
| 122 | + labels := extractLabels(metric) |
| 123 | + |
| 124 | + // Get the value based on metric type |
| 125 | + //value := extractValue(metric) |
| 126 | + |
| 127 | + switch metricName { |
| 128 | + case "trawler_version_info": |
| 129 | + // Include trawler details |
| 130 | + case "health_status": |
| 131 | + gatewayMetrics = append(gatewayMetrics, processHealthStatusMetric(labels)) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return gatewayMetrics |
| 136 | +} |
| 137 | + |
| 138 | +// extractLabels extracts label key-value pairs from a metric |
| 139 | +func extractLabels(metric *dto.Metric) map[string]string { |
| 140 | + labels := make(map[string]string) |
| 141 | + for _, label := range metric.GetLabel() { |
| 142 | + labels[label.GetName()] = label.GetValue() |
| 143 | + } |
| 144 | + return labels |
| 145 | +} |
| 146 | + |
| 147 | +// processStatusMetric handles status metrics specifically |
| 148 | +func processHealthStatusMetric(labels map[string]string) StatusOutput { |
| 149 | + |
| 150 | + // Initialize the entry if it doesn't exist |
| 151 | + statusMetric := StatusOutput{ |
| 152 | + Status: labels["condition"], |
| 153 | + Version: labels["version"], |
| 154 | + Name: labels["name"], |
| 155 | + Namespace: labels["namespace"], |
| 156 | + } |
| 157 | + |
| 158 | + // Append to gateway metrics |
| 159 | + return statusMetric |
| 160 | +} |
0 commit comments