Skip to content

Commit

Permalink
refactor(httpapi): logger belongs to endpoint
Browse files Browse the repository at this point in the history
This diff changes httpapi Endpoint and Descriptor such that the
logger field is part of the Endpoint rather than Descriptor.

This changes makes sense because the logger to use should be the
same for every API call. What matters, instead, is whether to log
bodies, which remains a property of the API Descriptor.

While I authored this diff originally as part of the work done
in #997, it seems it's
required in #1002 as well.

Hence, the reference issue is ooni/probe#2379.
  • Loading branch information
bassosimone committed Dec 9, 2022
1 parent 1f952b5 commit b5aa1d4
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 94 deletions.
4 changes: 2 additions & 2 deletions internal/engine/experiment/webconnectivity/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func Control(
ctx context.Context, sess model.ExperimentSession,
testhelpers []model.OOAPIService, creq ControlRequest) (ControlResponse, *model.OOAPIService, error) {
seqCaller := httpapi.NewSequenceCaller(
httpapi.MustNewPOSTJSONWithJSONResponseDescriptor(sess.Logger(), "/", creq).WithBodyLogging(true),
httpapi.NewEndpointList(sess.DefaultHTTPClient(), sess.UserAgent(), testhelpers...)...,
httpapi.MustNewPOSTJSONWithJSONResponseDescriptor("/", creq).WithBodyLogging(true),
httpapi.NewEndpointList(sess.DefaultHTTPClient(), sess.Logger(), sess.UserAgent(), testhelpers...)...,
)
sess.Logger().Infof("control for %s...", creq.HTTPRequest)
var out ControlResponse
Expand Down
4 changes: 2 additions & 2 deletions internal/experiment/webconnectivity/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ func (c *Control) Run(parentCtx context.Context) {

// create an httpapi sequence caller
seqCaller := httpapi.NewSequenceCaller(
httpapi.MustNewPOSTJSONWithJSONResponseDescriptor(c.Logger, "/", creq).WithBodyLogging(true),
httpapi.NewEndpointList(c.Session.DefaultHTTPClient(), c.Session.UserAgent(), c.TestHelpers...)...,
httpapi.MustNewPOSTJSONWithJSONResponseDescriptor("/", creq).WithBodyLogging(true),
httpapi.NewEndpointList(c.Session.DefaultHTTPClient(), c.Logger, c.Session.UserAgent(), c.TestHelpers...)...,
)

// issue the control request and wait for the response
Expand Down
10 changes: 5 additions & 5 deletions internal/httpapi/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func newRequest(ctx context.Context, endpoint *Endpoint, desc *Descriptor) (*htt
var reqBody io.Reader
if len(desc.RequestBody) > 0 {
reqBody = bytes.NewReader(desc.RequestBody)
desc.Logger.Debugf("httpapi: request body length: %d", len(desc.RequestBody))
endpoint.Logger.Debugf("httpapi: request body length: %d", len(desc.RequestBody))
if desc.LogBody {
desc.Logger.Debugf("httpapi: request body: %s", string(desc.RequestBody))
endpoint.Logger.Debugf("httpapi: request body: %s", string(desc.RequestBody))
}
}
request, err := http.NewRequestWithContext(ctx, desc.Method, URL.String(), reqBody)
Expand Down Expand Up @@ -120,9 +120,9 @@ func docall(endpoint *Endpoint, desc *Descriptor, request *http.Request) (*http.
if err != nil {
return response, nil, &errMaybeCensorship{err}
}
desc.Logger.Debugf("httpapi: response body length: %d bytes", len(data))
endpoint.Logger.Debugf("httpapi: response body length: %d bytes", len(data))
if desc.LogBody {
desc.Logger.Debugf("httpapi: response body: %s", string(data))
endpoint.Logger.Debugf("httpapi: response body: %s", string(data))
}
if response.StatusCode >= 400 {
return response, nil, &ErrHTTPRequestFailed{response.StatusCode}
Expand Down Expand Up @@ -174,7 +174,7 @@ func CallWithJSONResponse(ctx context.Context, desc *Descriptor, endpoint *Endpo
return err
}
if ctype := httpResp.Header.Get("Content-Type"); !goodContentTypeForJSON[ctype] {
desc.Logger.Warnf("httpapi: unexpected content-type: %s", ctype)
endpoint.Logger.Warnf("httpapi: unexpected content-type: %s", ctype)
// fallthrough
}
return json.Unmarshal(rawRespBody, response)
Expand Down
Loading

0 comments on commit b5aa1d4

Please sign in to comment.