-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor metrics and admin servers (#825)
* Refactor admin server * Refactor metrics server * Move endpoint mounting in admin.go file * Convert tests to table tests * Directly mount routes, move admin under api package * Move api/admin under api * Remove redundant error code
- Loading branch information
1 parent
0ee058d
commit 029ac6b
Showing
6 changed files
with
201 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// 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 api | ||
|
||
import ( | ||
"log/slog" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
"github.com/vechain/thor/v2/co" | ||
) | ||
|
||
func HTTPHandler(logLevel *slog.LevelVar) http.Handler { | ||
router := mux.NewRouter() | ||
sub := router.PathPrefix("/admin").Subrouter() | ||
sub.Path("/loglevel"). | ||
Methods(http.MethodGet). | ||
Name("get-log-level"). | ||
HandlerFunc(getLogLevelHandler(logLevel)) | ||
|
||
sub.Path("/loglevel"). | ||
Methods(http.MethodPost). | ||
Name("post-log-level"). | ||
HandlerFunc(postLogLevelHandler(logLevel)) | ||
|
||
return handlers.CompressHandler(router) | ||
} | ||
|
||
func StartAdminServer(addr string, logLevel *slog.LevelVar) (string, func(), error) { | ||
listener, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
return "", nil, errors.Wrapf(err, "listen admin API addr [%v]", addr) | ||
} | ||
|
||
router := mux.NewRouter() | ||
router.PathPrefix("/admin").Handler(HTTPHandler(logLevel)) | ||
handler := handlers.CompressHandler(router) | ||
|
||
srv := &http.Server{Handler: handler, ReadHeaderTimeout: time.Second, ReadTimeout: 5 * time.Second} | ||
var goes co.Goes | ||
goes.Go(func() { | ||
srv.Serve(listener) | ||
}) | ||
return "http://" + listener.Addr().String() + "/admin", func() { | ||
srv.Close() | ||
goes.Wait() | ||
}, nil | ||
} |
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,102 @@ | ||
// 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 api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
type TestCase struct { | ||
name string | ||
method string | ||
body interface{} | ||
expectedStatus int | ||
expectedLevel string | ||
expectedErrorMsg string | ||
} | ||
|
||
func marshalBody(tt TestCase, t *testing.T) []byte { | ||
var reqBody []byte | ||
var err error | ||
if tt.body != nil { | ||
reqBody, err = json.Marshal(tt.body) | ||
if err != nil { | ||
t.Fatalf("could not marshal request body: %v", err) | ||
} | ||
} | ||
return reqBody | ||
} | ||
|
||
func TestLogLevelHandler(t *testing.T) { | ||
tests := []TestCase{ | ||
{ | ||
name: "Valid POST input - set level to DEBUG", | ||
method: "POST", | ||
body: map[string]string{"level": "debug"}, | ||
expectedStatus: http.StatusOK, | ||
expectedLevel: "DEBUG", | ||
}, | ||
{ | ||
name: "Invalid POST input - invalid level", | ||
method: "POST", | ||
body: map[string]string{"level": "invalid_body"}, | ||
expectedStatus: http.StatusBadRequest, | ||
expectedErrorMsg: "Invalid verbosity level", | ||
}, | ||
{ | ||
name: "GET request - get current level INFO", | ||
method: "GET", | ||
body: nil, | ||
expectedStatus: http.StatusOK, | ||
expectedLevel: "INFO", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var logLevel slog.LevelVar | ||
logLevel.Set(slog.LevelInfo) | ||
|
||
reqBodyBytes := marshalBody(tt, t) | ||
|
||
req, err := http.NewRequest(tt.method, "/admin/loglevel", bytes.NewBuffer(reqBodyBytes)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler := http.HandlerFunc(HTTPHandler(&logLevel).ServeHTTP) | ||
handler.ServeHTTP(rr, req) | ||
|
||
if status := rr.Code; status != tt.expectedStatus { | ||
t.Errorf("handler returned wrong status code: got %v want %v", status, tt.expectedStatus) | ||
} | ||
|
||
if tt.expectedLevel != "" { | ||
var response logLevelResponse | ||
if err := json.NewDecoder(rr.Body).Decode(&response); err != nil { | ||
t.Fatalf("could not decode response: %v", err) | ||
} | ||
if response.CurrentLevel != tt.expectedLevel { | ||
t.Errorf("handler returned unexpected log level: got %v want %v", response.CurrentLevel, tt.expectedLevel) | ||
} | ||
} else { | ||
var response errorResponse | ||
if err := json.NewDecoder(rr.Body).Decode(&response); err != nil { | ||
t.Fatalf("could not decode response: %v", err) | ||
} | ||
if response.ErrorMessage != tt.expectedErrorMsg { | ||
t.Errorf("handler returned unexpected error message: got %v want %v", response.ErrorMessage, tt.expectedErrorMsg) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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,39 @@ | ||
// 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 api | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gorilla/handlers" | ||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
"github.com/vechain/thor/v2/co" | ||
"github.com/vechain/thor/v2/metrics" | ||
) | ||
|
||
func StartMetricsServer(addr string) (string, func(), error) { | ||
listener, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
return "", nil, errors.Wrapf(err, "listen metrics API addr [%v]", addr) | ||
} | ||
|
||
router := mux.NewRouter() | ||
router.PathPrefix("/metrics").Handler(metrics.HTTPHandler()) | ||
handler := handlers.CompressHandler(router) | ||
|
||
srv := &http.Server{Handler: handler, ReadHeaderTimeout: time.Second, ReadTimeout: 5 * time.Second} | ||
var goes co.Goes | ||
goes.Go(func() { | ||
srv.Serve(listener) | ||
}) | ||
return "http://" + listener.Addr().String() + "/metrics", func() { | ||
srv.Close() | ||
goes.Wait() | ||
}, nil | ||
} |
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