Skip to content

Commit

Permalink
Cleaned up debug printf's by adding a couple new independent loggers.
Browse files Browse the repository at this point in the history
Split out instance retrieval needed in ReregisterInstance() into new functions GetInstance() and readInstanceInto()
Small change to readAppInto() to match simpler form in readInstanceInto()
  • Loading branch information
cquinn committed Jun 17, 2014
1 parent d62037f commit 53581bd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 35 deletions.
2 changes: 1 addition & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (e *EurekaConnection) UpdateApp(app *Application) {
go func() {
for {
log.Notice("Updating app %s", app.Name)
err := e.readAppInto(app.Name, app)
err := e.readAppInto(app)
if err != nil {
log.Error("Failure updating %s in goroutine", app.Name)
}
Expand Down
7 changes: 7 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ import (
)

var log = logging.MustGetLogger("fargo")
var metadataLog = logging.MustGetLogger("fargo.metadata")
var marshalLog = logging.MustGetLogger("fargo.marshal")

func init() {
logging.SetLevel(logging.WARNING, "fargo.metadata")
logging.SetLevel(logging.WARNING, "fargo.marshal")
}
17 changes: 7 additions & 10 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package fargo

import (
"encoding/json"
//"fmt"
"strconv"
)

Expand All @@ -19,20 +18,20 @@ type getAppsResponseSingle struct {
// UnmarshalJSON is a custom JSON unmarshaler for GetAppsResponse to deal with
// sometimes non-wrapped Application arrays when there is only a single Application item.
func (r *GetAppsResponse) UnmarshalJSON(b []byte) error {
//fmt.Printf("GetAppsResponse.UnmarshalJSON b:%s\n", string(b))
marshalLog.Debug("GetAppsResponse.UnmarshalJSON b:%s\n", string(b))
var err error

// Normal array case
var ra getAppsResponseArray
if err = json.Unmarshal(b, &ra); err == nil {
//fmt.Printf("GetAppsResponse.UnmarshalJSON ra:%+v\n", ra)
marshalLog.Debug("GetAppsResponse.UnmarshalJSON ra:%+v\n", ra)
*r = GetAppsResponse(ra)
return nil
}
// Bogus non-wrapped case
var rs getAppsResponseSingle
if err = json.Unmarshal(b, &rs); err == nil {
//fmt.Printf("GetAppsResponse.UnmarshalJSON rs:%+v\n", rs)
marshalLog.Debug("GetAppsResponse.UnmarshalJSON rs:%+v\n", rs)
r.Applications = make([]*Application, 1, 1)
r.Applications[0] = rs.Application
r.AppsHashcode = rs.AppsHashcode
Expand All @@ -52,21 +51,21 @@ type applicationSingle struct {
// UnmarshalJSON is a custom JSON unmarshaler for Application to deal with
// sometimes non-wrapped Instance array when there is only a single Instance item.
func (a *Application) UnmarshalJSON(b []byte) error {
//fmt.Printf("Application.UnmarshalJSON b:%s\n", string(b))
marshalLog.Debug("Application.UnmarshalJSON b:%s\n", string(b))
var err error

// Normal array case
var aa applicationArray
if err = json.Unmarshal(b, &aa); err == nil {
//fmt.Printf("Application.UnmarshalJSON aa:%+v\n", aa)
marshalLog.Debug("Application.UnmarshalJSON aa:%+v\n", aa)
*a = Application(aa)
return nil
}

// Bogus non-wrapped case
var as applicationSingle
if err = json.Unmarshal(b, &as); err == nil {
//fmt.Printf("Application.UnmarshalJSON as:%+v\n", as)
marshalLog.Debug("Application.UnmarshalJSON as:%+v\n", as)
a.Name = as.Name
a.Instances = make([]*Instance, 1, 1)
a.Instances[0] = as.Instance
Expand All @@ -84,12 +83,10 @@ func (i *Instance) UnmarshalJSON(b []byte) error {
var err error
var ii instance
if err = json.Unmarshal(b, &ii); err == nil {
//fmt.Printf("Instance.UnmarshalJSON ii:%+v\n", ii)
marshalLog.Debug("Instance.UnmarshalJSON ii:%+v\n", ii)
*i = Instance(ii)
i.Port, _ = strconv.Atoi(ii.PortJ.Number)
i.SecurePort, _ = strconv.Atoi(ii.SecurePortJ.Number)
//i.Port = ii.PortJ.Number
//i.SecurePort = ii.SecurePortJ.Number
return nil
}
return err
Expand Down
5 changes: 3 additions & 2 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func (a *Application) ParseAllMetadata() error {
for _, instance := range a.Instances {
err := instance.Metadata.parse()
if err != nil {
log.Error("Failed parsing metadata for Instance=%s of Application=%s: %s", instance.HostName, a.Name, err.Error())
log.Error("Failed parsing metadata for Instance=%s of Application=%s: %s",
instance.HostName, a.Name, err.Error())
return err
}
}
Expand All @@ -26,7 +27,7 @@ func (im *InstanceMetadata) parse() error {
log.Debug("len(Metadata)==0. Quitting parsing.")
return nil
}
//log.Debug("InstanceMetadata.parse: %s", im.Raw)
metadataLog.Debug("InstanceMetadata.parse: %s", im.Raw)

if len(im.Raw) > 0 && im.Raw[0] == '{' {
// JSON
Expand Down
51 changes: 35 additions & 16 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (e *EurekaConnection) marshal(v interface{}) ([]byte, error) {
if e.UseJson {
out, err := json.Marshal(v)
if err != nil {
// marshal the xml *with* indents so it's readable in the error message
// marshal the JSON *with* indents so it's readable in the error message
out, _ := json.MarshalIndent(v, "", " ")
log.Error("Error marshalling JSON value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
return nil, err
Expand All @@ -28,7 +28,7 @@ func (e *EurekaConnection) marshal(v interface{}) ([]byte, error) {
} else {
out, err := xml.Marshal(v)
if err != nil {
// marshal the xml *with* indents so it's readable in the error message
// marshal the XML *with* indents so it's readable in the error message
out, _ := xml.MarshalIndent(v, "", " ")
log.Error("Error marshalling XML value=%v. Error:\"%s\" JSON body=\"%s\"", v, err.Error(), string(out))
return nil, err
Expand Down Expand Up @@ -72,8 +72,8 @@ func (e *EurekaConnection) GetApp(name string) (*Application, error) {
return v, nil
}

func (e *EurekaConnection) readAppInto(name string, app *Application) error {
tapp, err := e.GetApp(name)
func (e *EurekaConnection) readAppInto(app *Application) error {
tapp, err := e.GetApp(app.Name)
if err == nil {
*app = *tapp
}
Expand Down Expand Up @@ -167,21 +167,40 @@ func (e *EurekaConnection) ReregisterInstance(ins *Instance) error {
return fmt.Errorf("http returned %d possible failure registering instance\n", rcode)
}

// read back our registration to ensure that it stuck
// TODO(cq) is this really needed? Especially the unmarshal over our existing struct...
/*
body, rcode, err = getBody(reqURL + "/" + ins.HostName, e.UseJson)
if e.UseJson {
fmt.Printf("ReregisterInstance, reread JSON: %+v: %s\n", ins, string(body))
// TODO: would need to unmarshal RegisterInstanceJson here instead...
return json.Unmarshal(body, ins)
} else {
return xml.Unmarshal(body, ins)
}
*/
// read back our registration to pick up eureka-supplied values
e.readInstanceInto(ins)

return nil
}

// GetInstance gets an Instance from eureka given its app and hostname.
func (e *EurekaConnection) GetInstance(app, hostname string) (*Instance, error) {
slug := fmt.Sprintf("%s/%s/%s", EurekaURLSlugs["Apps"], app, hostname)
reqURL := e.generateURL(slug)
log.Debug("Getting instance with url %s", reqURL)
body, _, err := getBody(reqURL, e.UseJson)
if err != nil {
return nil, err
}
var ins *Instance
if e.UseJson {
var ij RegisterInstanceJson
err = json.Unmarshal(body, &ij)
ins = ij.Instance
} else {
err = xml.Unmarshal(body, &ins)
}
return ins, err
}

func (e *EurekaConnection) readInstanceInto(ins *Instance) error {
tins, err := e.GetInstance(ins.App, ins.HostName)
if err == nil {
*ins = *tins
}
return err
}

// DeregisterInstance will deregister the given Instance from eureka. This is good practice
// to do before exiting or otherwise going off line.
func (e *EurekaConnection) DeregisterInstance(ins *Instance) error {
Expand Down
3 changes: 2 additions & 1 deletion struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ type Instance struct {
}

// Port struct used for JSON [un]marshaling only
type Port struct { // json port looks like: "port":{"@enabled":"true", "$":"7101"},
// looks like: "port":{"@enabled":"true", "$":"7101"},
type Port struct {
Number string `json:"$"`
Enabled string `json:"@enabled"`
}
Expand Down
11 changes: 6 additions & 5 deletions tests/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package fargo_test
// MIT Licensed (see README.md) - Copyright (c) 2013 Hudl <@Hudl>

import (
"fmt"
"github.com/hudl/fargo"
. "github.com/smartystreets/goconvey/convey"
"testing"
Expand Down Expand Up @@ -97,27 +96,29 @@ func TestReregistration(t *testing.T) {
SecureVipAddress: "127.0.0.10",
Status: fargo.UP,
}
//fmt.Printf("\nTestReregistration0: ins:%s/%s\n", i.App, i.HostName)
for _, j := range []bool{false, true} {
e.UseJson = j
Convey("Register a TESTAPP instance", t, func() {
Convey("Instance registers correctly", func() {
//fmt.Printf("\nTestReregistration1: ins:%s/%s\n", i.App, i.HostName)
err := e.RegisterInstance(&i)
So(err, ShouldBeNil)
})
})
Convey("Reregister the TESTAPP instance", t, func() {
//fmt.Printf("\nTestReregistration2: ins:%s/%s\n", i.App, i.HostName)
Convey("Instance reregisters correctly", func() {
err := e.ReregisterInstance(&i)
So(err, ShouldBeNil)
})
fmt.Printf("\nTestReregistration3: ins:%s/%s\n", i.App, i.HostName)
Convey("Instance can check in", func() {
err := e.HeartBeatInstance(&i)
So(err, ShouldBeNil)
})
Convey("Instance can be gotten correctly", func() {
ii, err := e.GetInstance(i.App, i.HostName)
So(err, ShouldBeNil)
So(ii.App, ShouldEqual, i.App)
So(ii.HostName, ShouldEqual, i.HostName)
})
})
}
}
Expand Down

0 comments on commit 53581bd

Please sign in to comment.