~egtann/sjs

6d32275632eea83ff10ded6e03c1848aa79d8658 — Evan Tann 1 year, 9 months ago bb67a05
add host, role info to logs
2 files changed, 8 insertions(+), 2 deletions(-)

M http/client.go
M http/service.go
M http/client.go => http/client.go +5 -1
@@ 21,10 21,12 @@ type Client struct {
	apiKey    string
	selfURL   string
	serverURL string
	host      string
	role      string
	errCh     *sjs.OptErr
}

func NewClient(selfURL, sjsURL, apiKey string) *Client {
func NewClient(selfURL, sjsURL, apiKey, host, role string) *Client {
	return &Client{
		selfURL:   selfURL,
		serverURL: sjsURL,


@@ 84,6 86,8 @@ func (c *Client) notify() error {
		return errors.Wrap(err, "new request")
	}
	req.Header.Set("X-API-Key", c.apiKey)
	req.Header.Set("X-Host", c.host)
	req.Header.Set("X-Role", c.role)
	resp, err := c.client.Do(req)
	if err != nil {
		return errors.Wrap(err, "do")

M http/service.go => http/service.go +3 -1
@@ 110,9 110,11 @@ func (s *Service) getWorkers(w http.ResponseWriter, r *http.Request) {
// maintain worker state. If this service goes down and is restarted, workers
// are added again in seconds automatically due to this heartbeat.
func (s *Service) addWorker(w http.ResponseWriter, r *http.Request) {
	role := r.Header.Get("X-Role")
	host := r.Header.Get("X-Host")
	heartbeat := sjs.Heartbeat{}
	if err := json.NewDecoder(r.Body).Decode(&heartbeat); err != nil {
		msg := fmt.Sprintf("decode json: %s", err.Error())
		msg := fmt.Sprintf("%s %s: decode json: %s", role, host, err.Error())
		s.log.Printf("bad heartbeat: %s", msg)
		http.Error(w, msg, http.StatusBadRequest)
		return