|
| 1 | +// Copyright 2017 Canonical Ltd. |
| 2 | +// Licensed under the AGPLv3, see LICENCE file for details. |
| 3 | + |
| 4 | +package observer |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/json" |
| 8 | + |
| 9 | + "github.com/juju/errors" |
| 10 | + |
| 11 | + "github.com/juju/juju/core/auditlog" |
| 12 | + "github.com/juju/juju/rpc" |
| 13 | +) |
| 14 | + |
| 15 | +// NewRecorderFactory makes a new rpc.RecorderFactory to make |
| 16 | +// recorders that that will update the observer and the auditlog |
| 17 | +// recorder when it records a request or reply. The auditlog recorder |
| 18 | +// can be nil. |
| 19 | +func NewRecorderFactory(observerFactory rpc.ObserverFactory, recorder *auditlog.Recorder) rpc.RecorderFactory { |
| 20 | + return func() rpc.Recorder { |
| 21 | + return &combinedRecorder{ |
| 22 | + observer: observerFactory.RPCObserver(), |
| 23 | + recorder: recorder, |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// combinedRecorder wraps an observer (which might be a multiplexer) |
| 29 | +// up with an auditlog recorder into an rpc.Recorder. |
| 30 | +type combinedRecorder struct { |
| 31 | + observer rpc.Observer |
| 32 | + recorder *auditlog.Recorder |
| 33 | +} |
| 34 | + |
| 35 | +// ServerRequest implements rpc.Recorder. |
| 36 | +func (cr *combinedRecorder) ServerRequest(hdr *rpc.Header, body interface{}) error { |
| 37 | + cr.observer.ServerRequest(hdr, body) |
| 38 | + if cr.recorder == nil { |
| 39 | + return nil |
| 40 | + } |
| 41 | + // TODO(babbageclunk): make this configurable. |
| 42 | + jsonArgs, err := json.Marshal(body) |
| 43 | + if err != nil { |
| 44 | + return errors.Trace(err) |
| 45 | + } |
| 46 | + return errors.Trace(cr.recorder.AddRequest(auditlog.RequestArgs{ |
| 47 | + RequestID: hdr.RequestId, |
| 48 | + Facade: hdr.Request.Type, |
| 49 | + Method: hdr.Request.Action, |
| 50 | + Version: hdr.Request.Version, |
| 51 | + Args: string(jsonArgs), |
| 52 | + })) |
| 53 | +} |
| 54 | + |
| 55 | +// ServerReply implements rpc.Recorder. |
| 56 | +func (cr *combinedRecorder) ServerReply(req rpc.Request, replyHdr *rpc.Header, body interface{}) error { |
| 57 | + cr.observer.ServerReply(req, replyHdr, body) |
| 58 | + if cr.recorder == nil { |
| 59 | + return nil |
| 60 | + } |
| 61 | + var responseErrors []*auditlog.Error |
| 62 | + if replyHdr.Error == "" { |
| 63 | + var err error |
| 64 | + responseErrors, err = extractErrors(body) |
| 65 | + if err != nil { |
| 66 | + return errors.Trace(err) |
| 67 | + } |
| 68 | + } else { |
| 69 | + responseErrors = []*auditlog.Error{{ |
| 70 | + Message: replyHdr.Error, |
| 71 | + Code: replyHdr.ErrorCode, |
| 72 | + }} |
| 73 | + } |
| 74 | + return errors.Trace(cr.recorder.AddResponse(auditlog.ResponseErrorsArgs{ |
| 75 | + RequestID: replyHdr.RequestId, |
| 76 | + Errors: responseErrors, |
| 77 | + })) |
| 78 | +} |
| 79 | + |
| 80 | +func extractErrors(body interface{}) ([]*auditlog.Error, error) { |
| 81 | + // TODO(babbageclunk): use reflection to find errors in the response body. |
| 82 | + return nil, nil |
| 83 | +} |
0 commit comments