-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding Health endpoint #836
Open
otherview
wants to merge
19
commits into
master
Choose a base branch
from
pedro/health_endpoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+515
−121
Open
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
19db51a
Adding Health endpoint
otherview 22bc2c2
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview df30451
pr comments + 503 if not healthy
otherview ed0097c
refactored admin server and api + health endpoint tests
otherview 552629c
fix health condition
otherview 6fa89c1
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview 91ceb58
fix admin routing
otherview 0206c1f
added comments + changed from ChainSync to ChainBootstrapStatus
otherview 17c2ce9
Adding healthcheck for solo mode
otherview 2f461c8
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview 1797c22
adding solo + tests
otherview dbea45a
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview 15a18fc
fix log_level handler funcs
otherview bda5f97
Merge remote-tracking branch 'origin/master' into pedro/health_endpoint
otherview 6b22089
refactor health package + add p2p count
otherview 1fdc48b
remove solo methods
otherview 430c458
moving health service to api pkg
otherview f1b0000
added defaults + api health query
otherview c0aee23
pr comments
otherview File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package admin | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
"github.com/vechain/thor/v2/api/admin/loglevel" | ||
"github.com/vechain/thor/v2/health" | ||
|
||
healthAPI "github.com/vechain/thor/v2/api/admin/health" | ||
) | ||
|
||
func New(logLevel *slog.LevelVar, health *health.Health) http.HandlerFunc { | ||
router := mux.NewRouter() | ||
subRouter := router.PathPrefix("/admin").Subrouter() | ||
|
||
loglevel.New(logLevel).Mount(subRouter, "/loglevel") | ||
healthAPI.New(health).Mount(subRouter, "/health") | ||
|
||
handler := handlers.CompressHandler(router) | ||
|
||
return handler.ServeHTTP | ||
} |
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,47 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package health | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/vechain/thor/v2/api/utils" | ||
"github.com/vechain/thor/v2/health" | ||
) | ||
|
||
type Health struct { | ||
healthStatus *health.Health | ||
} | ||
|
||
func New(healthStatus *health.Health) *Health { | ||
return &Health{ | ||
healthStatus: healthStatus, | ||
} | ||
} | ||
|
||
func (h *Health) handleGetHealth(w http.ResponseWriter, _ *http.Request) error { | ||
acc, err := h.healthStatus.Status() | ||
otherview marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if !acc.Healthy { | ||
w.WriteHeader(http.StatusServiceUnavailable) // Set the status to 503 | ||
} else { | ||
w.WriteHeader(http.StatusOK) // Set the status to 200 | ||
} | ||
return utils.WriteJSON(w, acc) | ||
} | ||
|
||
func (h *Health) Mount(root *mux.Router, pathPrefix string) { | ||
sub := root.PathPrefix(pathPrefix).Subrouter() | ||
|
||
sub.Path(""). | ||
Methods(http.MethodGet). | ||
Name("health"). | ||
HandlerFunc(utils.WrapHandlerFunc(h.handleGetHealth)) | ||
} |
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,61 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package health | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/vechain/thor/v2/comm" | ||
"github.com/vechain/thor/v2/health" | ||
"github.com/vechain/thor/v2/test/testchain" | ||
"github.com/vechain/thor/v2/txpool" | ||
) | ||
|
||
var ts *httptest.Server | ||
|
||
func TestHealth(t *testing.T) { | ||
initAPIServer(t) | ||
|
||
var healthStatus health.Status | ||
respBody, statusCode := httpGet(t, ts.URL+"/health") | ||
require.NoError(t, json.Unmarshal(respBody, &healthStatus)) | ||
assert.False(t, healthStatus.Healthy) | ||
assert.Equal(t, http.StatusServiceUnavailable, statusCode) | ||
} | ||
|
||
func initAPIServer(t *testing.T) { | ||
thorChain, err := testchain.NewIntegrationTestChain() | ||
require.NoError(t, err) | ||
|
||
router := mux.NewRouter() | ||
New( | ||
health.New(thorChain.Repo(), comm.New(thorChain.Repo(), txpool.New(thorChain.Repo(), nil, txpool.Options{})), time.Second), | ||
).Mount(router, "/health") | ||
|
||
ts = httptest.NewServer(router) | ||
} | ||
|
||
func httpGet(t *testing.T, url string) ([]byte, int) { | ||
res, err := http.Get(url) //#nosec G107 | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer res.Body.Close() | ||
|
||
r, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return r, res.StatusCode | ||
} |
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,10 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package health | ||
|
||
type Response struct { | ||
Healthy bool `json:"healthy"` | ||
} |
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 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package loglevel | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
"github.com/vechain/thor/v2/api/utils" | ||
"github.com/vechain/thor/v2/log" | ||
) | ||
|
||
type LogLevel struct { | ||
logLevel *slog.LevelVar | ||
} | ||
|
||
func New(logLevel *slog.LevelVar) *LogLevel { | ||
return &LogLevel{ | ||
logLevel: logLevel, | ||
} | ||
} | ||
|
||
func (l *LogLevel) Mount(root *mux.Router, pathPrefix string) { | ||
sub := root.PathPrefix(pathPrefix).Subrouter() | ||
sub.Path(""). | ||
Methods(http.MethodGet). | ||
Name("get-log-level"). | ||
HandlerFunc(utils.WrapHandlerFunc(l.getLogLevelHandler)) | ||
|
||
sub.Path(""). | ||
Methods(http.MethodPost). | ||
Name("post-log-level"). | ||
HandlerFunc(utils.WrapHandlerFunc(l.postLogLevelHandler)) | ||
} | ||
|
||
func (l *LogLevel) getLogLevelHandler(w http.ResponseWriter, _ *http.Request) error { | ||
return utils.WriteJSON(w, Response{ | ||
CurrentLevel: l.logLevel.Level().String(), | ||
}) | ||
} | ||
|
||
func (l *LogLevel) postLogLevelHandler(w http.ResponseWriter, r *http.Request) error { | ||
var req Request | ||
|
||
if err := utils.ParseJSON(r.Body, &req); err != nil { | ||
return utils.BadRequest(errors.WithMessage(err, "Invalid request body")) | ||
} | ||
|
||
switch req.Level { | ||
case "debug": | ||
l.logLevel.Set(log.LevelDebug) | ||
case "info": | ||
l.logLevel.Set(log.LevelInfo) | ||
case "warn": | ||
l.logLevel.Set(log.LevelWarn) | ||
case "error": | ||
l.logLevel.Set(log.LevelError) | ||
case "trace": | ||
l.logLevel.Set(log.LevelTrace) | ||
case "crit": | ||
l.logLevel.Set(log.LevelCrit) | ||
default: | ||
return utils.BadRequest(errors.New("Invalid verbosity level")) | ||
} | ||
|
||
return utils.WriteJSON(w, Response{ | ||
CurrentLevel: l.logLevel.Level().String(), | ||
}) | ||
} |
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,14 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package loglevel | ||
|
||
type Request struct { | ||
Level string `json:"level"` | ||
} | ||
|
||
type Response struct { | ||
CurrentLevel string `json:"currentLevel"` | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potentially, this can live in the init functions of the thor and thor-solo ? |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The admin endpoints were buckled in this package and reformatted to follow the same pattern as other endpoints