Skip to content

Commit fc5776b

Browse files
committed
feat(grpc-gateway-v2): Add environment variable HTTP_GO_LOG_HTTP_HEADER to set http header logging for the default logger
1 parent faf5325 commit fc5776b

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

third_party/github.com/grpc-ecosystem/grpc-gateway-v2/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ go 1.24.0
44

55
require (
66
github.com/gin-gonic/gin v1.11.0
7-
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2
7+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.3
88
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3
9-
github.com/searKing/golang/go v1.2.124
9+
github.com/searKing/golang/go v1.2.136
1010
github.com/searKing/golang/third_party/google.golang.org/grpc v1.2.120
1111
google.golang.org/grpc v1.76.0
1212
google.golang.org/protobuf v1.36.10

third_party/github.com/grpc-ecosystem/grpc-gateway-v2/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
3535
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
3636
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
3737
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
38-
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2 h1:sGm2vDRFUrQJO/Veii4h4zG2vvqG6uWNkBHSTqXOZk0=
39-
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.2/go.mod h1:wd1YpapPLivG6nQgbf7ZkG1hhSOXDhhn4MLTknx2aAc=
38+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.3 h1:B+8ClL/kCQkRiU82d9xajRPKYMrB7E0MbtzWVi1K4ns=
39+
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.3.3/go.mod h1:NbCUVmiS4foBGBHOYlCT25+YmGpJ32dZPi75pGEUpj4=
4040
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 h1:NmZ1PKzSTQbuGHw9DGPFomqkkLWMC+vZCkfs+FHv1Vg=
4141
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3/go.mod h1:zQrxl1YP88HQlA6i9c63DSVPFklWpGX4OWAc9bFuaH4=
4242
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=

third_party/github.com/grpc-ecosystem/grpc-gateway-v2/grpc/logging.http.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"log/slog"
1010
"net/http"
11+
"os"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -27,6 +28,14 @@ var (
2728
// Logger will read existing and write new logging.Fields available in current context.
2829
// See `ExtractFields` and `InjectFields` for details.
2930
func HttpInterceptor(l logging.Logger) func(handler http.Handler) http.Handler {
31+
var logHttpHeader bool
32+
{
33+
vHeader := os.Getenv("HTTP_GO_LOG_HTTP_HEADER")
34+
if vh, err := strconv.ParseBool(vHeader); err == nil {
35+
logHttpHeader = vh
36+
}
37+
}
38+
3039
return func(handler http.Handler) http.Handler {
3140
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3241
var cost time_.Cost
@@ -35,16 +44,28 @@ func HttpInterceptor(l logging.Logger) func(handler http.Handler) http.Handler {
3544
attrs = append(attrs, slog.String(SystemTag[0], SystemTag[1]))
3645
attrs = append(attrs, slog.Time("http.start_time", time.Now()))
3746
attrs = append(attrs, extractLoggingFieldsFromHttpRequest(r)...)
38-
l.Log(r.Context(), logging.LevelInfo, fmt.Sprintf("http request received"), attrs...)
47+
48+
var reqAttrs = attrs
49+
if logHttpHeader {
50+
reqAttrs = append(reqAttrs, httpHeaderToAttr(r.Header, "http.request.header"))
51+
}
52+
l.Log(r.Context(), logging.LevelInfo, fmt.Sprintf("http request received"), reqAttrs...)
3953

4054
rw := http_.NewResponseWriterDelegator(w)
4155
handler.ServeHTTP(rw, r)
4256

43-
attrs = append(attrs, slog.String("http.status_code", slices_.FirstOrZero(http.StatusText(rw.Status()), "CODE("+strconv.FormatInt(int64(rw.Status()), 10)+")")),
57+
attrs = append(attrs,
58+
slog.String("http.status_code", slices_.FirstOrZero(http.StatusText(rw.Status()), "CODE("+strconv.FormatInt(int64(rw.Status()), 10)+")")),
4459
slog.Duration("cost", cost.Elapse()),
4560
slog.Int64("http.request_body_size", r.ContentLength),
4661
slog.Int64("http.response_body_size", rw.Written()))
47-
l.Log(r.Context(), logging.LevelInfo, fmt.Sprintf("finished http call with code %d", rw.Status()), attrs...)
62+
63+
var respAttrs = attrs
64+
if logHttpHeader {
65+
respAttrs = append(respAttrs, httpHeaderToAttr(r.Header, "http.response.header"))
66+
}
67+
l.Log(r.Context(), logging.LevelInfo, fmt.Sprintf("finished http call with status code %d", rw.Status()),
68+
respAttrs...)
4869
})
4970
}
5071
}
@@ -76,3 +97,11 @@ func extractLoggingFieldsFromHttpRequest(r *http.Request) []any {
7697
}
7798
return attrs
7899
}
100+
101+
func httpHeaderToAttr(h http.Header, k string) slog.Attr {
102+
var attrs []slog.Attr
103+
for k, v := range h {
104+
attrs = append(attrs, slog.Any(k, v))
105+
}
106+
return slog.GroupAttrs(k, attrs...)
107+
}

0 commit comments

Comments
 (0)