| 28 | } |
| 29 | |
| 30 | func (s *Server) prometheusDiscovery(rw http.ResponseWriter, req *http.Request) { |
| 31 | targets := []prometheusStaticConfig{} |
| 32 | |
| 33 | scheme := "http" |
| 34 | if req.TLS != nil { |
| 35 | scheme = "https" |
| 36 | } |
| 37 | |
| 38 | hosts := []string{req.Host} |
| 39 | |
| 40 | var listNode []string |
| 41 | |
| 42 | metrics := s.metrics.Get() |
| 43 | for _, m := range metrics { |
| 44 | if strings.Contains(m.Spec.Path, "{nodeName}") { |
| 45 | if listNode == nil { |
| 46 | listNode = s.dataSource.ListNodes() |
| 47 | } |
| 48 | for _, nodeName := range listNode { |
| 49 | targets = append(targets, prometheusStaticConfig{ |
| 50 | Targets: hosts, |
| 51 | Labels: map[string]string{ |
| 52 | "metrics_name": m.Name, |
| 53 | "__scheme__": scheme, |
| 54 | "__metrics_path__": strings.ReplaceAll(m.Spec.Path, "{nodeName}", nodeName), |
| 55 | }, |
| 56 | }) |
| 57 | } |
| 58 | } else { |
| 59 | targets = append(targets, prometheusStaticConfig{ |
| 60 | Targets: hosts, |
| 61 | Labels: map[string]string{ |
| 62 | "metrics_name": m.Name, |
| 63 | "__scheme__": scheme, |
| 64 | "__metrics_path__": m.Spec.Path, |
| 65 | }, |
| 66 | }) |
| 67 | } |
| 68 | } |
| 69 | rw.Header().Set("Content-Type", "application/json") |
| 70 | err := json.NewEncoder(rw).Encode(targets) |
| 71 | if err != nil { |
| 72 | http.Error(rw, err.Error(), http.StatusInternalServerError) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | type prometheusStaticConfig struct { |
| 77 | Targets []string `json:"targets"` |