-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/healthz endpoint accepts JSON now (#5558)
* /healthz endpoint accepts JSON now * Merge refs/heads/master into json-healthz * Merge refs/heads/master into json-healthz * Merge refs/heads/master into json-healthz * Merge refs/heads/master into json-healthz * Merge refs/heads/master into json-healthz * Merge refs/heads/master into json-healthz
- Loading branch information
Showing
5 changed files
with
184 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package prometheus | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/golang/gddo/httputil" | ||
) | ||
|
||
const ( | ||
contentTypePlainText = "text/plain" | ||
contentTypeJSON = "application/json" | ||
) | ||
|
||
// generatedResponse is a container for response output. | ||
type generatedResponse struct { | ||
// Err is protocol error, if any. | ||
Err string `json:"error"` | ||
|
||
// Data is response output, if any. | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
// negotiateContentType parses "Accept:" header and returns preferred content type string. | ||
func negotiateContentType(r *http.Request) string { | ||
contentTypes := []string{ | ||
contentTypePlainText, | ||
contentTypeJSON, | ||
} | ||
return httputil.NegotiateContentType(r, contentTypes, contentTypePlainText) | ||
} | ||
|
||
// writeResponse is content-type aware response writer. | ||
func writeResponse(w http.ResponseWriter, r *http.Request, response generatedResponse) error { | ||
if response.Err != "" { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} else { | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
switch negotiateContentType(r) { | ||
case contentTypePlainText: | ||
buf, ok := response.Data.(bytes.Buffer) | ||
if !ok { | ||
return fmt.Errorf("unexpected data: %v", response.Data) | ||
} | ||
if _, err := w.Write(buf.Bytes()); err != nil { | ||
return fmt.Errorf("could not write response body: %v", err) | ||
} | ||
case contentTypeJSON: | ||
w.Header().Set("Content-Type", contentTypeJSON) | ||
if err := json.NewEncoder(w).Encode(response); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters