ServeHTTP implements http.Handler.
(w http.ResponseWriter, r *http.Request)
| 77 | |
| 78 | // ServeHTTP implements http.Handler. |
| 79 | func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 80 | collects := r.URL.Query()["collect[]"] |
| 81 | h.logger.Debug("collect query:", "collects", collects) |
| 82 | |
| 83 | excludes := r.URL.Query()["exclude[]"] |
| 84 | h.logger.Debug("exclude query:", "excludes", excludes) |
| 85 | |
| 86 | if len(collects) == 0 && len(excludes) == 0 { |
| 87 | // No filters, use the prepared unfiltered handler. |
| 88 | h.unfilteredHandler.ServeHTTP(w, r) |
| 89 | return |
| 90 | } |
| 91 | |
| 92 | if len(collects) > 0 && len(excludes) > 0 { |
| 93 | h.logger.Debug("rejecting combined collect and exclude queries") |
| 94 | w.WriteHeader(http.StatusBadRequest) |
| 95 | w.Write([]byte("Combined collect and exclude queries are not allowed.")) |
| 96 | return |
| 97 | } |
| 98 | |
| 99 | filters := &collects |
| 100 | if len(excludes) > 0 { |
| 101 | // In exclude mode, filtered collectors = enabled - excludeed. |
| 102 | f := []string{} |
| 103 | for _, c := range h.enabledCollectors { |
| 104 | if (slices.Index(excludes, c)) == -1 { |
| 105 | f = append(f, c) |
| 106 | } |
| 107 | } |
| 108 | filters = &f |
| 109 | } |
| 110 | |
| 111 | // To serve filtered metrics, we create a filtering handler on the fly. |
| 112 | filteredHandler, err := h.innerHandler(*filters...) |
| 113 | if err != nil { |
| 114 | h.logger.Warn("Couldn't create filtered metrics handler:", "err", err) |
| 115 | w.WriteHeader(http.StatusBadRequest) |
| 116 | fmt.Fprintf(w, "Couldn't create filtered metrics handler: %s", err) |
| 117 | return |
| 118 | } |
| 119 | filteredHandler.ServeHTTP(w, r) |
| 120 | } |
| 121 | |
| 122 | // innerHandler is used to create both the one unfiltered http.Handler to be |
| 123 | // wrapped by the outer handler and also the filtered handlers created on the |