Skip to content

Commit

Permalink
Updated logging and fixed glitch with mock engine
Browse files Browse the repository at this point in the history
Signed-off-by: quobix <dave@quobix.com>
  • Loading branch information
daveshanley committed Nov 30, 2023
1 parent 1a67c50 commit e4630fb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/handle_http_traffic.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func handleHttpTraffic(wiretapConfig *shared.WiretapConfiguration, wtService *da
}

if httpErr != nil {
pterm.Fatal.Println(httpErr)
pterm.Error.Println(httpErr)
}
}()
}
5 changes: 1 addition & 4 deletions cmd/run_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/pb33f/libopenapi"
"github.com/pb33f/ranch/bus"
"github.com/pb33f/ranch/plank/pkg/server"
"github.com/pb33f/ranch/plank/utils"
"github.com/pb33f/wiretap/config"
"github.com/pb33f/wiretap/controls"
"github.com/pb33f/wiretap/daemon"
Expand Down Expand Up @@ -52,9 +51,7 @@ func runWiretapService(wiretapConfig *shared.WiretapConfiguration) (server.Platf
ranchConfig, _ := server.CreateServerConfig()
ranchConfig.Port, _ = strconv.Atoi(wiretapConfig.WebSocketPort)
ranchConfig.FabricConfig.EndpointConfig.Heartbeat = 0
ranchConfig.LogConfig.FormatOptions = &utils.LogFormatOption{
DisableTimestamp: true,
}
ranchConfig.Logger = wiretapConfig.Logger

// running TLS?
if wiretapConfig.CertificateKey != "" && wiretapConfig.Certificate != "" {
Expand Down
27 changes: 21 additions & 6 deletions daemon/handle_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/ranch/model"
"github.com/pb33f/ranch/plank/utils"
configModel "github.com/pb33f/wiretap/config"
"github.com/pb33f/wiretap/shared"
)
Expand Down Expand Up @@ -73,12 +72,17 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) {
// execute the new template
_ = tmpl.Execute(tmpFile, m)

ws.config.Logger.Info("[wiretap] static file request", "url", request.HttpRequest.URL.String(), "code", 200)

// serve it.
http.ServeFile(request.HttpResponseWriter, request.HttpRequest, tmpFile.Name())
return
}

if !localStat.IsDir() {

ws.config.Logger.Info("[wiretap] static file request", "url", request.HttpRequest.URL.String(), "code", 200)

http.ServeFile(request.HttpResponseWriter, request.HttpRequest, fp)
return
}
Expand Down Expand Up @@ -149,6 +153,8 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) {
var requestErrors []*errors.ValidationError
var responseErrors []*errors.ValidationError

ws.config.Logger.Info("[wiretap] handling API request", "url", request.HttpRequest.URL.String())

// check if we're going to fail hard on validation errors. (default is to skip this)
if ws.config.HardErrors {

Expand All @@ -170,10 +176,10 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) {
returnedResponse, returnedError = ws.callAPI(apiRequest)

if returnedResponse == nil && returnedError != nil {
utils.Log.Infof("[wiretap] request %s: Failed (%d)", apiRequest.URL.String(), 500)
config.Logger.Info("[wiretap] request failed", "url", apiRequest.URL.String(), "code", 500)
go ws.broadcastResponseError(request, CloneExistingResponse(returnedResponse), returnedError)
request.HttpResponseWriter.WriteHeader(500)
wtError := shared.GenerateError("Unable to call API", 500, returnedError.Error(), "")
wtError := shared.GenerateError("Unable to call API", 500, returnedError.Error(), "", returnedResponse)
_, _ = request.HttpResponseWriter.Write(shared.MarshalError(wtError))
return

Expand Down Expand Up @@ -209,7 +215,7 @@ func (ws *WiretapService) handleHttpRequest(request *model.Request) {
for k, v := range headers {
request.HttpResponseWriter.Header().Set(k, fmt.Sprint(v))
}
utils.Log.Infof("[wiretap] request %s: completed (%d)", request.HttpRequest.URL.String(), returnedResponse.StatusCode)
config.Logger.Info("[wiretap] request completed", "url", request.HttpRequest.URL.String(), "code", returnedResponse.StatusCode)

// if there are validation errors, set an error code
requestCode := config.HardErrorCode
Expand Down Expand Up @@ -266,9 +272,18 @@ func (ws *WiretapService) handleMockRequest(

// if there was an error building the mock, return a 404
if mockErr != nil && len(mock) == 0 {
utils.Log.Infof("[wiretap] mock mode request %s: error (%d)", newReq.URL.String(), 404)
config.Logger.Info("[wiretap] mock mode request error", "url", newReq.URL.String(), "code", 404)
request.HttpResponseWriter.WriteHeader(404)
wtError := shared.GenerateError("[mock error] unable to generate mock for request", 404, mockErr.Error(), "")
wtError := shared.GenerateError("[mock error] unable to generate mock for request", 404, mockErr.Error(), "", mock)
_, _ = request.HttpResponseWriter.Write(shared.MarshalError(wtError))
return
}

// if the mock exists, but there was an error, return the error
if mockErr != nil && len(mock) > 0 {
config.Logger.Info("[wiretap] mock mode request error", "url", newReq.URL.String(), "code", mockStatus)
request.HttpResponseWriter.WriteHeader(mockStatus)
wtError := shared.GenerateError("unable to serve mocked response", mockStatus, mockErr.Error(), "", nil)
_, _ = request.HttpResponseWriter.Write(shared.MarshalError(wtError))
return
}
Expand Down
9 changes: 7 additions & 2 deletions mock/mock_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ func (rme *ResponseMockEngine) runWorkflow(request *http.Request) ([]byte, int,
lo := rme.findLowestSuccessCode(operation)

// find the lowest success code.
mt, _ := rme.lookForResponseCodes(operation, request, []string{lo})
if mt == nil {
mt, noMT := rme.lookForResponseCodes(operation, request, []string{lo})
if mt == nil && noMT {
mtString := rme.extractMediaTypeHeader(request)
return rme.buildError(
415,
Expand All @@ -284,6 +284,7 @@ func (rme *ResponseMockEngine) runWorkflow(request *http.Request) ([]byte, int,
"build_mock_error",
), 415, nil
}

mock, mockErr := rme.mockEngine.GenerateMock(mt, rme.extractPreferred(request))
if mockErr != nil {
return rme.buildError(
Expand Down Expand Up @@ -328,6 +329,10 @@ func (rme *ResponseMockEngine) lookForResponseCodes(
}
responseBody := resp.Content[mediaTypeString]
if responseBody != nil {
// try and extract a default JSON response
return responseBody, false
} else {
responseBody = resp.Content["application/json"]
return responseBody, false
}
}
Expand Down
3 changes: 2 additions & 1 deletion shared/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ type WiretapError struct {
func GenerateError(title string,
status int,
detail string,
instance string) *WiretapError {
instance string, payload any) *WiretapError {
return &WiretapError{
Type: "https://pb33f.io/wiretap/error",
Title: title,
Status: status,
Detail: detail,
Instance: instance,
Payload: payload,
}
}

Expand Down

0 comments on commit e4630fb

Please sign in to comment.