Skip to content

Commit

Permalink
Merge pull request #95 from akamai/AT-40
Browse files Browse the repository at this point in the history
[AT-40][Add] Preliminary Logging CorrelationID
  • Loading branch information
martinstibbe committed Jul 13, 2020
2 parents c581a3c + dd48940 commit 77bd4a2
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 139 deletions.
99 changes: 76 additions & 23 deletions edgegrid/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package edgegrid
package edgegrid

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"os"
"strings"
"net/http"
"net/http/httputil"

logstd "log"

log "github.com/sirupsen/logrus"
)

Expand All @@ -31,22 +36,22 @@ var EdgegridLog *log.Logger
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)
Expand Down Expand Up @@ -85,24 +90,72 @@ func LogMultilinef(f func(formatter string, args ...interface{}), formatter stri
// 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 {
LogMultiline(EdgegridLog.Traceln, string(b))
}
}

func PrintHttpRequestCorrelation(req *http.Request, body bool, correlationid string) {

if req == nil {
return
}
b, err := httputil.DumpRequestOut(req, body)
if err == nil {
LogMultiline(EdgegridLog.Traceln, string(b))
PrintfCorrelation("[DEBUG] REQUEST", correlationid, prettyPrintJsonLines(b))
}
}

// 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 {
LogMultiline(EdgegridLog.Traceln, 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))
PrintfCorrelation("[DEBUG] RESPONSE ", correlationid, prettyPrintJsonLines(b))
}
}

func PrintfCorrelation(level string, correlationid string, msg string) {

if correlationid == "" {
logstd.Printf("%s %s\n", level, msg)
} else {
logstd.SetFlags(0)
logstd.Printf("%v %s\n", correlationid, msg)
}

}

// 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")
}
8 changes: 4 additions & 4 deletions papi-v1/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions papi-v1/cpcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
}
Expand Down Expand Up @@ -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",
Expand All @@ -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)
Expand Down
27 changes: 13 additions & 14 deletions papi-v1/edgehostnames.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Loading

0 comments on commit 77bd4a2

Please sign in to comment.