-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into pedro/add_metrics
- Loading branch information
Showing
7 changed files
with
179 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,11 +70,14 @@ jobs: | |
if: always() && (needs.publish-docker-image.result != 'success' || needs.run-unit-tests.result != 'success' || needs.lint.result != 'success' || needs.run-e2e-tests.result != 'success' || needs.license-check.result != 'success') | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Get the commit message | ||
id: commit_message | ||
# This is a workaround to get the first line of the commit message. Passing the entire message can cause the payload (JSON) to be invalid. | ||
run: | | ||
echo "commit_message=$(echo ${{ github.event.head_commit.message }} | head -n 1)" >> "$GITHUB_ENV" | ||
echo "commit_message=$(git show-branch --no-name HEAD)" >> "$GITHUB_ENV" | ||
- name: Notify Slack | ||
uses: slackapi/[email protected] | ||
|
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,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 api | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/inconshreveable/log15" | ||
) | ||
|
||
// RequestLoggerHandler returns a http handler to ensure requests are syphoned into the writer | ||
func RequestLoggerHandler(handler http.Handler, logger log15.Logger) http.Handler { | ||
fn := func(w http.ResponseWriter, r *http.Request) { | ||
// Read and log the body (note: this can only be done once) | ||
// Ensure you don't disrupt the request body for handlers that need to read it | ||
var bodyBytes []byte | ||
var err error | ||
if r.Body != nil { | ||
bodyBytes, err = io.ReadAll(r.Body) | ||
if err != nil { | ||
logger.Warn("unexpected body read error", "err", err) | ||
return // don't pass bad request to the next handler | ||
} | ||
r.Body = io.NopCloser(io.Reader(bytes.NewReader(bodyBytes))) | ||
} | ||
|
||
logger.Info("API Request", | ||
"timestamp", time.Now().Unix(), | ||
"URI", r.URL.String(), | ||
"Method", r.Method, | ||
"Body", string(bodyBytes), | ||
) | ||
|
||
// call the original http.Handler we're wrapping | ||
handler.ServeHTTP(w, r) | ||
} | ||
|
||
// http.HandlerFunc wraps a function so that it | ||
// implements http.Handler interface | ||
return http.HandlerFunc(fn) | ||
} |
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,95 @@ | ||
// 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/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/inconshreveable/log15" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// mockLogger is a simple logger implementation for testing purposes | ||
type mockLogger struct { | ||
loggedData []interface{} | ||
} | ||
|
||
func (m *mockLogger) New(ctx ...interface{}) log15.Logger { return m } | ||
|
||
func (m *mockLogger) GetHandler() log15.Handler { return nil } | ||
|
||
func (m *mockLogger) SetHandler(h log15.Handler) {} | ||
|
||
func (m *mockLogger) Debug(msg string, ctx ...interface{}) {} | ||
|
||
func (m *mockLogger) Error(msg string, ctx ...interface{}) {} | ||
|
||
func (m *mockLogger) Crit(msg string, ctx ...interface{}) {} | ||
|
||
func (m *mockLogger) Info(msg string, ctx ...interface{}) { | ||
m.loggedData = append(m.loggedData, ctx...) | ||
} | ||
|
||
func (m *mockLogger) Warn(msg string, ctx ...interface{}) { | ||
m.loggedData = append(m.loggedData, ctx...) | ||
} | ||
|
||
func (m *mockLogger) GetLoggedData() []interface{} { | ||
return m.loggedData | ||
} | ||
|
||
func TestRequestLoggerHandler(t *testing.T) { | ||
mockLog := &mockLogger{} | ||
|
||
// Define a test handler to wrap | ||
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("OK")) | ||
}) | ||
|
||
// Create the RequestLoggerHandler | ||
loggerHandler := RequestLoggerHandler(testHandler, mockLog) | ||
|
||
// Create a test HTTP request | ||
reqBody := "test body" | ||
req := httptest.NewRequest("POST", "http://example.com/foo", strings.NewReader(reqBody)) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
// Create a ResponseRecorder to record the response | ||
rr := httptest.NewRecorder() | ||
|
||
// Serve the HTTP request | ||
loggerHandler.ServeHTTP(rr, req) | ||
|
||
// Check the response status code | ||
assert.Equal(t, http.StatusOK, rr.Code) | ||
|
||
// Check the response body | ||
assert.Equal(t, "OK", rr.Body.String()) | ||
|
||
// Verify that the logger recorded the correct information | ||
loggedData := mockLog.GetLoggedData() | ||
assert.Contains(t, loggedData, "URI") | ||
assert.Contains(t, loggedData, "http://example.com/foo") | ||
assert.Contains(t, loggedData, "Method") | ||
assert.Contains(t, loggedData, "POST") | ||
assert.Contains(t, loggedData, "Body") | ||
assert.Contains(t, loggedData, reqBody) | ||
|
||
// Check if timestamp is present | ||
foundTimestamp := false | ||
for i := 0; i < len(loggedData); i += 2 { | ||
if loggedData[i] == "timestamp" { | ||
_, ok := loggedData[i+1].(int64) | ||
assert.True(t, ok, "timestamp should be an int64") | ||
foundTimestamp = true | ||
break | ||
} | ||
} | ||
assert.True(t, foundTimestamp, "timestamp should be logged") | ||
} |
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