From a133995f6f156997e6f1d0717c5e9389063dfec5 Mon Sep 17 00:00:00 2001 From: Martin Stibbe Date: Mon, 15 Jun 2020 21:07:21 -0500 Subject: [PATCH 1/7] [AT-40][Add] Preliminary Logging CorrelationID --- edgegrid/log.go | 74 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/edgegrid/log.go b/edgegrid/log.go index a86d2636..b81ab2a0 100644 --- a/edgegrid/log.go +++ b/edgegrid/log.go @@ -12,15 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -package edgegrid +package edgegrid import ( "bufio" "fmt" + "net/http" + "net/http/httputil" "os" "strings" - "net/http" - "net/http/httputil" + log "github.com/sirupsen/logrus" ) @@ -28,25 +29,28 @@ var logBuffer *bufio.Writer var LogFile *os.File var EdgegridLog *log.Logger +// LogCorrelationID ID for header and footer of log file outputs +var LogCorrelationID *string + func SetupLogging() { if EdgegridLog != nil { - return // already configured + return // already configured } - - EdgegridLog = log.New() + + EdgegridLog = log.New() EdgegridLog.SetFormatter(&log.TextFormatter{ DisableLevelTruncation: true, EnvironmentOverrideColors: true, }) - // Log file destination specified? If not, use default stdout - if logFileName := os.Getenv("AKAMAI_LOG_FILE"); logFileName != "" { + // Log file destination specified? If not, use default stdout + if logFileName := os.Getenv("AKAMAI_LOG_FILE"); logFileName != "" { // If the file doesn't exist, create it, or append to the file LogFile, err := os.OpenFile(logFileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { log.Fatal(err) } - EdgegridLog.SetOutput(LogFile) + EdgegridLog.SetOutput(LogFile) } EdgegridLog.SetLevel(log.PanicLevel) @@ -82,27 +86,51 @@ func LogMultilinef(f func(formatter string, args ...interface{}), formatter stri } } +func PrintLogHeader() { + if LogCorrelationID != nil { + strLogCorrelationIDValue := *LogCorrelationID + LogMultiline(EdgegridLog.Traceln, "START CORRELATION ID "+strLogCorrelationIDValue) + + } +} + +func PrintLogFooter() { + if LogCorrelationID != nil { + strLogCorrelationIDValue := *LogCorrelationID + LogMultiline(EdgegridLog.Traceln, "END CORRELATION ID "+strLogCorrelationIDValue) + LogCorrelationID = nil + } +} + // Utility func to print http req func PrintHttpRequest(req *http.Request, body bool) { - if req == nil { - return - } - b, err := httputil.DumpRequestOut(req, body) - if err == nil { - LogMultiline(EdgegridLog.Traceln, string(b)) - } + if req == nil { + return + } + b, err := httputil.DumpRequestOut(req, body) + if err == nil { + PrintLogHeader() + LogMultiline(EdgegridLog.Traceln, string(b)) + PrintLogFooter() + } } // Utility func to print http response func PrintHttpResponse(res *http.Response, body bool) { - if res == nil { - return - } - b, err := httputil.DumpResponse(res, body) - if err == nil { - LogMultiline(EdgegridLog.Traceln, string(b)) - } + if res == nil { + return + } + b, err := httputil.DumpResponse(res, body) + if err == nil { + PrintLogHeader() + LogMultiline(EdgegridLog.Traceln, string(b)) + PrintLogFooter() + } } +// Utility func to set correlationid +func SetLogCorrelationId(logCorrelationID string) { + LogCorrelationID = &logCorrelationID +} From 775cd719d773f99c4a22b2756daee692b4555805 Mon Sep 17 00:00:00 2001 From: Martin Stibbe Date: Mon, 29 Jun 2020 20:13:37 -0500 Subject: [PATCH 2/7] [AT-40][Change] Refactor Error logging with GUID --- edgegrid/log.go | 56 +++++++++++++++++++++++++--------------- papi-v1/contracts.go | 8 +++--- papi-v1/cpcodes.go | 18 ++++++------- papi-v1/edgehostnames.go | 27 ++++++++++--------- papi-v1/groups.go | 10 +++---- papi-v1/hostnames.go | 12 ++++----- papi-v1/products.go | 8 +++--- papi-v1/properties.go | 49 +++++++++++++++++------------------ papi-v1/ruleformats.go | 20 +++++++------- papi-v1/rules.go | 20 +++++++------- papi-v1/search.go | 8 +++--- papi-v1/versions.go | 27 +++++++++---------- 12 files changed, 138 insertions(+), 125 deletions(-) diff --git a/edgegrid/log.go b/edgegrid/log.go index b81ab2a0..08356775 100644 --- a/edgegrid/log.go +++ b/edgegrid/log.go @@ -16,23 +16,32 @@ package edgegrid import ( "bufio" + "context" "fmt" "net/http" "net/http/httputil" "os" "strings" + logstd "log" + log "github.com/sirupsen/logrus" ) var logBuffer *bufio.Writer var LogFile *os.File var EdgegridLog *log.Logger +var ctx context.Context + +type SomeContextKey string // LogCorrelationID ID for header and footer of log file outputs -var LogCorrelationID *string +//var LogCorrelationID *string func SetupLogging() { + keyA := SomeContextKey("LogCorrelationID") + ctx = context.Background() + ctx = context.WithValue(ctx, keyA, "foo") if EdgegridLog != nil { return // already configured @@ -86,33 +95,28 @@ func LogMultilinef(f func(formatter string, args ...interface{}), formatter stri } } -func PrintLogHeader() { - if LogCorrelationID != nil { - strLogCorrelationIDValue := *LogCorrelationID - LogMultiline(EdgegridLog.Traceln, "START CORRELATION ID "+strLogCorrelationIDValue) +// Utility func to print http req +func PrintHttpRequest(req *http.Request, body bool) { + if req == nil { + return } -} - -func PrintLogFooter() { - if LogCorrelationID != nil { - strLogCorrelationIDValue := *LogCorrelationID - LogMultiline(EdgegridLog.Traceln, "END CORRELATION ID "+strLogCorrelationIDValue) - LogCorrelationID = nil + b, err := httputil.DumpRequestOut(req, body) + if err == nil { + LogMultiline(EdgegridLog.Traceln, string(b)) + logstd.Printf("[DEBUG] REQUEST %s\n", string(b)) } } -// Utility func to print http req -func PrintHttpRequest(req *http.Request, body bool) { +func PrintHttpRequestCorrelation(req *http.Request, body bool, correlationid string) { if req == nil { return } b, err := httputil.DumpRequestOut(req, body) if err == nil { - PrintLogHeader() LogMultiline(EdgegridLog.Traceln, string(b)) - PrintLogFooter() + logstd.Printf("[DEBUG]%v REQUEST %s\n", correlationid, string(b)) } } @@ -124,13 +128,23 @@ func PrintHttpResponse(res *http.Response, body bool) { } b, err := httputil.DumpResponse(res, body) if err == nil { - PrintLogHeader() LogMultiline(EdgegridLog.Traceln, string(b)) - PrintLogFooter() + logstd.Printf("[DEBUG] RESPONSE %s\n", string(b)) + } +} + +func PrintHttpResponseCorrelation(res *http.Response, body bool, correlationid string) { + + if res == nil { + return + } + b, err := httputil.DumpResponse(res, body) + if err == nil { + LogMultiline(EdgegridLog.Traceln, string(b)) + logstd.Printf("[DEBUG]%v RESPONSE %s\n", correlationid, string(b)) } } -// Utility func to set correlationid -func SetLogCorrelationId(logCorrelationID string) { - LogCorrelationID = &logCorrelationID +func PrintfCorrelation(level string, correlationid string, msg string) { + logstd.Printf("%s%v %s\n", level, correlationid, msg) } diff --git a/papi-v1/contracts.go b/papi-v1/contracts.go index 626faf63..6dc17b67 100644 --- a/papi-v1/contracts.go +++ b/papi-v1/contracts.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/patrickmn/go-cache" ) @@ -47,7 +47,7 @@ func (contracts *Contracts) PostUnmarshalJSON() error { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listcontracts // Endpoint: GET /papi/v1/contracts -func (contracts *Contracts) GetContracts() error { +func (contracts *Contracts) GetContracts(correlationid string) error { cachecontracts, found := Profilecache.Get("contracts") if found { @@ -65,14 +65,14 @@ func (contracts *Contracts) GetContracts() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) diff --git a/papi-v1/cpcodes.go b/papi-v1/cpcodes.go index afb6bc92..9afe4192 100644 --- a/papi-v1/cpcodes.go +++ b/papi-v1/cpcodes.go @@ -8,8 +8,8 @@ import ( "strings" "time" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/patrickmn/go-cache" ) @@ -73,7 +73,7 @@ func (cpcodes *CpCodes) PostUnmarshalJSON() error { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listcpcodes // Endpoint: GET /papi/v1/cpcodes/{?contractId,groupId} -func (cpcodes *CpCodes) GetCpCodes() error { +func (cpcodes *CpCodes) GetCpCodes(correlationid string) error { cachecpcodes, found := Profilecache.Get("cpcodes") if found { json.Unmarshal(cachecpcodes.([]byte), cpcodes) @@ -98,14 +98,14 @@ func (cpcodes *CpCodes) GetCpCodes() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) @@ -120,9 +120,9 @@ func (cpcodes *CpCodes) GetCpCodes() error { } } -func (cpcodes *CpCodes) FindCpCode(nameOrId string) (*CpCode, error) { +func (cpcodes *CpCodes) FindCpCode(nameOrId string, correlationid string) (*CpCode, error) { if len(cpcodes.CpCodes.Items) == 0 { - err := cpcodes.GetCpCodes() + err := cpcodes.GetCpCodes(correlationid) if err != nil { return nil, err } @@ -250,7 +250,7 @@ func (cpcode *CpCode) ID() int { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#createanewcpcode // Endpoint: POST /papi/v1/cpcodes/{?contractId,groupId} -func (cpcode *CpCode) Save() error { +func (cpcode *CpCode) Save(correlationid string) error { req, err := client.NewJSONRequest( Config, "POST", @@ -265,14 +265,14 @@ func (cpcode *CpCode) Save() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) diff --git a/papi-v1/edgehostnames.go b/papi-v1/edgehostnames.go index f8e531a0..544e58b7 100644 --- a/papi-v1/edgehostnames.go +++ b/papi-v1/edgehostnames.go @@ -8,8 +8,8 @@ import ( "strings" "time" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/patrickmn/go-cache" ) @@ -61,7 +61,7 @@ func (edgeHostnames *EdgeHostnames) NewEdgeHostname() *EdgeHostname { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listedgehostnames // Endpoint: GET /papi/v1/edgehostnames/{?contractId,groupId,options} -func (edgeHostnames *EdgeHostnames) GetEdgeHostnames(contract *Contract, group *Group, options string) error { +func (edgeHostnames *EdgeHostnames) GetEdgeHostnames(contract *Contract, group *Group, options string, correlationid string) error { if contract == nil && group == nil { return errors.New("function requires at least \"group\" argument") @@ -97,14 +97,14 @@ func (edgeHostnames *EdgeHostnames) GetEdgeHostnames(contract *Contract, group * return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) @@ -194,7 +194,7 @@ func (edgeHostname *EdgeHostname) Init() { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getanedgehostname // Endpoint: GET /papi/v1/edgehostnames/{edgeHostnameId}{?contractId,groupId,options} -func (edgeHostname *EdgeHostname) GetEdgeHostname(options string) error { +func (edgeHostname *EdgeHostname) GetEdgeHostname(options string, correlationid string) error { if options != "" { options = "&options=" + options } @@ -215,14 +215,14 @@ func (edgeHostname *EdgeHostname) GetEdgeHostname(options string) error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { if res.StatusCode == 404 { @@ -232,7 +232,7 @@ func (edgeHostname *EdgeHostname) GetEdgeHostname(options string) error { group := NewGroup(NewGroups()) group.GroupID = edgeHostname.parent.GroupID - edgeHostname.parent.GetEdgeHostnames(contract, group, "") + edgeHostname.parent.GetEdgeHostnames(contract, group, "", correlationid) newEdgeHostname, err := edgeHostname.parent.FindEdgeHostname(edgeHostname) if err != nil || newEdgeHostname == nil { return client.NewAPIError(res) @@ -280,7 +280,7 @@ func (edgeHostname *EdgeHostname) GetEdgeHostname(options string) error { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#createanewedgehostname // Endpoint: POST /papi/v1/edgehostnames/{?contractId,groupId,options} -func (edgeHostname *EdgeHostname) Save(options string) error { +func (edgeHostname *EdgeHostname) Save(options string, correlationid string) error { if options != "" { options = "&options=" + options } @@ -299,15 +299,14 @@ func (edgeHostname *EdgeHostname) Save(options string) error { return err } - - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) @@ -352,7 +351,7 @@ func (edgeHostname *EdgeHostname) Save(options string) error { // if edgeHostname.Status == edgegrid.StatusActive { // // EdgeHostname activated successfully // } -func (edgeHostname *EdgeHostname) PollStatus(options string) bool { +func (edgeHostname *EdgeHostname) PollStatus(options string, correlationid string) bool { currentStatus := edgeHostname.Status var retry time.Duration = 0 for currentStatus != StatusActive { @@ -363,7 +362,7 @@ func (edgeHostname *EdgeHostname) PollStatus(options string) bool { retry -= time.Minute - err := edgeHostname.GetEdgeHostname(options) + err := edgeHostname.GetEdgeHostname(options, correlationid) if err != nil { edgeHostname.StatusChange <- false return false diff --git a/papi-v1/groups.go b/papi-v1/groups.go index 8b1b9faa..d90d0653 100644 --- a/papi-v1/groups.go +++ b/papi-v1/groups.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/patrickmn/go-cache" ) @@ -46,7 +46,7 @@ func (groups *Groups) PostUnmarshalJSON() error { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listgroups // Endpoint: GET /papi/v1/groups/ -func (groups *Groups) GetGroups() error { +func (groups *Groups) GetGroups(correlationid string) error { cachegroups, found := Profilecache.Get("groups") if found { json.Unmarshal(cachegroups.([]byte), groups) @@ -62,14 +62,14 @@ func (groups *Groups) GetGroups() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) @@ -226,7 +226,7 @@ func (group *Group) GetCpCodes(contract *Contract) (*CpCodes, error) { } // GetEdgeHostnames retrieves all Edge hostnames associated with a given group/contract -func (group *Group) GetEdgeHostnames(contract *Contract, options string) (*EdgeHostnames, error) { +func (group *Group) GetEdgeHostnames(contract *Contract, options string, correlationid string) (*EdgeHostnames, error) { return GetEdgeHostnames(contract, group, options) } diff --git a/papi-v1/hostnames.go b/papi-v1/hostnames.go index 900cff07..36b2b5e7 100644 --- a/papi-v1/hostnames.go +++ b/papi-v1/hostnames.go @@ -3,8 +3,8 @@ package papi import ( "fmt" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" ) // Hostnames is a collection of Property Hostnames @@ -54,16 +54,16 @@ func (hostnames *Hostnames) PostUnmarshalJSON() error { // See: Property.GetHostnames() // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listapropertyshostnames // Endpoint: GET /papi/v1/properties/{propertyId}/versions/{propertyVersion}/hostnames/{?contractId,groupId} -func (hostnames *Hostnames) GetHostnames(version *Version) error { +func (hostnames *Hostnames) GetHostnames(version *Version, correlationid string) error { if version == nil { property := NewProperty(NewProperties()) property.PropertyID = hostnames.PropertyID - err := property.GetProperty() + err := property.GetProperty(correlationid) if err != nil { return err } - version, err = property.GetLatestVersion("") + version, err = property.GetLatestVersion("", correlationid) if err != nil { return err } @@ -85,14 +85,14 @@ func (hostnames *Hostnames) GetHostnames(version *Version) error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) diff --git a/papi-v1/products.go b/papi-v1/products.go index a210242e..dd95bf15 100644 --- a/papi-v1/products.go +++ b/papi-v1/products.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/patrickmn/go-cache" ) @@ -47,7 +47,7 @@ func (products *Products) PostUnmarshalJSON() error { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listproducts // Endpoint: GET /papi/v1/products/{?contractId} -func (products *Products) GetProducts(contract *Contract) error { +func (products *Products) GetProducts(contract *Contract, correlationid string) error { cacheproducts, found := Profilecache.Get("products") if found { json.Unmarshal(cacheproducts.([]byte), products) @@ -66,14 +66,14 @@ func (products *Products) GetProducts(contract *Contract) error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) diff --git a/papi-v1/properties.go b/papi-v1/properties.go index 362999de..5b1163b7 100644 --- a/papi-v1/properties.go +++ b/papi-v1/properties.go @@ -4,8 +4,7 @@ import ( "fmt" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" - + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" ) // Properties is a collection of PAPI Property resources @@ -46,7 +45,7 @@ func (properties *Properties) PostUnmarshalJSON() error { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listproperties // Endpoint: GET /papi/v1/properties/{?contractId,groupId} -func (properties *Properties) GetProperties(contract *Contract, group *Group) error { +func (properties *Properties) GetProperties(contract *Contract, group *Group, correlationid string) error { if contract == nil { contract = NewContract(NewContracts()) contract.ContractID = group.ContractIDs[0] @@ -67,7 +66,7 @@ func (properties *Properties) GetProperties(contract *Contract, group *Group) er return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) @@ -75,7 +74,7 @@ func (properties *Properties) GetProperties(contract *Contract, group *Group) er return nil } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) @@ -182,7 +181,7 @@ func (property *Property) PreMarshalJSON() error { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getaproperty // Endpoint: GET /papi/v1/properties/{propertyId}{?contractId,groupId} -func (property *Property) GetProperty() error { +func (property *Property) GetProperty(correlationid string) error { req, err := client.NewRequest( Config, "GET", @@ -196,14 +195,14 @@ func (property *Property) GetProperty() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) @@ -266,10 +265,10 @@ func (property *Property) GetAvailableBehaviors() (*AvailableBehaviors, error) { // See: Rules.GetRules // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getaruletree // Endpoint: GET /papi/v1/properties/{propertyId}/versions/{propertyVersion}/rules/{?contractId,groupId} -func (property *Property) GetRules() (*Rules, error) { +func (property *Property) GetRules(correlationid string) (*Rules, error) { rules := NewRules() - if err := rules.GetRules(property); err != nil { + if err := rules.GetRules(property, correlationid); err != nil { return nil, err } @@ -281,9 +280,9 @@ func (property *Property) GetRules() (*Rules, error) { // See: Rules.GetRulesDigest() // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getaruletreesdigest // Endpoint: HEAD /papi/v1/properties/{propertyId}/versions/{propertyVersion}/rules/{?contractId,groupId} -func (property *Property) GetRulesDigest() (string, error) { +func (property *Property) GetRulesDigest(correlationid string) (string, error) { rules := NewRules() - return rules.GetRulesDigest(property) + return rules.GetRulesDigest(property, correlationid) } // GetVersions retrieves all versions for a a given property @@ -291,9 +290,9 @@ func (property *Property) GetRulesDigest() (string, error) { // See: Versions.GetVersions() // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listversions // Endpoint: GET /papi/v1/properties/{propertyId}/versions/{?contractId,groupId} -func (property *Property) GetVersions() (*Versions, error) { +func (property *Property) GetVersions(correlationid string) (*Versions, error) { versions := NewVersions() - err := versions.GetVersions(property) + err := versions.GetVersions(property, correlationid) if err != nil { return nil, err } @@ -306,11 +305,11 @@ func (property *Property) GetVersions() (*Versions, error) { // See: Versions.GetLatestVersion() // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getthelatestversion // Endpoint: GET /papi/v1/properties/{propertyId}/versions/latest{?contractId,groupId,activatedOn} -func (property *Property) GetLatestVersion(activatedOn NetworkValue) (*Version, error) { +func (property *Property) GetLatestVersion(activatedOn NetworkValue, correlationid string) (*Version, error) { versions := NewVersions() versions.PropertyID = property.PropertyID - return versions.GetLatestVersion(activatedOn) + return versions.GetLatestVersion(activatedOn, correlationid) } // GetHostnames retrieves hostnames assigned to a given property @@ -320,7 +319,7 @@ func (property *Property) GetLatestVersion(activatedOn NetworkValue) (*Version, // See: Hostnames.GetHostnames() // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getpropertyversionhostnames // Endpoint: GET /papi/v1/properties/{propertyId}/versions/{propertyVersion}/hostnames/{?contractId,groupId} -func (property *Property) GetHostnames(version *Version) (*Hostnames, error) { +func (property *Property) GetHostnames(version *Version, correlationid string) (*Hostnames, error) { hostnames := NewHostnames() hostnames.PropertyID = property.PropertyID hostnames.ContractID = property.Contract.ContractID @@ -328,12 +327,12 @@ func (property *Property) GetHostnames(version *Version) (*Hostnames, error) { if version == nil { var err error - version, err = property.GetLatestVersion("") + version, err = property.GetLatestVersion("", correlationid) if err != nil { return nil, err } } - err := hostnames.GetHostnames(version) + err := hostnames.GetHostnames(version, correlationid) if err != nil { return nil, err } @@ -369,7 +368,7 @@ func (property *Property) PostUnmarshalJSON() error { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#createorcloneaproperty // Endpoint: POST /papi/v1/properties/{?contractId,groupId} -func (property *Property) Save() error { +func (property *Property) Save(correlationid string) error { req, err := client.NewJSONRequest( Config, "POST", @@ -384,14 +383,14 @@ func (property *Property) Save() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) @@ -463,7 +462,7 @@ func (property *Property) Activate(activation *Activation, acknowledgeWarnings b // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#removeaproperty // Endpoint: DELETE /papi/v1/properties/{propertyId}{?contractId,groupId} -func (property *Property) Delete() error { +func (property *Property) Delete(correlationid string) error { // /papi/v1/properties/{propertyId}{?contractId,groupId} req, err := client.NewRequest( Config, @@ -478,14 +477,14 @@ func (property *Property) Delete() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) diff --git a/papi-v1/ruleformats.go b/papi-v1/ruleformats.go index b5a5d0ec..388efb2a 100644 --- a/papi-v1/ruleformats.go +++ b/papi-v1/ruleformats.go @@ -5,8 +5,8 @@ import ( "io/ioutil" "sort" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/xeipuuv/gojsonschema" ) @@ -30,7 +30,7 @@ func NewRuleFormats() *RuleFormats { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listruleformats // Endpoint: GET /papi/v1/rule-formats -func (ruleFormats *RuleFormats) GetRuleFormats() error { +func (ruleFormats *RuleFormats) GetRuleFormats(correlationid string) error { req, err := client.NewRequest( Config, "GET", @@ -41,15 +41,15 @@ func (ruleFormats *RuleFormats) GetRuleFormats() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) - + edge.PrintHttpResponseCorrelation(res, true, correlationid) + if client.IsError(res) { return client.NewAPIError(res) } @@ -63,9 +63,9 @@ func (ruleFormats *RuleFormats) GetRuleFormats() error { return nil } -func (ruleFormats *RuleFormats) GetLatest() (string, error) { +func (ruleFormats *RuleFormats) GetLatest(correlationid string) (string, error) { if len(ruleFormats.RuleFormats.Items) == 0 { - err := ruleFormats.GetRuleFormats() + err := ruleFormats.GetRuleFormats(correlationid) if err != nil { return "", err } @@ -78,7 +78,7 @@ func (ruleFormats *RuleFormats) GetLatest() (string, error) { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getaruleformatsschema // Endpoint: /papi/v1/schemas/products/{productId}/{ruleFormat} -func (ruleFormats *RuleFormats) GetSchema(product string, ruleFormat string) (*gojsonschema.Schema, error) { +func (ruleFormats *RuleFormats) GetSchema(product string, ruleFormat string, correlationid string) (*gojsonschema.Schema, error) { req, err := client.NewRequest( Config, "GET", @@ -93,14 +93,14 @@ func (ruleFormats *RuleFormats) GetSchema(product string, ruleFormat string) (*g return nil, err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return nil, err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return nil, client.NewAPIError(res) diff --git a/papi-v1/rules.go b/papi-v1/rules.go index 1229c214..ec7f7f05 100644 --- a/papi-v1/rules.go +++ b/papi-v1/rules.go @@ -4,8 +4,8 @@ import ( "fmt" "strings" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" ) // Rules is a collection of property rules @@ -45,7 +45,7 @@ func (rules *Rules) PreMarshalJSON() error { // See: Property.GetRules // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getaruletree // Endpoint: GET /papi/v1/properties/{propertyId}/versions/{propertyVersion}/rules/{?contractId,groupId} -func (rules *Rules) GetRules(property *Property) error { +func (rules *Rules) GetRules(property *Property, correlationid string) error { req, err := client.NewRequest( Config, "GET", @@ -60,14 +60,14 @@ func (rules *Rules) GetRules(property *Property) error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) @@ -85,7 +85,7 @@ func (rules *Rules) GetRules(property *Property) error { // See: Property.GetRulesDigest() // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getaruletreesdigest // Endpoint: HEAD /papi/v1/properties/{propertyId}/versions/{propertyVersion}/rules/{?contractId,groupId} -func (rules *Rules) GetRulesDigest(property *Property) (string, error) { +func (rules *Rules) GetRulesDigest(property *Property, correlationid string) (string, error) { req, err := client.NewRequest( Config, "HEAD", @@ -100,14 +100,14 @@ func (rules *Rules) GetRulesDigest(property *Property) (string, error) { return "", err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return "", err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return "", client.NewAPIError(res) @@ -120,7 +120,7 @@ func (rules *Rules) GetRulesDigest(property *Property) (string, error) { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#putpropertyversionrules // Endpoint: PUT /papi/v1/properties/{propertyId}/versions/{propertyVersion}/rules{?contractId,groupId} -func (rules *Rules) Save() error { +func (rules *Rules) Save(correlationid string) error { rules.Errors = []*RuleErrors{} req, err := client.NewJSONRequest( @@ -137,14 +137,14 @@ func (rules *Rules) Save() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) diff --git a/papi-v1/search.go b/papi-v1/search.go index 1d111b4c..614d2d9f 100644 --- a/papi-v1/search.go +++ b/papi-v1/search.go @@ -3,8 +3,8 @@ package papi import ( "time" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" ) type SearchKey string @@ -37,7 +37,7 @@ type SearchResult struct { // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#postfindbyvalue // Endpoint: POST /papi/v1/search/find-by-value -func Search(searchBy SearchKey, propertyName string) (*SearchResult, error) { +func Search(searchBy SearchKey, propertyName string, correlationid string) (*SearchResult, error) { req, err := client.NewJSONRequest( Config, "POST", @@ -49,14 +49,14 @@ func Search(searchBy SearchKey, propertyName string) (*SearchResult, error) { return nil, err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return nil, err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return nil, client.NewAPIError(res) diff --git a/papi-v1/versions.go b/papi-v1/versions.go index 99256b61..dff14a1a 100644 --- a/papi-v1/versions.go +++ b/papi-v1/versions.go @@ -3,9 +3,10 @@ package papi import ( "errors" "fmt" - edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" - "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" "time" + + "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1" + edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid" ) // Versions contains a collection of Property Versions @@ -63,7 +64,7 @@ func (versions *Versions) AddVersion(version *Version) { // See: Property.GetVersions() // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listversions // Endpoint: GET /papi/v1/properties/{propertyId}/versions/{?contractId,groupId} -func (versions *Versions) GetVersions(property *Property) error { +func (versions *Versions) GetVersions(property *Property, correlationid string) error { if property == nil { return errors.New("You must provide a property") } @@ -81,14 +82,14 @@ func (versions *Versions) GetVersions(property *Property) error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if err = client.BodyJSON(res, versions); err != nil { return err @@ -102,7 +103,7 @@ func (versions *Versions) GetVersions(property *Property) error { // See: Property.GetLatestVersion() // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#getthelatestversion // Endpoint: GET /papi/v1/properties/{propertyId}/versions/latest{?contractId,groupId,activatedOn} -func (versions *Versions) GetLatestVersion(activatedOn NetworkValue) (*Version, error) { +func (versions *Versions) GetLatestVersion(activatedOn NetworkValue, correlationid string) (*Version, error) { if activatedOn != "" { activatedOn = "?activatedOn=" + activatedOn } @@ -121,14 +122,14 @@ func (versions *Versions) GetLatestVersion(activatedOn NetworkValue) (*Version, return nil, err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return nil, err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return nil, client.NewAPIError(res) @@ -143,10 +144,10 @@ func (versions *Versions) GetLatestVersion(activatedOn NetworkValue) (*Version, } // NewVersion creates a new version associated with the Versions collection -func (versions *Versions) NewVersion(createFromVersion *Version, useEtagStrict bool) *Version { +func (versions *Versions) NewVersion(createFromVersion *Version, useEtagStrict bool, correlationid string) *Version { if createFromVersion == nil { var err error - createFromVersion, err = versions.GetLatestVersion("") + createFromVersion, err = versions.GetLatestVersion("", correlationid) if err != nil { return nil } @@ -274,7 +275,7 @@ func (version *Version) HasBeenActivated(activatedOn NetworkValue) (bool, error) // // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#createanewversion // Endpoint: POST /papi/v1/properties/{propertyId}/versions/{?contractId,groupId} -func (version *Version) Save() error { +func (version *Version) Save(correlationid string) error { if version.PropertyVersion != 0 { return fmt.Errorf("version (%d) already exists", version.PropertyVersion) } @@ -292,14 +293,14 @@ func (version *Version) Save() error { return err } - edge.PrintHttpRequest(req, true) + edge.PrintHttpRequestCorrelation(req, true, correlationid) res, err := client.Do(Config, req) if err != nil { return err } - edge.PrintHttpResponse(res, true) + edge.PrintHttpResponseCorrelation(res, true, correlationid) if client.IsError(res) { return client.NewAPIError(res) From 8ce316ef28d7f920b10ae37907c9a09a0a78478d Mon Sep 17 00:00:00 2001 From: Martin Stibbe Date: Mon, 29 Jun 2020 20:23:43 -0500 Subject: [PATCH 3/7] [AT-40[Change] Fix tests --- papi-v1/rules_test.go | 8 +++----- papi-v1/service.go | 14 +++++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/papi-v1/rules_test.go b/papi-v1/rules_test.go index 35848fc4..5911e6f4 100644 --- a/papi-v1/rules_test.go +++ b/papi-v1/rules_test.go @@ -1091,8 +1091,6 @@ func TestRule_AddChildRule(t *testing.T) { &Rule{ Name: "Child Rule", }, - - }, }, }, @@ -1746,7 +1744,7 @@ func TestRules_GetRules_Locked(t *testing.T) { property.PropertyID = "prp_123" property.LatestVersion = 1 - rules, err := property.GetRules() + rules, err := property.GetRules("") assert.NoError(t, err) assert.Equal(t, "44b00323-df7a-4e6b-8ed7-2a49c92f6e44", rules.Rule.Children[0].UUID) @@ -1789,7 +1787,7 @@ func TestRules_GetRules_CustomOverrides(t *testing.T) { property.PropertyID = "prp_123" property.LatestVersion = 1 - rules, err := property.GetRules() + rules, err := property.GetRules("") assert.NoError(t, err) assert.Equal(t, "cbo_12345", rules.Rule.CustomOverride.OverrideID) @@ -1833,7 +1831,7 @@ func TestRules_GetRules_Variables(t *testing.T) { property.PropertyID = "prp_123" property.LatestVersion = 1 - rules, err := property.GetRules() + rules, err := property.GetRules("") assert.NoError(t, err) assert.Len(t, rules.Rule.Variables, 1) diff --git a/papi-v1/service.go b/papi-v1/service.go index c1be0c09..0ce585bd 100644 --- a/papi-v1/service.go +++ b/papi-v1/service.go @@ -15,7 +15,7 @@ var ( // GetGroups retrieves all groups func GetGroups() (*Groups, error) { groups := NewGroups() - if err := groups.GetGroups(); err != nil { + if err := groups.GetGroups(""); err != nil { return nil, err } @@ -25,7 +25,7 @@ func GetGroups() (*Groups, error) { // GetContracts retrieves all contracts func GetContracts() (*Contracts, error) { contracts := NewContracts() - if err := contracts.GetContracts(); err != nil { + if err := contracts.GetContracts(""); err != nil { return nil, err } @@ -35,7 +35,7 @@ func GetContracts() (*Contracts, error) { // GetProducts retrieves all products func GetProducts(contract *Contract) (*Products, error) { products := NewProducts() - if err := products.GetProducts(contract); err != nil { + if err := products.GetProducts(contract, ""); err != nil { return nil, err } @@ -45,7 +45,7 @@ func GetProducts(contract *Contract) (*Products, error) { // GetEdgeHostnames retrieves all edge hostnames func GetEdgeHostnames(contract *Contract, group *Group, options string) (*EdgeHostnames, error) { edgeHostnames := NewEdgeHostnames() - if err := edgeHostnames.GetEdgeHostnames(contract, group, options); err != nil { + if err := edgeHostnames.GetEdgeHostnames(contract, group, options, ""); err != nil { return nil, err } @@ -57,7 +57,7 @@ func GetEdgeHostnames(contract *Contract, group *Group, options string) (*EdgeHo // API Docs: https://developer.akamai.com/api/luna/papi/resources.html#listcpcodes func GetCpCodes(contract *Contract, group *Group) (*CpCodes, error) { cpcodes := NewCpCodes(contract, group) - if err := cpcodes.GetCpCodes(); err != nil { + if err := cpcodes.GetCpCodes(""); err != nil { return nil, err } @@ -67,7 +67,7 @@ func GetCpCodes(contract *Contract, group *Group) (*CpCodes, error) { // GetProperties retrieves all properties for a given contract/group func GetProperties(contract *Contract, group *Group) (*Properties, error) { properties := NewProperties() - if err := properties.GetProperties(contract, group); err != nil { + if err := properties.GetProperties(contract, group, ""); err != nil { return nil, err } @@ -77,7 +77,7 @@ func GetProperties(contract *Contract, group *Group) (*Properties, error) { // GetVersions retrieves all versions for a given property func GetVersions(property *Property) (*Versions, error) { versions := NewVersions() - if err := versions.GetVersions(property); err != nil { + if err := versions.GetVersions(property, ""); err != nil { return nil, err } From cc4146249f2bd585ad1f3c24a117e7bd0252b2b4 Mon Sep 17 00:00:00 2001 From: Martin Stibbe Date: Mon, 13 Jul 2020 09:56:57 -0500 Subject: [PATCH 4/7] [AT-40][Add] Remove extra date timestamp from wrapped logs --- edgegrid/log.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/edgegrid/log.go b/edgegrid/log.go index 08356775..72e12832 100644 --- a/edgegrid/log.go +++ b/edgegrid/log.go @@ -146,5 +146,12 @@ func PrintHttpResponseCorrelation(res *http.Response, body bool, correlationid s } func PrintfCorrelation(level string, correlationid string, msg string) { - logstd.Printf("%s%v %s\n", level, correlationid, msg) + + if correlationid == "" { + logstd.Printf("%s %s\n", level, msg) + } else { + logstd.SetFlags(0) + logstd.Printf("%v %s\n", correlationid, msg) + } + } From 366f386962711abeae59562fc30109802c695449 Mon Sep 17 00:00:00 2001 From: Martin Stibbe Date: Mon, 13 Jul 2020 10:03:56 -0500 Subject: [PATCH 5/7] [AT-40][Change] Remove duplicate logging --- edgegrid/log.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/edgegrid/log.go b/edgegrid/log.go index 72e12832..5fe04836 100644 --- a/edgegrid/log.go +++ b/edgegrid/log.go @@ -104,7 +104,6 @@ func PrintHttpRequest(req *http.Request, body bool) { b, err := httputil.DumpRequestOut(req, body) if err == nil { LogMultiline(EdgegridLog.Traceln, string(b)) - logstd.Printf("[DEBUG] REQUEST %s\n", string(b)) } } @@ -116,7 +115,6 @@ func PrintHttpRequestCorrelation(req *http.Request, body bool, correlationid str b, err := httputil.DumpRequestOut(req, body) if err == nil { LogMultiline(EdgegridLog.Traceln, string(b)) - logstd.Printf("[DEBUG]%v REQUEST %s\n", correlationid, string(b)) } } From 624154abe0f3a7bcabddabca871a3db9139d7174 Mon Sep 17 00:00:00 2001 From: Martin Stibbe Date: Mon, 13 Jul 2020 10:37:44 -0500 Subject: [PATCH 6/7] [AT-40][Change] Remove context POC --- edgegrid/log.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/edgegrid/log.go b/edgegrid/log.go index 5fe04836..1af021b6 100644 --- a/edgegrid/log.go +++ b/edgegrid/log.go @@ -16,7 +16,6 @@ package edgegrid import ( "bufio" - "context" "fmt" "net/http" "net/http/httputil" @@ -31,17 +30,8 @@ import ( var logBuffer *bufio.Writer var LogFile *os.File var EdgegridLog *log.Logger -var ctx context.Context - -type SomeContextKey string - -// LogCorrelationID ID for header and footer of log file outputs -//var LogCorrelationID *string func SetupLogging() { - keyA := SomeContextKey("LogCorrelationID") - ctx = context.Background() - ctx = context.WithValue(ctx, keyA, "foo") if EdgegridLog != nil { return // already configured From dd489400a2a55d977350433fad450cba34cf9e16 Mon Sep 17 00:00:00 2001 From: Martin Stibbe Date: Mon, 13 Jul 2020 11:00:44 -0500 Subject: [PATCH 7/7] [AT-40][Add] Add pretty print json call standard function for REQ/RESP --- edgegrid/log.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/edgegrid/log.go b/edgegrid/log.go index 1af021b6..1b98517b 100644 --- a/edgegrid/log.go +++ b/edgegrid/log.go @@ -16,6 +16,8 @@ package edgegrid import ( "bufio" + "bytes" + "encoding/json" "fmt" "net/http" "net/http/httputil" @@ -105,6 +107,7 @@ func PrintHttpRequestCorrelation(req *http.Request, body bool, correlationid str b, err := httputil.DumpRequestOut(req, body) if err == nil { LogMultiline(EdgegridLog.Traceln, string(b)) + PrintfCorrelation("[DEBUG] REQUEST", correlationid, prettyPrintJsonLines(b)) } } @@ -117,7 +120,6 @@ func PrintHttpResponse(res *http.Response, body bool) { b, err := httputil.DumpResponse(res, body) if err == nil { LogMultiline(EdgegridLog.Traceln, string(b)) - logstd.Printf("[DEBUG] RESPONSE %s\n", string(b)) } } @@ -129,7 +131,7 @@ func PrintHttpResponseCorrelation(res *http.Response, body bool, correlationid s b, err := httputil.DumpResponse(res, body) if err == nil { LogMultiline(EdgegridLog.Traceln, string(b)) - logstd.Printf("[DEBUG]%v RESPONSE %s\n", correlationid, string(b)) + PrintfCorrelation("[DEBUG] RESPONSE ", correlationid, prettyPrintJsonLines(b)) } } @@ -143,3 +145,17 @@ func PrintfCorrelation(level string, correlationid string, msg string) { } } + +// prettyPrintJsonLines iterates through a []byte line-by-line, +// transforming any lines that are complete json into pretty-printed json. +func prettyPrintJsonLines(b []byte) string { + parts := strings.Split(string(b), "\n") + for i, p := range parts { + if b := []byte(p); json.Valid(b) { + var out bytes.Buffer + json.Indent(&out, b, "", " ") + parts[i] = out.String() + } + } + return strings.Join(parts, "\n") +}