-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add healthchecks.io service (#42)
- Loading branch information
Showing
10 changed files
with
474 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package healthchecks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/crazy-max/gohealthchecks" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type Configuration struct { | ||
BaseURL string `json:"base_url"` | ||
UUID string `json:"uuid"` | ||
StartedMessage string `json:"started_message"` | ||
FailedMessage string `json:"failed_message"` | ||
} | ||
|
||
type Manager struct { | ||
client *gohealthchecks.Client | ||
baseURL *url.URL | ||
uuid string | ||
startedMessage string | ||
failedMessage string | ||
} | ||
|
||
func NewHealthchecksManager( | ||
config Configuration, | ||
) *Manager { | ||
manager := &Manager{} | ||
|
||
log.Info().Msg("Checking configuration for Healthchecks") | ||
|
||
if !manager.hasValidConfigurationInFile(config) { | ||
return nil | ||
} | ||
|
||
return manager | ||
} | ||
|
||
func (m *Manager) hasValidConfigurationInFile(config Configuration) bool { | ||
baseRawURL := config.BaseURL | ||
if baseRawURL == "" { | ||
log.Info(). | ||
Msg("BaseURL is empty, use default URL https://hc-ping.com/") | ||
|
||
baseRawURL = "https://hc-ping.com/" | ||
} | ||
|
||
baseURL, err := url.Parse(baseRawURL) | ||
if err != nil { | ||
log.Error(). | ||
Err(err). | ||
Str("base_url", baseRawURL). | ||
Msg("BaseURL is invalid") | ||
|
||
return false | ||
} | ||
|
||
if !strings.HasSuffix(baseURL.Path, "/") { | ||
baseURL.Path += "/" | ||
} | ||
|
||
m.baseURL = baseURL | ||
|
||
if config.UUID == "" { | ||
log.Error(). | ||
Msg("UUID is empty") | ||
|
||
return false | ||
} | ||
|
||
m.uuid = config.UUID | ||
|
||
m.startedMessage = config.StartedMessage | ||
if m.startedMessage == "" { | ||
log.Info(). | ||
Msg(`StartedMessage is empty, use default "discord-bot started"`) | ||
|
||
m.startedMessage = "discord-bot started" | ||
} | ||
|
||
m.failedMessage = config.FailedMessage | ||
if m.failedMessage == "" { | ||
log.Info(). | ||
Msg(`FailedMessage is empty, use default "discord-bot stopped"`) | ||
|
||
m.failedMessage = "discord-bot stopped" | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (m *Manager) Run() error { | ||
m.client = gohealthchecks.NewClient( | ||
&gohealthchecks.ClientOptions{ | ||
BaseURL: m.baseURL, | ||
}, | ||
) | ||
|
||
err := m.client.Start( | ||
context.Background(), | ||
gohealthchecks.PingingOptions{ | ||
UUID: m.uuid, | ||
Logs: m.startedMessage, | ||
}, | ||
) | ||
if err != nil { | ||
log.Error(). | ||
Err(err). | ||
Msg("Could not send Start HealthChecks client") | ||
|
||
return fmt.Errorf("%w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *Manager) Fail() { | ||
err := m.client.Fail( | ||
context.Background(), | ||
gohealthchecks.PingingOptions{ | ||
UUID: m.uuid, | ||
Logs: m.failedMessage, | ||
}, | ||
) | ||
if err != nil { | ||
log.Error(). | ||
Err(err). | ||
Msg("Could not send Fail HealthChecks client") | ||
} | ||
} |
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,89 @@ | ||
//nolint:paralleltest | ||
package healthchecks_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/blueprintue/discord-bot/healthchecks" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFail(t *testing.T) { | ||
var bufferLogs bytes.Buffer | ||
log.Logger = zerolog.New(&bufferLogs).Level(zerolog.TraceLevel).With().Logger() | ||
|
||
currentRequestIdx := 0 | ||
|
||
svr := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
if currentRequestIdx == 0 { | ||
require.Equal(t, "/00000000-0000-0000-0000-000000000000/start", req.RequestURI) | ||
startedMessage, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, "starts", string(startedMessage)) | ||
} else { | ||
require.Equal(t, "/00000000-0000-0000-0000-000000000000/fail", req.RequestURI) | ||
failedMessage, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, "stops", string(failedMessage)) | ||
} | ||
|
||
currentRequestIdx++ | ||
|
||
res.WriteHeader(http.StatusOK) | ||
})) | ||
defer svr.Close() | ||
|
||
healthchecksManager := healthchecks.NewHealthchecksManager(healthchecks.Configuration{ | ||
BaseURL: svr.URL, | ||
UUID: "00000000-0000-0000-0000-000000000000", | ||
StartedMessage: "starts", | ||
FailedMessage: "stops", | ||
}) | ||
require.NotNil(t, healthchecksManager) | ||
|
||
err := healthchecksManager.Run() | ||
require.NoError(t, err) | ||
|
||
bufferLogs.Reset() | ||
|
||
healthchecksManager.Fail() | ||
|
||
parts := strings.Split(bufferLogs.String(), "\n") | ||
require.Equal(t, ``, parts[0]) | ||
} | ||
|
||
func TestFail_Errors(t *testing.T) { | ||
var bufferLogs bytes.Buffer | ||
log.Logger = zerolog.New(&bufferLogs).Level(zerolog.TraceLevel).With().Logger() | ||
|
||
svr := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, _ *http.Request) { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
})) | ||
defer svr.Close() | ||
|
||
healthchecksManager := healthchecks.NewHealthchecksManager(healthchecks.Configuration{ | ||
BaseURL: svr.URL, | ||
UUID: "00000000-0000-0000-0000-000000000000", | ||
StartedMessage: "starts", | ||
FailedMessage: "stops", | ||
}) | ||
require.NotNil(t, healthchecksManager) | ||
|
||
err := healthchecksManager.Run() | ||
require.Error(t, err) | ||
|
||
bufferLogs.Reset() | ||
|
||
healthchecksManager.Fail() | ||
|
||
parts := strings.Split(bufferLogs.String(), "\n") | ||
require.Equal(t, `{"level":"error","error":"HTTP error 500","message":"Could not send Fail HealthChecks client"}`, parts[0]) | ||
require.Equal(t, ``, parts[1]) | ||
} |
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,74 @@ | ||
//nolint:paralleltest | ||
package healthchecks_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/blueprintue/discord-bot/healthchecks" | ||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestRun(t *testing.T) { | ||
var bufferLogs bytes.Buffer | ||
log.Logger = zerolog.New(&bufferLogs).Level(zerolog.TraceLevel).With().Logger() | ||
|
||
svr := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { | ||
require.Equal(t, "/00000000-0000-0000-0000-000000000000/start", req.RequestURI) | ||
startedMessage, err := io.ReadAll(req.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, "starts", string(startedMessage)) | ||
|
||
res.WriteHeader(http.StatusOK) | ||
})) | ||
defer svr.Close() | ||
|
||
healthchecksManager := healthchecks.NewHealthchecksManager(healthchecks.Configuration{ | ||
BaseURL: svr.URL, | ||
UUID: "00000000-0000-0000-0000-000000000000", | ||
StartedMessage: "starts", | ||
FailedMessage: "stops", | ||
}) | ||
require.NotNil(t, healthchecksManager) | ||
|
||
bufferLogs.Reset() | ||
|
||
err := healthchecksManager.Run() | ||
require.NoError(t, err) | ||
|
||
parts := strings.Split(bufferLogs.String(), "\n") | ||
require.Equal(t, ``, parts[0]) | ||
} | ||
|
||
func TestRun_Errors(t *testing.T) { | ||
var bufferLogs bytes.Buffer | ||
log.Logger = zerolog.New(&bufferLogs).Level(zerolog.TraceLevel).With().Logger() | ||
|
||
svr := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, _ *http.Request) { | ||
res.WriteHeader(http.StatusInternalServerError) | ||
})) | ||
defer svr.Close() | ||
|
||
healthchecksManager := healthchecks.NewHealthchecksManager(healthchecks.Configuration{ | ||
BaseURL: svr.URL, | ||
UUID: "00000000-0000-0000-0000-000000000000", | ||
StartedMessage: "starts", | ||
FailedMessage: "stops", | ||
}) | ||
require.NotNil(t, healthchecksManager) | ||
|
||
bufferLogs.Reset() | ||
|
||
err := healthchecksManager.Run() | ||
require.Error(t, err) | ||
|
||
parts := strings.Split(bufferLogs.String(), "\n") | ||
require.Equal(t, `{"level":"error","error":"HTTP error 500","message":"Could not send Start HealthChecks client"}`, parts[0]) | ||
require.Equal(t, ``, parts[1]) | ||
} |
Oops, something went wrong.