-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yauheni Sliaptsou
committed
Mar 16, 2021
1 parent
85fd758
commit 3171144
Showing
9 changed files
with
144 additions
and
0 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
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
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,41 @@ | ||
package observability | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// InitProbes will initialize, register and expose, via http server, the probes. | ||
func InitProbes(enabled bool, port int, endpoint string) error { | ||
if !enabled { | ||
return nil | ||
} | ||
|
||
http.HandleFunc(endpoint, livenessHandler) | ||
|
||
probes := &http.Server{ | ||
Addr: net.JoinHostPort("", strconv.Itoa(port)), | ||
ReadTimeout: 1 * time.Second, | ||
WriteTimeout: 1 * time.Second, | ||
} | ||
|
||
// Starts HTTP server exposing the probes path | ||
go func() { | ||
log.Info().Msgf("Starting to serve handler %s, port %d", endpoint, port) | ||
if err := probes.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
log.Err(err).Msg("Failed to listen and serve http server") | ||
} | ||
}() | ||
|
||
return nil | ||
} | ||
|
||
func livenessHandler(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(`{"health":"OK"}`)) | ||
} |
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,30 @@ | ||
package observability | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestLivenessHandler(t *testing.T) { | ||
req := httptest.NewRequest("GET", "/healthz", nil) | ||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(livenessHandler) | ||
|
||
handler.ServeHTTP(rr, req) | ||
|
||
if contentType := rr.Header().Get("Content-Type"); contentType != "application/json" { | ||
t.Errorf("handler returned wrong status content type: got %v want %v", | ||
contentType, "application/json") | ||
} | ||
|
||
if status := rr.Code; status != http.StatusOK { | ||
t.Errorf("handler returned wrong status code: got %v want %v", | ||
status, http.StatusOK) | ||
} | ||
|
||
if body := rr.Body.String(); body != `{"health":"OK"}` { | ||
t.Errorf("handler returned wrong body: got %v want %v", | ||
body, http.StatusText(http.StatusOK)) | ||
} | ||
} |