Replace log statements with zap

Signed-off-by: Sachin Kamboj <skamboj1@bloomberg.net>
This commit is contained in:
Sachin Kamboj
2020-04-06 22:18:04 -04:00
parent d33d6d6636
commit 2e1c799a25
5 changed files with 20 additions and 15 deletions

View File

@@ -23,12 +23,12 @@ import (
"image"
"image/color"
"image/png"
"log"
"net/http"
"sort"
"strconv"
"time"
"go.uber.org/zap"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
@@ -143,12 +143,12 @@ func HeatmapHandler(w http.ResponseWriter, r *http.Request) {
buffer := new(bytes.Buffer)
if err := png.Encode(buffer, canvas); err != nil {
log.Println("error encoding png", err)
zap.L().Error("error encoding png", zap.Error(err))
}
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes())))
if _, err := w.Write(buffer.Bytes()); err != nil {
log.Println("error writing heatmap buffer out", err)
zap.L().Error("error writing heatmap buffer out", zap.Error(err))
}
}

View File

@@ -16,11 +16,14 @@ package goldpinger
import (
"io/ioutil"
"log"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// namespace is the namespace for the goldpinger pod
var namespace = getNamespace()
// GoldpingerPod contains just the basic info needed to ping and keep track of a given goldpinger pod
type GoldpingerPod struct {
Name string // Name is the name of the pod
@@ -31,7 +34,7 @@ type GoldpingerPod struct {
func getNamespace() string {
b, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
log.Println("Unable to determine namespace: ", err.Error())
zap.L().Warn("Unable to determine namespace", zap.Error(err))
return ""
}
namespace := string(b)
@@ -41,9 +44,9 @@ func getNamespace() string {
// GetAllPods returns a mapping from a pod name to a pointer to a GoldpingerPod(s)
func GetAllPods() map[string]*GoldpingerPod {
timer := GetLabeledKubernetesCallsTimer()
pods, err := GoldpingerConfig.KubernetesClient.CoreV1().Pods(getNamespace()).List(metav1.ListOptions{LabelSelector: GoldpingerConfig.LabelSelector})
pods, err := GoldpingerConfig.KubernetesClient.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: GoldpingerConfig.LabelSelector})
if err != nil {
log.Println("Error getting pods for selector: ", err.Error())
zap.L().Error("Error getting pods for selector", zap.String("selector", GoldpingerConfig.LabelSelector), zap.Error(err))
CountError("kubernetes_api")
} else {
timer.ObserveDuration()

View File

@@ -16,12 +16,12 @@ package goldpinger
import (
"context"
"log"
"time"
"github.com/bloomberg/goldpinger/v3/pkg/models"
"github.com/go-openapi/strfmt"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
var (
@@ -104,7 +104,7 @@ func init() {
prometheus.MustRegister(goldpingerResponseTimeKubernetesHistogram)
prometheus.MustRegister(goldpingerErrorsCounter)
prometheus.MustRegister(goldpingerDnsErrorsCounter)
log.Println("Metrics setup - see /metrics")
zap.L().Info("Metrics setup - see /metrics")
}
func GetStats(ctx context.Context) *models.PingResults {

View File

@@ -17,13 +17,14 @@ package goldpinger
import (
"context"
"fmt"
"log"
"time"
"go.uber.org/zap"
)
func StartUpdater() {
if GoldpingerConfig.RefreshInterval <= 0 {
log.Println("Not creating updater, period is 0")
zap.L().Info("Not creating updater, refresh interval is negative", zap.Int("RefreshInterval", GoldpingerConfig.RefreshInterval))
return
}
@@ -42,7 +43,7 @@ func StartUpdater() {
}
}
if len(troublemakers) > 0 {
log.Println("Updater ran into trouble with these peers: ", troublemakers)
zap.L().Warn("Updater ran into trouble with these peers", zap.Strings("troublemakers", troublemakers))
}
cancel()

View File

@@ -19,7 +19,6 @@ package restapi
import (
"context"
"crypto/tls"
"log"
"net/http"
"time"
@@ -28,6 +27,7 @@ import (
"github.com/go-openapi/errors"
"github.com/go-openapi/runtime"
"github.com/go-openapi/runtime/middleware"
"go.uber.org/zap"
"github.com/bloomberg/goldpinger/v3/pkg/goldpinger"
"github.com/bloomberg/goldpinger/v3/pkg/restapi/operations"
@@ -49,6 +49,7 @@ func configureFlags(api *operations.GoldpingerAPI) {
func configureAPI(api *operations.GoldpingerAPI) http.Handler {
// configure the api here
api.Logger = zap.S().Infof
api.ServeError = errors.ServeError
api.JSONConsumer = runtime.JSONConsumer()
@@ -128,7 +129,7 @@ func setupMiddlewares(handler http.Handler) http.Handler {
}
func fileServerMiddleware(next http.Handler) http.Handler {
log.Println("Added the static middleware")
zap.L().Info("Added the static middleware")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fileServer := http.FileServer(http.Dir(goldpinger.GoldpingerConfig.StaticFilePath))
if r.URL.Path == "/" {
@@ -145,7 +146,7 @@ func fileServerMiddleware(next http.Handler) http.Handler {
}
func prometheusMetricsMiddleware(next http.Handler) http.Handler {
log.Println("Added the prometheus middleware")
zap.L().Info("Added the prometheus middleware")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/metrics" {
http.StripPrefix("/metrics", promhttp.Handler()).ServeHTTP(w, r)