Skip to content

Commit

Permalink
Merge pull request #403 from xmidt-org/logger-basculehelper
Browse files Browse the repository at this point in the history
Added zap logger and basculehelper package
  • Loading branch information
maurafortino authored Jun 15, 2023
2 parents 157db94 + 44382da commit 6478bfb
Show file tree
Hide file tree
Showing 15 changed files with 977 additions and 280 deletions.
26 changes: 26 additions & 0 deletions anclaHelper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"github.com/xmidt-org/ancla"

// nolint:staticcheck
"github.com/xmidt-org/webpa-common/v2/xmetrics"
)

func NewHelperMeasures(p xmetrics.Registry) ancla.Measures {
return ancla.Measures{
ChrysomPollsTotalCounterName: p.NewCounterVec(ancla.ChrysomPollsTotalCounterName),
WebhookListSizeGaugeName: p.NewPrometheusGauge(ancla.WebhookListSizeGaugeName),
}
}

func AnclaHelperMetrics() []xmetrics.Metric {
return []xmetrics.Metric{
{
Name: ancla.ChrysomPollsTotalCounterName,
Type: xmetrics.CounterType,
Help: "Counter for the number of polls (and their success/failure outcomes) to fetch new items.",
LabelNames: []string{ancla.OutcomeLabel},
},
}
}
29 changes: 20 additions & 9 deletions basculeLogging.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"context"
"fmt"
"net/http"
"strings"
"time"

"github.com/go-kit/log"
"github.com/xmidt-org/candlelight"

// nolint:staticcheck
"github.com/xmidt-org/webpa-common/v2/logging"
"github.com/xmidt-org/sallust"
"go.uber.org/zap"
)

func sanitizeHeaders(headers http.Header) (filtered http.Header) {
Expand All @@ -24,19 +24,30 @@ func sanitizeHeaders(headers http.Header) (filtered http.Header) {
return
}

func setLogger(logger log.Logger) func(delegate http.Handler) http.Handler {
func setLogger(logger *zap.Logger) func(delegate http.Handler) http.Handler {
return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
kvs := []interface{}{"requestHeaders", sanitizeHeaders(r.Header), "requestURL", r.URL.EscapedPath(), "method", r.Method}
kvs, _ = candlelight.AppendTraceInfo(r.Context(), kvs)
ctx := r.WithContext(logging.WithLogger(r.Context(), log.With(logger, kvs...)))
delegate.ServeHTTP(w, ctx)
ctx := r.Context()
ctx = addFieldsToLog(ctx, logger, kvs)
delegate.ServeHTTP(w, r.WithContext(ctx))
})
}
}

func getLogger(ctx context.Context) log.Logger {
logger := log.With(logging.GetLogger(ctx), "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
func getLogger(ctx context.Context) *zap.Logger {
logger := sallust.Get(ctx).With(zap.Time("ts", time.Now().UTC()), zap.Any("caller", zap.WithCaller(true)))
return logger
}

func addFieldsToLog(ctx context.Context, logger *zap.Logger, kvs []interface{}) context.Context {

for i := 0; i <= len(kvs)-2; i += 2 {
logger = logger.With(zap.Any(fmt.Sprint(kvs[i]), kvs[i+1]))
}

return sallust.With(ctx, logger)

}
11 changes: 3 additions & 8 deletions caduceus_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ package main
import (
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"go.uber.org/zap"

"github.com/go-kit/kit/metrics"
"github.com/xmidt-org/ancla"

// nolint:staticcheck
"github.com/xmidt-org/webpa-common/v2/logging"
"github.com/xmidt-org/wrp-go/v3"
)

Expand Down Expand Up @@ -70,12 +67,10 @@ type RequestHandler interface {

type CaduceusHandler struct {
senderWrapper SenderWrapper
log.Logger
*zap.Logger
}

func (ch *CaduceusHandler) HandleRequest(workerID int, msg *wrp.Message) {
ch.Log(level.Key(), level.InfoValue(), "workerID", workerID, logging.MessageKey(), "Worker received a request, now passing"+
" to sender")

ch.Logger.Info("Worker received a request, now passing to sender", zap.Int("workerId", workerID))
ch.senderWrapper.Queue(msg)
}
4 changes: 2 additions & 2 deletions caduceus_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (

"github.com/stretchr/testify/mock"
// nolint:staticcheck
"github.com/xmidt-org/webpa-common/v2/logging"
"github.com/xmidt-org/webpa-common/v2/adapter"
"github.com/xmidt-org/wrp-go/v3"
)

func TestCaduceusHandler(t *testing.T) {
logger := logging.DefaultLogger()
logger := adapter.DefaultLogger().Logger

fakeSenderWrapper := new(mockSenderWrapper)
fakeSenderWrapper.On("Queue", mock.AnythingOfType("*wrp.Message")).Return().Once()
Expand Down
38 changes: 18 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ require (
emperror.dev/emperror v0.33.0
github.com/davecgh/go-spew v1.1.1
github.com/go-kit/kit v0.12.0
github.com/go-kit/log v0.2.1
github.com/gorilla/mux v1.8.0
github.com/justinas/alice v1.2.0
github.com/prometheus/client_golang v1.15.1
github.com/satori/go.uuid v1.2.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
github.com/xmidt-org/ancla v0.3.10
github.com/xmidt-org/bascule v0.11.1
github.com/xmidt-org/ancla v0.3.11
github.com/xmidt-org/bascule v0.11.5
github.com/xmidt-org/candlelight v0.0.16
github.com/xmidt-org/clortho v0.0.4
github.com/xmidt-org/httpaux v0.3.2
github.com/xmidt-org/sallust v0.2.0
github.com/xmidt-org/sallust v0.2.2
github.com/xmidt-org/touchstone v0.1.2
github.com/xmidt-org/webpa-common/v2 v2.0.7
github.com/xmidt-org/webpa-common/v2 v2.1.4
github.com/xmidt-org/wrp-go/v3 v3.1.6
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.40.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.40.0
Expand All @@ -32,7 +31,7 @@ require (
emperror.dev/errors v0.8.1 // indirect
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2 // indirect
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/armon/go-metrics v0.4.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/billhathaway/consistentHash v0.0.0-20140718022140-addea16d2229 // indirect
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 // indirect
Expand All @@ -42,20 +41,21 @@ require (
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-zookeeper/zk v1.0.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/goph/emperror v0.17.3-0.20190703203600-60a8d9faa17b // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.14.0 // indirect
github.com/hashicorp/consul/api v1.20.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.1 // indirect
github.com/hashicorp/consul/api v1.21.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.2 // indirect
github.com/hashicorp/go-hclog v1.4.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
Expand All @@ -71,15 +71,15 @@ require (
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
Expand All @@ -91,11 +91,9 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/xmidt-org/argus v0.9.1 // indirect
github.com/xmidt-org/arrange v0.3.0 // indirect
github.com/xmidt-org/argus v0.9.10 // indirect
github.com/xmidt-org/arrange v0.4.0 // indirect
github.com/xmidt-org/chronon v0.1.1 // indirect
github.com/xmidt-org/themis v0.4.8 // indirect
github.com/xmidt-org/webpa-common v1.11.9 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
Expand All @@ -109,8 +107,8 @@ require (
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.15.0 // indirect
go.uber.org/fx v1.18.2 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
Expand All @@ -120,6 +118,6 @@ require (
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 6478bfb

Please sign in to comment.