feat: add rest/httpc to make http requests governacible (#1638)
* feat: change x-trace-id to traceparent to follow opentelemetry * feat: add rest/httpc to make http requests governacible * chore: remove blank lines
This commit is contained in:
8
rest/httpc/internal/interceptor.go
Normal file
8
rest/httpc/internal/interceptor.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package internal
|
||||
|
||||
import "net/http"
|
||||
|
||||
type (
|
||||
Interceptor func(r *http.Request) (*http.Request, ResponseHandler)
|
||||
ResponseHandler func(*http.Response)
|
||||
)
|
||||
28
rest/httpc/internal/loginterceptor.go
Normal file
28
rest/httpc/internal/loginterceptor.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
func LogInterceptor(r *http.Request) (*http.Request, ResponseHandler) {
|
||||
start := timex.Now()
|
||||
return r, func(resp *http.Response) {
|
||||
duration := timex.Since(start)
|
||||
var tc propagation.TraceContext
|
||||
ctx := tc.Extract(r.Context(), propagation.HeaderCarrier(resp.Header))
|
||||
logger := logx.WithContext(ctx).WithDuration(duration)
|
||||
if isOkResponse(resp.StatusCode) {
|
||||
logger.Infof("[HTTP] %d - %s %s/%s", resp.StatusCode, r.Method, r.Host, r.RequestURI)
|
||||
} else {
|
||||
logger.Errorf("[HTTP] %d - %s %s/%s", resp.StatusCode, r.Method, r.Host, r.RequestURI)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isOkResponse(code int) bool {
|
||||
return code < http.StatusBadRequest
|
||||
}
|
||||
34
rest/httpc/internal/loginterceptor_test.go
Normal file
34
rest/httpc/internal/loginterceptor_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLogInterceptor(t *testing.T) {
|
||||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
}))
|
||||
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
||||
assert.Nil(t, err)
|
||||
req, handler := LogInterceptor(req)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
assert.Nil(t, err)
|
||||
handler(resp)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestLogInterceptorServerError(t *testing.T) {
|
||||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
req, err := http.NewRequest(http.MethodGet, svr.URL, nil)
|
||||
assert.Nil(t, err)
|
||||
req, handler := LogInterceptor(req)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
assert.Nil(t, err)
|
||||
handler(resp)
|
||||
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
|
||||
}
|
||||
Reference in New Issue
Block a user