github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/publicapi/endpoint_health.go (about)

     1  package publicapi
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/filecoin-project/bacalhau/pkg/model"
     7  	"github.com/filecoin-project/bacalhau/pkg/types"
     8  	"github.com/rs/zerolog/log"
     9  )
    10  
    11  var LINESOFLOGTOPRINT = 100
    12  
    13  func GenerateHealthData() types.HealthInfo {
    14  	var healthInfo types.HealthInfo
    15  
    16  	// Generating all, free, used amounts for each - in case these are different mounts, they'll have different
    17  	// All and Free values, if they're all on the same machine, then those values should be the same
    18  	// If "All" is 0, that means the directory does not exist
    19  	healthInfo.DiskFreeSpace.IPFSMount = MountUsage("/data/ipfs")
    20  	healthInfo.DiskFreeSpace.ROOT = MountUsage("/")
    21  	healthInfo.DiskFreeSpace.TMP = MountUsage("/tmp")
    22  
    23  	return healthInfo
    24  }
    25  
    26  // livez godoc
    27  //
    28  //	@ID			livez
    29  //	@Tags		Utils
    30  //	@Produce	text/plain
    31  //	@Success	200	{object}	string	"TODO"
    32  //	@Router		/livez [get]
    33  func (apiServer *APIServer) livez(res http.ResponseWriter, req *http.Request) {
    34  	// Extremely simple liveness check (should be fine to be public / no-auth)
    35  	log.Ctx(req.Context()).Debug().Msg("Received OK request")
    36  	res.Header().Add("Content-Type", "text/plain")
    37  	res.WriteHeader(http.StatusOK)
    38  	_, err := res.Write([]byte("OK"))
    39  	if err != nil {
    40  		log.Ctx(req.Context()).Warn().Err(err).Msg("Error writing body for OK request.")
    41  	}
    42  }
    43  
    44  // logz godoc
    45  //
    46  //	@ID			logz
    47  //	@Tags		Utils
    48  //	@Produce	text/plain
    49  //	@Success	200	{object}	string	"TODO"
    50  //	@Router		/logz [get]
    51  func (apiServer *APIServer) logz(res http.ResponseWriter, req *http.Request) {
    52  	log.Ctx(req.Context()).Debug().Msg("Received logz request")
    53  	res.Header().Add("Content-Type", "text/plain")
    54  	res.WriteHeader(http.StatusOK)
    55  	fileOutput, err := TailFile(LINESOFLOGTOPRINT, "/tmp/ipfs.log")
    56  	if err != nil {
    57  		missingLogFileMsg := "File not found at /tmp/ipfs.log"
    58  		log.Ctx(req.Context()).Warn().Msgf(missingLogFileMsg)
    59  		_, err = res.Write([]byte("File not found at /tmp/ipfs.log"))
    60  		if err != nil {
    61  			log.Ctx(req.Context()).Warn().Msgf("Could not write response body for missing log file to response.")
    62  		}
    63  		return
    64  	}
    65  	_, err = res.Write(fileOutput)
    66  	if err != nil {
    67  		log.Ctx(req.Context()).Warn().Msg("Error writing body for logz request.")
    68  	}
    69  }
    70  
    71  // readyz godoc
    72  //
    73  //	@ID			readyz
    74  //	@Tags		Utils
    75  //	@Produce	text/plain
    76  //	@Success	200	{object}	string
    77  //	@Router		/readyz [get]
    78  func (apiServer *APIServer) readyz(res http.ResponseWriter, req *http.Request) {
    79  	log.Ctx(req.Context()).Debug().Msg("Received readyz request.")
    80  	// TODO: Add checker for queue that this node can accept submissions
    81  	isReady := true
    82  
    83  	res.Header().Add("Content-Type", "text/plain")
    84  	if !isReady {
    85  		res.WriteHeader(http.StatusServiceUnavailable)
    86  	}
    87  	res.WriteHeader(http.StatusOK)
    88  	_, err := res.Write([]byte("READY"))
    89  	if err != nil {
    90  		log.Ctx(req.Context()).Warn().Msg("Error writing body for readyz request.")
    91  	}
    92  }
    93  
    94  // healthz godoc
    95  //
    96  //	@ID			healthz
    97  //	@Tags		Utils
    98  //	@Produce	json
    99  //	@Success	200	{object}	types.HealthInfo
   100  //	@Router		/healthz [get]
   101  func (apiServer *APIServer) healthz(res http.ResponseWriter, req *http.Request) {
   102  	// TODO: A list of health information. Should require authing (of some kind)
   103  	log.Ctx(req.Context()).Debug().Msg("Received healthz request.")
   104  	res.Header().Add("Content-Type", "application/json")
   105  	res.WriteHeader(http.StatusOK)
   106  
   107  	// Ideas:
   108  	// CPU usage
   109  
   110  	healthInfo := GenerateHealthData()
   111  	healthJSONBlob, _ := model.JSONMarshalWithMax(healthInfo)
   112  
   113  	_, err := res.Write(healthJSONBlob)
   114  	if err != nil {
   115  		log.Ctx(req.Context()).Warn().Msg("Error writing body for healthz request.")
   116  	}
   117  }
   118  
   119  // varz godoc
   120  //
   121  //	@ID			varz
   122  //	@Tags		Utils
   123  //	@Produce	json
   124  //	@Success	200	{object}	json.RawMessage
   125  //	@Router		/varz [get]
   126  func (apiServer *APIServer) varz(res http.ResponseWriter, req *http.Request) {
   127  	// TODO: Fill in with the configuration settings for this node
   128  	res.WriteHeader(http.StatusOK)
   129  	res.Header().Add("Content-Type", "application/json")
   130  
   131  	_, err := res.Write([]byte("{}"))
   132  	if err != nil {
   133  		log.Ctx(req.Context()).Warn().Msg("Error writing body for varz request.")
   134  	}
   135  }