Skip to content

Commit

Permalink
Log HTTP calls in reproxy and bootstrap
Browse files Browse the repository at this point in the history
Splitting out
[]
into smaller changes. Log HTTP calls - primarily useful since we make
HTTP calls to get token expiry for token we get from creds exchange.

Test: Unit tests
Bug: b/300945159
Change-Id: Ie9d1f846696f277231daf7a54d0a16a71d122029
GitOrigin-RevId: e38f7f5961e574ab25830feeb7cef8b7981d22db
  • Loading branch information
gkousik authored and copybara-github committed Oct 19, 2023
1 parent b1509ba commit 1420fc9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/bootstrap/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//internal/pkg/bootstrap",
"//internal/pkg/logger",
"//internal/pkg/logger/event",
"//internal/pkg/loghttp",
"//internal/pkg/pathtranslator",
"//internal/pkg/rbeflag",
"//internal/pkg/stats",
Expand Down
6 changes: 6 additions & 0 deletions cmd/bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/bazelbuild/reclient/internal/pkg/bootstrap"
"github.com/bazelbuild/reclient/internal/pkg/logger"
"github.com/bazelbuild/reclient/internal/pkg/logger/event"
"github.com/bazelbuild/reclient/internal/pkg/loghttp"
"github.com/bazelbuild/reclient/internal/pkg/pathtranslator"
"github.com/bazelbuild/reclient/internal/pkg/rbeflag"
"github.com/bazelbuild/reclient/internal/pkg/stats"
Expand Down Expand Up @@ -76,6 +77,7 @@ var (
remoteDisabled = flag.Bool("remote_disabled", false, "Whether to disable all remote operations and run all actions locally.")
cacheDir = flag.String("cache_dir", "", "Directory from which to load the cache files at startup and update at shutdown.")
metricsUploader = flag.String("metrics_uploader", defaultMetricsUploader(), "Path to the metrics uploader binary.")
logHTTPCalls = flag.Bool("log_http_calls", false, "Log all http requests made with the default http client.")
)

func main() {
Expand All @@ -84,6 +86,10 @@ func main() {
rbeflag.Parse()
version.PrintAndExitOnVersionFlag(true)

if *logHTTPCalls {
loghttp.Register()
}

if !*fastLogCollection && *asyncReproxyShutdown {
*asyncReproxyShutdown = false
log.Info("--async_reproxy_termination=true is not compatible with --fast_log_collection=false, falling back to synchronous shutdown.")
Expand Down
1 change: 1 addition & 0 deletions cmd/reproxy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
"//internal/pkg/localresources",
"//internal/pkg/localresources/usage",
"//internal/pkg/logger",
"//internal/pkg/loghttp",
"//internal/pkg/monitoring",
"//internal/pkg/pathtranslator",
"//internal/pkg/rbeflag",
Expand Down
5 changes: 5 additions & 0 deletions cmd/reproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/bazelbuild/reclient/internal/pkg/localresources"
"github.com/bazelbuild/reclient/internal/pkg/localresources/usage"
"github.com/bazelbuild/reclient/internal/pkg/logger"
"github.com/bazelbuild/reclient/internal/pkg/loghttp"
"github.com/bazelbuild/reclient/internal/pkg/monitoring"
"github.com/bazelbuild/reclient/internal/pkg/pathtranslator"
"github.com/bazelbuild/reclient/internal/pkg/rbeflag"
Expand Down Expand Up @@ -127,6 +128,7 @@ var (
credsFile = flag.String("creds_file", "", "Path to file where short-lived credentials are stored. If the file includes a token, reproxy will update the token if it refreshes it. Token refresh is only applicable if use_external_auth_token is used.")
waitForShutdownRPC = flag.Bool("wait_for_shutdown_rpc", false, "If set, will only shutdown after 3 SIGINT signals")
useCasNg = flag.Bool("use_casng", false, "Use casng pkg.")
logHTTPCalls = flag.Bool("log_http_calls", false, "Log all http requests made with the default http client.")
)

func verifyFlags() {
Expand Down Expand Up @@ -162,6 +164,9 @@ Use this flag if you're using custom llvm build as your toolchain and your llvm
rbeflag.Parse()
rbeflag.LogAllFlags(0)
defer log.Flush()
if *logHTTPCalls {
loghttp.Register()
}
defer func() {
pf, err := reproxypid.ReadFile(*serverAddr)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions internal/pkg/loghttp/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "loghttp",
srcs = ["loghttp.go"],
importpath = "github.com/bazelbuild/reclient/internal/pkg/loghttp",
visibility = ["//:__subpackages__"],
deps = [
"@com_github_golang_glog//:go_default_library",
"@com_github_google_uuid//:uuid",
],
)
38 changes: 38 additions & 0 deletions internal/pkg/loghttp/loghttp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package loghttp

import (
"net/http"
"net/http/httputil"

log "github.com/golang/glog"
"github.com/google/uuid"
)

// Register wraps http.DefaultTransport with logging of the requests and responses.
func Register() {
http.DefaultTransport = &glogTransport{base: http.DefaultTransport}
}

type glogTransport struct {
base http.RoundTripper
}

func (g *glogTransport) RoundTrip(req *http.Request) (*http.Response, error) {
reqid := uuid.New().String()
if reqStr, err := httputil.DumpRequest(req, true); err == nil {
log.Infof("%v: HTTP Request: %v", reqid, string(reqStr))
} else {
log.Infof("%v: HTTP Request: %v", reqid, req)
}
resp, err := g.base.RoundTrip(req)
if err != nil {
log.Infof("%v: HTTP Error: %v", reqid, err)
return nil, err
}
if respStr, err := httputil.DumpResponse(resp, true); err == nil {
log.Infof("%v: HTTP Response: %v", reqid, string(respStr))
} else {
log.Infof("%v: HTTP Response: %v", reqid, resp)
}
return resp, nil
}

0 comments on commit 1420fc9

Please sign in to comment.