Revert "[VOL-3069]Pass Context in methods which are performing logging and need the context"
This reverts commit 3c425fbeabed17ec8dad437678b4d105deaf2fbe.
Reason for revert: Merging higher-priority patches first.
Change-Id: Iaa03a5977357dcd86de358d76e90cc54cd6b1fa5
diff --git a/pkg/probe/common.go b/pkg/probe/common.go
index 14857ab..211419d 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.CLogger
+var logger log.Logger
func init() {
// Setup this package so that it's log level can be modified at run time
var err error
- logger, err = log.RegisterPackage(log.JSON, log.ErrorLevel, log.Fields{"pkg": "probe"})
+ logger, err = log.AddPackage(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 732d6df..e89d5bc 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(ctx context.Context, names ...string) {
+func (p *Probe) RegisterService(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(ctx, "probe-service-registered", log.Fields{"service-name": name})
+ logger.Debugw("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(ctx context.Context, name string, status ServiceStatus) {
+func (p *Probe) UpdateStatus(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(ctx, "probe-service-status-updated",
+ logger.Debugw("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(ctx, name, status)
+ p.UpdateStatus(name, status)
}
}
@@ -228,26 +228,25 @@
}
}
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(ctx, "write-response", log.Fields{"error": err})
+ logger.Errorw("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(ctx, "write-response", log.Fields{"error": err})
+ logger.Errorw("write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
comma = ", "
}
if _, err := w.Write([]byte("}")); err != nil {
- logger.Errorw(ctx, "write-response", log.Fields{"error": err})
+ logger.Errorw("write-response", log.Fields{"error": err})
w.WriteHeader(http.StatusInternalServerError)
return
}
@@ -255,7 +254,7 @@
}
// ListenAndServe implements 3 HTTP endpoints on the given port for healthz, readz, and detailz. Returns only on error
-func (p *Probe) ListenAndServe(ctx context.Context, address string) {
+func (p *Probe) ListenAndServe(address string) {
mux := http.NewServeMux()
// Returns the result of the readyFunc calculation
@@ -270,7 +269,7 @@
Addr: address,
Handler: mux,
}
- logger.Fatal(ctx, s.ListenAndServe())
+ logger.Fatal(s.ListenAndServe())
}
func (p *Probe) IsReady() bool {
diff --git a/pkg/probe/probe_test.go b/pkg/probe/probe_test.go
index cae17e5..77cd6a8 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(context.Background(), "one")
+ p.RegisterService("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(context.Background(), "one", "two", "three", "four")
+ p.RegisterService("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{}
- ctx := context.Background()
- p.RegisterService(ctx, "one")
- p.RegisterService(ctx, "two")
- p.RegisterService(ctx, "three", "four")
+
+ p.RegisterService("one")
+ p.RegisterService("two")
+ p.RegisterService("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(context.Background(), "one", "one", "one", "two")
+ p.RegisterService("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{}
- ctx := context.Background()
- p.RegisterService(ctx, "one")
- p.RegisterService(ctx, "one")
- p.RegisterService(ctx, "one", "two")
+
+ p.RegisterService("one")
+ p.RegisterService("one")
+ p.RegisterService("one", "two")
assert.Equal(t, 2, len(p.status), "wrong number of services")
@@ -146,9 +146,9 @@
func TestUpdateStatus(t *testing.T) {
p := &Probe{}
- ctx := context.Background()
- p.RegisterService(ctx, "one", "two")
- p.UpdateStatus(ctx, "one", ServiceStatusRunning)
+
+ p.RegisterService("one", "two")
+ p.UpdateStatus("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{}
- ctx := context.Background()
- p.RegisterService(ctx, "one", "two")
- p.UpdateStatus(ctx, "one", ServiceStatusRunning)
+
+ p.RegisterService("one", "two")
+ p.UpdateStatus("one", ServiceStatusRunning)
assert.Equal(t, ServiceStatusRunning, p.status["one"], "status not set")
assert.Equal(t, ServiceStatusUnknown, p.status["two"], "status set")
- p.RegisterService(ctx, "one", "three")
+ p.RegisterService("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(context.Background(), "one", "two")
+ p.RegisterService("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(context.Background(), "one", "two")
+ p.RegisterService("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(context.Background(), "one", "two")
+ p.RegisterService("one", "two")
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -233,10 +233,9 @@
func TestReadzWithServicesDefault(t *testing.T) {
p := &Probe{}
- ctx := context.Background()
- p.RegisterService(ctx, "one", "two")
- p.UpdateStatus(ctx, "one", ServiceStatusRunning)
- p.UpdateStatus(ctx, "two", ServiceStatusRunning)
+ p.RegisterService("one", "two")
+ p.UpdateStatus("one", ServiceStatusRunning)
+ p.UpdateStatus("two", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -247,8 +246,8 @@
func TestReadzWithServicesDefaultOne(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one", "two")
- p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
+ p.RegisterService("one", "two")
+ p.UpdateStatus("one", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/readz", nil)
w := httptest.NewRecorder()
@@ -269,7 +268,7 @@
func TestHealthzWithServicesWithTrue(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysTrue)
- p.RegisterService(context.Background(), "one", "two")
+ p.RegisterService("one", "two")
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -280,7 +279,7 @@
func TestHealthzWithServicesWithDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one", "two")
+ p.RegisterService("one", "two")
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -301,9 +300,9 @@
func TestHealthzWithServicesDefault(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one", "two")
- p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
- p.UpdateStatus(context.Background(), "two", ServiceStatusRunning)
+ p.RegisterService("one", "two")
+ p.UpdateStatus("one", ServiceStatusRunning)
+ p.UpdateStatus("two", ServiceStatusRunning)
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -314,8 +313,8 @@
func TestHealthzWithServicesDefaultFailed(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one", "two")
- p.UpdateStatus(context.Background(), "one", ServiceStatusFailed)
+ p.RegisterService("one", "two")
+ p.UpdateStatus("one", ServiceStatusFailed)
req := httptest.NewRequest("GET", "http://example.com/healthz", nil)
w := httptest.NewRecorder()
@@ -333,7 +332,7 @@
func TestGetProbeFromContext(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one")
+ p.RegisterService("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")
@@ -348,7 +347,7 @@
func TestUpdateStatusFromContext(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one")
+ p.RegisterService("one")
ctx := context.WithValue(context.Background(), ProbeContextKey, p)
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -360,7 +359,7 @@
func TestUpdateStatusFromNilContext(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one")
+ p.RegisterService("one")
// nolint: staticcheck
UpdateStatusFromContext(nil, "one", ServiceStatusRunning)
@@ -373,7 +372,7 @@
func TestUpdateStatusFromContextWithoutProbe(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one")
+ p.RegisterService("one")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -387,7 +386,7 @@
func TestUpdateStatusFromContextWrongType(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one")
+ p.RegisterService("one")
ctx := context.WithValue(context.Background(), ProbeContextKey, "Teapot")
UpdateStatusFromContext(ctx, "one", ServiceStatusRunning)
@@ -400,7 +399,7 @@
func TestUpdateStatusNoRegistered(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
- p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
+ p.UpdateStatus("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")
@@ -410,7 +409,7 @@
func TestIsReadyTrue(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysTrue).WithHealthFunc(AlwaysFalse)
- p.RegisterService(context.Background(), "SomeService")
+ p.RegisterService("SomeService")
assert.True(t, p.IsReady(), "IsReady should have been true")
}
@@ -418,7 +417,7 @@
func TestIsReadyFalse(t *testing.T) {
p := (&Probe{}).WithReadyFunc(AlwaysFalse).WithHealthFunc(AlwaysFalse)
- p.RegisterService(context.Background(), "SomeService")
+ p.RegisterService("SomeService")
assert.False(t, p.IsReady(), "IsReady should have been false")
}
@@ -426,8 +425,8 @@
func TestGetStatus(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one", "two")
- p.UpdateStatus(context.Background(), "one", ServiceStatusRunning)
+ p.RegisterService("one", "two")
+ p.UpdateStatus("one", ServiceStatusRunning)
ss := p.GetStatus("one")
assert.Equal(t, ServiceStatusRunning, ss, "Service status should have been ServiceStatusRunning")
@@ -436,7 +435,7 @@
func TestGetStatusMissingService(t *testing.T) {
p := &Probe{}
- p.RegisterService(context.Background(), "one", "two")
+ p.RegisterService("one", "two")
ss := p.GetStatus("three")
assert.Equal(t, ServiceStatusUnknown, ss, "Service status should have been ServiceStatusUnknown")