mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 22:59:58 +00:00
TUN-8792: Make diag/system endpoint always return a JSON
## Summary Change the system information collector and respective http handler so that it always returns a JSON. Closes [TUN-8792](https://jira.cfdata.org/browse/TUN-8792)
This commit is contained in:
@@ -59,6 +59,11 @@ func (handler *Handler) InstallEndpoints(router *http.ServeMux) {
|
||||
router.HandleFunc(systemInformationEndpoint, handler.SystemHandler)
|
||||
}
|
||||
|
||||
type SystemInformationResponse struct {
|
||||
Info *SystemInformation `json:"info"`
|
||||
Err error `json:"errors"`
|
||||
}
|
||||
|
||||
func (handler *Handler) SystemHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
logger := handler.log.With().Str(collectorField, systemCollectorName).Logger()
|
||||
logger.Info().Msg("Collection started")
|
||||
@@ -69,30 +74,15 @@ func (handler *Handler) SystemHandler(writer http.ResponseWriter, request *http.
|
||||
|
||||
defer cancel()
|
||||
|
||||
info, rawInfo, err := handler.systemCollector.Collect(ctx)
|
||||
if err != nil {
|
||||
logger.Error().Err(err).Msg("error occurred whilst collecting system information")
|
||||
info, err := handler.systemCollector.Collect(ctx)
|
||||
|
||||
if rawInfo != "" {
|
||||
logger.Info().Msg("using raw information fallback")
|
||||
bytes := []byte(rawInfo)
|
||||
writeResponse(writer, bytes, &logger)
|
||||
} else {
|
||||
logger.Error().Msg("no raw information available")
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if info == nil {
|
||||
logger.Error().Msgf("system information collection is nil")
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
response := SystemInformationResponse{
|
||||
Info: info,
|
||||
Err: err,
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(writer)
|
||||
|
||||
err = encoder.Encode(info)
|
||||
err = encoder.Encode(response)
|
||||
if err != nil {
|
||||
logger.Error().Err(err).Msgf("error occurred whilst serializing information")
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
|
Reference in New Issue
Block a user