[VOL-3069]Pass Context in methods which are performing logging and need the context
Change-Id: I3d9e1c3eff95d60dde46d44d16bed4805f7447f5
diff --git a/pkg/probe/common.go b/pkg/probe/common.go
index 211419d..14857ab 100644
--- a/pkg/probe/common.go
+++ b/pkg/probe/common.go
@@ -19,12 +19,12 @@
"github.com/opencord/voltha-lib-go/v3/pkg/log"
)
-var logger log.Logger
+var logger log.CLogger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.AddPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "probe"})
+ logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "probe"})
if err != nil {
panic(err)
}
diff --git a/pkg/probe/probe.go b/pkg/probe/probe.go
index e89d5bc..732d6df 100644
--- a/pkg/probe/probe.go
+++ b/pkg/probe/probe.go
@@ -109,7 +109,7 @@
}
// RegisterService register one or more service names with the probe, status will be track against service name
-func (p *Probe) RegisterService(names ...string) {
+func (p *Probe) RegisterService(ctx context.Context, names ...string) {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.status == nil {
@@ -118,7 +118,7 @@
for _, name := range names {
if _, ok := p.status[name]; !ok {
p.status[name] = ServiceStatusUnknown
- logger.Debugw("probe-service-registered", log.Fields{"service-name": name})
+ logger.Debugw(ctx, "probe-service-registered", log.Fields{"service-name": name})
}
}
@@ -136,7 +136,7 @@
}
// UpdateStatus utility function to send a service update to the probe
-func (p *Probe) UpdateStatus(name string, status ServiceStatus) {
+func (p *Probe) UpdateStatus(ctx context.Context, name string, status ServiceStatus) {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.status == nil {
@@ -161,7 +161,7 @@
} else {
p.isHealthy = defaultHealthFunc(p.status)
}
- logger.Debugw("probe-service-status-updated",
+ logger.Debugw(ctx, "probe-service-status-updated",
log.Fields{
"service-name": name,
"status": status.String(),
@@ -204,7 +204,7 @@
func UpdateStatusFromContext(ctx context.Context, name string, status ServiceStatus) {
p := GetProbeFromContext(ctx)
if p != nil {
- p.UpdateStatus(name, status)
+ p.UpdateStatus(ctx, name, status)
}
}
@@ -228,25 +228,26 @@
}
}
func (p *Probe) detailzFunc(w http.ResponseWriter, req *http.Request) {
+ ctx := context.Background()
p.mutex.RLock()
defer p.mutex.RUnlock()
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write([]byte("{")); err != nil {
- logger.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw(ctx, "write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
comma := ""
for c, s := range p.status {
if _, err := w.Write([]byte(fmt.Sprintf("%s\"%s\": \"%s\"", comma, c, s.String()))); err != nil {
- logger.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw(ctx, "write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
comma = ", "
}
if _, err := w.Write([]byte("}")); err != nil {
- logger.Errorw("write-response", log.Fields{"error": err})
+ logger.Errorw(ctx, "write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -254,7 +255,7 @@
}
// ListenAndServe implements 3 HTTP endpoints on the given port for healthz, readz, and detailz. Returns only on error
-func (p *Probe) ListenAndServe(address string) {
+func (p *Probe) ListenAndServe(ctx context.Context, address string) {
mux := http.NewServeMux()
// Returns the result of the readyFunc calculation
@@ -269,7 +270,7 @@
Addr: address,
Handler: mux,
}
- logger.Fatal(s.ListenAndServe())
+ logger.Fatal(ctx, s.ListenAndServe())
}
func (p *Probe) IsReady() bool {
diff --git a/pkg/probe/probe_test.go b/pkg/probe/probe_test.go
index 77cd6a8..cae17e5 100644
--- a/pkg/probe/probe_test.go
+++ b/pkg/probe/probe_test.go
@@ -72,7 +72,7 @@
func TestRegisterOneService(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
assert.Equal(t, 1, len(p.status), "wrong number of services")
@@ -83,7 +83,7 @@
func TestRegisterMultipleServices(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two", "three", "four")
+ p.RegisterService(context.Background(), "one", "two", "three", "four")
assert.Equal(t, 4, len(p.status), "wrong number of services")
@@ -99,10 +99,10 @@
func TestRegisterMultipleServicesIncremental(t *testing.T) {
p := &Probe{}
-
- p.RegisterService("one")
- p.RegisterService("two")
- p.RegisterService("three", "four")
+ ctx := context.Background()
+ p.RegisterService(ctx, "one")
+ p.RegisterService(ctx, "two")
+ p.RegisterService(ctx, "three", "four")
assert.Equal(t, 4, len(p.status), "wrong number of services")
@@ -119,7 +119,7 @@
func TestRegisterMultipleServicesDuplicates(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "one", "one", "two")
+ p.RegisterService(context.Background(), "one", "one", "one", "two")
assert.Equal(t, 2, len(p.status), "wrong number of services")
@@ -131,10 +131,10 @@
func TestRegisterMultipleServicesDuplicatesIncremental(t *testing.T) {
p := &Probe{}
-
- p.RegisterService("one")
- p.RegisterService("one")
- p.RegisterService("one", "two")
+ ctx := context.Background()
+ p.RegisterService(ctx, "one")
+ p.RegisterService(ctx, "one")
+ p.RegisterService(ctx, "one", "two")
assert.Equal(t, 2, len(p.status), "wrong number of services")
@@ -146,9 +146,9 @@
func TestUpdateStatus(t *testing.T) {
p := &Probe{}
-
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
+ ctx := context.Background()
+ p.RegisterService(ctx, "one", "two")
+ p.UpdateStatus(ctx, "one", ServiceStatusRunning)
assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
@@ -156,14 +156,14 @@
func TestRegisterOverwriteStatus(t *testing.T) {
p := &Probe{}
-
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
+ ctx := context.Background()
+ p.RegisterService(ctx, "one", "two")
+ p.UpdateStatus(ctx, "one", ServiceStatusRunning)
assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
- p.RegisterService("one", "three")
+ p.RegisterService(ctx, "one", "three")
assert.Equal(t, 3, len(p.status), "wrong number of services")
assert.Equal(t, ServiceStatusRunning, p.status["one"], "status overridden")
assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
@@ -172,7 +172,7 @@
func TestDetailzWithServies(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/detailz", nil)
w := httptest.NewRecorder()
@@ -201,7 +201,7 @@
func TestReadzWithServicesWithTrue(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -212,7 +212,7 @@
func TestReadzWithServicesWithDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -233,9 +233,10 @@
func TestReadzWithServicesDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
- p.UpdateStatus("two", ServiceStatusRunning)
+ ctx := context.Background()
+ p.RegisterService(ctx, "one", "two")
+ p.UpdateStatus(ctx, "one", ServiceStatusRunning)
+ p.UpdateStatus(ctx, "two", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -246,8 +247,8 @@
func TestReadzWithServicesDefaultOne(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
+ p.RegisterService(context.Background(), "one", "two")
+ p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -268,7 +269,7 @@
func TestHealthzWithServicesWithTrue(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -279,7 +280,7 @@
func TestHealthzWithServicesWithDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -300,9 +301,9 @@
func TestHealthzWithServicesDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
- p.UpdateStatus("two", ServiceStatusRunning)
+ p.RegisterService(context.Background(), "one", "two")
+ p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
+ p.UpdateStatus(context.Background(), "two", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -313,8 +314,8 @@
func TestHealthzWithServicesDefaultFailed(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusFailed)
+ p.RegisterService(context.Background(), "one", "two")
+ p.UpdateStatus(context.Background(), "one", ServiceStatusFailed)
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -332,7 +333,7 @@
func TestGetProbeFromContext(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
ctx := context.WithValue(context.Background(), ProbeContextKey, p)
pc := GetProbeFromContext(ctx)
assert.Equal(t, p, pc, "Probe from context was not identical to original probe")
@@ -347,7 +348,7 @@
func TestUpdateStatusFromContext(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
ctx := context.WithValue(context.Background(), ProbeContextKey, p)
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -359,7 +360,7 @@
func TestUpdateStatusFromNilContext(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
// nolint: staticcheck
UpdateStatusFromContext(nil, "one", ServiceStatusRunning)
@@ -372,7 +373,7 @@
func TestUpdateStatusFromContextWithoutProbe(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -386,7 +387,7 @@
func TestUpdateStatusFromContextWrongType(t *testing.T) {
p := &Probe{}
- p.RegisterService("one")
+ p.RegisterService(context.Background(), "one")
ctx := context.WithValue(context.Background(), ProbeContextKey, "Teapot")
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -399,7 +400,7 @@
func TestUpdateStatusNoRegistered(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
- p.UpdateStatus("one", ServiceStatusRunning)
+ p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
assert.Equal(t, 1, len(p.status), "wrong number of services")
_, ok := p.status["one"]
assert.True(t, ok, "unable to find registered service")
@@ -409,7 +410,7 @@
func TestIsReadyTrue(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
- p.RegisterService("SomeService")
+ p.RegisterService(context.Background(), "SomeService")
assert.True(t, p.IsReady(), "IsReady should have been true")
}
@@ -417,7 +418,7 @@
func TestIsReadyFalse(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysFalse).WithHealthFunc(AlwaysFalse)
- p.RegisterService("SomeService")
+ p.RegisterService(context.Background(), "SomeService")
assert.False(t, p.IsReady(), "IsReady should have been false")
}
@@ -425,8 +426,8 @@
func TestGetStatus(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
- p.UpdateStatus("one", ServiceStatusRunning)
+ p.RegisterService(context.Background(), "one", "two")
+ p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
ss := p.GetStatus("one")
assert.Equal(t, ServiceStatusRunning, ss, "Service status should have been ServiceStatusRunning")
@@ -435,7 +436,7 @@
func TestGetStatusMissingService(t *testing.T) {
p := &Probe{}
- p.RegisterService("one", "two")
+ p.RegisterService(context.Background(), "one", "two")
ss := p.GetStatus("three")
assert.Equal(t, ServiceStatusUnknown, ss, "Service status should have been ServiceStatusUnknown")