From 746a7bb65b43dabbd254c9ffaa4a21156ad8cb4b Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Thu, 2 Jan 2025 13:42:18 +0000 Subject: [PATCH 01/58] update pulsar mock --- go.mod | 2 + go.sum | 6 ++ internal/common/grpc/grpc.go | 135 ++++++++++++++++------------------- 3 files changed, 70 insertions(+), 73 deletions(-) diff --git a/go.mod b/go.mod index c01b5c50f49..62c092ac947 100644 --- a/go.mod +++ b/go.mod @@ -139,6 +139,8 @@ require ( github.com/goreleaser/nfpm/v2 v2.35.3 // indirect github.com/gorilla/css v1.0.0 // indirect github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect + github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 // indirect + github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0 // indirect github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-immutable-radix v1.3.1 // indirect diff --git a/go.sum b/go.sum index f63c61de172..09f3aead36b 100644 --- a/go.sum +++ b/go.sum @@ -271,6 +271,12 @@ github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWet github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= +github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 h1:qnpSQwGEnkcRpTqNOIR6bJbR0gAorgP9CSALpRcKoAA= +github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1/go.mod h1:lXGCsh6c22WGtjr+qGHj1otzZpV/1kwTMAqkwZsnWRU= +github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 h1:pRhl55Yx1eC7BZ1N+BBWwnKaMyD8uC+34TLdndZMAKk= +github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0/go.mod h1:XKMd7iuf/RGPSMJ/U4HP0zS2Z9Fh8Ps9a+6X26m/tmI= +github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0 h1:kQ0NI7W1B3HwiN5gAYtY+XFItDPbLBwYRxAqbFTyDes= +github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0/go.mod h1:zrT2dxOAjNFPRGjTUe2Xmb4q4YdUwVvQFV6xiCSf+z0= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo= diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index c4f858f3ba1..42b7227cec0 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -3,17 +3,12 @@ package grpc import ( "crypto/tls" "fmt" - "net" - "runtime/debug" - "sync" - "time" - grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" - grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth" grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" - grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery" - grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" - grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" + grpc_prometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" + grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" + grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery" + "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/codes" @@ -21,6 +16,10 @@ import ( _ "google.golang.org/grpc/encoding/gzip" "google.golang.org/grpc/keepalive" "google.golang.org/grpc/status" + "net" + "runtime/debug" + "sync" + "time" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/armadaerrors" @@ -37,77 +36,46 @@ func CreateGrpcServer( keepaliveEnforcementPolicy keepalive.EnforcementPolicy, authServices []auth.AuthService, tlsConfig configuration.TlsConfig, - logrusOptions ...grpc_logrus.Option, + loggingOpts ...grpc_logrus.Option, ) *grpc.Server { - // Logging, authentication, etc. are implemented via gRPC interceptors - // (i.e., via functions that are called before handling the actual request). - // There are separate interceptors for unary and streaming gRPC calls. - unaryInterceptors := []grpc.UnaryServerInterceptor{} - streamInterceptors := []grpc.StreamServerInterceptor{} - - // Automatically recover from panics - // NOTE This must be the first interceptor, so it can handle panics in any subsequently added interceptor - recovery := grpc_recovery.WithRecoveryHandler(panicRecoveryHandler) - unaryInterceptors = append(unaryInterceptors, grpc_recovery.UnaryServerInterceptor(recovery)) - streamInterceptors = append(streamInterceptors, grpc_recovery.StreamServerInterceptor(recovery)) - - // Logging (using logrus) - // By default, information contained in the request context is logged - // tagsExtractor pulls information out of the request payload (a protobuf) and stores it in - // the context, such that it is logged. - messageDefault := log.NewEntry(log.StandardLogger()) - tagsExtractor := grpc_ctxtags.WithFieldExtractor(grpc_ctxtags.CodeGenRequestFieldExtractor) - unaryInterceptors = append(unaryInterceptors, - grpc_ctxtags.UnaryServerInterceptor(tagsExtractor), - requestid.UnaryServerInterceptor(false), - armadaerrors.UnaryServerInterceptor(2000), - grpc_logrus.UnaryServerInterceptor(messageDefault, logrusOptions...), - ) - streamInterceptors = append(streamInterceptors, - grpc_ctxtags.StreamServerInterceptor(tagsExtractor), - requestid.StreamServerInterceptor(false), - armadaerrors.StreamServerInterceptor(2000), - grpc_logrus.StreamServerInterceptor(messageDefault, logrusOptions...), - ) - // Authentication - // The provided authServices represents a list of services that can be used to authenticate - // the client (e.g., username/password and OpenId). authFunction is a combination of these. - authFunction := auth.CreateGrpcMiddlewareAuthFunction(auth.NewMultiAuthService(authServices)) - unaryInterceptors = append(unaryInterceptors, grpc_auth.UnaryServerInterceptor(authFunction)) - streamInterceptors = append(streamInterceptors, grpc_auth.StreamServerInterceptor(authFunction)) + defaultLogger := log.NewEntry(log.StandardLogger()) - // Prometheus timeseries collection integration - grpc_prometheus.EnableHandlingTimeHistogram() - unaryInterceptors = append(unaryInterceptors, grpc_prometheus.UnaryServerInterceptor) - streamInterceptors = append(streamInterceptors, grpc_prometheus.StreamServerInterceptor) + authFunction := auth.CreateGrpcMiddlewareAuthFunction(auth.NewMultiAuthService(authServices)) + srvMetrics := setupPromMetrics() - serverOptions := []grpc.ServerOption{ + return grpc.NewServer( grpc.KeepaliveParams(keepaliveParams), grpc.KeepaliveEnforcementPolicy(keepaliveEnforcementPolicy), - grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(streamInterceptors...)), - grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(unaryInterceptors...)), - } - - if tlsConfig.Enabled { - cachedCertificateService := certs.NewCachedCertificateService(tlsConfig.CertPath, tlsConfig.KeyPath, time.Minute) - go func() { - cachedCertificateService.Run(armadacontext.Background()) - }() - tlsCreds := credentials.NewTLS(&tls.Config{ - GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) { - cert := cachedCertificateService.GetCertificate() - if cert == nil { - return nil, fmt.Errorf("unexpectedly received nil from certificate cache") - } - return cert, nil - }, - }) - serverOptions = append(serverOptions, grpc.Creds(tlsCreds)) - } + setupTls(tlsConfig), + grpc.ChainUnaryInterceptor( + srvMetrics.UnaryServerInterceptor(), + requestid.UnaryServerInterceptor(false), + grpc_logrus.UnaryServerInterceptor(defaultLogger, loggingOpts...), + armadaerrors.UnaryServerInterceptor(2000), + grpc_auth.UnaryServerInterceptor(authFunction), + grpc_recovery.UnaryServerInterceptor(grpc_recovery.WithRecoveryHandler(panicRecoveryHandler)), + ), + grpc.ChainStreamInterceptor( + srvMetrics.StreamServerInterceptor(), + requestid.StreamServerInterceptor(false), + grpc_logrus.StreamServerInterceptor(defaultLogger, loggingOpts...), + armadaerrors.StreamServerInterceptor(2000), + grpc_auth.StreamServerInterceptor(authFunction), + grpc_recovery.StreamServerInterceptor(grpc_recovery.WithRecoveryHandler(panicRecoveryHandler)), + ), + ) +} - // Interceptors are registered at server creation - return grpc.NewServer(serverOptions...) +func setupPromMetrics() *grpc_prometheus.ServerMetrics { + srvMetrics := grpc_prometheus.NewServerMetrics( + grpc_prometheus.WithServerHandlingTimeHistogram( + grpc_prometheus.WithHistogramBuckets([]float64{0.001, 0.01, 0.1, 0.3, 0.6, 1, 3, 6, 9, 20, 30, 60, 90, 120}), + ), + ) + reg := prometheus.NewRegistry() + reg.MustRegister(srvMetrics) + return srvMetrics } // TODO We don't need this function. Just do this at the caller. @@ -143,6 +111,27 @@ func CreateShutdownHandler(ctx *armadacontext.Context, gracePeriod time.Duration } } +func setupTls(tlsConfig configuration.TlsConfig) grpc.ServerOption { + if !tlsConfig.Enabled { + return grpc.EmptyServerOption{} + } + + cachedCertificateService := certs.NewCachedCertificateService(tlsConfig.CertPath, tlsConfig.KeyPath, time.Minute) + go func() { + cachedCertificateService.Run(armadacontext.Background()) + }() + tlsCreds := credentials.NewTLS(&tls.Config{ + GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) { + cert := cachedCertificateService.GetCertificate() + if cert == nil { + return nil, fmt.Errorf("unexpectedly received nil from certificate cache") + } + return cert, nil + }, + }) + return grpc.Creds(tlsCreds) +} + // This function is called whenever a gRPC handler panics. func panicRecoveryHandler(p interface{}) (err error) { log.Errorf("Request triggered panic with cause %v \n%s", p, string(debug.Stack())) From 64355fe7603cb30c0556d9ac2236e92ca7c50f91 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Fri, 3 Jan 2025 09:30:42 +0000 Subject: [PATCH 02/58] go mod tidy --- go.mod | 4 ++-- go.sum | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index bf2e80cfc97..fc9685c31ef 100644 --- a/go.mod +++ b/go.mod @@ -72,6 +72,8 @@ require ( github.com/go-playground/validator/v10 v10.15.4 github.com/gogo/status v1.1.1 github.com/goreleaser/goreleaser v1.24.0 + github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 + github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0 github.com/jackc/pgx/v5 v5.5.4 github.com/jessevdk/go-flags v1.5.0 github.com/magefile/mage v1.14.0 @@ -139,8 +141,6 @@ require ( github.com/goreleaser/nfpm/v2 v2.35.3 // indirect github.com/gorilla/css v1.0.0 // indirect github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect - github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 // indirect - github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0 // indirect github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect github.com/hamba/avro/v2 v2.22.2-0.20240625062549-66aad10411d9 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect diff --git a/go.sum b/go.sum index 273bad6e5ac..801e54c7a76 100644 --- a/go.sum +++ b/go.sum @@ -302,8 +302,6 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDa github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 h1:qnpSQwGEnkcRpTqNOIR6bJbR0gAorgP9CSALpRcKoAA= github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1/go.mod h1:lXGCsh6c22WGtjr+qGHj1otzZpV/1kwTMAqkwZsnWRU= -github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 h1:pRhl55Yx1eC7BZ1N+BBWwnKaMyD8uC+34TLdndZMAKk= -github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0/go.mod h1:XKMd7iuf/RGPSMJ/U4HP0zS2Z9Fh8Ps9a+6X26m/tmI= github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0 h1:kQ0NI7W1B3HwiN5gAYtY+XFItDPbLBwYRxAqbFTyDes= github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0/go.mod h1:zrT2dxOAjNFPRGjTUe2Xmb4q4YdUwVvQFV6xiCSf+z0= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= From 2bc7d8d846648a5357dad052282ca7eead93b89a Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Fri, 3 Jan 2025 09:36:45 +0000 Subject: [PATCH 03/58] import order --- internal/common/grpc/grpc.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index 42b7227cec0..8a35db3bbf9 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -3,6 +3,10 @@ package grpc import ( "crypto/tls" "fmt" + "net" + "runtime/debug" + "sync" + "time" grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" @@ -16,10 +20,6 @@ import ( _ "google.golang.org/grpc/encoding/gzip" "google.golang.org/grpc/keepalive" "google.golang.org/grpc/status" - "net" - "runtime/debug" - "sync" - "time" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/armadaerrors" From 0fc3e7f3d8c21f822a7f0d3d5fa4c886ba6068b5 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Fri, 3 Jan 2025 16:34:57 +0000 Subject: [PATCH 04/58] wip --- .../common/armadacontext/armada_context.go | 4 +- .../armadacontext/armada_context_test.go | 9 --- internal/common/armadaerrors/errors_test.go | 2 +- internal/common/auth/common.go | 16 +---- internal/common/auth/kubernetes_test.go | 4 +- .../common/grpc/armadacontext/interceptors.go | 37 ++++++++++ internal/common/grpc/grpc.go | 45 +++++++++--- internal/common/logging/interceptors.go | 50 ------------- internal/common/logging/interceptors_test.go | 70 ------------------- .../common/requestid/interceptors_test.go | 2 +- internal/executor/service/lease_requester.go | 2 +- .../testsuite/eventwatcher/eventwatcher.go | 2 +- pkg/client/connection.go | 2 +- 13 files changed, 84 insertions(+), 161 deletions(-) create mode 100644 internal/common/grpc/armadacontext/interceptors.go delete mode 100644 internal/common/logging/interceptors.go delete mode 100644 internal/common/logging/interceptors_test.go diff --git a/internal/common/armadacontext/armada_context.go b/internal/common/armadacontext/armada_context.go index 665c19c51f9..5515ebe1f14 100644 --- a/internal/common/armadacontext/armada_context.go +++ b/internal/common/armadacontext/armada_context.go @@ -4,7 +4,6 @@ import ( "context" "time" - "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" "github.com/sirupsen/logrus" "golang.org/x/sync/errgroup" ) @@ -39,8 +38,7 @@ func FromGrpcCtx(ctx context.Context) *Context { if ok { return armadaCtx } - log := ctxlogrus.Extract(ctx) - return New(ctx, log) + return New(ctx, logrus.NewEntry(logrus.StandardLogger())) } // New returns an armada context that encapsulates both a go context and a logger diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index 4cda401c1b1..ef7c84aa705 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -5,8 +5,6 @@ import ( "testing" "time" - "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" ) @@ -19,13 +17,6 @@ func TestNew(t *testing.T) { require.Equal(t, context.Background(), ctx.Context) } -func TestFromGrpcContext(t *testing.T) { - grpcCtx := ctxlogrus.ToContext(context.Background(), defaultLogger) - ctx := FromGrpcCtx(grpcCtx) - require.Equal(t, grpcCtx, ctx.Context) - require.Equal(t, defaultLogger, ctx.FieldLogger) -} - func TestBackground(t *testing.T) { ctx := Background() require.Equal(t, ctx.Context, context.Background()) diff --git a/internal/common/armadaerrors/errors_test.go b/internal/common/armadaerrors/errors_test.go index fa70cf43e32..da72a6458c0 100644 --- a/internal/common/armadaerrors/errors_test.go +++ b/internal/common/armadaerrors/errors_test.go @@ -9,7 +9,7 @@ import ( "time" "github.com/apache/pulsar-client-go/pulsar" - grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" + grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" "github.com/pkg/errors" "github.com/redis/go-redis/v9" "github.com/stretchr/testify/assert" diff --git a/internal/common/auth/common.go b/internal/common/auth/common.go index f3bd468cffd..62a22949b93 100644 --- a/internal/common/auth/common.go +++ b/internal/common/auth/common.go @@ -4,11 +4,9 @@ import ( "context" "net/http" + "github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata" "golang.org/x/exp/slices" - grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags" - "github.com/grpc-ecosystem/go-grpc-middleware/util/metautils" - "github.com/armadaproject/armada/internal/common/util" ) @@ -120,16 +118,11 @@ type AuthService interface { // request context for logging purposes. func CreateGrpcMiddlewareAuthFunction(authService AuthService) func(ctx context.Context) (context.Context, error) { return func(ctx context.Context) (context.Context, error) { - authHeader := metautils.ExtractIncoming(ctx).Get("authorization") + authHeader := metadata.ExtractIncoming(ctx).Get("authorization") principal, err := authService.Authenticate(ctx, authHeader) if err != nil { return nil, err } - - // record username for request logging - grpc_ctxtags.Extract(ctx).Set("user", principal.GetName()) - grpc_ctxtags.Extract(ctx).Set("authService", principal.GetAuthMethod()) - return WithPrincipal(ctx, principal), nil } } @@ -150,11 +143,6 @@ func CreateHttpMiddlewareAuthFunction(authService AuthService) func(w http.Respo http.Error(w, "auth error:"+err.Error(), http.StatusInternalServerError) return nil, err } - - // record username for request logging - grpc_ctxtags.Extract(ctx).Set("user", principal.GetName()) - grpc_ctxtags.Extract(ctx).Set("authService", principal.GetAuthMethod()) - return WithPrincipal(ctx, principal), nil } } diff --git a/internal/common/auth/kubernetes_test.go b/internal/common/auth/kubernetes_test.go index 01da9d02b1b..d29ceb4c17b 100644 --- a/internal/common/auth/kubernetes_test.go +++ b/internal/common/auth/kubernetes_test.go @@ -9,7 +9,7 @@ import ( "testing" "time" - "github.com/grpc-ecosystem/go-grpc-middleware/util/metautils" + grpc_metadata "github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata" "github.com/patrickmn/go-cache" "github.com/stretchr/testify/assert" authv1 "k8s.io/api/authentication/v1" @@ -151,7 +151,7 @@ func TestAuthenticateKubernetes(t *testing.T) { // Create authentication context payload := createKubernetesAuthPayload(testToken, testCA) ctx := context.Background() - metadata := metautils.ExtractIncoming(ctx) + metadata := grpc_metadata.ExtractIncoming(ctx) metadata.Set("authorization", payload) ctx = metadata.ToIncoming(ctx) diff --git a/internal/common/grpc/armadacontext/interceptors.go b/internal/common/grpc/armadacontext/interceptors.go new file mode 100644 index 00000000000..9622e6791a5 --- /dev/null +++ b/internal/common/grpc/armadacontext/interceptors.go @@ -0,0 +1,37 @@ +package armadacontext + +import ( + "context" + + grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" + "github.com/sirupsen/logrus" + "google.golang.org/grpc" + + "github.com/armadaproject/armada/internal/common/armadacontext" + "github.com/armadaproject/armada/internal/common/auth" + "github.com/armadaproject/armada/internal/common/requestid" +) + +func UnaryServerInterceptor() grpc.UnaryServerInterceptor { + return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { + armadaCtx := createArmadaCtx(ctx) + return handler(armadaCtx, req) + } +} + +func StreamServerInterceptor() grpc.StreamServerInterceptor { + return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { + armadaCtx := createArmadaCtx(stream.Context()) + wrapped := grpc_middleware.WrapServerStream(stream) + wrapped.WrappedContext = armadaCtx + return handler(srv, wrapped) + } +} + +func createArmadaCtx(ctx context.Context) *armadacontext.Context { + log := logrus.WithFields(map[string]interface{}{ + "requestId": requestid.FromContextOrMissing(ctx), + "user": auth.GetPrincipal(ctx).GetName(), + }) + return armadacontext.New(ctx, log) +} diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index 8a35db3bbf9..b8e3a022870 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -1,6 +1,7 @@ package grpc import ( + "context" "crypto/tls" "fmt" "net" @@ -8,10 +9,11 @@ import ( "sync" "time" - grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus" grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" + grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery" + "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" "google.golang.org/grpc" @@ -25,6 +27,7 @@ import ( "github.com/armadaproject/armada/internal/common/armadaerrors" "github.com/armadaproject/armada/internal/common/auth" "github.com/armadaproject/armada/internal/common/certs" + grpc_armadacontext "github.com/armadaproject/armada/internal/common/grpc/armadacontext" "github.com/armadaproject/armada/internal/common/grpc/configuration" "github.com/armadaproject/armada/internal/common/requestid" ) @@ -36,14 +39,13 @@ func CreateGrpcServer( keepaliveEnforcementPolicy keepalive.EnforcementPolicy, authServices []auth.AuthService, tlsConfig configuration.TlsConfig, - loggingOpts ...grpc_logrus.Option, ) *grpc.Server { - defaultLogger := log.NewEntry(log.StandardLogger()) - authFunction := auth.CreateGrpcMiddlewareAuthFunction(auth.NewMultiAuthService(authServices)) srvMetrics := setupPromMetrics() + loggerOpts := []grpc_logging.Option{grpc_logging.WithLogOnEvents(grpc_logging.StartCall, grpc_logging.FinishCall)} + return grpc.NewServer( grpc.KeepaliveParams(keepaliveParams), grpc.KeepaliveEnforcementPolicy(keepaliveEnforcementPolicy), @@ -51,17 +53,19 @@ func CreateGrpcServer( grpc.ChainUnaryInterceptor( srvMetrics.UnaryServerInterceptor(), requestid.UnaryServerInterceptor(false), - grpc_logrus.UnaryServerInterceptor(defaultLogger, loggingOpts...), - armadaerrors.UnaryServerInterceptor(2000), grpc_auth.UnaryServerInterceptor(authFunction), + grpc_armadacontext.UnaryServerInterceptor(), + grpc_logging.UnaryServerInterceptor(InterceptorLogger(), loggerOpts...), + armadaerrors.UnaryServerInterceptor(2000), grpc_recovery.UnaryServerInterceptor(grpc_recovery.WithRecoveryHandler(panicRecoveryHandler)), ), grpc.ChainStreamInterceptor( srvMetrics.StreamServerInterceptor(), requestid.StreamServerInterceptor(false), - grpc_logrus.StreamServerInterceptor(defaultLogger, loggingOpts...), - armadaerrors.StreamServerInterceptor(2000), grpc_auth.StreamServerInterceptor(authFunction), + grpc_armadacontext.StreamServerInterceptor(), + grpc_logging.StreamServerInterceptor(InterceptorLogger(), loggerOpts...), + armadaerrors.StreamServerInterceptor(2000), grpc_recovery.StreamServerInterceptor(grpc_recovery.WithRecoveryHandler(panicRecoveryHandler)), ), ) @@ -137,3 +141,28 @@ func panicRecoveryHandler(p interface{}) (err error) { log.Errorf("Request triggered panic with cause %v \n%s", p, string(debug.Stack())) return status.Errorf(codes.Internal, "Internal server error caused by %v", p) } + +func InterceptorLogger() grpc_logging.Logger { + return grpc_logging.LoggerFunc(func(ctx context.Context, lvl grpc_logging.Level, msg string, fields ...any) { + armadaCtx := armadacontext.FromGrpcCtx(ctx) + logFields := make(map[string]any, len(fields)/2) + i := grpc_logging.Fields(fields).Iterator() + for i.Next() { + k, v := i.At() + logFields[k] = v + } + l := armadaCtx.WithFields(logFields) + switch lvl { + case grpc_logging.LevelDebug: + l.Debug(msg) + case grpc_logging.LevelInfo: + l.Info(msg) + case grpc_logging.LevelWarn: + l.Warn(msg) + case grpc_logging.LevelError: + l.Error(msg) + default: + panic(fmt.Sprintf("unknown level %v", lvl)) + } + }) +} diff --git a/internal/common/logging/interceptors.go b/internal/common/logging/interceptors.go deleted file mode 100644 index 33b4ed48889..00000000000 --- a/internal/common/logging/interceptors.go +++ /dev/null @@ -1,50 +0,0 @@ -package logging - -import ( - "context" - "fmt" - - "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" - "github.com/sirupsen/logrus" - "google.golang.org/grpc" - - "github.com/armadaproject/armada/internal/common/requestid" -) - -// UnaryServerInterceptor returns an interceptor that adds the request id as a -// field to the logrus logger embedded in the context. If an error occurs in the handler, -// it also adds a stack trace. -func UnaryServerInterceptor() grpc.UnaryServerInterceptor { - return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { - if id, ok := requestid.FromContext(ctx); ok { - ctxlogrus.AddFields(ctx, logrus.Fields{requestid.MetadataKey: id}) - } - rv, err := handler(ctx, req) - if err != nil { - // %+v prints a stack trace for pkg/errors errors - ctxlogrus.AddFields(ctx, logrus.Fields{"errorVerbose": fmt.Sprintf("%+v", err)}) - } - return rv, err - } -} - -// StreamServerInterceptor returns an interceptor that adds the request id as a -// field to the logrus logger embedded in the context. If an error occurs in the handler, -// it also adds a stack trace. -func StreamServerInterceptor() grpc.StreamServerInterceptor { - return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { - ctx := stream.Context() - if id, ok := requestid.FromContext(ctx); ok { - ctxlogrus.AddFields(ctx, logrus.Fields{requestid.MetadataKey: id}) - } - // The logrus logger only logs at the end of the call. As streaming calls may last a long time - // We also log here which will produce a log line at the start of the call - ctxlogrus.Extract(ctx).Infof("started streaming call") - err := handler(srv, stream) - if err != nil { - // %+v prints a stack trace for pkg/errors errors - ctxlogrus.AddFields(ctx, logrus.Fields{"errorVerbose": fmt.Sprintf("%+v", err)}) - } - return err - } -} diff --git a/internal/common/logging/interceptors_test.go b/internal/common/logging/interceptors_test.go deleted file mode 100644 index 2d3f76ff159..00000000000 --- a/internal/common/logging/interceptors_test.go +++ /dev/null @@ -1,70 +0,0 @@ -package logging - -import ( - "context" - "testing" - - grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" - "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus" - "github.com/renstrom/shortuuid" - "github.com/sirupsen/logrus" - "github.com/stretchr/testify/require" - "google.golang.org/grpc" - "google.golang.org/grpc/metadata" - - "github.com/armadaproject/armada/internal/common/requestid" -) - -func TestUnaryServerInterceptor(t *testing.T) { - ctx := context.Background() - ctx = metadata.NewIncomingContext(ctx, metadata.New(map[string]string{})) - id := shortuuid.New() - ctx, ok := requestid.AddToIncomingContext(ctx, id) - require.True(t, ok, "error adding request id to context") - logger := logrus.New() - entry := logrus.NewEntry(logger) - ctx = ctxlogrus.ToContext(ctx, entry) - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - entry := ctxlogrus.Extract(ctx) - for _, field := range entry.Data { - if s, ok := field.(string); ok && s == id { - return nil, nil - } - } - t.Fatal("request id was not added as a logger field") - return nil, nil - } - - f := UnaryServerInterceptor() - _, err := f(ctx, nil, nil, handler) - require.NoError(t, err) -} - -func TestStreamServerInterceptor(t *testing.T) { - ctx := context.Background() - ctx = metadata.NewIncomingContext(ctx, metadata.New(map[string]string{})) - id := shortuuid.New() - ctx, ok := requestid.AddToIncomingContext(ctx, id) - require.True(t, ok, "error adding request id to context") - - logger := logrus.New() - entry := logrus.NewEntry(logger) - ctx = ctxlogrus.ToContext(ctx, entry) - stream := &grpc_middleware.WrappedServerStream{} - stream.WrappedContext = ctx - handler := func(srv interface{}, stream grpc.ServerStream) error { - ctx := stream.Context() - entry := ctxlogrus.Extract(ctx) - for _, field := range entry.Data { - if s, ok := field.(string); ok && s == id { - return nil - } - } - t.Fatal("request id was not added as a logger field") - return nil - } - - f := StreamServerInterceptor() - err := f(nil, stream, nil, handler) - require.NoError(t, err) -} diff --git a/internal/common/requestid/interceptors_test.go b/internal/common/requestid/interceptors_test.go index 30cbd63dffa..584bf8798f5 100644 --- a/internal/common/requestid/interceptors_test.go +++ b/internal/common/requestid/interceptors_test.go @@ -4,7 +4,7 @@ import ( "context" "testing" - grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" + grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" "github.com/renstrom/shortuuid" "github.com/stretchr/testify/require" "google.golang.org/grpc" diff --git a/internal/executor/service/lease_requester.go b/internal/executor/service/lease_requester.go index c95f31b4725..c9451962b89 100644 --- a/internal/executor/service/lease_requester.go +++ b/internal/executor/service/lease_requester.go @@ -4,7 +4,7 @@ import ( "fmt" "io" - grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry" + grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry" "github.com/pkg/errors" log "github.com/sirupsen/logrus" "google.golang.org/grpc" diff --git a/internal/testsuite/eventwatcher/eventwatcher.go b/internal/testsuite/eventwatcher/eventwatcher.go index 51d619d4050..c39f1d100c0 100644 --- a/internal/testsuite/eventwatcher/eventwatcher.go +++ b/internal/testsuite/eventwatcher/eventwatcher.go @@ -12,7 +12,7 @@ import ( "strings" "time" - "github.com/grpc-ecosystem/go-grpc-middleware/util/backoffutils" + "github.com/grpc-ecosystem/go-grpc-middleware/v2/util/backoffutils" "github.com/pkg/errors" "golang.org/x/exp/maps" "golang.org/x/sync/errgroup" diff --git a/pkg/client/connection.go b/pkg/client/connection.go index 64e3b5dfed5..a9b086b93f6 100644 --- a/pkg/client/connection.go +++ b/pkg/client/connection.go @@ -6,7 +6,7 @@ import ( "strings" "time" - grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry" + grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry" "github.com/pkg/errors" "google.golang.org/grpc" "google.golang.org/grpc/credentials" From 93ac227c1611dee9efbb16fc048b7137944f4efa Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Fri, 3 Jan 2025 16:38:56 +0000 Subject: [PATCH 05/58] wip --- go.mod | 1 - go.sum | 21 --------------------- internal/common/requestid/interceptors.go | 2 +- internal/scheduler/schedulerapp.go | 21 +-------------------- 4 files changed, 2 insertions(+), 43 deletions(-) diff --git a/go.mod b/go.mod index fc9685c31ef..c2623ca95ce 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,6 @@ require ( github.com/golang/protobuf v1.5.4 github.com/google/go-cmp v0.6.0 // indirect github.com/google/uuid v1.6.0 - github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/hashicorp/go-memdb v1.3.4 diff --git a/go.sum b/go.sum index 801e54c7a76..1491cc31df8 100644 --- a/go.sum +++ b/go.sum @@ -73,7 +73,6 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= -github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/immutable v0.4.3 h1:GYHcksoJ9K6HyAUpGxwZURrbTkXA0Dh4otXGqbhdrjA= github.com/benbjohnson/immutable v0.4.3/go.mod h1:qJIKKSmdqz1tVzNtst1DZzvaqOU1onk1rc03IeM3Owk= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -170,8 +169,6 @@ github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= -github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= -github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -208,7 +205,6 @@ github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91 github.com/go-playground/validator/v10 v10.15.4 h1:zMXza4EpOdooxPel5xDqXEdXG5r+WggpvnAKMsalBjs= github.com/go-playground/validator/v10 v10.15.4/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= @@ -298,8 +294,6 @@ github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWetyv/0+wjACpqVH+eVULgEjkurDLq3goeM= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= -github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= -github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 h1:qnpSQwGEnkcRpTqNOIR6bJbR0gAorgP9CSALpRcKoAA= github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1/go.mod h1:lXGCsh6c22WGtjr+qGHj1otzZpV/1kwTMAqkwZsnWRU= github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.2.0 h1:kQ0NI7W1B3HwiN5gAYtY+XFItDPbLBwYRxAqbFTyDes= @@ -373,7 +367,6 @@ github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8 github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -466,7 +459,6 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8 github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug= github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM= -github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc= github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ= github.com/pborman/getopt v0.0.0-20180729010549-6fdd0a2c7117/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o= @@ -479,7 +471,6 @@ github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pierrec/lz4/v4 v4.1.8 h1:ieHkV+i2BRzngO4Wd/3HGowuZStgq6QkPsD1eolNAO4= github.com/pierrec/lz4/v4 v4.1.8/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -525,7 +516,6 @@ github.com/shirou/gopsutil/v3 v3.23.12 h1:z90NtUkp3bMtmICZKpC4+WaknU1eXtp5vtbQ11 github.com/shirou/gopsutil/v3 v3.23.12/go.mod h1:1FrWgea594Jp7qmjHUUPlJDTPgcsb9mGnXDxavtikzM= github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= @@ -545,7 +535,6 @@ github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= @@ -613,16 +602,12 @@ go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU= go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee331t6JAXeK2bcyhLOOc= go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o= -go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM= -go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -716,7 +701,6 @@ golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -738,7 +722,6 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -780,7 +763,6 @@ golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191113191852-77e3bb0ad9e7/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191115202509-3a792d9c32b2/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= @@ -836,7 +818,6 @@ google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150/go.mod h1:n3cpQtvx google.golang.org/genproto v0.0.0-20200204135345-fa8e72b47b90/go.mod h1:GmwEX6Z4W5gMy59cAlVYjN9JhxgbQH6Gn+gFDQe2lzA= google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200423170343-7949de9c1215/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= @@ -855,7 +836,6 @@ google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQ google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= @@ -902,7 +882,6 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/common/requestid/interceptors.go b/internal/common/requestid/interceptors.go index b5ee03cd058..2e775545319 100644 --- a/internal/common/requestid/interceptors.go +++ b/internal/common/requestid/interceptors.go @@ -3,7 +3,7 @@ package requestid import ( "context" - grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" + grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" "github.com/renstrom/shortuuid" "google.golang.org/grpc" "google.golang.org/grpc/metadata" diff --git a/internal/scheduler/schedulerapp.go b/internal/scheduler/schedulerapp.go index 7482c38ffa7..4779b10657d 100644 --- a/internal/scheduler/schedulerapp.go +++ b/internal/scheduler/schedulerapp.go @@ -10,11 +10,9 @@ import ( "github.com/apache/pulsar-client-go/pulsar" "github.com/google/uuid" - grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" - "google.golang.org/grpc/codes" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -183,7 +181,7 @@ func Run(config schedulerconfig.Configuration) error { if err != nil { return errors.WithMessage(err, "error creating auth services") } - grpcServer := grpcCommon.CreateGrpcServer(config.Grpc.KeepaliveParams, config.Grpc.KeepaliveEnforcementPolicy, authServices, config.Grpc.Tls, createLogrusLoggingOption()) + grpcServer := grpcCommon.CreateGrpcServer(config.Grpc.KeepaliveParams, config.Grpc.KeepaliveEnforcementPolicy, authServices, config.Grpc.Tls) defer grpcServer.GracefulStop() lis, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Grpc.Port)) if err != nil { @@ -362,20 +360,3 @@ func loadClusterConfig(ctx *armadacontext.Context) (*rest.Config, error) { ctx.Info("Running with in cluster client configuration") return config, err } - -// This changes the default logrus grpc logging to log OK messages at trace level -// The reason for doing this are: -// - Reduced logging -// - We only care about failures, so lets only log failures -// - We normally use these logs to work out who is calling us, however the Executor API is not public -// and is only called by other Armada components -func createLogrusLoggingOption() grpc_logrus.Option { - return grpc_logrus.WithLevels(func(code codes.Code) log.Level { - switch code { - case codes.OK: - return log.TraceLevel - default: - return grpc_logrus.DefaultCodeToLevel(code) - } - }) -} From 7999eccd68a00bc25a003084be099438e169ae7c Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Fri, 3 Jan 2025 16:39:21 +0000 Subject: [PATCH 06/58] wip --- internal/common/grpc/grpc.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index b8e3a022870..885ca2208ce 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -40,7 +40,6 @@ func CreateGrpcServer( authServices []auth.AuthService, tlsConfig configuration.TlsConfig, ) *grpc.Server { - authFunction := auth.CreateGrpcMiddlewareAuthFunction(auth.NewMultiAuthService(authServices)) srvMetrics := setupPromMetrics() From f98b23cbdcf9eada565a9c291a8da10cfec6b60b Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Fri, 3 Jan 2025 16:54:47 +0000 Subject: [PATCH 07/58] supress logging --- internal/common/grpc/grpc.go | 5 ++++- internal/scheduler/schedulerapp.go | 21 ++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index 885ca2208ce..9c63757c5a0 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -39,11 +39,14 @@ func CreateGrpcServer( keepaliveEnforcementPolicy keepalive.EnforcementPolicy, authServices []auth.AuthService, tlsConfig configuration.TlsConfig, + loggerOpts ...grpc_logging.Option, ) *grpc.Server { authFunction := auth.CreateGrpcMiddlewareAuthFunction(auth.NewMultiAuthService(authServices)) srvMetrics := setupPromMetrics() - loggerOpts := []grpc_logging.Option{grpc_logging.WithLogOnEvents(grpc_logging.StartCall, grpc_logging.FinishCall)} + loggerOpts = append( + loggerOpts, + grpc_logging.WithLogOnEvents(grpc_logging.StartCall, grpc_logging.FinishCall)) return grpc.NewServer( grpc.KeepaliveParams(keepaliveParams), diff --git a/internal/scheduler/schedulerapp.go b/internal/scheduler/schedulerapp.go index 4779b10657d..a9016d00944 100644 --- a/internal/scheduler/schedulerapp.go +++ b/internal/scheduler/schedulerapp.go @@ -2,6 +2,7 @@ package scheduler import ( "fmt" + "google.golang.org/grpc/codes" "net" "net/http" _ "net/http/pprof" @@ -46,6 +47,7 @@ import ( "github.com/armadaproject/armada/pkg/armadaevents" "github.com/armadaproject/armada/pkg/client" "github.com/armadaproject/armada/pkg/executorapi" + grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" ) // Run sets up a Scheduler application and runs it until a SIGTERM is received @@ -181,7 +183,7 @@ func Run(config schedulerconfig.Configuration) error { if err != nil { return errors.WithMessage(err, "error creating auth services") } - grpcServer := grpcCommon.CreateGrpcServer(config.Grpc.KeepaliveParams, config.Grpc.KeepaliveEnforcementPolicy, authServices, config.Grpc.Tls) + grpcServer := grpcCommon.CreateGrpcServer(config.Grpc.KeepaliveParams, config.Grpc.KeepaliveEnforcementPolicy, authServices, config.Grpc.Tls, createGrpcLoggingOption()) defer grpcServer.GracefulStop() lis, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Grpc.Port)) if err != nil { @@ -360,3 +362,20 @@ func loadClusterConfig(ctx *armadacontext.Context) (*rest.Config, error) { ctx.Info("Running with in cluster client configuration") return config, err } + +// This changes the default logrus grpc logging to log OK messages at trace level +// The reason for doing this are: +// - Reduced logging +// - We only care about failures, so lets only log failures +// - We normally use these logs to work out who is calling us, however the Executor API is not public +// and is only called by other Armada components +func createGrpcLoggingOption() grpc_logging.Option { + return grpc_logging.WithLevels(func(code codes.Code) grpc_logging.Level { + switch code { + case codes.OK: + return grpc_logging.LevelDebug + default: + return grpc_logging.DefaultServerCodeToLevel(code) + } + }) +} From 60c04fbb69a9dbff35d1363ef8bc35bfa7943fe0 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Fri, 3 Jan 2025 16:55:29 +0000 Subject: [PATCH 08/58] lint --- internal/scheduler/schedulerapp.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/scheduler/schedulerapp.go b/internal/scheduler/schedulerapp.go index a9016d00944..9bbd0a01b53 100644 --- a/internal/scheduler/schedulerapp.go +++ b/internal/scheduler/schedulerapp.go @@ -2,7 +2,6 @@ package scheduler import ( "fmt" - "google.golang.org/grpc/codes" "net" "net/http" _ "net/http/pprof" @@ -11,9 +10,11 @@ import ( "github.com/apache/pulsar-client-go/pulsar" "github.com/google/uuid" + grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" + "google.golang.org/grpc/codes" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -47,7 +48,6 @@ import ( "github.com/armadaproject/armada/pkg/armadaevents" "github.com/armadaproject/armada/pkg/client" "github.com/armadaproject/armada/pkg/executorapi" - grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" ) // Run sets up a Scheduler application and runs it until a SIGTERM is received From 4be8a0a7b1ef4cbfa824a3dcb1d156562cecfb09 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 11:29:16 +0000 Subject: [PATCH 09/58] lint --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index a650830b832..ec10e0dda13 100644 --- a/go.mod +++ b/go.mod @@ -196,6 +196,7 @@ require ( go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect golang.org/x/crypto v0.28.0 // indirect golang.org/x/mod v0.21.0 // indirect golang.org/x/sys v0.26.0 // indirect diff --git a/go.sum b/go.sum index ac75f1140ad..04393865913 100644 --- a/go.sum +++ b/go.sum @@ -619,6 +619,8 @@ go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9i go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= From e1ced01335b828b38801891aa6ee68df344b86c8 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 12:57:12 +0000 Subject: [PATCH 10/58] wip --- internal/binoculars/server.go | 4 +- internal/binoculars/service/cordon_test.go | 4 +- internal/binoculars/service/logs.go | 2 +- .../common/armadacontext/armada_context.go | 100 ++++++++--- .../armadacontext/armada_context_test.go | 160 +++++++++--------- internal/common/certs/cached_certificate.go | 3 +- internal/common/cluster/kubernetes_client.go | 2 +- internal/common/etcdhealth/etcdhealth.go | 5 +- internal/common/etcdhealth/etcdhealth_test.go | 4 +- internal/common/eventutil/eventutil.go | 2 +- .../common/grpc/armadacontext/interceptors.go | 9 +- internal/common/grpc/gateway.go | 2 +- internal/common/grpc/grpc.go | 8 +- internal/common/health/http_handler.go | 2 +- .../common/healthmonitor/healthmonitor.go | 4 +- .../healthmonitor/manualhealthmonitor.go | 4 +- .../healthmonitor/multihealthmonitor.go | 4 +- internal/common/ingest/batch.go | 2 +- .../common/ingest/controlplaneevents/utils.go | 4 +- internal/common/ingest/ingestion_pipeline.go | 2 +- internal/common/ingest/jobsetevents/utils.go | 5 +- internal/common/ingest/retry.go | 2 +- internal/common/logging/formatter.go | 13 -- internal/common/logging/formatter_test.go | 24 --- internal/common/logging/logging.go | 86 +++++++++- internal/common/logging/null_logger.go | 12 +- internal/common/logging/stacktrace.go | 9 +- internal/common/metrics/provider.go | 8 +- internal/common/profiling/http.go | 4 +- internal/common/startup.go | 8 +- internal/eventingester/convert/conversions.go | 2 +- internal/eventingester/ingester.go | 2 +- internal/eventingester/store/eventstore.go | 5 +- internal/executor/application.go | 8 +- internal/executor/context/cluster_context.go | 2 +- internal/executor/fake/application.go | 5 +- internal/executor/fake/context/context.go | 2 +- internal/executor/job/job_run_state_store.go | 2 +- .../executor/job/processors/preempt_runs.go | 2 +- .../executor/job/processors/remove_runs.go | 2 +- internal/executor/job/submit.go | 2 +- .../metrics/pod_metrics/cluster_context.go | 2 +- .../podchecks/container_state_checks.go | 6 +- internal/executor/podchecks/event_checks.go | 2 +- internal/executor/podchecks/pod_checks.go | 2 +- .../executor/reporter/job_event_reporter.go | 5 +- .../executor/service/cluster_allocation.go | 5 +- internal/executor/service/job_requester.go | 2 +- internal/executor/service/lease_requester.go | 2 +- .../executor/service/pod_issue_handler.go | 2 +- internal/executor/service/resource_cleanup.go | 2 +- internal/executor/util/pod_util.go | 2 +- .../utilisation/cluster_utilisation.go | 2 +- .../utilisation/job_utilisation_reporter.go | 2 +- .../executor/utilisation/pod_utilisation.go | 2 +- .../pod_utilisation_custom_metrics.go | 2 +- .../pod_utilisation_kubelet_metrics.go | 5 +- .../utilisation/prometheus_scraping.go | 7 +- .../lookoutingesterv2/dbloadtester/queue.go | 2 +- .../dbloadtester/simulator.go | 2 +- internal/lookoutingesterv2/ingester.go | 2 +- .../instructions/instructions.go | 2 +- .../lookoutingesterv2/lookoutdb/insertion.go | 2 +- internal/lookoutv2/application.go | 13 +- internal/lookoutv2/generate/main.go | 2 +- internal/lookoutv2/pruner/pruner.go | 2 +- internal/lookoutv2/repository/getjoberror.go | 2 +- .../repository/getjobrundebugmessage.go | 2 +- .../lookoutv2/repository/getjobrunerror.go | 2 +- internal/lookoutv2/repository/getjobspec.go | 2 +- internal/lookoutv2/repository/querybuilder.go | 4 +- internal/lookoutv2/repository/util.go | 18 +- internal/scheduler/api.go | 2 +- internal/scheduler/nodedb/nodeiteration.go | 2 +- internal/scheduler/schedulerapp.go | 4 +- internal/scheduler/scheduling/context/job.go | 4 +- .../scheduler/scheduling/scheduling_algo.go | 6 +- internal/scheduleringester/ingester.go | 2 +- internal/scheduleringester/instructions.go | 2 +- .../server/event/conversion/conversions.go | 3 +- internal/server/event/event.go | 2 +- internal/server/event/event_repository.go | 2 +- internal/server/server.go | 2 +- pkg/client/auth/kubernetes/authentication.go | 2 +- pkg/client/auth/oidc/kubernetes.go | 4 +- pkg/client/auth/oidc/pkce.go | 11 +- pkg/client/load-test.go | 11 +- pkg/client/queue/get_all.go | 12 +- pkg/client/watch.go | 2 +- 89 files changed, 393 insertions(+), 321 deletions(-) delete mode 100644 internal/common/logging/formatter.go delete mode 100644 internal/common/logging/formatter_test.go diff --git a/internal/binoculars/server.go b/internal/binoculars/server.go index bf14abc18f2..f11ccf7ff69 100644 --- a/internal/binoculars/server.go +++ b/internal/binoculars/server.go @@ -5,7 +5,6 @@ import ( "sync" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/binoculars/configuration" "github.com/armadaproject/armada/internal/binoculars/server" @@ -13,7 +12,8 @@ import ( "github.com/armadaproject/armada/internal/common/auth" "github.com/armadaproject/armada/internal/common/cluster" grpcCommon "github.com/armadaproject/armada/internal/common/grpc" - "github.com/armadaproject/armada/pkg/api/binoculars" + log "github.com/armadaproject/armada/internal/common/logging" + ) func StartUp(config *configuration.BinocularsConfig) (func(), *sync.WaitGroup) { diff --git a/internal/binoculars/service/cordon_test.go b/internal/binoculars/service/cordon_test.go index 43197004d9e..91efa8e425f 100644 --- a/internal/binoculars/service/cordon_test.go +++ b/internal/binoculars/service/cordon_test.go @@ -4,9 +4,9 @@ import ( "context" "encoding/json" "fmt" + "github.com/armadaproject/armada/internal/common/logging" "testing" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc/codes" @@ -81,7 +81,7 @@ func TestCordonNode(t *testing.T) { cordonService, client := setupTest(t, cordonConfig, FakePermissionChecker{ReturnValue: true}) ctx := auth.WithPrincipal(context.Background(), principal) - err := cordonService.CordonNode(armadacontext.New(ctx, logrus.NewEntry(logrus.New())), &binoculars.CordonRequest{ + err := cordonService.CordonNode(armadacontext.New(ctx, logging.NewLogger()), &binoculars.CordonRequest{ NodeName: defaultNode.Name, }) assert.Nil(t, err) diff --git a/internal/binoculars/service/logs.go b/internal/binoculars/service/logs.go index 8bc0f38d1be..34ca6b417ce 100644 --- a/internal/binoculars/service/logs.go +++ b/internal/binoculars/service/logs.go @@ -5,13 +5,13 @@ import ( "strings" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/auth" "github.com/armadaproject/armada/internal/common/cluster" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/pkg/api/binoculars" ) diff --git a/internal/common/armadacontext/armada_context.go b/internal/common/armadacontext/armada_context.go index 5515ebe1f14..b496660c092 100644 --- a/internal/common/armadacontext/armada_context.go +++ b/internal/common/armadacontext/armada_context.go @@ -4,48 +4,98 @@ import ( "context" "time" - "github.com/sirupsen/logrus" "golang.org/x/sync/errgroup" + + "github.com/armadaproject/armada/internal/common/logging" ) // Context is an extension of Go's context which also includes a logger. This allows us to pass round a contextual logger // while retaining type-safety type Context struct { context.Context - logrus.FieldLogger + Logger *logging.Logger +} + +// Debug logs a message at level Debug +func (ctx *Context) Debug(msg string) { + ctx.Logger.Debug(msg) +} + +// Info logs a message at level Info +func (ctx *Context) Info(msg string) { + ctx.Logger.Info(msg) +} + +// Warn logs a message at level Warn +func (ctx *Context) Warn(msg string) { + ctx.Logger.Warn(msg) +} + +// Error logs a message at level Error +func (ctx *Context) Error(msg string) { + ctx.Logger.Error(msg) +} + +// Panic logs a message at level Panic +func (ctx *Context) Panic(msg string) { + ctx.Logger.Panic(msg) +} + +// Fatal logs a message at level Fatal then the process will exit with status set to 1. +func (ctx *Context) Fatal(msg string) { + ctx.Logger.Fatal(msg) +} + +// Debugf logs a message at level Debug. +func (ctx *Context) Debugf(format string, args ...interface{}) { + ctx.Logger.Debugf(format, args...) +} + +// Infof logs a message at level Info. +func (ctx *Context) Infof(format string, args ...interface{}) { + ctx.Logger.Infof(format, args...) +} + +// Warnf logs a message at level Warn. +func (ctx *Context) Warnf(format string, args ...interface{}) { + ctx.Logger.Warnf(format, args...) +} + +// Errorf logs a message at level Error. +func (ctx *Context) Errorf(format string, args ...interface{}) { + ctx.Logger.Errorf(format, args...) } // Background creates an empty context with a default logger. It is analogous to context.Background() func Background() *Context { return &Context{ - Context: context.Background(), - FieldLogger: logrus.NewEntry(logrus.StandardLogger()), + Context: context.Background(), + Logger: logging.NewLogger(), } } // TODO creates an empty context with a default logger. It is analogous to context.TODO() func TODO() *Context { return &Context{ - Context: context.TODO(), - FieldLogger: logrus.NewEntry(logrus.StandardLogger()), + Context: context.TODO(), + Logger: logging.NewLogger(), } } -// FromGrpcCtx creates a context where the logger is extracted via ctxlogrus's Extract() method. -// Note that this will result in a no-op logger if a logger hasn't already been inserted into the context via ctxlogrus +// FromGrpcCtx Converts a context.Context to an armadacontext.Context func FromGrpcCtx(ctx context.Context) *Context { armadaCtx, ok := ctx.(*Context) if ok { return armadaCtx } - return New(ctx, logrus.NewEntry(logrus.StandardLogger())) + return New(ctx, logging.NewLogger()) } // New returns an armada context that encapsulates both a go context and a logger -func New(ctx context.Context, log *logrus.Entry) *Context { +func New(ctx context.Context, log *logging.Logger) *Context { return &Context{ - Context: ctx, - FieldLogger: log, + Context: ctx, + Logger: log, } } @@ -53,8 +103,8 @@ func New(ctx context.Context, log *logrus.Entry) *Context { func WithCancel(parent *Context) (*Context, context.CancelFunc) { c, cancel := context.WithCancel(parent.Context) return &Context{ - Context: c, - FieldLogger: parent.FieldLogger, + Context: c, + Logger: parent.Logger, }, cancel } @@ -63,8 +113,8 @@ func WithCancel(parent *Context) (*Context, context.CancelFunc) { func WithDeadline(parent *Context, d time.Time) (*Context, context.CancelFunc) { c, cancel := context.WithDeadline(parent.Context, d) return &Context{ - Context: c, - FieldLogger: parent.FieldLogger, + Context: c, + Logger: parent.Logger, }, cancel } @@ -76,16 +126,16 @@ func WithTimeout(parent *Context, timeout time.Duration) (*Context, context.Canc // WithLogField returns a copy of parent with the supplied key-value added to the logger func WithLogField(parent *Context, key string, val interface{}) *Context { return &Context{ - Context: parent.Context, - FieldLogger: parent.FieldLogger.WithField(key, val), + Context: parent.Context, + Logger: parent.Logger.With(key, val), } } // WithLogFields returns a copy of parent with the supplied key-values added to the logger -func WithLogFields(parent *Context, fields logrus.Fields) *Context { +func WithLogFields(parent *Context, fields map[string]any) *Context { return &Context{ - Context: parent.Context, - FieldLogger: parent.FieldLogger.WithFields(fields), + Context: parent.Context, + Logger: parent.Logger.With(fields), } } @@ -93,8 +143,8 @@ func WithLogFields(parent *Context, fields logrus.Fields) *Context { // val. It is analogous to context.WithValue() func WithValue(parent *Context, key, val any) *Context { return &Context{ - Context: context.WithValue(parent, key, val), - FieldLogger: parent.FieldLogger, + Context: context.WithValue(parent, key, val), + Logger: parent.Logger, } } @@ -103,7 +153,7 @@ func WithValue(parent *Context, key, val any) *Context { func ErrGroup(ctx *Context) (*errgroup.Group, *Context) { group, goctx := errgroup.WithContext(ctx) return group, &Context{ - Context: goctx, - FieldLogger: ctx.FieldLogger, + Context: goctx, + Logger: ctx.Logger, } } diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index ef7c84aa705..5ae785a731d 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -1,80 +1,80 @@ -package armadacontext - -import ( - "context" - "testing" - "time" - - "github.com/sirupsen/logrus" - "github.com/stretchr/testify/require" -) - -var defaultLogger = logrus.WithField("foo", "bar") - -func TestNew(t *testing.T) { - ctx := New(context.Background(), defaultLogger) - require.Equal(t, defaultLogger, ctx.FieldLogger) - require.Equal(t, context.Background(), ctx.Context) -} - -func TestBackground(t *testing.T) { - ctx := Background() - require.Equal(t, ctx.Context, context.Background()) -} - -func TestTODO(t *testing.T) { - ctx := TODO() - require.Equal(t, ctx.Context, context.TODO()) -} - -func TestWithLogField(t *testing.T) { - ctx := WithLogField(Background(), "fish", "chips") - require.Equal(t, context.Background(), ctx.Context) - require.Equal(t, logrus.Fields{"fish": "chips"}, ctx.FieldLogger.(*logrus.Entry).Data) -} - -func TestWithLogFields(t *testing.T) { - ctx := WithLogFields(Background(), logrus.Fields{"fish": "chips", "salt": "pepper"}) - require.Equal(t, context.Background(), ctx.Context) - require.Equal(t, logrus.Fields{"fish": "chips", "salt": "pepper"}, ctx.FieldLogger.(*logrus.Entry).Data) -} - -func TestWithTimeout(t *testing.T) { - ctx, _ := WithTimeout(Background(), 100*time.Millisecond) - testDeadline(t, ctx) -} - -func TestWithDeadline(t *testing.T) { - ctx, _ := WithDeadline(Background(), time.Now().Add(100*time.Millisecond)) - testDeadline(t, ctx) -} - -func TestWithValue(t *testing.T) { - ctx := WithValue(Background(), "foo", "bar") - require.Equal(t, "bar", ctx.Value("foo")) -} - -func testDeadline(t *testing.T, c *Context) { - t.Helper() - d := quiescent(t) - timer := time.NewTimer(d) - defer timer.Stop() - select { - case <-timer.C: - t.Fatalf("context not timed out after %v", d) - case <-c.Done(): - } - if e := c.Err(); e != context.DeadlineExceeded { - t.Errorf("c.Err() == %v; want %v", e, context.DeadlineExceeded) - } -} - -func quiescent(t *testing.T) time.Duration { - deadline, ok := t.Deadline() - if !ok { - return 5 * time.Second - } - - const arbitraryCleanupMargin = 1 * time.Second - return time.Until(deadline) - arbitraryCleanupMargin -} +//package armadacontext +// +//import ( +// "context" +// "testing" +// "time" +// +// "github.com/sirupsen/logrus" +// "github.com/stretchr/testify/require" +//) +// +//var defaultLogger = logrus.WithField("foo", "bar") +// +//func TestNew(t *testing.T) { +// ctx := New(context.Background(), defaultLogger) +// require.Equal(t, defaultLogger, ctx.FieldLogger) +// require.Equal(t, context.Background(), ctx.Context) +//} +// +//func TestBackground(t *testing.T) { +// ctx := Background() +// require.Equal(t, ctx.Context, context.Background()) +//} +// +//func TestTODO(t *testing.T) { +// ctx := TODO() +// require.Equal(t, ctx.Context, context.TODO()) +//} +// +//func TestWithLogField(t *testing.T) { +// ctx := WithLogField(Background(), "fish", "chips") +// require.Equal(t, context.Background(), ctx.Context) +// require.Equal(t, logrus.Fields{"fish": "chips"}, ctx.FieldLogger.(*logrus.Entry).Data) +//} +// +//func TestWithLogFields(t *testing.T) { +// ctx := WithLogFields(Background(), logrus.Fields{"fish": "chips", "salt": "pepper"}) +// require.Equal(t, context.Background(), ctx.Context) +// require.Equal(t, logrus.Fields{"fish": "chips", "salt": "pepper"}, ctx.FieldLogger.(*logrus.Entry).Data) +//} +// +//func TestWithTimeout(t *testing.T) { +// ctx, _ := WithTimeout(Background(), 100*time.Millisecond) +// testDeadline(t, ctx) +//} +// +//func TestWithDeadline(t *testing.T) { +// ctx, _ := WithDeadline(Background(), time.Now().Add(100*time.Millisecond)) +// testDeadline(t, ctx) +//} +// +//func TestWithValue(t *testing.T) { +// ctx := WithValue(Background(), "foo", "bar") +// require.Equal(t, "bar", ctx.Value("foo")) +//} +// +//func testDeadline(t *testing.T, c *Context) { +// t.Helper() +// d := quiescent(t) +// timer := time.NewTimer(d) +// defer timer.Stop() +// select { +// case <-timer.C: +// t.Fatalf("context not timed out after %v", d) +// case <-c.Done(): +// } +// if e := c.Err(); e != context.DeadlineExceeded { +// t.Errorf("c.Err() == %v; want %v", e, context.DeadlineExceeded) +// } +//} +// +//func quiescent(t *testing.T) time.Duration { +// deadline, ok := t.Deadline() +// if !ok { +// return 5 * time.Second +// } +// +// const arbitraryCleanupMargin = 1 * time.Second +// return time.Until(deadline) - arbitraryCleanupMargin +//} diff --git a/internal/common/certs/cached_certificate.go b/internal/common/certs/cached_certificate.go index 72b7f6ea250..10ea5ea27da 100644 --- a/internal/common/certs/cached_certificate.go +++ b/internal/common/certs/cached_certificate.go @@ -6,9 +6,8 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" - "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" ) type CachedCertificateService struct { diff --git a/internal/common/cluster/kubernetes_client.go b/internal/common/cluster/kubernetes_client.go index 1c280fa55a6..9af32096d6e 100644 --- a/internal/common/cluster/kubernetes_client.go +++ b/internal/common/cluster/kubernetes_client.go @@ -2,13 +2,13 @@ package cluster import ( "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/flowcontrol" "github.com/armadaproject/armada/internal/common/armadaerrors" + log "github.com/armadaproject/armada/internal/common/logging" ) type KubernetesClientProvider interface { diff --git a/internal/common/etcdhealth/etcdhealth.go b/internal/common/etcdhealth/etcdhealth.go index 49be27a22fe..09f191ac4e4 100644 --- a/internal/common/etcdhealth/etcdhealth.go +++ b/internal/common/etcdhealth/etcdhealth.go @@ -6,7 +6,6 @@ import ( "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" - "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/healthmonitor" @@ -184,8 +183,8 @@ func (srv *EtcdReplicaHealthMonitor) sizeFraction() float64 { return srv.etcdSizeBytes / srv.etcdCapacityBytes } -func (srv *EtcdReplicaHealthMonitor) Run(ctx *armadacontext.Context, log *logrus.Entry) error { - log = log.WithField("service", "EtcdHealthMonitor") +func (srv *EtcdReplicaHealthMonitor) Run(ctx *armadacontext.Context, log *logging.Logger) error { + log = log.With("service", "EtcdHealthMonitor") log.Info("starting etcd health monitor") defer log.Info("stopping etcd health monitor") ticker := time.NewTicker(srv.scrapeInterval) diff --git a/internal/common/etcdhealth/etcdhealth_test.go b/internal/common/etcdhealth/etcdhealth_test.go index 474d4df0e3a..0a38def34ee 100644 --- a/internal/common/etcdhealth/etcdhealth_test.go +++ b/internal/common/etcdhealth/etcdhealth_test.go @@ -4,11 +4,11 @@ import ( "testing" "time" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/healthmonitor" + "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/metrics" ) @@ -26,7 +26,7 @@ func TestEtcdReplicaHealthMonitor(t *testing.T) { ctx, cancel := armadacontext.WithCancel(armadacontext.Background()) defer cancel() g, ctx := armadacontext.ErrGroup(ctx) - g.Go(func() error { return hm.Run(ctx, logrus.NewEntry(logrus.New())) }) + g.Go(func() error { return hm.Run(ctx, logging.NewLogger()) }) // Should still be unavailable due to missing metrics. hm.BlockUntilNextMetricsCollection(ctx) diff --git a/internal/common/eventutil/eventutil.go b/internal/common/eventutil/eventutil.go index 27fc7ef6f55..fa126b155e5 100644 --- a/internal/common/eventutil/eventutil.go +++ b/internal/common/eventutil/eventutil.go @@ -6,13 +6,13 @@ import ( "github.com/gogo/protobuf/proto" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" networking "k8s.io/api/networking/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "github.com/armadaproject/armada/internal/common/armadaerrors" + log "github.com/armadaproject/armada/internal/common/logging" protoutil "github.com/armadaproject/armada/internal/common/proto" "github.com/armadaproject/armada/internal/common/slices" "github.com/armadaproject/armada/pkg/api" diff --git a/internal/common/grpc/armadacontext/interceptors.go b/internal/common/grpc/armadacontext/interceptors.go index 9622e6791a5..d2ead0ba373 100644 --- a/internal/common/grpc/armadacontext/interceptors.go +++ b/internal/common/grpc/armadacontext/interceptors.go @@ -4,11 +4,11 @@ import ( "context" grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" - "github.com/sirupsen/logrus" "google.golang.org/grpc" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/auth" + "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/requestid" ) @@ -29,9 +29,8 @@ func StreamServerInterceptor() grpc.StreamServerInterceptor { } func createArmadaCtx(ctx context.Context) *armadacontext.Context { - log := logrus.WithFields(map[string]interface{}{ - "requestId": requestid.FromContextOrMissing(ctx), - "user": auth.GetPrincipal(ctx).GetName(), - }) + log := logging.NewLogger(). + With("requestId", requestid.FromContextOrMissing(ctx)). + With("user", auth.GetPrincipal(ctx).GetName()) return armadacontext.New(ctx, log) } diff --git a/internal/common/grpc/gateway.go b/internal/common/grpc/gateway.go index 8b6e59dc2ad..66f8f3d0c99 100644 --- a/internal/common/grpc/gateway.go +++ b/internal/common/grpc/gateway.go @@ -8,9 +8,9 @@ import ( "path" "strings" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/go-openapi/runtime/middleware" "github.com/grpc-ecosystem/grpc-gateway/runtime" - log "github.com/sirupsen/logrus" "golang.org/x/exp/slices" "google.golang.org/grpc" "google.golang.org/grpc/credentials" diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index 9c63757c5a0..34e0b06bbd2 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -15,7 +15,6 @@ import ( grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/recovery" "github.com/prometheus/client_golang/prometheus" - log "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/credentials" @@ -29,6 +28,7 @@ import ( "github.com/armadaproject/armada/internal/common/certs" grpc_armadacontext "github.com/armadaproject/armada/internal/common/grpc/armadacontext" "github.com/armadaproject/armada/internal/common/grpc/configuration" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/requestid" ) @@ -92,9 +92,9 @@ func Listen(port uint16, grpcServer *grpc.Server, wg *sync.WaitGroup) { } go func() { - defer log.Println("Stopping server.") + defer log.Infof("Stopping server.") - log.Printf("Grpc listening on %d", port) + log.Infof("Grpc listening on %d", port) if err := grpcServer.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } @@ -153,7 +153,7 @@ func InterceptorLogger() grpc_logging.Logger { k, v := i.At() logFields[k] = v } - l := armadaCtx.WithFields(logFields) + l := armadaCtx.Logger.With(logFields) switch lvl { case grpc_logging.LevelDebug: l.Debug(msg) diff --git a/internal/common/health/http_handler.go b/internal/common/health/http_handler.go index 5dd2c9b5444..38b1a0f1f82 100644 --- a/internal/common/health/http_handler.go +++ b/internal/common/health/http_handler.go @@ -3,7 +3,7 @@ package health import ( "net/http" - log "github.com/sirupsen/logrus" + log "github.com/armadaproject/armada/internal/common/logging" ) // TODO Doesn't need to exist. Just give a Checker directly. diff --git a/internal/common/healthmonitor/healthmonitor.go b/internal/common/healthmonitor/healthmonitor.go index d5c6b151c1e..e81059fff58 100644 --- a/internal/common/healthmonitor/healthmonitor.go +++ b/internal/common/healthmonitor/healthmonitor.go @@ -2,9 +2,9 @@ package healthmonitor import ( "github.com/prometheus/client_golang/prometheus" - "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" + "github.com/armadaproject/armada/internal/common/logging" ) const ( @@ -25,5 +25,5 @@ type HealthMonitor interface { // Run initialises and starts the health checker. // Run may be blocking and should be run within a separate goroutine. // Must be called before IsHealthy() or any prometheus.Collector interface methods. - Run(*armadacontext.Context, *logrus.Entry) error + Run(*armadacontext.Context, *logging.Logger) error } diff --git a/internal/common/healthmonitor/manualhealthmonitor.go b/internal/common/healthmonitor/manualhealthmonitor.go index 7aa2f525068..163e8566656 100644 --- a/internal/common/healthmonitor/manualhealthmonitor.go +++ b/internal/common/healthmonitor/manualhealthmonitor.go @@ -4,9 +4,9 @@ import ( "sync" "github.com/prometheus/client_golang/prometheus" - "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" + "github.com/armadaproject/armada/internal/common/logging" ) // ManualHealthMonitor is a manually controlled health monitor. @@ -47,7 +47,7 @@ func (srv *ManualHealthMonitor) IsHealthy() (bool, string, error) { } } -func (srv *ManualHealthMonitor) Run(_ *armadacontext.Context, _ *logrus.Entry) error { +func (srv *ManualHealthMonitor) Run(_ *armadacontext.Context, _ *logging.Logger) error { return nil } diff --git a/internal/common/healthmonitor/multihealthmonitor.go b/internal/common/healthmonitor/multihealthmonitor.go index a9f03643d10..dc717d2ad75 100644 --- a/internal/common/healthmonitor/multihealthmonitor.go +++ b/internal/common/healthmonitor/multihealthmonitor.go @@ -2,11 +2,11 @@ package healthmonitor import ( "fmt" + "github.com/armadaproject/armada/internal/common/logging" "sync" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" - "github.com/sirupsen/logrus" "golang.org/x/exp/maps" "github.com/armadaproject/armada/internal/common/armadacontext" @@ -100,7 +100,7 @@ func (srv *MultiHealthMonitor) IsHealthy() (ok bool, reason string, err error) { } // Run initialises prometheus metrics and starts any child health checkers. -func (srv *MultiHealthMonitor) Run(ctx *armadacontext.Context, log *logrus.Entry) error { +func (srv *MultiHealthMonitor) Run(ctx *armadacontext.Context, log *logging.Logger) error { g, ctx := armadacontext.ErrGroup(ctx) for _, healthMonitor := range srv.healthMonitorsByName { healthMonitor := healthMonitor diff --git a/internal/common/ingest/batch.go b/internal/common/ingest/batch.go index 02a35f7490c..f2c4867ce10 100644 --- a/internal/common/ingest/batch.go +++ b/internal/common/ingest/batch.go @@ -4,10 +4,10 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" "k8s.io/utils/clock" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" ) // Batcher batches up events from a channel. Batches are created whenever maxItems have been diff --git a/internal/common/ingest/controlplaneevents/utils.go b/internal/common/ingest/controlplaneevents/utils.go index 13182165ba5..f388d5c30ae 100644 --- a/internal/common/ingest/controlplaneevents/utils.go +++ b/internal/common/ingest/controlplaneevents/utils.go @@ -2,12 +2,12 @@ package controlplaneevents import ( "github.com/apache/pulsar-client-go/pulsar" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/eventutil" commonmetrics "github.com/armadaproject/armada/internal/common/ingest/metrics" "github.com/armadaproject/armada/internal/common/ingest/utils" - "github.com/armadaproject/armada/pkg/controlplaneevents" + log "github.com/armadaproject/armada/internal/common/logging" + ) func MessageUnmarshaller(msg pulsar.ConsumerMessage, metrics *commonmetrics.Metrics) *utils.EventsWithIds[*controlplaneevents.Event] { diff --git a/internal/common/ingest/ingestion_pipeline.go b/internal/common/ingest/ingestion_pipeline.go index 160cf677268..a8f39e07ec6 100644 --- a/internal/common/ingest/ingestion_pipeline.go +++ b/internal/common/ingest/ingestion_pipeline.go @@ -7,12 +7,12 @@ import ( "github.com/apache/pulsar-client-go/pulsar" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" commonconfig "github.com/armadaproject/armada/internal/common/config" commonmetrics "github.com/armadaproject/armada/internal/common/ingest/metrics" "github.com/armadaproject/armada/internal/common/ingest/utils" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/pulsarutils" "github.com/armadaproject/armada/internal/common/util" ) diff --git a/internal/common/ingest/jobsetevents/utils.go b/internal/common/ingest/jobsetevents/utils.go index 0de21b7d27a..69942d1e3d3 100644 --- a/internal/common/ingest/jobsetevents/utils.go +++ b/internal/common/ingest/jobsetevents/utils.go @@ -2,13 +2,14 @@ package jobsetevents import ( "github.com/apache/pulsar-client-go/pulsar" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/eventutil" commonmetrics "github.com/armadaproject/armada/internal/common/ingest/metrics" "github.com/armadaproject/armada/internal/common/ingest/utils" + log "github.com/armadaproject/armada/internal/common/logging" + pr protoutil "github.com/armadaproject/armada/internal/common/proto" - "github.com/armadaproject/armada/pkg/armadaevents" + log "github.com/armadaproject/armada/internal/common/logging" ) func MessageUnmarshaller(msg pulsar.ConsumerMessage, metrics *commonmetrics.Metrics) *utils.EventsWithIds[*armadaevents.EventSequence] { diff --git a/internal/common/ingest/retry.go b/internal/common/ingest/retry.go index aa66a7c9166..810b0c346a2 100644 --- a/internal/common/ingest/retry.go +++ b/internal/common/ingest/retry.go @@ -3,7 +3,7 @@ package ingest import ( "time" - log "github.com/sirupsen/logrus" + log "github.com/armadaproject/armada/internal/common/logging" ) // WithRetry executes the supplied action until it either completes successfully or it returns false, indicating that diff --git a/internal/common/logging/formatter.go b/internal/common/logging/formatter.go deleted file mode 100644 index f6db9902424..00000000000 --- a/internal/common/logging/formatter.go +++ /dev/null @@ -1,13 +0,0 @@ -package logging - -import ( - "fmt" - - log "github.com/sirupsen/logrus" -) - -type CommandLineFormatter struct{} - -func (f *CommandLineFormatter) Format(entry *log.Entry) ([]byte, error) { - return []byte(fmt.Sprintf("%s\n", entry.Message)), nil -} diff --git a/internal/common/logging/formatter_test.go b/internal/common/logging/formatter_test.go deleted file mode 100644 index bf79fca89aa..00000000000 --- a/internal/common/logging/formatter_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package logging - -import ( - "testing" - - log "github.com/sirupsen/logrus" - "github.com/stretchr/testify/assert" -) - -func TestCommandLineFormatter(t *testing.T) { - commandLineFormatter := new(CommandLineFormatter) - - testMessage := "Test" - expectedOutput := testMessage + "\n" - - event := log.Entry{ - Message: testMessage, - } - - output, err := commandLineFormatter.Format(&event) - - assert.Nil(t, err) - assert.Equal(t, expectedOutput, string(output[:])) -} diff --git a/internal/common/logging/logging.go b/internal/common/logging/logging.go index 8351631cdd6..e3db5508adc 100644 --- a/internal/common/logging/logging.go +++ b/internal/common/logging/logging.go @@ -4,12 +4,84 @@ import ( "go.uber.org/zap" ) -// Debug logs a message at level Debug on the standard logger. +type Logger struct { + undlerlying *zap.SugaredLogger +} + +func (l *Logger) WithError(err error) *Logger { + return &Logger{ + undlerlying: zap.S().With("error", err.Error()), + } +} + +func (l *Logger) With(args ...any) *Logger { + return &Logger{ + undlerlying: zap.S().With(args), + } +} + +func NewLogger() *Logger { + return &Logger{ + undlerlying: zap.S(), + } +} + +// Debug logs a message at level Debug +func (l *Logger) Debug(msg string) { + l.undlerlying.Debug(msg) +} + +// Info logs a message at level Info +func (l *Logger) Info(msg string) { + l.undlerlying.Info(msg) +} + +// Warn logs a message at level Warn +func (l *Logger) Warn(msg string) { + l.undlerlying.Warn(msg) +} + +// Error logs a message at level Error +func (l *Logger) Error(msg string) { + l.undlerlying.Error(msg) +} + +// Panic logs a message at level Panic +func (l *Logger) Panic(msg string) { + l.undlerlying.Panic(msg) +} + +// Fatal logs a message at level Fatal then the process will exit with status set to 1. +func (l *Logger) Fatal(msg string) { + l.undlerlying.Fatal(msg) +} + +// Debugf logs a message at level Debug. +func (l *Logger) Debugf(format string, args ...interface{}) { + l.undlerlying.Debugf(format, args...) +} + +// Infof logs a message at level Info. +func (l *Logger) Infof(format string, args ...interface{}) { + zap.S().Infof(format, args...) +} + +// Warnf logs a message at level Warn. +func (l *Logger) Warnf(format string, args ...interface{}) { + zap.S().Warnf(format, args...) +} + +// Errorf logs a message at level Error. +func (l *Logger) Errorf(format string, args ...interface{}) { + zap.S().Errorf(format, args...) +} + +// Debug logs a message at level Debug. func Debug(msg string) { zap.S().Debug(msg) } -// Info logs a message at level Info on the standard logger. +// Info logs a message at level Info. func Info(msg string) { zap.S().Info(msg) } @@ -64,6 +136,12 @@ func Fatalf(format string, args ...interface{}) { zap.S().Fatalf(format, args...) } -func WithError(err error) *zap.SugaredLogger { - return zap.S().With("error", err) +func With(args ...any) *Logger { + return &Logger{ + undlerlying: zap.S().With(args), + } +} + +func WithError(err error) *Logger { + return With("error", err.Error()) } diff --git a/internal/common/logging/null_logger.go b/internal/common/logging/null_logger.go index 25326d3691b..5cb5370902f 100644 --- a/internal/common/logging/null_logger.go +++ b/internal/common/logging/null_logger.go @@ -1,14 +1,10 @@ package logging import ( - "io" - - "github.com/sirupsen/logrus" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) -var NullLogger = &logrus.Logger{ - Out: io.Discard, - Formatter: new(logrus.TextFormatter), - Hooks: make(logrus.LevelHooks), - Level: logrus.PanicLevel, +var NullLogger = &Logger{ + undlerlying: zap.New(zapcore.NewNopCore()).Sugar(), } diff --git a/internal/common/logging/stacktrace.go b/internal/common/logging/stacktrace.go index 7d546915b31..612fcc982a7 100644 --- a/internal/common/logging/stacktrace.go +++ b/internal/common/logging/stacktrace.go @@ -2,7 +2,6 @@ package logging import ( "github.com/pkg/errors" - "github.com/sirupsen/logrus" ) // Unexported but considered part of the stable interface of pkg/errors. @@ -10,12 +9,12 @@ type stackTracer interface { StackTrace() errors.StackTrace } -// WithStacktrace returns a new logrus.FieldLogger obtained by adding error information and, if available, a stack trace -// as fields to the provided logrus.FieldLogger. -func WithStacktrace(logger logrus.FieldLogger, err error) logrus.FieldLogger { +// WithStacktrace returns a new Logger obtained by adding error information and, if available, a stack trace +// as fields +func WithStacktrace(logger *Logger, err error) *Logger { logger = logger.WithError(err) if stackErr, ok := err.(stackTracer); ok { - return logger.WithField("stacktrace", stackErr.StackTrace()) + return logger.With("stacktrace", stackErr.StackTrace()) } else { return logger } diff --git a/internal/common/metrics/provider.go b/internal/common/metrics/provider.go index 7280f461fa0..63d82021086 100644 --- a/internal/common/metrics/provider.go +++ b/internal/common/metrics/provider.go @@ -9,11 +9,11 @@ import ( "sync" "time" - "github.com/sirupsen/logrus" + "github.com/armadaproject/armada/internal/common/logging" ) type MetricsProvider interface { - Collect(context.Context, *logrus.Entry) (map[string]float64, error) + Collect(context.Context, *logging.Logger) (map[string]float64, error) } type ManualMetricsProvider struct { @@ -36,7 +36,7 @@ func (srv *ManualMetricsProvider) WithCollectionDelay(d time.Duration) *ManualMe return srv } -func (srv *ManualMetricsProvider) Collect(_ context.Context, _ *logrus.Entry) (map[string]float64, error) { +func (srv *ManualMetricsProvider) Collect(_ context.Context, _ *logging.Logger) (map[string]float64, error) { srv.mu.Lock() defer srv.mu.Unlock() if srv.collectionDelay != 0 { @@ -61,7 +61,7 @@ func NewHttpMetricsProvider(url string, client *http.Client) *HttpMetricsProvide } } -func (srv *HttpMetricsProvider) Collect(ctx context.Context, _ *logrus.Entry) (map[string]float64, error) { +func (srv *HttpMetricsProvider) Collect(ctx context.Context, _ *logging.Logger) (map[string]float64, error) { req, err := http.NewRequestWithContext(ctx, "GET", srv.url, nil) if err != nil { return nil, err diff --git a/internal/common/profiling/http.go b/internal/common/profiling/http.go index 5557ef190c4..686edd46477 100644 --- a/internal/common/profiling/http.go +++ b/internal/common/profiling/http.go @@ -6,12 +6,12 @@ import ( "net/http" _ "net/http/pprof" - log "github.com/sirupsen/logrus" "golang.org/x/sync/errgroup" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/auth" "github.com/armadaproject/armada/internal/common/logging" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling/configuration" "github.com/armadaproject/armada/internal/common/serve" ) @@ -54,7 +54,7 @@ func SetupPprof(config *configuration.ProfilingConfig, ctx *armadacontext.Contex serveFunc := func() error { if err := serve.ListenAndServe(ctx, pprofServer); err != nil { - logging.WithStacktrace(ctx, err).Error("pprof server failure") + logging.WithStacktrace(ctx.Logger, err).Error("pprof server failure") } return err } diff --git a/internal/common/startup.go b/internal/common/startup.go index 535fb628c5e..0196e9e570d 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -18,14 +18,14 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" - log "github.com/sirupsen/logrus" + "github.com/spf13/pflag" "github.com/spf13/viper" "github.com/weaveworks/promrus" "github.com/armadaproject/armada/internal/common/armadacontext" commonconfig "github.com/armadaproject/armada/internal/common/config" - "github.com/armadaproject/armada/internal/common/logging" + log "github.com/armadaproject/armada/internal/common/logging" ) const baseConfigFileName = "config" @@ -36,7 +36,7 @@ const logTimestampFormat = "2006-01-02T15:04:05.999Z07:00" func BindCommandlineArguments() { err := viper.BindPFlags(pflag.CommandLine) if err != nil { - log.Error() + log.Error(err.Error()) os.Exit(-1) } } @@ -72,7 +72,7 @@ func LoadConfig(config commonconfig.Config, defaultPath string, overrideConfigs var metadata mapstructure.Metadata customHooks := append(slices.Clone(commonconfig.CustomHooks), func(c *mapstructure.DecoderConfig) { c.Metadata = &metadata }) if err := v.Unmarshal(config, customHooks...); err != nil { - log.Error(err) + log.Error(err.Error()) os.Exit(-1) } diff --git a/internal/eventingester/convert/conversions.go b/internal/eventingester/convert/conversions.go index 17f69384f40..cbbd629419e 100644 --- a/internal/eventingester/convert/conversions.go +++ b/internal/eventingester/convert/conversions.go @@ -3,7 +3,6 @@ package convert import ( "github.com/gogo/protobuf/proto" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/compress" @@ -11,6 +10,7 @@ import ( "github.com/armadaproject/armada/internal/common/ingest" "github.com/armadaproject/armada/internal/common/ingest/metrics" "github.com/armadaproject/armada/internal/common/ingest/utils" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/eventingester/model" "github.com/armadaproject/armada/pkg/armadaevents" ) diff --git a/internal/eventingester/ingester.go b/internal/eventingester/ingester.go index b6b7c207cbc..bf0185665fb 100644 --- a/internal/eventingester/ingester.go +++ b/internal/eventingester/ingester.go @@ -7,7 +7,6 @@ import ( "github.com/apache/pulsar-client-go/pulsar" "github.com/pkg/errors" "github.com/redis/go-redis/v9" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/app" @@ -15,6 +14,7 @@ import ( "github.com/armadaproject/armada/internal/common/compress" "github.com/armadaproject/armada/internal/common/ingest" "github.com/armadaproject/armada/internal/common/ingest/jobsetevents" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/internal/eventingester/configuration" "github.com/armadaproject/armada/internal/eventingester/convert" diff --git a/internal/eventingester/store/eventstore.go b/internal/eventingester/store/eventstore.go index 5682acc8f0d..c46a0aa2e00 100644 --- a/internal/eventingester/store/eventstore.go +++ b/internal/eventingester/store/eventstore.go @@ -6,12 +6,13 @@ import ( "github.com/hashicorp/go-multierror" "github.com/redis/go-redis/v9" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/ingest" + log "github.com/armadaproject/armada/internal/common/logging" + "github "github.com/armadaproject/armada/internal/eventingester/configuration" - "github.com/armadaproject/armada/internal/eventingester/model" + log "github.com/armadaproject/armada/internal/common/logging" ) const ( diff --git a/internal/executor/application.go b/internal/executor/application.go index b737eca2a71..b6d46d1005d 100644 --- a/internal/executor/application.go +++ b/internal/executor/application.go @@ -11,7 +11,6 @@ import ( "github.com/go-playground/validator/v10" grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" "github.com/prometheus/client_golang/prometheus" - "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/keepalive" "k8s.io/utils/clock" @@ -20,6 +19,7 @@ import ( "github.com/armadaproject/armada/internal/common/cluster" "github.com/armadaproject/armada/internal/common/etcdhealth" "github.com/armadaproject/armada/internal/common/healthmonitor" + "github.com/armadaproject/armada/internal/common/logging" common_metrics "github.com/armadaproject/armada/internal/common/metrics" "github.com/armadaproject/armada/internal/common/task" "github.com/armadaproject/armada/internal/common/util" @@ -39,7 +39,7 @@ import ( "github.com/armadaproject/armada/pkg/executorapi" ) -func StartUp(ctx *armadacontext.Context, log *logrus.Entry, config configuration.ExecutorConfiguration) (func(), *sync.WaitGroup) { +func StartUp(ctx *armadacontext.Context, log *logging.Logger, config configuration.ExecutorConfiguration) (func(), *sync.WaitGroup) { err := validateConfig(config) if err != nil { log.Errorf("Invalid config: %s", err) @@ -117,7 +117,7 @@ func StartUp(ctx *armadacontext.Context, log *logrus.Entry, config configuration } func StartUpWithContext( - log *logrus.Entry, + log *logging.Logger, config configuration.ExecutorConfiguration, clusterContext executor_context.ClusterContext, clusterHealthMonitor healthmonitor.HealthMonitor, @@ -167,7 +167,7 @@ func StartUpWithContext( } func setupExecutorApiComponents( - log *logrus.Entry, + log *logging.Logger, config configuration.ExecutorConfiguration, clusterContext executor_context.ClusterContext, clusterHealthMonitor healthmonitor.HealthMonitor, diff --git a/internal/executor/context/cluster_context.go b/internal/executor/context/cluster_context.go index 53773658e45..25c4cc8176a 100644 --- a/internal/executor/context/cluster_context.go +++ b/internal/executor/context/cluster_context.go @@ -6,7 +6,6 @@ import ( "time" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" discovery "k8s.io/api/discovery/v1" networking "k8s.io/api/networking/v1" @@ -27,6 +26,7 @@ import ( "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/cluster" + log "github.com/armadaproject/armada/internal/common/logging" util2 "github.com/armadaproject/armada/internal/common/util" "github.com/armadaproject/armada/internal/executor/configuration" "github.com/armadaproject/armada/internal/executor/domain" diff --git a/internal/executor/fake/application.go b/internal/executor/fake/application.go index 8f26b43405f..75036837d1b 100644 --- a/internal/executor/fake/application.go +++ b/internal/executor/fake/application.go @@ -1,10 +1,9 @@ package fake import ( + "github.com/armadaproject/armada/internal/common/logging" "sync" - "github.com/sirupsen/logrus" - "github.com/armadaproject/armada/internal/common/task" "github.com/armadaproject/armada/internal/executor" "github.com/armadaproject/armada/internal/executor/configuration" @@ -16,7 +15,7 @@ func StartUp(config configuration.ExecutorConfiguration, nodes []*context.NodeSp wg := &sync.WaitGroup{} wg.Add(1) return executor.StartUpWithContext( - logrus.NewEntry(logrus.StandardLogger()), + logging.NewLogger(), config, context.NewFakeClusterContext(config.Application, config.Kubernetes.NodeIdLabel, nodes), nil, diff --git a/internal/executor/fake/context/context.go b/internal/executor/fake/context/context.go index e4ec04a0c9e..612cd33ecd8 100644 --- a/internal/executor/fake/context/context.go +++ b/internal/executor/fake/context/context.go @@ -12,7 +12,6 @@ import ( "github.com/google/uuid" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "golang.org/x/exp/maps" v1 "k8s.io/api/core/v1" discovery "k8s.io/api/discovery/v1" @@ -24,6 +23,7 @@ import ( "k8s.io/kubelet/pkg/apis/stats/v1alpha1" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" armadaresource "github.com/armadaproject/armada/internal/common/resource" "github.com/armadaproject/armada/internal/executor/configuration" cluster_context "github.com/armadaproject/armada/internal/executor/context" diff --git a/internal/executor/job/job_run_state_store.go b/internal/executor/job/job_run_state_store.go index dfe648fd7eb..646edc1f229 100644 --- a/internal/executor/job/job_run_state_store.go +++ b/internal/executor/job/job_run_state_store.go @@ -4,10 +4,10 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/cache" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/util" ) diff --git a/internal/executor/job/processors/preempt_runs.go b/internal/executor/job/processors/preempt_runs.go index 9d98a73cfde..959c86f9d42 100644 --- a/internal/executor/job/processors/preempt_runs.go +++ b/internal/executor/job/processors/preempt_runs.go @@ -4,10 +4,10 @@ import ( "fmt" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" executorContext "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/domain" "github.com/armadaproject/armada/internal/executor/job" diff --git a/internal/executor/job/processors/remove_runs.go b/internal/executor/job/processors/remove_runs.go index 37942110605..d81f2958e5d 100644 --- a/internal/executor/job/processors/remove_runs.go +++ b/internal/executor/job/processors/remove_runs.go @@ -3,10 +3,10 @@ package processors import ( "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" executorContext "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/domain" "github.com/armadaproject/armada/internal/executor/job" diff --git a/internal/executor/job/submit.go b/internal/executor/job/submit.go index 9943a0f3b68..03265ba3395 100644 --- a/internal/executor/job/submit.go +++ b/internal/executor/job/submit.go @@ -6,12 +6,12 @@ import ( "sync" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" k8s_errors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/armadaproject/armada/internal/common/armadaerrors" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/util" "github.com/armadaproject/armada/internal/executor/configuration" "github.com/armadaproject/armada/internal/executor/context" diff --git a/internal/executor/metrics/pod_metrics/cluster_context.go b/internal/executor/metrics/pod_metrics/cluster_context.go index e86881411b1..028c5f1f86a 100644 --- a/internal/executor/metrics/pod_metrics/cluster_context.go +++ b/internal/executor/metrics/pod_metrics/cluster_context.go @@ -3,11 +3,11 @@ package pod_metrics import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/client-go/tools/cache" + log "github.com/armadaproject/armada/internal/common/logging" armadaresource "github.com/armadaproject/armada/internal/common/resource" "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/domain" diff --git a/internal/executor/podchecks/container_state_checks.go b/internal/executor/podchecks/container_state_checks.go index 51826afd88b..aaaee5faa48 100644 --- a/internal/executor/podchecks/container_state_checks.go +++ b/internal/executor/podchecks/container_state_checks.go @@ -6,11 +6,11 @@ import ( "strings" "time" + v1 "k8s.io/api/core/v1" + + log "github.com/armadaproject/armada/internal/common/logging" config "github.com/armadaproject/armada/internal/executor/configuration/podchecks" "github.com/armadaproject/armada/internal/executor/util" - - log "github.com/sirupsen/logrus" - v1 "k8s.io/api/core/v1" ) type containerStateChecker interface { diff --git a/internal/executor/podchecks/event_checks.go b/internal/executor/podchecks/event_checks.go index ddfe5469c4d..ee0a6a24631 100644 --- a/internal/executor/podchecks/event_checks.go +++ b/internal/executor/podchecks/event_checks.go @@ -6,9 +6,9 @@ import ( "strings" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" + log "github.com/armadaproject/armada/internal/common/logging" config "github.com/armadaproject/armada/internal/executor/configuration/podchecks" ) diff --git a/internal/executor/podchecks/pod_checks.go b/internal/executor/podchecks/pod_checks.go index 496de36fd89..73be56223b1 100644 --- a/internal/executor/podchecks/pod_checks.go +++ b/internal/executor/podchecks/pod_checks.go @@ -5,9 +5,9 @@ import ( "strings" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/slices" config "github.com/armadaproject/armada/internal/executor/configuration/podchecks" "github.com/armadaproject/armada/internal/executor/util" diff --git a/internal/executor/reporter/job_event_reporter.go b/internal/executor/reporter/job_event_reporter.go index 002b2d8d833..3be27df5e7a 100644 --- a/internal/executor/reporter/job_event_reporter.go +++ b/internal/executor/reporter/job_event_reporter.go @@ -4,15 +4,16 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/cache" "k8s.io/utils/clock" + log "github.com/armadaproject/armada/internal/common/logging" + clusterCont clusterContext "github.com/armadaproject/armada/internal/executor/context" domain2 "github.com/armadaproject/armada/internal/executor/domain" "github.com/armadaproject/armada/internal/executor/job" - "github.com/armadaproject/armada/internal/executor/util" + log "github.com/armadaproject/armada/internal/common/logging" ) type EventReporter interface { diff --git a/internal/executor/service/cluster_allocation.go b/internal/executor/service/cluster_allocation.go index 405a7308128..875beadc351 100644 --- a/internal/executor/service/cluster_allocation.go +++ b/internal/executor/service/cluster_allocation.go @@ -3,12 +3,11 @@ package service import ( "fmt" - "github.com/sirupsen/logrus" - log "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/api/errors" "github.com/armadaproject/armada/internal/common/healthmonitor" "github.com/armadaproject/armada/internal/common/logging" + log "github.com/armadaproject/armada/internal/common/logging" armadaresource "github.com/armadaproject/armada/internal/common/resource" util2 "github.com/armadaproject/armada/internal/common/util" executorContext "github.com/armadaproject/armada/internal/executor/context" @@ -49,7 +48,7 @@ func (allocationService *ClusterAllocationService) AllocateSpareClusterCapacity( // If a health monitor is provided, avoid leasing jobs when the cluster is unhealthy. if allocationService.clusterHealthMonitor != nil { if ok, reason, err := allocationService.clusterHealthMonitor.IsHealthy(); err != nil { - logging.WithStacktrace(logrus.NewEntry(logrus.StandardLogger()), err).Error("failed to check cluster health") + logging.WithStacktrace(logging.NewLogger(), err).Error("failed to check cluster health") return } else if !ok { log.Warnf("cluster is not healthy; will not request more jobs: %s", reason) diff --git a/internal/executor/service/job_requester.go b/internal/executor/service/job_requester.go index 9f06c0c0cc6..f6dd2c38b2d 100644 --- a/internal/executor/service/job_requester.go +++ b/internal/executor/service/job_requester.go @@ -4,10 +4,10 @@ import ( "fmt" "time" - log "github.com/sirupsen/logrus" "golang.org/x/exp/maps" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/slices" "github.com/armadaproject/armada/internal/executor/configuration" executorContext "github.com/armadaproject/armada/internal/executor/context" diff --git a/internal/executor/service/lease_requester.go b/internal/executor/service/lease_requester.go index c9451962b89..39913fd0e79 100644 --- a/internal/executor/service/lease_requester.go +++ b/internal/executor/service/lease_requester.go @@ -6,11 +6,11 @@ import ( grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/retry" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/encoding/gzip" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" armadaresource "github.com/armadaproject/armada/internal/common/resource" clusterContext "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/pkg/executorapi" diff --git a/internal/executor/service/pod_issue_handler.go b/internal/executor/service/pod_issue_handler.go index c94a5b175e0..72e9ea57531 100644 --- a/internal/executor/service/pod_issue_handler.go +++ b/internal/executor/service/pod_issue_handler.go @@ -6,13 +6,13 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/cache" "k8s.io/kubectl/pkg/describe" "k8s.io/utils/clock" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/executor/configuration" executorContext "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/job" diff --git a/internal/executor/service/resource_cleanup.go b/internal/executor/service/resource_cleanup.go index 2657d142750..41196e20721 100644 --- a/internal/executor/service/resource_cleanup.go +++ b/internal/executor/service/resource_cleanup.go @@ -4,10 +4,10 @@ import ( "sort" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/cache" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/executor/configuration" clusterContext "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/util" diff --git a/internal/executor/util/pod_util.go b/internal/executor/util/pod_util.go index 4848f8044cd..72543c14875 100644 --- a/internal/executor/util/pod_util.go +++ b/internal/executor/util/pod_util.go @@ -6,11 +6,11 @@ import ( "strconv" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/selection" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/util" "github.com/armadaproject/armada/internal/executor/domain" "github.com/armadaproject/armada/internal/server/configuration" diff --git a/internal/executor/utilisation/cluster_utilisation.go b/internal/executor/utilisation/cluster_utilisation.go index a26726a8ea5..e2549731796 100644 --- a/internal/executor/utilisation/cluster_utilisation.go +++ b/internal/executor/utilisation/cluster_utilisation.go @@ -4,9 +4,9 @@ import ( "fmt" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" + log "github.com/armadaproject/armada/internal/common/logging" armadaresource "github.com/armadaproject/armada/internal/common/resource" armadaslices "github.com/armadaproject/armada/internal/common/slices" "github.com/armadaproject/armada/internal/executor/context" diff --git a/internal/executor/utilisation/job_utilisation_reporter.go b/internal/executor/utilisation/job_utilisation_reporter.go index d40ce0919ae..b4387db22bf 100644 --- a/internal/executor/utilisation/job_utilisation_reporter.go +++ b/internal/executor/utilisation/job_utilisation_reporter.go @@ -4,10 +4,10 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/cache" + log "github.com/armadaproject/armada/internal/common/logging" clusterContext "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/domain" "github.com/armadaproject/armada/internal/executor/reporter" diff --git a/internal/executor/utilisation/pod_utilisation.go b/internal/executor/utilisation/pod_utilisation.go index ae8d43092c5..ec075b198e8 100644 --- a/internal/executor/utilisation/pod_utilisation.go +++ b/internal/executor/utilisation/pod_utilisation.go @@ -5,9 +5,9 @@ import ( "sync" "time" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" + log "github.com/armadaproject/armada/internal/common/logging" armadaresource "github.com/armadaproject/armada/internal/common/resource" commonUtil "github.com/armadaproject/armada/internal/common/util" "github.com/armadaproject/armada/internal/executor/configuration" diff --git a/internal/executor/utilisation/pod_utilisation_custom_metrics.go b/internal/executor/utilisation/pod_utilisation_custom_metrics.go index dffaf5c25c5..bf6102dc1f8 100644 --- a/internal/executor/utilisation/pod_utilisation_custom_metrics.go +++ b/internal/executor/utilisation/pod_utilisation_custom_metrics.go @@ -5,11 +5,11 @@ import ( "net/http" "github.com/prometheus/common/model" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/utils/clock" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/executor/configuration" clusterContext "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/domain" diff --git a/internal/executor/utilisation/pod_utilisation_kubelet_metrics.go b/internal/executor/utilisation/pod_utilisation_kubelet_metrics.go index d7b32d01c7e..4a7aef1ac11 100644 --- a/internal/executor/utilisation/pod_utilisation_kubelet_metrics.go +++ b/internal/executor/utilisation/pod_utilisation_kubelet_metrics.go @@ -4,13 +4,12 @@ import ( "sync" "time" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/kubelet/pkg/apis/stats/v1alpha1" - log "github.com/sirupsen/logrus" - v1 "k8s.io/api/core/v1" - "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" clusterContext "github.com/armadaproject/armada/internal/executor/context" "github.com/armadaproject/armada/internal/executor/domain" ) diff --git a/internal/executor/utilisation/prometheus_scraping.go b/internal/executor/utilisation/prometheus_scraping.go index 2e71dcf4cac..be98f0964f3 100644 --- a/internal/executor/utilisation/prometheus_scraping.go +++ b/internal/executor/utilisation/prometheus_scraping.go @@ -6,13 +6,12 @@ import ( "net/http" "sync" + "github.com/prometheus/common/expfmt" + "github.com/prometheus/common/model" discovery "k8s.io/api/discovery/v1" + log "github.com/armadaproject/armada/internal/common/logging" commonUtil "github.com/armadaproject/armada/internal/common/util" - - "github.com/prometheus/common/expfmt" - "github.com/prometheus/common/model" - log "github.com/sirupsen/logrus" ) type httpGetter interface { diff --git a/internal/lookoutingesterv2/dbloadtester/queue.go b/internal/lookoutingesterv2/dbloadtester/queue.go index 6ad9775488f..7ff5be5dd28 100644 --- a/internal/lookoutingesterv2/dbloadtester/queue.go +++ b/internal/lookoutingesterv2/dbloadtester/queue.go @@ -8,11 +8,11 @@ import ( "github.com/apache/pulsar-client-go/pulsar" "github.com/google/uuid" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "github.com/armadaproject/armada/internal/common/database/lookout" "github.com/armadaproject/armada/internal/common/ingest/utils" + log "github.com/armadaproject/armada/internal/common/logging" protoutil "github.com/armadaproject/armada/internal/common/proto" "github.com/armadaproject/armada/internal/common/util" "github.com/armadaproject/armada/pkg/armadaevents" diff --git a/internal/lookoutingesterv2/dbloadtester/simulator.go b/internal/lookoutingesterv2/dbloadtester/simulator.go index c15e31825c9..b298bf74827 100644 --- a/internal/lookoutingesterv2/dbloadtester/simulator.go +++ b/internal/lookoutingesterv2/dbloadtester/simulator.go @@ -8,7 +8,6 @@ import ( "time" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "github.com/armadaproject/armada/internal/common/armadacontext" @@ -16,6 +15,7 @@ import ( "github.com/armadaproject/armada/internal/common/database" "github.com/armadaproject/armada/internal/common/ingest" "github.com/armadaproject/armada/internal/common/ingest/utils" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/lookoutingesterv2/configuration" "github.com/armadaproject/armada/internal/lookoutingesterv2/instructions" "github.com/armadaproject/armada/internal/lookoutingesterv2/lookoutdb" diff --git a/internal/lookoutingesterv2/ingester.go b/internal/lookoutingesterv2/ingester.go index 38eceaf7665..feadfce9939 100644 --- a/internal/lookoutingesterv2/ingester.go +++ b/internal/lookoutingesterv2/ingester.go @@ -5,7 +5,6 @@ import ( "github.com/apache/pulsar-client-go/pulsar" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/app" @@ -14,6 +13,7 @@ import ( "github.com/armadaproject/armada/internal/common/database" "github.com/armadaproject/armada/internal/common/ingest" "github.com/armadaproject/armada/internal/common/ingest/jobsetevents" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/internal/lookoutingesterv2/configuration" "github.com/armadaproject/armada/internal/lookoutingesterv2/instructions" diff --git a/internal/lookoutingesterv2/instructions/instructions.go b/internal/lookoutingesterv2/instructions/instructions.go index 6be893b85ea..6bfc0d2347a 100644 --- a/internal/lookoutingesterv2/instructions/instructions.go +++ b/internal/lookoutingesterv2/instructions/instructions.go @@ -5,7 +5,6 @@ import ( "time" "github.com/gogo/protobuf/proto" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/utils/pointer" @@ -15,6 +14,7 @@ import ( "github.com/armadaproject/armada/internal/common/eventutil" "github.com/armadaproject/armada/internal/common/ingest/metrics" "github.com/armadaproject/armada/internal/common/ingest/utils" + log "github.com/armadaproject/armada/internal/common/logging" protoutil "github.com/armadaproject/armada/internal/common/proto" "github.com/armadaproject/armada/internal/common/util" "github.com/armadaproject/armada/internal/lookoutingesterv2/model" diff --git a/internal/lookoutingesterv2/lookoutdb/insertion.go b/internal/lookoutingesterv2/lookoutdb/insertion.go index 14af9937ffa..6b68c40d27c 100644 --- a/internal/lookoutingesterv2/lookoutdb/insertion.go +++ b/internal/lookoutingesterv2/lookoutdb/insertion.go @@ -8,12 +8,12 @@ import ( "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/armadaerrors" "github.com/armadaproject/armada/internal/common/database/lookout" commonmetrics "github.com/armadaproject/armada/internal/common/ingest/metrics" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/lookoutingesterv2/metrics" "github.com/armadaproject/armada/internal/lookoutingesterv2/model" ) diff --git a/internal/lookoutv2/application.go b/internal/lookoutv2/application.go index bf7c03a9ecd..ee26199923f 100644 --- a/internal/lookoutv2/application.go +++ b/internal/lookoutv2/application.go @@ -4,22 +4,21 @@ package lookoutv2 import ( "github.com/IBM/pgxpoolprometheus" - "github.com/go-openapi/loads" - "github.com/go-openapi/runtime/middleware" - "github.com/jessevdk/go-flags" - "github.com/prometheus/client_golang/prometheus" - "github.com/sirupsen/logrus" - "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/compress" "github.com/armadaproject/armada/internal/common/database" + "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/slices" "github.com/armadaproject/armada/internal/lookoutv2/configuration" "github.com/armadaproject/armada/internal/lookoutv2/conversions" "github.com/armadaproject/armada/internal/lookoutv2/gen/restapi" "github.com/armadaproject/armada/internal/lookoutv2/gen/restapi/operations" "github.com/armadaproject/armada/internal/lookoutv2/repository" + "github.com/go-openapi/loads" + "github.com/go-openapi/runtime/middleware" + "github.com/jessevdk/go-flags" + "github.com/prometheus/client_golang/prometheus" ) func Serve(configuration configuration.LookoutV2Config) error { @@ -48,7 +47,7 @@ func Serve(configuration configuration.LookoutV2Config) error { // create new service API api := operations.NewLookoutAPI(swaggerSpec) - logger := logrus.NewEntry(logrus.StandardLogger()) + logger := logging.NewLogger() api.Logger = logger.Debugf diff --git a/internal/lookoutv2/generate/main.go b/internal/lookoutv2/generate/main.go index a916b94fd2d..28a607d89df 100644 --- a/internal/lookoutv2/generate/main.go +++ b/internal/lookoutv2/generate/main.go @@ -6,7 +6,7 @@ import ( "os/exec" "path/filepath" - log "github.com/sirupsen/logrus" + log "github.com/armadaproject/armada/internal/common/logging" ) const ( diff --git a/internal/lookoutv2/pruner/pruner.go b/internal/lookoutv2/pruner/pruner.go index 397bb3b1538..425185a3e33 100644 --- a/internal/lookoutv2/pruner/pruner.go +++ b/internal/lookoutv2/pruner/pruner.go @@ -6,10 +6,10 @@ import ( "github.com/hashicorp/go-multierror" "github.com/jackc/pgx/v5" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "k8s.io/utils/clock" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" ) func PruneDb( diff --git a/internal/lookoutv2/repository/getjoberror.go b/internal/lookoutv2/repository/getjoberror.go index ebcb14a59fa..d418ed89990 100644 --- a/internal/lookoutv2/repository/getjoberror.go +++ b/internal/lookoutv2/repository/getjoberror.go @@ -4,10 +4,10 @@ import ( "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/compress" + log "github.com/armadaproject/armada/internal/common/logging" ) type GetJobErrorRepository interface { diff --git a/internal/lookoutv2/repository/getjobrundebugmessage.go b/internal/lookoutv2/repository/getjobrundebugmessage.go index 5b4841dc7c7..ace8f6d0ca7 100644 --- a/internal/lookoutv2/repository/getjobrundebugmessage.go +++ b/internal/lookoutv2/repository/getjobrundebugmessage.go @@ -4,10 +4,10 @@ import ( "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/compress" + log "github.com/armadaproject/armada/internal/common/logging" ) type GetJobRunDebugMessageRepository interface { diff --git a/internal/lookoutv2/repository/getjobrunerror.go b/internal/lookoutv2/repository/getjobrunerror.go index b878c9291fb..b97b0ab8206 100644 --- a/internal/lookoutv2/repository/getjobrunerror.go +++ b/internal/lookoutv2/repository/getjobrunerror.go @@ -4,10 +4,10 @@ import ( "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/compress" + log "github.com/armadaproject/armada/internal/common/logging" ) type GetJobRunErrorRepository interface { diff --git a/internal/lookoutv2/repository/getjobspec.go b/internal/lookoutv2/repository/getjobspec.go index 8e51fe89871..ea1216e3a30 100644 --- a/internal/lookoutv2/repository/getjobspec.go +++ b/internal/lookoutv2/repository/getjobspec.go @@ -5,10 +5,10 @@ import ( "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/compress" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/pkg/api" ) diff --git a/internal/lookoutv2/repository/querybuilder.go b/internal/lookoutv2/repository/querybuilder.go index 01255998086..18c93c3ed2f 100644 --- a/internal/lookoutv2/repository/querybuilder.go +++ b/internal/lookoutv2/repository/querybuilder.go @@ -5,10 +5,10 @@ import ( "strings" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "golang.org/x/exp/slices" "github.com/armadaproject/armada/internal/common/database/lookout" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/lookoutv2/model" ) @@ -411,7 +411,7 @@ func operatorForMatch(match string) (string, error) { return "<=", nil default: err := errors.Errorf("unsupported match type: %s", match) - logrus.Error(err) + log.Error(err.Error()) return "", err } } diff --git a/internal/lookoutv2/repository/util.go b/internal/lookoutv2/repository/util.go index 1072f91a77b..97d85cf9a53 100644 --- a/internal/lookoutv2/repository/util.go +++ b/internal/lookoutv2/repository/util.go @@ -8,7 +8,6 @@ import ( "github.com/apache/pulsar-client-go/pulsar" "github.com/gogo/protobuf/types" "github.com/google/uuid" - log "github.com/sirupsen/logrus" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" "k8s.io/utils/clock" @@ -18,6 +17,7 @@ import ( "github.com/armadaproject/armada/internal/common/database/lookout" "github.com/armadaproject/armada/internal/common/eventutil" "github.com/armadaproject/armada/internal/common/ingest/utils" + log "github.com/armadaproject/armada/internal/common/logging" protoutil "github.com/armadaproject/armada/internal/common/proto" "github.com/armadaproject/armada/internal/common/pulsarutils" "github.com/armadaproject/armada/internal/common/util" @@ -706,25 +706,25 @@ func prefixAnnotations(prefix string, annotations map[string]string) map[string] func logQueryDebug(query *Query, description string) { log. - WithField("query", removeNewlinesAndTabs(query.Sql)). - WithField("values", query.Args). + With("query", removeNewlinesAndTabs(query.Sql)). + With("values", query.Args). Debug(description) } func logQueryError(query *Query, description string, duration time.Duration) { log. - WithField("query", removeNewlinesAndTabs(query.Sql)). - WithField("values", query.Args). - WithField("duration", duration). + With("query", removeNewlinesAndTabs(query.Sql)). + With("values", query.Args). + With("duration", duration). Errorf("Error executing %s query", description) } func logSlowQuery(query *Query, description string, duration time.Duration) { if duration > 5*time.Second { log. - WithField("query", removeNewlinesAndTabs(query.Sql)). - WithField("values", query.Args). - WithField("duration", duration). + With("query", removeNewlinesAndTabs(query.Sql)). + With("values", query.Args). + With("duration", duration). Infof("Slow %s query detected", description) } } diff --git a/internal/scheduler/api.go b/internal/scheduler/api.go index daf8efe8fd8..d6164f99a2a 100644 --- a/internal/scheduler/api.go +++ b/internal/scheduler/api.go @@ -7,7 +7,6 @@ import ( "github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/types" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" v1 "k8s.io/api/core/v1" @@ -18,6 +17,7 @@ import ( "github.com/armadaproject/armada/internal/common/auth" "github.com/armadaproject/armada/internal/common/compress" "github.com/armadaproject/armada/internal/common/logging" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/maps" "github.com/armadaproject/armada/internal/common/pulsarutils" priorityTypes "github.com/armadaproject/armada/internal/common/types" diff --git a/internal/scheduler/nodedb/nodeiteration.go b/internal/scheduler/nodedb/nodeiteration.go index 3e171007139..d8260270fa2 100644 --- a/internal/scheduler/nodedb/nodeiteration.go +++ b/internal/scheduler/nodedb/nodeiteration.go @@ -6,9 +6,9 @@ import ( "github.com/hashicorp/go-memdb" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "golang.org/x/exp/slices" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/scheduler/internaltypes" ) diff --git a/internal/scheduler/schedulerapp.go b/internal/scheduler/schedulerapp.go index 9bbd0a01b53..d69280ebd9a 100644 --- a/internal/scheduler/schedulerapp.go +++ b/internal/scheduler/schedulerapp.go @@ -13,7 +13,6 @@ import ( grpc_logging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" - log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" @@ -27,6 +26,7 @@ import ( grpcCommon "github.com/armadaproject/armada/internal/common/grpc" "github.com/armadaproject/armada/internal/common/health" "github.com/armadaproject/armada/internal/common/logging" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/internal/common/pulsarutils" "github.com/armadaproject/armada/internal/common/pulsarutils/jobsetevents" @@ -363,7 +363,7 @@ func loadClusterConfig(ctx *armadacontext.Context) (*rest.Config, error) { return config, err } -// This changes the default logrus grpc logging to log OK messages at trace level +// This changes the default grpc logging to log OK messages at trace level // The reason for doing this are: // - Reduced logging // - We only care about failures, so lets only log failures diff --git a/internal/scheduler/scheduling/context/job.go b/internal/scheduler/scheduling/context/job.go index e9ad86ed71a..81ea403c5ce 100644 --- a/internal/scheduler/scheduling/context/job.go +++ b/internal/scheduler/scheduling/context/job.go @@ -8,11 +8,11 @@ import ( "time" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "golang.org/x/exp/maps" v1 "k8s.io/api/core/v1" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" armadamaps "github.com/armadaproject/armada/internal/common/maps" armadaslices "github.com/armadaproject/armada/internal/common/slices" schedulerconfig "github.com/armadaproject/armada/internal/scheduler/configuration" @@ -196,7 +196,7 @@ func JobSchedulingContextsFromJobs[J *jobdb.Job](jobs []J) []*JobSchedulingConte func JobSchedulingContextFromJob(job *jobdb.Job) *JobSchedulingContext { gangInfo, err := GangInfoFromLegacySchedulerJob(job) if err != nil { - logrus.Errorf("failed to extract gang info from job %s: %s", job.Id(), err) + log.Errorf("failed to extract gang info from job %s: %s", job.Id(), err) } return &JobSchedulingContext{ Created: time.Now(), diff --git a/internal/scheduler/scheduling/scheduling_algo.go b/internal/scheduler/scheduling/scheduling_algo.go index 7c70c902881..ec1a5b4e651 100644 --- a/internal/scheduler/scheduling/scheduling_algo.go +++ b/internal/scheduler/scheduling/scheduling_algo.go @@ -6,13 +6,13 @@ import ( "time" "github.com/pkg/errors" - "github.com/sirupsen/logrus" "golang.org/x/exp/maps" "golang.org/x/exp/slices" "golang.org/x/time/rate" "k8s.io/utils/clock" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" armadamaps "github.com/armadaproject/armada/internal/common/maps" armadaslices "github.com/armadaproject/armada/internal/common/slices" "github.com/armadaproject/armada/internal/scheduler/configuration" @@ -603,7 +603,7 @@ func (l *FairSchedulingAlgo) populateNodeDb(nodeDb *nodedb.NodeDb, homeJobs []*j } nodeId := job.LatestRun().NodeId() if _, ok := nodesById[nodeId]; !ok { - logrus.Errorf( + log.Errorf( "job %s assigned to node %s on executor %s, but no such node found", job.Id(), nodeId, job.LatestRun().Executor(), ) @@ -618,7 +618,7 @@ func (l *FairSchedulingAlgo) populateNodeDb(nodeDb *nodedb.NodeDb, homeJobs []*j nodeId := job.LatestRun().NodeId() node, ok := nodesById[nodeId] if !ok { - logrus.Errorf( + log.Errorf( "job %s assigned to node %s on executor %s, but no such node found", job.Id(), nodeId, job.LatestRun().Executor(), ) diff --git a/internal/scheduleringester/ingester.go b/internal/scheduleringester/ingester.go index 92e13647b18..74934daf9c9 100644 --- a/internal/scheduleringester/ingester.go +++ b/internal/scheduleringester/ingester.go @@ -6,7 +6,6 @@ import ( "github.com/apache/pulsar-client-go/pulsar" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/app" @@ -16,6 +15,7 @@ import ( controlplaneevents_ingest_utils "github.com/armadaproject/armada/internal/common/ingest/controlplaneevents" "github.com/armadaproject/armada/internal/common/ingest/jobsetevents" "github.com/armadaproject/armada/internal/common/ingest/metrics" + log "github.com/armadaproject/armada/internal/common/loggin "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/pkg/armadaevents" "github.com/armadaproject/armada/pkg/controlplaneevents" diff --git a/internal/scheduleringester/instructions.go b/internal/scheduleringester/instructions.go index 2c971a95df8..d64a2f0b83e 100644 --- a/internal/scheduleringester/instructions.go +++ b/internal/scheduleringester/instructions.go @@ -6,7 +6,6 @@ import ( "github.com/gogo/protobuf/proto" "github.com/google/uuid" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "golang.org/x/exp/maps" "golang.org/x/exp/slices" @@ -14,6 +13,7 @@ import ( "github.com/armadaproject/armada/internal/common/compress" "github.com/armadaproject/armada/internal/common/ingest/metrics" "github.com/armadaproject/armada/internal/common/ingest/utils" + log "github.com/armadaproject/armada/internal/common/logging" protoutil "github.com/armadaproject/armada/internal/common/proto" "github.com/armadaproject/armada/internal/scheduler/adapters" schedulerdb "github.com/armadaproject/armada/internal/scheduler/database" diff --git a/internal/server/event/conversion/conversions.go b/internal/server/event/conversion/conversions.go index 227f9084a93..7db8eaf72d4 100644 --- a/internal/server/event/conversion/conversions.go +++ b/internal/server/event/conversion/conversions.go @@ -3,9 +3,8 @@ package conversion import ( "time" - log "github.com/sirupsen/logrus" - "github.com/armadaproject/armada/internal/common/eventutil" + log "github.com/armadaproject/armada/internal/common/logging" protoutil "github.com/armadaproject/armada/internal/common/proto" "github.com/armadaproject/armada/pkg/api" "github.com/armadaproject/armada/pkg/armadaevents" diff --git a/internal/server/event/event.go b/internal/server/event/event.go index f2cb1b9d843..9c59c7bebfd 100644 --- a/internal/server/event/event.go +++ b/internal/server/event/event.go @@ -6,13 +6,13 @@ import ( "github.com/gogo/protobuf/types" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/armadaerrors" "github.com/armadaproject/armada/internal/common/auth" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/server/event/sequence" "github.com/armadaproject/armada/internal/server/permissions" armadaqueue "github.com/armadaproject/armada/internal/server/queue" diff --git a/internal/server/event/event_repository.go b/internal/server/event/event_repository.go index 124abdd021c..4292f7553b5 100644 --- a/internal/server/event/event_repository.go +++ b/internal/server/event/event_repository.go @@ -10,10 +10,10 @@ import ( pool "github.com/jolestar/go-commons-pool" "github.com/pkg/errors" "github.com/redis/go-redis/v9" - log "github.com/sirupsen/logrus" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/compress" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/server/event/conversion" "github.com/armadaproject/armada/internal/server/event/sequence" "github.com/armadaproject/armada/pkg/api" diff --git a/internal/server/server.go b/internal/server/server.go index badcc5689f8..e35e7974244 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -12,7 +12,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/redis/go-redis/extra/redisprometheus/v9" "github.com/redis/go-redis/v9" - log "github.com/sirupsen/logrus" "google.golang.org/grpc" "github.com/armadaproject/armada/internal/common/armadacontext" @@ -21,6 +20,7 @@ import ( "github.com/armadaproject/armada/internal/common/database" grpcCommon "github.com/armadaproject/armada/internal/common/grpc" "github.com/armadaproject/armada/internal/common/health" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/pulsarutils" controlplaneeventspulsarutils "github.com/armadaproject/armada/internal/common/pulsarutils/controlplaneevents" "github.com/armadaproject/armada/internal/common/pulsarutils/jobsetevents" diff --git a/pkg/client/auth/kubernetes/authentication.go b/pkg/client/auth/kubernetes/authentication.go index 85f14fe3c19..a3bb4a47f7a 100644 --- a/pkg/client/auth/kubernetes/authentication.go +++ b/pkg/client/auth/kubernetes/authentication.go @@ -4,13 +4,13 @@ import ( "context" "os" - log "github.com/sirupsen/logrus" "golang.org/x/oauth2" authv1 "k8s.io/api/authentication/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/pkg/client/auth/oidc" ) diff --git a/pkg/client/auth/oidc/kubernetes.go b/pkg/client/auth/oidc/kubernetes.go index 3d540e6598d..806229cbbe5 100644 --- a/pkg/client/auth/oidc/kubernetes.go +++ b/pkg/client/auth/oidc/kubernetes.go @@ -12,8 +12,8 @@ import ( "strings" "time" - log "github.com/sirupsen/logrus" - "golang.org/x/oauth2" + log "github.com/armadaproject/armada/internal/common/logging" + ) type KubernetesDetails struct { diff --git a/pkg/client/auth/oidc/pkce.go b/pkg/client/auth/oidc/pkce.go index f50b7bf2491..b2f4b706b0a 100644 --- a/pkg/client/auth/oidc/pkce.go +++ b/pkg/client/auth/oidc/pkce.go @@ -12,12 +12,10 @@ import ( "strconv" "strings" - "github.com/sirupsen/logrus" - - "github.com/armadaproject/armada/internal/common/logging" - openId "github.com/coreos/go-oidc" "golang.org/x/oauth2" + + log "github.com/armadaproject/armada/internal/common/logging" ) type PKCEDetails struct { @@ -29,7 +27,6 @@ type PKCEDetails struct { func AuthenticatePkce(config PKCEDetails) (*TokenCredentials, error) { ctx := context.Background() - log := logrus.StandardLogger().WithField("auth", "AuthenticatePkce") result := make(chan *oauth2.Token) errorResult := make(chan error) @@ -97,14 +94,14 @@ func AuthenticatePkce(config PKCEDetails) (*TokenCredentials, error) { go func() { if err := server.Serve(listener); err != nil { - logging.WithStacktrace(log, err).Error("unable to serve") + log.WithStacktrace(log.NewLogger(), err).Error("unable to serve") } }() cmd, err := openBrowser("http://" + localUrl) defer func() { if err := cmd.Process.Kill(); err != nil { - logging.WithStacktrace(log, err).Error("unable to kill process") + log.WithStacktrace(log.NewLogger(), err).Error("unable to kill process") } }() diff --git a/pkg/client/load-test.go b/pkg/client/load-test.go index 448e2000b0d..0a2594525ca 100644 --- a/pkg/client/load-test.go +++ b/pkg/client/load-test.go @@ -6,13 +6,11 @@ import ( "sync" "time" - "github.com/sirupsen/logrus" - log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" "github.com/armadaproject/armada/internal/common" - "github.com/armadaproject/armada/internal/common/logging" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/pkg/api" "github.com/armadaproject/armada/pkg/client/domain" ) @@ -231,7 +229,6 @@ func (apiLoadTester ArmadaLoadTester) monitorJobsUntilCompletion( jobIds chan string, eventChannel chan api.Event, ) []string { - log := logrus.StandardLogger().WithField("Armada", "LoadTester") var submittedIds []string = nil go func() { ids := []string{} @@ -258,7 +255,7 @@ func (apiLoadTester ArmadaLoadTester) monitorJobsUntilCompletion( return nil }) if err != nil { - logging.WithStacktrace(log, err).Error("unable to monitor jobs") + log.WithStacktrace(log.NewLogger(), err).Error("unable to monitor jobs") } return submittedIds } @@ -281,8 +278,6 @@ func createJobSubmitRequestItems(jobDescs []*domain.JobSubmissionDescription) [] } func (apiLoadTester ArmadaLoadTester) cancelRemainingJobs(queue string, jobSetId string) { - log := logrus.StandardLogger().WithField("Armada", "LoadTester") - err := WithSubmitClient(apiLoadTester.apiConnectionDetails, func(client api.SubmitClient) error { timeout, _ := common.ContextWithDefaultTimeout() cancelRequest := &api.JobCancelRequest{ @@ -293,7 +288,7 @@ func (apiLoadTester ArmadaLoadTester) cancelRemainingJobs(queue string, jobSetId return err }) if err != nil { - logging.WithStacktrace(log, err).Error("unable to cancel jobs") + log.WithStacktrace(log.NewLogger(), err).Error("unable to cancel jobs") } } diff --git a/pkg/client/queue/get_all.go b/pkg/client/queue/get_all.go index 2357115792d..785732f02e0 100644 --- a/pkg/client/queue/get_all.go +++ b/pkg/client/queue/get_all.go @@ -6,12 +6,12 @@ import ( "io" "time" - "github.com/armadaproject/armada/pkg/api" - "github.com/armadaproject/armada/pkg/client" - - log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + + log "github.com/armadaproject/armada/internal/common/logging" + "github.com/armadaproject/armada/pkg/api" + "github.com/armadaproject/armada/pkg/client" ) type GetAllAPI func() ([]*api.Queue, error) @@ -35,7 +35,7 @@ func GetAll(getConnectionDetails client.ConnectionDetails) GetAllAPI { allQueues := make([]*api.Queue, 0) queueStream, err := client.GetQueues(ctx, &api.StreamingQueueGetRequest{}) if err != nil { - log.Error(err) + log.Error(err.Error()) return nil, err } @@ -57,7 +57,7 @@ func GetAll(getConnectionDetails client.ConnectionDetails) GetAllAPI { return nil, e } if !isTransportClosingError(e) { - log.Error(e) + log.Error(e.Error()) } break } diff --git a/pkg/client/watch.go b/pkg/client/watch.go index 2b9a96ae498..b333fb95ff3 100644 --- a/pkg/client/watch.go +++ b/pkg/client/watch.go @@ -5,10 +5,10 @@ import ( "io" "time" - log "github.com/sirupsen/logrus" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/util" "github.com/armadaproject/armada/pkg/api" "github.com/armadaproject/armada/pkg/client/domain" From 8c184b02a3e0b50556092f26e7e4799d7244c542 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 16:39:20 +0000 Subject: [PATCH 11/58] wip --- cmd/server/main.go | 4 +- go.mod | 1 - go.sum | 6 +- .../common/armadacontext/armada_context.go | 46 +++--- .../armadacontext/armada_context_test.go | 3 +- internal/common/etcdhealth/etcdhealth.go | 10 +- .../common/grpc/armadacontext/interceptors.go | 6 +- internal/common/grpc/grpc.go | 8 +- internal/common/ingest/jobsetevents/utils.go | 3 +- internal/common/logging/logging.go | 143 ++++++++++++------ internal/common/logging/pulsar_adapter.go | 71 +++++++++ internal/common/logging/stacktrace.go | 21 --- internal/common/profiling/http.go | 3 +- internal/common/pulsarutils/publisher.go | 5 +- internal/common/pulsarutils/pulsarclient.go | 2 + internal/common/resource/resource_test.go | 2 +- internal/common/serve/static.go | 3 +- internal/common/startup.go | 108 ++++++------- .../executor/reporter/job_event_reporter.go | 3 +- internal/lookoutv2/repository/util.go | 10 +- internal/scheduler/nodedb/nodeiteration.go | 2 +- pkg/client/auth/oidc/kubernetes.go | 3 +- pkg/client/auth/oidc/pkce.go | 4 +- pkg/client/load-test.go | 4 +- pkg/client/watch.go | 6 +- 25 files changed, 281 insertions(+), 196 deletions(-) create mode 100644 internal/common/logging/pulsar_adapter.go delete mode 100644 internal/common/logging/stacktrace.go diff --git a/cmd/server/main.go b/cmd/server/main.go index c7318dcecc0..55aaf59e2c7 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -8,7 +8,6 @@ import ( "syscall" "time" - log "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" @@ -17,6 +16,7 @@ import ( gateway "github.com/armadaproject/armada/internal/common/grpc" "github.com/armadaproject/armada/internal/common/health" "github.com/armadaproject/armada/internal/common/logging" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/internal/server" "github.com/armadaproject/armada/internal/server/configuration" @@ -117,6 +117,6 @@ func main() { }() if err := g.Wait(); err != nil { - logging.WithStacktrace(log.NewEntry(log.StandardLogger()), err).Error("Armada server shut down") + logging.WithStacktrace(err).Error("Armada server shut down") } } diff --git a/go.mod b/go.mod index 9c0d71ca678..bc4c4388e9b 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,6 @@ require ( github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 github.com/stretchr/testify v1.9.0 - github.com/weaveworks/promrus v1.2.0 golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c golang.org/x/net v0.30.0 golang.org/x/oauth2 v0.23.0 diff --git a/go.sum b/go.sum index 1491cc31df8..476a4ddf187 100644 --- a/go.sum +++ b/go.sum @@ -560,8 +560,6 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= -github.com/weaveworks/promrus v1.2.0 h1:jOLf6pe6/vss4qGHjXmGz4oDJQA+AOCqEL3FvvZGz7M= -github.com/weaveworks/promrus v1.2.0/go.mod h1:SaE82+OJ91yqjrE1rsvBWVzNZKcHYFtMUyS1+Ogs/KA= github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= @@ -604,10 +602,14 @@ go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee33 go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= diff --git a/internal/common/armadacontext/armada_context.go b/internal/common/armadacontext/armada_context.go index b496660c092..ed49bf2fc94 100644 --- a/internal/common/armadacontext/armada_context.go +++ b/internal/common/armadacontext/armada_context.go @@ -13,64 +13,64 @@ import ( // while retaining type-safety type Context struct { context.Context - Logger *logging.Logger + logger *logging.Logger } // Debug logs a message at level Debug func (ctx *Context) Debug(msg string) { - ctx.Logger.Debug(msg) + ctx.logger.Debug(msg) } // Info logs a message at level Info func (ctx *Context) Info(msg string) { - ctx.Logger.Info(msg) + ctx.logger.Info(msg) } // Warn logs a message at level Warn func (ctx *Context) Warn(msg string) { - ctx.Logger.Warn(msg) + ctx.logger.Warn(msg) } // Error logs a message at level Error func (ctx *Context) Error(msg string) { - ctx.Logger.Error(msg) + ctx.logger.Error(msg) } // Panic logs a message at level Panic func (ctx *Context) Panic(msg string) { - ctx.Logger.Panic(msg) + ctx.logger.Panic(msg) } // Fatal logs a message at level Fatal then the process will exit with status set to 1. func (ctx *Context) Fatal(msg string) { - ctx.Logger.Fatal(msg) + ctx.logger.Fatal(msg) } // Debugf logs a message at level Debug. func (ctx *Context) Debugf(format string, args ...interface{}) { - ctx.Logger.Debugf(format, args...) + ctx.logger.Debugf(format, args...) } // Infof logs a message at level Info. func (ctx *Context) Infof(format string, args ...interface{}) { - ctx.Logger.Infof(format, args...) + ctx.logger.Infof(format, args...) } // Warnf logs a message at level Warn. func (ctx *Context) Warnf(format string, args ...interface{}) { - ctx.Logger.Warnf(format, args...) + ctx.logger.Warnf(format, args...) } // Errorf logs a message at level Error. func (ctx *Context) Errorf(format string, args ...interface{}) { - ctx.Logger.Errorf(format, args...) + ctx.logger.Errorf(format, args...) } // Background creates an empty context with a default logger. It is analogous to context.Background() func Background() *Context { return &Context{ Context: context.Background(), - Logger: logging.NewLogger(), + logger: logging.StdLogger().WithCallerSkip(1), } } @@ -78,7 +78,7 @@ func Background() *Context { func TODO() *Context { return &Context{ Context: context.TODO(), - Logger: logging.NewLogger(), + logger: logging.StdLogger().WithCallerSkip(1), } } @@ -88,23 +88,27 @@ func FromGrpcCtx(ctx context.Context) *Context { if ok { return armadaCtx } - return New(ctx, logging.NewLogger()) + return New(ctx, logging.StdLogger().WithCallerSkip(1)) } // New returns an armada context that encapsulates both a go context and a logger func New(ctx context.Context, log *logging.Logger) *Context { return &Context{ Context: ctx, - Logger: log, + logger: log, } } +func (ctx *Context) Logger() *logging.Logger { + return ctx.logger.WithCallerSkip(-1) +} + // WithCancel returns a copy of parent with a new Done channel. It is analogous to context.WithCancel() func WithCancel(parent *Context) (*Context, context.CancelFunc) { c, cancel := context.WithCancel(parent.Context) return &Context{ Context: c, - Logger: parent.Logger, + logger: parent.logger, }, cancel } @@ -114,7 +118,7 @@ func WithDeadline(parent *Context, d time.Time) (*Context, context.CancelFunc) { c, cancel := context.WithDeadline(parent.Context, d) return &Context{ Context: c, - Logger: parent.Logger, + logger: parent.logger, }, cancel } @@ -127,7 +131,7 @@ func WithTimeout(parent *Context, timeout time.Duration) (*Context, context.Canc func WithLogField(parent *Context, key string, val interface{}) *Context { return &Context{ Context: parent.Context, - Logger: parent.Logger.With(key, val), + logger: parent.logger.WithField(key, val), } } @@ -135,7 +139,7 @@ func WithLogField(parent *Context, key string, val interface{}) *Context { func WithLogFields(parent *Context, fields map[string]any) *Context { return &Context{ Context: parent.Context, - Logger: parent.Logger.With(fields), + logger: parent.logger.WithFields(fields), } } @@ -144,7 +148,7 @@ func WithLogFields(parent *Context, fields map[string]any) *Context { func WithValue(parent *Context, key, val any) *Context { return &Context{ Context: context.WithValue(parent, key, val), - Logger: parent.Logger, + logger: parent.logger, } } @@ -154,6 +158,6 @@ func ErrGroup(ctx *Context) (*errgroup.Group, *Context) { group, goctx := errgroup.WithContext(ctx) return group, &Context{ Context: goctx, - Logger: ctx.Logger, + logger: ctx.logger, } } diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index 5ae785a731d..5cb5621b43c 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -1,4 +1,5 @@ -//package armadacontext +package armadacontext + // //import ( // "context" diff --git a/internal/common/etcdhealth/etcdhealth.go b/internal/common/etcdhealth/etcdhealth.go index 09f191ac4e4..dfa4acb96b3 100644 --- a/internal/common/etcdhealth/etcdhealth.go +++ b/internal/common/etcdhealth/etcdhealth.go @@ -184,7 +184,7 @@ func (srv *EtcdReplicaHealthMonitor) sizeFraction() float64 { } func (srv *EtcdReplicaHealthMonitor) Run(ctx *armadacontext.Context, log *logging.Logger) error { - log = log.With("service", "EtcdHealthMonitor") + log = log.WithField("service", "EtcdHealthMonitor") log.Info("starting etcd health monitor") defer log.Info("stopping etcd health monitor") ticker := time.NewTicker(srv.scrapeInterval) @@ -199,20 +199,20 @@ func (srv *EtcdReplicaHealthMonitor) Run(ctx *armadacontext.Context, log *loggin srv.mu.Lock() srv.timeOfMostRecentCollectionAttempt = time.Now() if err != nil { - logging.WithStacktrace(log, err).Errorf("failed to scrape etcd metrics from %s", srv.name) + log.WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) } else { success := true if err := srv.setSizeInUseBytesFromMetrics(metrics); err != nil { success = false - logging.WithStacktrace(log, err).Errorf("failed to scrape etcd metrics from %s", srv.name) + log.WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) } if err := srv.setSizeBytesFromMetrics(metrics); err != nil { success = false - logging.WithStacktrace(log, err).Errorf("failed to scrape etcd metrics from %s", srv.name) + log.WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) } if err := srv.setCapacityBytesFromMetrics(metrics); err != nil { success = false - logging.WithStacktrace(log, err).Errorf("failed to scrape etcd metrics from %s", srv.name) + log.WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) } if success { srv.timeOfMostRecentSuccessfulCollectionAttempt = srv.timeOfMostRecentCollectionAttempt diff --git a/internal/common/grpc/armadacontext/interceptors.go b/internal/common/grpc/armadacontext/interceptors.go index d2ead0ba373..9148940f2e5 100644 --- a/internal/common/grpc/armadacontext/interceptors.go +++ b/internal/common/grpc/armadacontext/interceptors.go @@ -29,8 +29,8 @@ func StreamServerInterceptor() grpc.StreamServerInterceptor { } func createArmadaCtx(ctx context.Context) *armadacontext.Context { - log := logging.NewLogger(). - With("requestId", requestid.FromContextOrMissing(ctx)). - With("user", auth.GetPrincipal(ctx).GetName()) + log := logging.StdLogger(). + WithField("requestId", requestid.FromContextOrMissing(ctx)). + WithField("user", auth.GetPrincipal(ctx).GetName()) return armadacontext.New(ctx, log) } diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index 34e0b06bbd2..b7240e9d86a 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -147,13 +147,7 @@ func panicRecoveryHandler(p interface{}) (err error) { func InterceptorLogger() grpc_logging.Logger { return grpc_logging.LoggerFunc(func(ctx context.Context, lvl grpc_logging.Level, msg string, fields ...any) { armadaCtx := armadacontext.FromGrpcCtx(ctx) - logFields := make(map[string]any, len(fields)/2) - i := grpc_logging.Fields(fields).Iterator() - for i.Next() { - k, v := i.At() - logFields[k] = v - } - l := armadaCtx.Logger.With(logFields) + l := armadaCtx.Logger().WithField(fields...) switch lvl { case grpc_logging.LevelDebug: l.Debug(msg) diff --git a/internal/common/ingest/jobsetevents/utils.go b/internal/common/ingest/jobsetevents/utils.go index 69942d1e3d3..a1b36c9d293 100644 --- a/internal/common/ingest/jobsetevents/utils.go +++ b/internal/common/ingest/jobsetevents/utils.go @@ -7,9 +7,8 @@ import ( commonmetrics "github.com/armadaproject/armada/internal/common/ingest/metrics" "github.com/armadaproject/armada/internal/common/ingest/utils" log "github.com/armadaproject/armada/internal/common/logging" - pr protoutil "github.com/armadaproject/armada/internal/common/proto" - log "github.com/armadaproject/armada/internal/common/logging" + "github.com/armadaproject/armada/pkg/armadaevents" ) func MessageUnmarshaller(msg pulsar.ConsumerMessage, metrics *commonmetrics.Metrics) *utils.EventsWithIds[*armadaevents.EventSequence] { diff --git a/internal/common/logging/logging.go b/internal/common/logging/logging.go index e3db5508adc..1f5bf2bcb75 100644 --- a/internal/common/logging/logging.go +++ b/internal/common/logging/logging.go @@ -1,59 +1,53 @@ package logging import ( + "github.com/pkg/errors" "go.uber.org/zap" ) -type Logger struct { - undlerlying *zap.SugaredLogger +// Unexported but considered part of the stable interface of pkg/errors. +type stackTracer interface { + StackTrace() errors.StackTrace } -func (l *Logger) WithError(err error) *Logger { - return &Logger{ - undlerlying: zap.S().With("error", err.Error()), - } -} - -func (l *Logger) With(args ...any) *Logger { - return &Logger{ - undlerlying: zap.S().With(args), - } +type Logger struct { + undlerlying *zap.SugaredLogger } -func NewLogger() *Logger { +func FromZap(l *zap.Logger) *Logger { return &Logger{ - undlerlying: zap.S(), + undlerlying: l.Sugar(), } } // Debug logs a message at level Debug -func (l *Logger) Debug(msg string) { - l.undlerlying.Debug(msg) +func (l *Logger) Debug(args ...any) { + l.undlerlying.Debug(args) } // Info logs a message at level Info -func (l *Logger) Info(msg string) { - l.undlerlying.Info(msg) +func (l *Logger) Info(args ...any) { + l.undlerlying.Info(args) } // Warn logs a message at level Warn -func (l *Logger) Warn(msg string) { - l.undlerlying.Warn(msg) +func (l *Logger) Warn(args ...any) { + l.undlerlying.Warn(args) } // Error logs a message at level Error -func (l *Logger) Error(msg string) { - l.undlerlying.Error(msg) +func (l *Logger) Error(args ...any) { + l.undlerlying.Error(args) } // Panic logs a message at level Panic -func (l *Logger) Panic(msg string) { - l.undlerlying.Panic(msg) +func (l *Logger) Panic(args ...any) { + l.undlerlying.Panic(args) } // Fatal logs a message at level Fatal then the process will exit with status set to 1. -func (l *Logger) Fatal(msg string) { - l.undlerlying.Fatal(msg) +func (l *Logger) Fatal(args string) { + l.undlerlying.Fatal(args) } // Debugf logs a message at level Debug. @@ -63,85 +57,136 @@ func (l *Logger) Debugf(format string, args ...interface{}) { // Infof logs a message at level Info. func (l *Logger) Infof(format string, args ...interface{}) { - zap.S().Infof(format, args...) + l.undlerlying.Infof(format, args...) } // Warnf logs a message at level Warn. func (l *Logger) Warnf(format string, args ...interface{}) { - zap.S().Warnf(format, args...) + l.undlerlying.Warnf(format, args...) } // Errorf logs a message at level Error. func (l *Logger) Errorf(format string, args ...interface{}) { - zap.S().Errorf(format, args...) + l.undlerlying.Errorf(format, args...) +} + +// Fatalf logs a message at level Fatal. +func (l *Logger) Fatalf(format string, args ...interface{}) { + l.undlerlying.Fatalf(format, args...) +} + +func (l *Logger) WithError(err error) *Logger { + return l.WithField("error", err.Error()) +} + +// WithStacktrace returns a new Logger obtained by adding error information and, if available, a stack trace +// as fields +func (l *Logger) WithStacktrace(err error) *Logger { + logger := l.WithError(err) + if stackErr, ok := err.(stackTracer); ok { + return logger.WithField("stacktrace", stackErr.StackTrace()) + } else { + return logger + } +} + +func (l *Logger) WithField(args ...any) *Logger { + return &Logger{ + undlerlying: l.undlerlying.With(args...), + } +} + +func (l *Logger) WithFields(args map[string]any) *Logger { + fields := make([]any, 0, len(args)) + for key, value := range args { + fields = append(fields, zap.Any(key, value)) + } + return With(fields...) +} + +func (l *Logger) WithCallerSkip(skip int) *Logger { + return &Logger{ + undlerlying: l.undlerlying.WithOptions(zap.AddCallerSkip(skip)), + } +} + +var stdLogger = &Logger{zap.S()} + +func StdLogger() *Logger { + return stdLogger +} + +func SetDefaultLogger(l *Logger) { + stdLogger = l } // Debug logs a message at level Debug. func Debug(msg string) { - zap.S().Debug(msg) + stdLogger.Debug(msg) } // Info logs a message at level Info. func Info(msg string) { - zap.S().Info(msg) + stdLogger.Info(msg) } // Warn logs a message at level Warn on the standard logger. func Warn(msg string) { - zap.S().Warn(msg) + stdLogger.Warn(msg) } // Error logs a message at level Error on the standard logger. func Error(msg string) { - zap.S().Error(msg) + stdLogger.Error(msg) } // Panic logs a message at level Panic on the standard logger. func Panic(msg string) { - zap.S().Panic(msg) + stdLogger.Panic(msg) } // Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1. func Fatal(msg string) { - zap.S().Fatal(msg) + stdLogger.Fatal(msg) } // Debugf logs a message at level Debug on the standard logger. func Debugf(format string, args ...interface{}) { - zap.S().Debugf(format, args...) + stdLogger.Debugf(format, args...) } // Infof logs a message at level Info on the standard logger. func Infof(format string, args ...interface{}) { - zap.S().Infof(format, args...) + stdLogger.Infof(format, args...) } // Warnf logs a message at level Warn on the standard logger. func Warnf(format string, args ...interface{}) { - zap.S().Warnf(format, args...) + stdLogger.Warnf(format, args...) } // Errorf logs a message at level Error on the standard logger. func Errorf(format string, args ...interface{}) { - zap.S().Errorf(format, args...) -} - -// Panicf logs a message at level Panic on the standard logger. -func Panicf(format string, args ...interface{}) { - zap.S().Panicf(format, args...) + stdLogger.Errorf(format, args...) } // Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1. func Fatalf(format string, args ...interface{}) { - zap.S().Fatalf(format, args...) + stdLogger.Fatalf(format, args...) } func With(args ...any) *Logger { - return &Logger{ - undlerlying: zap.S().With(args), - } + return stdLogger.WithField(args...) +} + +func WithMany(args map[string]any) *Logger { + return stdLogger.WithField(args) } func WithError(err error) *Logger { - return With("error", err.Error()) + return stdLogger.WithError(err) +} + +func WithStacktrace(err error) *Logger { + return stdLogger.WithStacktrace(err) } diff --git a/internal/common/logging/pulsar_adapter.go b/internal/common/logging/pulsar_adapter.go new file mode 100644 index 00000000000..8d6f298a85f --- /dev/null +++ b/internal/common/logging/pulsar_adapter.go @@ -0,0 +1,71 @@ +package logging + +import ( + pulsarlog "github.com/apache/pulsar-client-go/pulsar/log" +) + +type pulsarWrapper struct { + l *Logger +} + +func NewPulsarLogger() pulsarlog.Logger { + return &pulsarWrapper{ + l: StdLogger(), + } +} + +func (p pulsarWrapper) SubLogger(fs pulsarlog.Fields) pulsarlog.Logger { + return &pulsarWrapper{ + l: p.l.WithFields(fs), + } +} + +func (p pulsarWrapper) WithFields(fs pulsarlog.Fields) pulsarlog.Entry { + return &pulsarWrapper{ + l: p.l.WithFields(fs), + } +} + +func (p pulsarWrapper) WithField(name string, value interface{}) pulsarlog.Entry { + return &pulsarWrapper{ + l: p.l.WithField(name, value), + } +} + +func (p pulsarWrapper) WithError(err error) pulsarlog.Entry { + return &pulsarWrapper{ + l: p.l.WithError(err), + } +} + +func (p pulsarWrapper) Debug(args ...any) { + p.l.Debug(args) +} + +func (p pulsarWrapper) Info(args ...any) { + p.l.Info(args) +} + +func (p pulsarWrapper) Warn(args ...any) { + p.l.Warn(args) +} + +func (p pulsarWrapper) Error(args ...any) { + p.l.Error(args) +} + +func (p pulsarWrapper) Debugf(format string, args ...any) { + p.l.Debugf(format, args) +} + +func (p pulsarWrapper) Infof(format string, args ...any) { + p.l.Infof(format, args) +} + +func (p pulsarWrapper) Warnf(format string, args ...any) { + p.l.Warnf(format, args) +} + +func (p pulsarWrapper) Errorf(format string, args ...any) { + p.l.Errorf(format, args) +} diff --git a/internal/common/logging/stacktrace.go b/internal/common/logging/stacktrace.go deleted file mode 100644 index 612fcc982a7..00000000000 --- a/internal/common/logging/stacktrace.go +++ /dev/null @@ -1,21 +0,0 @@ -package logging - -import ( - "github.com/pkg/errors" -) - -// Unexported but considered part of the stable interface of pkg/errors. -type stackTracer interface { - StackTrace() errors.StackTrace -} - -// WithStacktrace returns a new Logger obtained by adding error information and, if available, a stack trace -// as fields -func WithStacktrace(logger *Logger, err error) *Logger { - logger = logger.WithError(err) - if stackErr, ok := err.(stackTracer); ok { - return logger.With("stacktrace", stackErr.StackTrace()) - } else { - return logger - } -} diff --git a/internal/common/profiling/http.go b/internal/common/profiling/http.go index 686edd46477..88f60344443 100644 --- a/internal/common/profiling/http.go +++ b/internal/common/profiling/http.go @@ -10,7 +10,6 @@ import ( "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/auth" - "github.com/armadaproject/armada/internal/common/logging" log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling/configuration" "github.com/armadaproject/armada/internal/common/serve" @@ -54,7 +53,7 @@ func SetupPprof(config *configuration.ProfilingConfig, ctx *armadacontext.Contex serveFunc := func() error { if err := serve.ListenAndServe(ctx, pprofServer); err != nil { - logging.WithStacktrace(ctx.Logger, err).Error("pprof server failure") + ctx.Logger().WithStacktrace(err).Error("pprof server failure") } return err } diff --git a/internal/common/pulsarutils/publisher.go b/internal/common/pulsarutils/publisher.go index cb4113f1d4f..90b20839cd9 100644 --- a/internal/common/pulsarutils/publisher.go +++ b/internal/common/pulsarutils/publisher.go @@ -10,7 +10,6 @@ import ( "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/ingest/utils" - "github.com/armadaproject/armada/internal/common/logging" psutils "github.com/armadaproject/armada/internal/common/pulsarutils/utils" ) @@ -78,8 +77,8 @@ func (p *PulsarPublisher[T]) PublishMessages(ctx *armadacontext.Context, events for _, msg := range msgs { p.producer.SendAsync(sendCtx, msg, func(_ pulsar.MessageID, _ *pulsar.ProducerMessage, err error) { if err != nil { - logging. - WithStacktrace(ctx, err). + ctx.Logger(). + WithStacktrace(err). Error("error sending message to Pulsar") errored = true } diff --git a/internal/common/pulsarutils/pulsarclient.go b/internal/common/pulsarutils/pulsarclient.go index 22d53c852f5..9e439b0dcb4 100644 --- a/internal/common/pulsarutils/pulsarclient.go +++ b/internal/common/pulsarutils/pulsarclient.go @@ -1,6 +1,7 @@ package pulsarutils import ( + "github.com/armadaproject/armada/internal/common/logging" "strings" "github.com/apache/pulsar-client-go/pulsar" @@ -39,5 +40,6 @@ func NewPulsarClient(config *commonconfig.PulsarConfig) (pulsar.Client, error) { TLSAllowInsecureConnection: config.TLSAllowInsecureConnection, MaxConnectionsPerBroker: config.MaxConnectionsPerBroker, Authentication: authentication, + Logger: logging.NewPulsarLogger(), }) } diff --git a/internal/common/resource/resource_test.go b/internal/common/resource/resource_test.go index 80752ca18aa..184d1fa9067 100644 --- a/internal/common/resource/resource_test.go +++ b/internal/common/resource/resource_test.go @@ -142,7 +142,7 @@ func TestTotalResourceRequest_ShouldSumAllContainers(t *testing.T) { func TestTotalResourceRequestShouldReportMaxInitContainerValues(t *testing.T) { highCpuResource := makeContainerResource(1000, 5) highRamResource := makeContainerResource(100, 500) - // With init containers, it should take the max of each individual resource from all init containers + // WithField init containers, it should take the max of each individual resource from all init containers expectedResult := makeContainerResource(1000, 500) pod := makePodWithResource([]*v1.ResourceList{}, []*v1.ResourceList{&highCpuResource, &highRamResource}) diff --git a/internal/common/serve/static.go b/internal/common/serve/static.go index 662965de0b9..355ae05d8db 100644 --- a/internal/common/serve/static.go +++ b/internal/common/serve/static.go @@ -7,7 +7,6 @@ import ( "github.com/pkg/errors" "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/logging" ) // dirWithIndexFallback is a http.FileSystem that serves the index.html file at @@ -41,7 +40,7 @@ func ListenAndServe(ctx *armadacontext.Context, server *http.Server) error { // Shutdown server on ctx done. <-ctx.Done() if err := server.Shutdown(ctx); err != nil { - logging.WithStacktrace(ctx, err).Errorf("failed to shutdown server serving %s", server.Addr) + ctx.Logger().WithStacktrace(err).Errorf("failed to shutdown server serving %s", server.Addr) } }() if err := server.ListenAndServe(); err != nil { diff --git a/internal/common/startup.go b/internal/common/startup.go index 0196e9e570d..a39e922037a 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -3,27 +3,22 @@ package common import ( "crypto/tls" "fmt" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" "net/http" "os" - "path" - "runtime" - "strconv" "strings" "time" "github.com/mitchellh/mapstructure" - "golang.org/x/exp/slices" - - "github.com/armadaproject/armada/internal/common/certs" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/spf13/pflag" "github.com/spf13/viper" - "github.com/weaveworks/promrus" + "golang.org/x/exp/slices" "github.com/armadaproject/armada/internal/common/armadacontext" + "github.com/armadaproject/armada/internal/common/certs" commonconfig "github.com/armadaproject/armada/internal/common/config" log "github.com/armadaproject/armada/internal/common/logging" ) @@ -90,7 +85,7 @@ func LoadConfig(config commonconfig.Config, defaultPath string, overrideConfigs } if err := config.Validate(); err != nil { - log.Error(commonconfig.FormatValidationErrors(err)) + log.Error(commonconfig.FormatValidationErrors(err).Error()) os.Exit(-1) } @@ -103,58 +98,45 @@ func UnmarshalKey(v *viper.Viper, key string, item interface{}) error { // TODO Move logging-related code out of common into a new package internal/logging func ConfigureCommandLineLogging() { - commandLineFormatter := new(logging.CommandLineFormatter) - log.SetFormatter(commandLineFormatter) - log.SetOutput(os.Stdout) + panic("not implemented") } func ConfigureLogging() { - log.SetLevel(readEnvironmentLogLevel()) - log.SetFormatter(readEnvironmentLogFormat()) - log.SetReportCaller(true) - log.SetOutput(os.Stdout) + pe := zap.NewProductionEncoderConfig() + pe.EncodeTime = zapcore.ISO8601TimeEncoder + pe.ConsoleSeparator = " " + pe.EncodeLevel = zapcore.CapitalLevelEncoder + consoleEncoder := zapcore.NewConsoleEncoder(pe) + pe.EncodeCaller = ShortCallerEncoder + + level := zap.InfoLevel + core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), level) + l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) + log.SetDefaultLogger(log.FromZap(l)) } -func readEnvironmentLogLevel() log.Level { +func readEnvironmentLogLevel() zapcore.Level { level, ok := os.LookupEnv("LOG_LEVEL") if ok { - logLevel, err := log.ParseLevel(level) - if err == nil { - return logLevel + // Parse the log level + switch level { + case "debug": + return zapcore.DebugLevel + case "info": + return zapcore.InfoLevel + case "warn", "warning": + return zapcore.WarnLevel + case "error": + return zapcore.ErrorLevel + case "panic": + return zapcore.PanicLevel + case "fatal": + return zapcore.FatalLevel + default: + println(fmt.Sprintf("Unknown log level %s", level)) } } - return log.InfoLevel -} - -func readEnvironmentLogFormat() log.Formatter { - formatStr, ok := os.LookupEnv("LOG_FORMAT") - if !ok { - formatStr = "colourful" - } - - textFormatter := &log.TextFormatter{ - ForceColors: true, - FullTimestamp: true, - TimestampFormat: logTimestampFormat, - CallerPrettyfier: func(frame *runtime.Frame) (function string, file string) { - fileName := path.Base(frame.File) + ":" + strconv.Itoa(frame.Line) - return "", fileName - }, - } - - switch strings.ToLower(formatStr) { - case "json": - return &log.JSONFormatter{TimestampFormat: logTimestampFormat} - case "colourful": - return textFormatter - case "text": - textFormatter.ForceColors = false - textFormatter.DisableColors = true - return textFormatter - default: - println(os.Stderr, fmt.Sprintf("Unknown log format %s, defaulting to colourful format", formatStr)) - return textFormatter - } + return zapcore.InfoLevel } func ServeMetrics(port uint16) (shutdown func()) { @@ -162,9 +144,6 @@ func ServeMetrics(port uint16) (shutdown func()) { } func ServeMetricsFor(port uint16, gatherer prometheus.Gatherer) (shutdown func()) { - hook := promrus.MustNewPrometheusHook() - log.AddHook(hook) - mux := http.NewServeMux() mux.Handle("/metrics", promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{})) return ServeHttp(port, mux) @@ -192,7 +171,7 @@ func serveHttp(port uint16, mux http.Handler, useTls bool, certFile, keyFile str } go func() { - log.Printf("Starting %s server listening on %d", scheme, port) + log.Infof("Starting %s server listening on %d", scheme, port) var err error if useTls { certWatcher := certs.NewCachedCertificateService(certFile, keyFile, time.Minute) @@ -217,10 +196,23 @@ func serveHttp(port uint16, mux http.Handler, useTls bool, certFile, keyFile str return func() { ctx, cancel := armadacontext.WithTimeout(armadacontext.Background(), 5*time.Second) defer cancel() - log.Printf("Stopping %s server listening on %d", scheme, port) + log.Infof("Stopping %s server listening on %d", scheme, port) e := srv.Shutdown(ctx) if e != nil { panic(e) } } } + +// ShortCallerEncoder serializes a caller in package/file:line format, trimming +// all but the final directory from the full path. +func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { + trimmed := caller.TrimmedPath() + lastSlash := strings.LastIndexByte(trimmed, '/') + if lastSlash != -1 && lastSlash != len(trimmed)-1 { + fileName := trimmed[lastSlash+1:] + enc.AppendString(fileName) + } else { + enc.AppendString(trimmed) + } +} diff --git a/internal/executor/reporter/job_event_reporter.go b/internal/executor/reporter/job_event_reporter.go index 3be27df5e7a..3df11d42696 100644 --- a/internal/executor/reporter/job_event_reporter.go +++ b/internal/executor/reporter/job_event_reporter.go @@ -9,11 +9,10 @@ import ( "k8s.io/utils/clock" log "github.com/armadaproject/armada/internal/common/logging" - clusterCont clusterContext "github.com/armadaproject/armada/internal/executor/context" domain2 "github.com/armadaproject/armada/internal/executor/domain" "github.com/armadaproject/armada/internal/executor/job" - log "github.com/armadaproject/armada/internal/common/logging" + "github.com/armadaproject/armada/internal/executor/util" ) type EventReporter interface { diff --git a/internal/lookoutv2/repository/util.go b/internal/lookoutv2/repository/util.go index 97d85cf9a53..6768262b0a4 100644 --- a/internal/lookoutv2/repository/util.go +++ b/internal/lookoutv2/repository/util.go @@ -707,15 +707,15 @@ func prefixAnnotations(prefix string, annotations map[string]string) map[string] func logQueryDebug(query *Query, description string) { log. With("query", removeNewlinesAndTabs(query.Sql)). - With("values", query.Args). + WithField("values", query.Args). Debug(description) } func logQueryError(query *Query, description string, duration time.Duration) { log. With("query", removeNewlinesAndTabs(query.Sql)). - With("values", query.Args). - With("duration", duration). + WithField("values", query.Args). + WithField("duration", duration). Errorf("Error executing %s query", description) } @@ -723,8 +723,8 @@ func logSlowQuery(query *Query, description string, duration time.Duration) { if duration > 5*time.Second { log. With("query", removeNewlinesAndTabs(query.Sql)). - With("values", query.Args). - With("duration", duration). + WithField("values", query.Args). + WithField("duration", duration). Infof("Slow %s query detected", description) } } diff --git a/internal/scheduler/nodedb/nodeiteration.go b/internal/scheduler/nodedb/nodeiteration.go index d8260270fa2..fda04565657 100644 --- a/internal/scheduler/nodedb/nodeiteration.go +++ b/internal/scheduler/nodedb/nodeiteration.go @@ -219,7 +219,7 @@ type NodeTypeIterator struct { priority int32 // Used to index into node.keys to assert that keys are always increasing. // This to detect if the iterator gets stuck. - // TODO(albin): With better testing we should be able to remove this. + // TODO(albin): WithField better testing we should be able to remove this. keyIndex int // Name of the memdb index used for node iteration. // Should correspond to the priority set for this iterator. diff --git a/pkg/client/auth/oidc/kubernetes.go b/pkg/client/auth/oidc/kubernetes.go index 806229cbbe5..4e141f4bf4c 100644 --- a/pkg/client/auth/oidc/kubernetes.go +++ b/pkg/client/auth/oidc/kubernetes.go @@ -12,8 +12,9 @@ import ( "strings" "time" - log "github.com/armadaproject/armada/internal/common/logging" + "golang.org/x/oauth2" + log "github.com/armadaproject/armada/internal/common/logging" ) type KubernetesDetails struct { diff --git a/pkg/client/auth/oidc/pkce.go b/pkg/client/auth/oidc/pkce.go index b2f4b706b0a..7d02a47a366 100644 --- a/pkg/client/auth/oidc/pkce.go +++ b/pkg/client/auth/oidc/pkce.go @@ -94,14 +94,14 @@ func AuthenticatePkce(config PKCEDetails) (*TokenCredentials, error) { go func() { if err := server.Serve(listener); err != nil { - log.WithStacktrace(log.NewLogger(), err).Error("unable to serve") + log.WithStacktrace(err).Error("unable to serve") } }() cmd, err := openBrowser("http://" + localUrl) defer func() { if err := cmd.Process.Kill(); err != nil { - log.WithStacktrace(log.NewLogger(), err).Error("unable to kill process") + log.WithStacktrace(err).Error("unable to kill process") } }() diff --git a/pkg/client/load-test.go b/pkg/client/load-test.go index 0a2594525ca..ac947603289 100644 --- a/pkg/client/load-test.go +++ b/pkg/client/load-test.go @@ -255,7 +255,7 @@ func (apiLoadTester ArmadaLoadTester) monitorJobsUntilCompletion( return nil }) if err != nil { - log.WithStacktrace(log.NewLogger(), err).Error("unable to monitor jobs") + log.WithStacktrace(err).Error("unable to monitor jobs") } return submittedIds } @@ -288,7 +288,7 @@ func (apiLoadTester ArmadaLoadTester) cancelRemainingJobs(queue string, jobSetId return err }) if err != nil { - log.WithStacktrace(log.NewLogger(), err).Error("unable to cancel jobs") + log.WithStacktrace(err).Error("unable to cancel jobs") } } diff --git a/pkg/client/watch.go b/pkg/client/watch.go index b333fb95ff3..7b395cf5938 100644 --- a/pkg/client/watch.go +++ b/pkg/client/watch.go @@ -71,7 +71,7 @@ func WatchJobSetWithJobIdsFilter( ) if e != nil { - log.Error(e) + log.Error(e.Error()) time.Sleep(5 * time.Second) continue } @@ -94,7 +94,7 @@ func WatchJobSetWithJobIdsFilter( return state } if !isTransportClosingError(e) { - log.Error(e) + log.Error(e.Error()) } time.Sleep(5 * time.Second) break @@ -104,7 +104,7 @@ func WatchJobSetWithJobIdsFilter( event, e := api.UnwrapEvent(msg.Message) if e != nil { // This can mean that the event type reported from server is unknown to the client - log.Error(e) + log.Error(e.Error()) continue } From da74217b987efc8e8aaf44dc1ceda68afa29defb Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 17:39:56 +0000 Subject: [PATCH 12/58] wip --- internal/common/etcdhealth/etcdhealth_test.go | 2 +- .../common/ingest/controlplaneevents/utils.go | 2 +- internal/eventingester/store/eventstore.go | 3 +-- .../executor/service/cluster_allocation.go | 2 +- internal/lookoutv2/application.go | 2 +- internal/scheduler/api.go | 3 +-- internal/scheduler/metrics.go | 5 ++--- internal/scheduler/scheduler.go | 17 +++++++++------- internal/scheduler/schedulerapp.go | 5 ++--- .../scheduling/preempting_queue_scheduler.go | 20 +++++++++---------- internal/scheduler/submitcheck.go | 13 ++++++------ internal/scheduleringester/ingester.go | 2 +- 12 files changed, 37 insertions(+), 39 deletions(-) diff --git a/internal/common/etcdhealth/etcdhealth_test.go b/internal/common/etcdhealth/etcdhealth_test.go index 0a38def34ee..cc81b7e0e18 100644 --- a/internal/common/etcdhealth/etcdhealth_test.go +++ b/internal/common/etcdhealth/etcdhealth_test.go @@ -26,7 +26,7 @@ func TestEtcdReplicaHealthMonitor(t *testing.T) { ctx, cancel := armadacontext.WithCancel(armadacontext.Background()) defer cancel() g, ctx := armadacontext.ErrGroup(ctx) - g.Go(func() error { return hm.Run(ctx, logging.NewLogger()) }) + g.Go(func() error { return hm.Run(ctx, logging.StdLogger()) }) // Should still be unavailable due to missing metrics. hm.BlockUntilNextMetricsCollection(ctx) diff --git a/internal/common/ingest/controlplaneevents/utils.go b/internal/common/ingest/controlplaneevents/utils.go index f388d5c30ae..836548d27a4 100644 --- a/internal/common/ingest/controlplaneevents/utils.go +++ b/internal/common/ingest/controlplaneevents/utils.go @@ -7,7 +7,7 @@ import ( commonmetrics "github.com/armadaproject/armada/internal/common/ingest/metrics" "github.com/armadaproject/armada/internal/common/ingest/utils" log "github.com/armadaproject/armada/internal/common/logging" - + "github.com/armadaproject/armada/pkg/controlplaneevents" ) func MessageUnmarshaller(msg pulsar.ConsumerMessage, metrics *commonmetrics.Metrics) *utils.EventsWithIds[*controlplaneevents.Event] { diff --git a/internal/eventingester/store/eventstore.go b/internal/eventingester/store/eventstore.go index c46a0aa2e00..d650161544e 100644 --- a/internal/eventingester/store/eventstore.go +++ b/internal/eventingester/store/eventstore.go @@ -1,6 +1,7 @@ package store import ( + "github.com/armadaproject/armada/internal/eventingester/model" "regexp" "time" @@ -10,9 +11,7 @@ import ( "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/ingest" log "github.com/armadaproject/armada/internal/common/logging" - "github "github.com/armadaproject/armada/internal/eventingester/configuration" - log "github.com/armadaproject/armada/internal/common/logging" ) const ( diff --git a/internal/executor/service/cluster_allocation.go b/internal/executor/service/cluster_allocation.go index 875beadc351..703d41a0526 100644 --- a/internal/executor/service/cluster_allocation.go +++ b/internal/executor/service/cluster_allocation.go @@ -48,7 +48,7 @@ func (allocationService *ClusterAllocationService) AllocateSpareClusterCapacity( // If a health monitor is provided, avoid leasing jobs when the cluster is unhealthy. if allocationService.clusterHealthMonitor != nil { if ok, reason, err := allocationService.clusterHealthMonitor.IsHealthy(); err != nil { - logging.WithStacktrace(logging.NewLogger(), err).Error("failed to check cluster health") + logging.WithStacktrace(err).Error("failed to check cluster health") return } else if !ok { log.Warnf("cluster is not healthy; will not request more jobs: %s", reason) diff --git a/internal/lookoutv2/application.go b/internal/lookoutv2/application.go index ee26199923f..3969947ed91 100644 --- a/internal/lookoutv2/application.go +++ b/internal/lookoutv2/application.go @@ -47,7 +47,7 @@ func Serve(configuration configuration.LookoutV2Config) error { // create new service API api := operations.NewLookoutAPI(swaggerSpec) - logger := logging.NewLogger() + logger := logging.StdLogger() api.Logger = logger.Debugf diff --git a/internal/scheduler/api.go b/internal/scheduler/api.go index d6164f99a2a..608faa82a85 100644 --- a/internal/scheduler/api.go +++ b/internal/scheduler/api.go @@ -16,7 +16,6 @@ import ( "github.com/armadaproject/armada/internal/common/armadaerrors" "github.com/armadaproject/armada/internal/common/auth" "github.com/armadaproject/armada/internal/common/compress" - "github.com/armadaproject/armada/internal/common/logging" log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/maps" "github.com/armadaproject/armada/internal/common/pulsarutils" @@ -371,7 +370,7 @@ func (srv *ExecutorApi) executorFromLeaseRequest(ctx *armadacontext.Context, req now := srv.clock.Now().UTC() for _, nodeInfo := range req.Nodes { if node, err := executorapi.NewNodeFromNodeInfo(nodeInfo, req.ExecutorId, srv.allowedPriorities, now); err != nil { - logging.WithStacktrace(ctx, err).Warnf( + ctx.Logger().WithStacktrace(err).Warnf( "skipping node %s from executor %s", nodeInfo.GetName(), req.GetExecutorId(), ) } else { diff --git a/internal/scheduler/metrics.go b/internal/scheduler/metrics.go index 8cc2f3f49d1..a64f040efb5 100644 --- a/internal/scheduler/metrics.go +++ b/internal/scheduler/metrics.go @@ -10,7 +10,6 @@ import ( "k8s.io/utils/clock" "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/logging" armadamaps "github.com/armadaproject/armada/internal/common/maps" commonmetrics "github.com/armadaproject/armada/internal/common/metrics" "github.com/armadaproject/armada/internal/common/resource" @@ -103,8 +102,8 @@ func (c *MetricsCollector) Run(ctx *armadacontext.Context) error { case <-ticker.C(): err := c.refresh(ctx) if err != nil { - logging. - WithStacktrace(ctx, err). + ctx.Logger(). + WithStacktrace(err). Warnf("error refreshing metrics state") } } diff --git a/internal/scheduler/scheduler.go b/internal/scheduler/scheduler.go index fd796e1bd82..356817ddaef 100644 --- a/internal/scheduler/scheduler.go +++ b/internal/scheduler/scheduler.go @@ -13,7 +13,6 @@ import ( "k8s.io/utils/clock" "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/logging" protoutil "github.com/armadaproject/armada/internal/common/proto" "github.com/armadaproject/armada/internal/scheduler/database" "github.com/armadaproject/armada/internal/scheduler/jobdb" @@ -153,7 +152,7 @@ func (s *Scheduler) Run(ctx *armadacontext.Context) error { syncContext, cancel := armadacontext.WithTimeout(ctx, 5*time.Minute) err := s.ensureDbUpToDate(syncContext, 1*time.Second) if err != nil { - logging.WithStacktrace(ctx, err).Error("could not become leader") + ctx.Logger().WithStacktrace(err).Error("could not become leader") leaderToken = leader.InvalidLeaderToken() } else { fullUpdate = true @@ -175,7 +174,7 @@ func (s *Scheduler) Run(ctx *armadacontext.Context) error { result, err := s.cycle(ctx, fullUpdate, leaderToken, shouldSchedule) if err != nil { - logging.WithStacktrace(ctx, err).Error("scheduling cycle failure") + ctx.Logger().WithStacktrace(err).Error("scheduling cycle failure") leaderToken = leader.InvalidLeaderToken() } @@ -981,7 +980,9 @@ func (s *Scheduler) initialise(ctx *armadacontext.Context) error { return nil default: if _, _, err := s.syncState(ctx, true); err != nil { - logging.WithStacktrace(ctx, err).Error("failed to initialise; trying again in 1 second") + ctx.Logger(). + WithStacktrace(err). + Error("failed to initialise; trying again in 1 second") time.Sleep(1 * time.Second) } else { ctx.Info("initialisation succeeded") @@ -1008,7 +1009,9 @@ func (s *Scheduler) ensureDbUpToDate(ctx *armadacontext.Context, pollInterval ti default: numSent, err = s.publisher.PublishMarkers(ctx, groupId) if err != nil { - logging.WithStacktrace(ctx, err).Error("Error sending marker messages to pulsar") + ctx.Logger(). + WithStacktrace(err). + Error("Error sending marker messages to pulsar") s.clock.Sleep(pollInterval) } else { messagesSent = true @@ -1024,8 +1027,8 @@ func (s *Scheduler) ensureDbUpToDate(ctx *armadacontext.Context, pollInterval ti default: numReceived, err := s.jobRepository.CountReceivedPartitions(ctx, groupId) if err != nil { - logging. - WithStacktrace(ctx, err). + ctx.Logger(). + WithStacktrace(err). Error("Error querying the database or marker messages") } if numSent == numReceived { diff --git a/internal/scheduler/schedulerapp.go b/internal/scheduler/schedulerapp.go index d69280ebd9a..c45097a59df 100644 --- a/internal/scheduler/schedulerapp.go +++ b/internal/scheduler/schedulerapp.go @@ -25,7 +25,6 @@ import ( dbcommon "github.com/armadaproject/armada/internal/common/database" grpcCommon "github.com/armadaproject/armada/internal/common/grpc" "github.com/armadaproject/armada/internal/common/health" - "github.com/armadaproject/armada/internal/common/logging" log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/internal/common/pulsarutils" @@ -118,8 +117,8 @@ func Run(config schedulerconfig.Configuration) error { defer func() { err := conn.Close() if err != nil { - logging. - WithStacktrace(ctx, err). + ctx.Logger(). + WithStacktrace(err). Warnf("Armada api client didn't close down cleanly") } }() diff --git a/internal/scheduler/scheduling/preempting_queue_scheduler.go b/internal/scheduler/scheduling/preempting_queue_scheduler.go index 47207a5b8e0..c5609fc7c3f 100644 --- a/internal/scheduler/scheduling/preempting_queue_scheduler.go +++ b/internal/scheduler/scheduling/preempting_queue_scheduler.go @@ -99,7 +99,7 @@ func (sch *PreemptingQueueScheduler) Schedule(ctx *armadacontext.Context) (*Sche scheduledJobsById := make(map[string]*schedulercontext.JobSchedulingContext) // Evict preemptible jobs. - ctx.WithField("stage", "scheduling-algo").Infof("Evicting preemptible jobs") + ctx.Logger().WithField("stage", "scheduling-algo").Infof("Evicting preemptible jobs") evictorResult, inMemoryJobRepo, err := sch.evict( armadacontext.WithLogField(ctx, "stage", "evict for resource balancing"), NewNodeEvictor( @@ -144,14 +144,14 @@ func (sch *PreemptingQueueScheduler) Schedule(ctx *armadacontext.Context) (*Sche if err != nil { return nil, err } - ctx.WithField("stage", "scheduling-algo").Info("Finished evicting preemptible jobs") + ctx.Logger().WithField("stage", "scheduling-algo").Info("Finished evicting preemptible jobs") for _, jctx := range evictorResult.EvictedJctxsByJobId { preemptedJobsById[jctx.JobId] = jctx } maps.Copy(sch.nodeIdByJobId, evictorResult.NodeIdByJobId) // Re-schedule evicted jobs/schedule new jobs. - ctx.WithField("stage", "scheduling-algo").Info("Performing initial scheduling of jobs onto nodes") + ctx.Logger().WithField("stage", "scheduling-algo").Info("Performing initial scheduling of jobs onto nodes") schedulerResult, err := sch.schedule( armadacontext.WithLogField(ctx, "stage", "re-schedule after balancing eviction"), inMemoryJobRepo, @@ -162,7 +162,7 @@ func (sch *PreemptingQueueScheduler) Schedule(ctx *armadacontext.Context) (*Sche if err != nil { return nil, err } - ctx.WithField("stage", "scheduling-algo").Info("Finished initial scheduling of jobs onto nodes") + ctx.Logger().WithField("stage", "scheduling-algo").Info("Finished initial scheduling of jobs onto nodes") for _, jctx := range schedulerResult.ScheduledJobs { if _, ok := preemptedJobsById[jctx.JobId]; ok { delete(preemptedJobsById, jctx.JobId) @@ -173,7 +173,7 @@ func (sch *PreemptingQueueScheduler) Schedule(ctx *armadacontext.Context) (*Sche maps.Copy(sch.nodeIdByJobId, schedulerResult.NodeIdByJobId) // Evict jobs on oversubscribed nodes. - ctx.WithField("stage", "scheduling-algo").Info("Evicting jobs from oversubscribed nodes") + ctx.Logger().WithField("stage", "scheduling-algo").Info("Evicting jobs from oversubscribed nodes") reevictResult, inMemoryJobRepo, err := sch.evict( armadacontext.WithLogField(ctx, "stage", "evict oversubscribed"), NewOversubscribedEvictor( @@ -185,7 +185,7 @@ func (sch *PreemptingQueueScheduler) Schedule(ctx *armadacontext.Context) (*Sche if err != nil { return nil, err } - ctx.WithField("stage", "scheduling-algo").Info("Finished evicting jobs from oversubscribed nodes") + ctx.Logger().WithField("stage", "scheduling-algo").Info("Finished evicting jobs from oversubscribed nodes") scheduledAndEvictedJobsById := armadamaps.FilterKeys( scheduledJobsById, func(jobId string) bool { @@ -205,7 +205,7 @@ func (sch *PreemptingQueueScheduler) Schedule(ctx *armadacontext.Context) (*Sche // Re-schedule evicted jobs/schedule new jobs. // Only necessary if a non-zero number of jobs were evicted. if len(reevictResult.EvictedJctxsByJobId) > 0 { - ctx.WithField("stage", "scheduling-algo").Info("Performing second scheduling ") + ctx.Logger().WithField("stage", "scheduling-algo").Info("Performing second scheduling ") rescheduleSchedulerResult, rescheduleErr := sch.schedule( armadacontext.WithLogField(ctx, "stage", "schedule after oversubscribed eviction"), inMemoryJobRepo, @@ -217,7 +217,7 @@ func (sch *PreemptingQueueScheduler) Schedule(ctx *armadacontext.Context) (*Sche if rescheduleErr != nil { return nil, rescheduleErr } - ctx.WithField("stage", "scheduling-algo").Info("Finished second scheduling pass") + ctx.Logger().WithField("stage", "scheduling-algo").Info("Finished second scheduling pass") for _, jctx := range rescheduleSchedulerResult.ScheduledJobs { if _, ok := preemptedJobsById[jctx.JobId]; ok { delete(preemptedJobsById, jctx.JobId) @@ -231,14 +231,14 @@ func (sch *PreemptingQueueScheduler) Schedule(ctx *armadacontext.Context) (*Sche preemptedJobs := maps.Values(preemptedJobsById) scheduledJobs := maps.Values(scheduledJobsById) - ctx.WithField("stage", "scheduling-algo").Infof("Unbinding %d preempted and %d evicted jobs", len(preemptedJobs), len(maps.Values(scheduledAndEvictedJobsById))) + ctx.Logger().WithField("stage", "scheduling-algo").Infof("Unbinding %d preempted and %d evicted jobs", len(preemptedJobs), len(maps.Values(scheduledAndEvictedJobsById))) if err := sch.unbindJobs(append( slices.Clone(preemptedJobs), maps.Values(scheduledAndEvictedJobsById)...), ); err != nil { return nil, err } - ctx.WithField("stage", "scheduling-algo").Infof("Finished unbinding preempted and evicted jobs") + ctx.Logger().WithField("stage", "scheduling-algo").Infof("Finished unbinding preempted and evicted jobs") PopulatePreemptionDescriptions(preemptedJobs, scheduledJobs) schedulercontext.PrintJobSummary(ctx, "Preempting running jobs;", preemptedJobs) diff --git a/internal/scheduler/submitcheck.go b/internal/scheduler/submitcheck.go index 49bee2d2786..def6dd7b317 100644 --- a/internal/scheduler/submitcheck.go +++ b/internal/scheduler/submitcheck.go @@ -11,7 +11,6 @@ import ( "k8s.io/utils/clock" "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/logging" armadaslices "github.com/armadaproject/armada/internal/common/slices" "github.com/armadaproject/armada/internal/scheduler/configuration" "github.com/armadaproject/armada/internal/scheduler/database" @@ -90,16 +89,16 @@ func (srv *SubmitChecker) Run(ctx *armadacontext.Context) error { func (srv *SubmitChecker) updateExecutors(ctx *armadacontext.Context) { queues, err := srv.queueCache.GetAll(ctx) if err != nil { - logging. - WithStacktrace(ctx, err). + ctx.Logger(). + WithStacktrace(err). Error("Error fetching queues") return } executors, err := srv.executorRepository.GetExecutors(ctx) if err != nil { - logging. - WithStacktrace(ctx, err). + ctx.Logger(). + WithStacktrace(err). Error("Error fetching executors") return } @@ -140,8 +139,8 @@ func (srv *SubmitChecker) updateExecutors(ctx *armadacontext.Context) { nodeDb: nodeDb, } } else { - logging. - WithStacktrace(ctx, err). + ctx.Logger(). + WithStacktrace(err). Warnf("Error constructing nodedb for executor: %s", ex.Id) } } diff --git a/internal/scheduleringester/ingester.go b/internal/scheduleringester/ingester.go index 74934daf9c9..fe0578f6028 100644 --- a/internal/scheduleringester/ingester.go +++ b/internal/scheduleringester/ingester.go @@ -15,7 +15,7 @@ import ( controlplaneevents_ingest_utils "github.com/armadaproject/armada/internal/common/ingest/controlplaneevents" "github.com/armadaproject/armada/internal/common/ingest/jobsetevents" "github.com/armadaproject/armada/internal/common/ingest/metrics" - log "github.com/armadaproject/armada/internal/common/loggin + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/pkg/armadaevents" "github.com/armadaproject/armada/pkg/controlplaneevents" From e249f2d410d3afcf792f28c1489b6c4a7aa333f4 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 17:49:45 +0000 Subject: [PATCH 13/58] wip --- internal/binoculars/server.go | 2 +- internal/binoculars/service/cordon_test.go | 5 +++-- internal/common/grpc/gateway.go | 3 ++- .../healthmonitor/multihealthmonitor.go | 2 +- internal/common/pulsarutils/pulsarclient.go | 2 +- internal/common/startup.go | 4 ++-- internal/eventingester/store/eventstore.go | 2 +- internal/executor/fake/application.go | 5 +++-- .../lookoutingesterv2/dbloadtester/queue.go | 2 +- internal/lookoutv2/application.go | 9 ++++---- ..._driven_preempting_queue_scheduler_test.go | 2 +- .../preempting_queue_scheduler_test.go | 21 +++++++++---------- internal/scheduler/simulator/simulator.go | 7 ++----- 13 files changed, 33 insertions(+), 33 deletions(-) diff --git a/internal/binoculars/server.go b/internal/binoculars/server.go index f11ccf7ff69..d1d604f7c67 100644 --- a/internal/binoculars/server.go +++ b/internal/binoculars/server.go @@ -13,7 +13,7 @@ import ( "github.com/armadaproject/armada/internal/common/cluster" grpcCommon "github.com/armadaproject/armada/internal/common/grpc" log "github.com/armadaproject/armada/internal/common/logging" - + "github.com/armadaproject/armada/pkg/api/binoculars" ) func StartUp(config *configuration.BinocularsConfig) (func(), *sync.WaitGroup) { diff --git a/internal/binoculars/service/cordon_test.go b/internal/binoculars/service/cordon_test.go index 91efa8e425f..1245b2ee1f1 100644 --- a/internal/binoculars/service/cordon_test.go +++ b/internal/binoculars/service/cordon_test.go @@ -4,9 +4,10 @@ import ( "context" "encoding/json" "fmt" - "github.com/armadaproject/armada/internal/common/logging" "testing" + "github.com/armadaproject/armada/internal/common/logging" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "google.golang.org/grpc/codes" @@ -81,7 +82,7 @@ func TestCordonNode(t *testing.T) { cordonService, client := setupTest(t, cordonConfig, FakePermissionChecker{ReturnValue: true}) ctx := auth.WithPrincipal(context.Background(), principal) - err := cordonService.CordonNode(armadacontext.New(ctx, logging.NewLogger()), &binoculars.CordonRequest{ + err := cordonService.CordonNode(armadacontext.New(ctx, logging.StdLogger()), &binoculars.CordonRequest{ NodeName: defaultNode.Name, }) assert.Nil(t, err) diff --git a/internal/common/grpc/gateway.go b/internal/common/grpc/gateway.go index 66f8f3d0c99..9ae6aeddd09 100644 --- a/internal/common/grpc/gateway.go +++ b/internal/common/grpc/gateway.go @@ -8,13 +8,14 @@ import ( "path" "strings" - log "github.com/armadaproject/armada/internal/common/logging" "github.com/go-openapi/runtime/middleware" "github.com/grpc-ecosystem/grpc-gateway/runtime" "golang.org/x/exp/slices" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials/insecure" + + log "github.com/armadaproject/armada/internal/common/logging" ) // CreateGatewayHandler configures the gRPC API gateway diff --git a/internal/common/healthmonitor/multihealthmonitor.go b/internal/common/healthmonitor/multihealthmonitor.go index dc717d2ad75..3c4629ca8c4 100644 --- a/internal/common/healthmonitor/multihealthmonitor.go +++ b/internal/common/healthmonitor/multihealthmonitor.go @@ -2,7 +2,6 @@ package healthmonitor import ( "fmt" - "github.com/armadaproject/armada/internal/common/logging" "sync" "github.com/pkg/errors" @@ -10,6 +9,7 @@ import ( "golang.org/x/exp/maps" "github.com/armadaproject/armada/internal/common/armadacontext" + "github.com/armadaproject/armada/internal/common/logging" ) // MultiHealthMonitor wraps multiple HealthMonitors and itself implements the HealthMonitor interface. diff --git a/internal/common/pulsarutils/pulsarclient.go b/internal/common/pulsarutils/pulsarclient.go index 9e439b0dcb4..477910ee905 100644 --- a/internal/common/pulsarutils/pulsarclient.go +++ b/internal/common/pulsarutils/pulsarclient.go @@ -1,7 +1,6 @@ package pulsarutils import ( - "github.com/armadaproject/armada/internal/common/logging" "strings" "github.com/apache/pulsar-client-go/pulsar" @@ -9,6 +8,7 @@ import ( "github.com/armadaproject/armada/internal/common/armadaerrors" commonconfig "github.com/armadaproject/armada/internal/common/config" + "github.com/armadaproject/armada/internal/common/logging" ) func NewPulsarClient(config *commonconfig.PulsarConfig) (pulsar.Client, error) { diff --git a/internal/common/startup.go b/internal/common/startup.go index a39e922037a..038ea20f3e6 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -3,8 +3,6 @@ package common import ( "crypto/tls" "fmt" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" "net/http" "os" "strings" @@ -15,6 +13,8 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spf13/pflag" "github.com/spf13/viper" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" "golang.org/x/exp/slices" "github.com/armadaproject/armada/internal/common/armadacontext" diff --git a/internal/eventingester/store/eventstore.go b/internal/eventingester/store/eventstore.go index d650161544e..20a5012a48f 100644 --- a/internal/eventingester/store/eventstore.go +++ b/internal/eventingester/store/eventstore.go @@ -1,7 +1,6 @@ package store import ( - "github.com/armadaproject/armada/internal/eventingester/model" "regexp" "time" @@ -12,6 +11,7 @@ import ( "github.com/armadaproject/armada/internal/common/ingest" log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/eventingester/configuration" + "github.com/armadaproject/armada/internal/eventingester/model" ) const ( diff --git a/internal/executor/fake/application.go b/internal/executor/fake/application.go index 75036837d1b..e02200a5219 100644 --- a/internal/executor/fake/application.go +++ b/internal/executor/fake/application.go @@ -1,9 +1,10 @@ package fake import ( - "github.com/armadaproject/armada/internal/common/logging" "sync" + "github.com/armadaproject/armada/internal/common/logging" + "github.com/armadaproject/armada/internal/common/task" "github.com/armadaproject/armada/internal/executor" "github.com/armadaproject/armada/internal/executor/configuration" @@ -15,7 +16,7 @@ func StartUp(config configuration.ExecutorConfiguration, nodes []*context.NodeSp wg := &sync.WaitGroup{} wg.Add(1) return executor.StartUpWithContext( - logging.NewLogger(), + logging.StdLogger(), config, context.NewFakeClusterContext(config.Application, config.Kubernetes.NodeIdLabel, nodes), nil, diff --git a/internal/lookoutingesterv2/dbloadtester/queue.go b/internal/lookoutingesterv2/dbloadtester/queue.go index 7ff5be5dd28..30c60cbe3b5 100644 --- a/internal/lookoutingesterv2/dbloadtester/queue.go +++ b/internal/lookoutingesterv2/dbloadtester/queue.go @@ -79,7 +79,7 @@ func (q *QueueEventGenerator) Generate(eventsCh chan<- *utils.EventsWithIds[*arm } events, err := q.generateEventsAtTime(i) if err != nil { - log.Panicf("failed to generate events %s", err) + log.Fatalf("failed to generate events %s", err) } if len(events) == 0 { continue diff --git a/internal/lookoutv2/application.go b/internal/lookoutv2/application.go index 3969947ed91..ec6dbf6b10e 100644 --- a/internal/lookoutv2/application.go +++ b/internal/lookoutv2/application.go @@ -4,6 +4,11 @@ package lookoutv2 import ( "github.com/IBM/pgxpoolprometheus" + "github.com/go-openapi/loads" + "github.com/go-openapi/runtime/middleware" + "github.com/jessevdk/go-flags" + "github.com/prometheus/client_golang/prometheus" + "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/compress" @@ -15,10 +20,6 @@ import ( "github.com/armadaproject/armada/internal/lookoutv2/gen/restapi" "github.com/armadaproject/armada/internal/lookoutv2/gen/restapi/operations" "github.com/armadaproject/armada/internal/lookoutv2/repository" - "github.com/go-openapi/loads" - "github.com/go-openapi/runtime/middleware" - "github.com/jessevdk/go-flags" - "github.com/prometheus/client_golang/prometheus" ) func Serve(configuration configuration.LookoutV2Config) error { diff --git a/internal/scheduler/scheduling/market_driven_preempting_queue_scheduler_test.go b/internal/scheduler/scheduling/market_driven_preempting_queue_scheduler_test.go index e2363a19b37..24ece4726f3 100644 --- a/internal/scheduler/scheduling/market_driven_preempting_queue_scheduler_test.go +++ b/internal/scheduler/scheduling/market_driven_preempting_queue_scheduler_test.go @@ -351,7 +351,7 @@ func TestMarketDrivenPreemptingQueueScheduler(t *testing.T) { cordonedNodes := map[int]bool{} ctx := armadacontext.Background() for i, round := range tc.Rounds { - ctx.FieldLogger = ctx.WithField("round", i) + ctx = armadacontext.WithLogField(ctx, "round", i) ctx.Infof("starting scheduling round %d", i) jobsByNodeId := map[string][]*jobdb.Job{} diff --git a/internal/scheduler/scheduling/preempting_queue_scheduler_test.go b/internal/scheduler/scheduling/preempting_queue_scheduler_test.go index 88d70e37257..52c27aca4b0 100644 --- a/internal/scheduler/scheduling/preempting_queue_scheduler_test.go +++ b/internal/scheduler/scheduling/preempting_queue_scheduler_test.go @@ -1,6 +1,7 @@ package scheduling import ( + "context" "fmt" "math/rand" "testing" @@ -25,7 +26,7 @@ import ( "github.com/armadaproject/armada/internal/scheduler/jobdb" "github.com/armadaproject/armada/internal/scheduler/nodedb" schedulerconstraints "github.com/armadaproject/armada/internal/scheduler/scheduling/constraints" - "github.com/armadaproject/armada/internal/scheduler/scheduling/context" + schedulingcontext "github.com/armadaproject/armada/internal/scheduler/scheduling/context" "github.com/armadaproject/armada/internal/scheduler/scheduling/fairness" "github.com/armadaproject/armada/internal/scheduler/testfixtures" "github.com/armadaproject/armada/pkg/api" @@ -1939,7 +1940,7 @@ func TestPreemptingQueueScheduler(t *testing.T) { cordonedNodes := map[int]bool{} ctx := armadacontext.Background() for i, round := range tc.Rounds { - ctx.FieldLogger = ctx.WithField("round", i) + ctx = armadacontext.WithLogField(ctx, "round", i) ctx.Infof("starting scheduling round %d", i) jobsByNodeId := map[string][]*jobdb.Job{} @@ -2019,7 +2020,7 @@ func TestPreemptingQueueScheduler(t *testing.T) { tc.SchedulingConfig, ) require.NoError(t, err) - sctx := context.NewSchedulingContext( + sctx := schedulingcontext.NewSchedulingContext( testfixtures.TestPool, fairnessCostProvider, limiter, @@ -2205,7 +2206,7 @@ func TestPreemptingQueueScheduler(t *testing.T) { // which jobs are preempted). slices.SortFunc( result.ScheduledJobs, - func(a, b *context.JobSchedulingContext) int { + func(a, b *schedulingcontext.JobSchedulingContext) int { if a.Job.SubmitTime().Before(b.Job.SubmitTime()) { return -1 } else if b.Job.SubmitTime().Before(a.Job.SubmitTime()) { @@ -2238,7 +2239,7 @@ func TestPreemptingQueueScheduler(t *testing.T) { } } -func jobIdsByQueueFromJobContexts(jctxs []*context.JobSchedulingContext) map[string][]string { +func jobIdsByQueueFromJobContexts(jctxs []*schedulingcontext.JobSchedulingContext) map[string][]string { rv := make(map[string][]string) for _, jctx := range jctxs { job := jctx.Job @@ -2332,9 +2333,7 @@ func BenchmarkPreemptingQueueScheduler(b *testing.B) { } for name, tc := range tests { b.Run(name, func(b *testing.B) { - ctx := armadacontext.Background() - ctx.FieldLogger = logging.NullLogger - + ctx := armadacontext.New(context.Background(), logging.NullLogger) jobsByQueue := make(map[string][]*jobdb.Job) priorityFactorByQueue := make(map[string]float64) for i := 0; i < tc.NumQueues; i++ { @@ -2380,7 +2379,7 @@ func BenchmarkPreemptingQueueScheduler(b *testing.B) { tc.SchedulingConfig, ) require.NoError(b, err) - sctx := context.NewSchedulingContext( + sctx := schedulingcontext.NewSchedulingContext( testfixtures.TestPool, fairnessCostProvider, limiter, @@ -2426,7 +2425,7 @@ func BenchmarkPreemptingQueueScheduler(b *testing.B) { err = jobDbTxn.BatchDelete( armadaslices.Map( result.ScheduledJobs, - func(jctx *context.JobSchedulingContext) string { + func(jctx *schedulingcontext.JobSchedulingContext) string { return jctx.JobId }, ), @@ -2451,7 +2450,7 @@ func BenchmarkPreemptingQueueScheduler(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { - sctx := context.NewSchedulingContext( + sctx := schedulingcontext.NewSchedulingContext( "pool", fairnessCostProvider, limiter, diff --git a/internal/scheduler/simulator/simulator.go b/internal/scheduler/simulator/simulator.go index a2059fa317b..12d3754f808 100644 --- a/internal/scheduler/simulator/simulator.go +++ b/internal/scheduler/simulator/simulator.go @@ -525,7 +525,7 @@ func (s *Simulator) pushScheduleEvent(time time.Time) { func (s *Simulator) handleSimulatorEvent(ctx *armadacontext.Context, event Event) error { s.time = event.time - ctx = armadacontext.New(ctx.Context, ctx.FieldLogger.WithField("simulated time", event.time)) + ctx = armadacontext.WithLogField(ctx, "simulated time", event.time) switch e := event.eventSequenceOrScheduleEvent.(type) { case *armadaevents.EventSequence: if err := s.handleEventSequence(ctx, e); err != nil { @@ -609,10 +609,7 @@ func (s *Simulator) handleScheduleEvent(ctx *armadacontext.Context) error { schedulerCtx := ctx if s.SuppressSchedulerLogs { - schedulerCtx = &armadacontext.Context{ - Context: ctx.Context, - FieldLogger: logging.NullLogger, - } + schedulerCtx = armadacontext.New(ctx.Context, logging.NullLogger) } result, err := sch.Schedule(schedulerCtx) if err != nil { From d490b0b41b6fa067999f2965c58a4585a23f1f9d Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 18:04:19 +0000 Subject: [PATCH 14/58] wip --- cmd/executor/main.go | 4 +- cmd/fakeexecutor/main.go | 2 +- .../common/armadacontext/armada_context.go | 5 ++ internal/common/etcdhealth/etcdhealth.go | 18 +++---- internal/common/etcdhealth/etcdhealth_test.go | 3 +- .../common/healthmonitor/healthmonitor.go | 3 +- .../healthmonitor/manualhealthmonitor.go | 3 +- .../healthmonitor/multihealthmonitor.go | 5 +- internal/common/metrics/provider.go | 8 ++- internal/executor/application.go | 49 ++++++++----------- internal/executor/fake/application.go | 7 ++- 11 files changed, 47 insertions(+), 60 deletions(-) diff --git a/cmd/executor/main.go b/cmd/executor/main.go index 04e53b73f41..a91c0304f49 100644 --- a/cmd/executor/main.go +++ b/cmd/executor/main.go @@ -7,13 +7,13 @@ import ( "syscall" "github.com/prometheus/client_golang/prometheus" - log "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/health" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/internal/executor" "github.com/armadaproject/armada/internal/executor/configuration" @@ -61,7 +61,7 @@ func main() { ) defer shutdownMetricServer() - shutdown, wg := executor.StartUp(armadacontext.Background(), log.NewEntry(log.StandardLogger()), config) + shutdown, wg := executor.StartUp(armadacontext.Background(), config) go func() { <-shutdownChannel shutdown() diff --git a/cmd/fakeexecutor/main.go b/cmd/fakeexecutor/main.go index 831d56b650e..d24ffce0e62 100644 --- a/cmd/fakeexecutor/main.go +++ b/cmd/fakeexecutor/main.go @@ -54,7 +54,7 @@ func main() { shutdownMetricServer := common.ServeMetrics(config.Metric.Port) defer shutdownMetricServer() - shutdown, wg := fake.StartUp(config, nodes) + shutdown, wg := fake.StartUp(armadacontext.Background(), config, nodes) go func() { <-shutdownChannel shutdown() diff --git a/internal/common/armadacontext/armada_context.go b/internal/common/armadacontext/armada_context.go index ed49bf2fc94..3b3c7f19fb7 100644 --- a/internal/common/armadacontext/armada_context.go +++ b/internal/common/armadacontext/armada_context.go @@ -66,6 +66,11 @@ func (ctx *Context) Errorf(format string, args ...interface{}) { ctx.logger.Errorf(format, args...) } +// Fatalf logs a message at level Fatal. +func (ctx *Context) Fatalf(format string, args ...interface{}) { + ctx.logger.Fatalf(format, args...) +} + // Background creates an empty context with a default logger. It is analogous to context.Background() func Background() *Context { return &Context{ diff --git a/internal/common/etcdhealth/etcdhealth.go b/internal/common/etcdhealth/etcdhealth.go index dfa4acb96b3..1d836c520c1 100644 --- a/internal/common/etcdhealth/etcdhealth.go +++ b/internal/common/etcdhealth/etcdhealth.go @@ -9,7 +9,6 @@ import ( "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/healthmonitor" - "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/metrics" ) @@ -183,10 +182,9 @@ func (srv *EtcdReplicaHealthMonitor) sizeFraction() float64 { return srv.etcdSizeBytes / srv.etcdCapacityBytes } -func (srv *EtcdReplicaHealthMonitor) Run(ctx *armadacontext.Context, log *logging.Logger) error { - log = log.WithField("service", "EtcdHealthMonitor") - log.Info("starting etcd health monitor") - defer log.Info("stopping etcd health monitor") +func (srv *EtcdReplicaHealthMonitor) Run(ctx *armadacontext.Context) error { + ctx.Info("starting etcd health monitor") + defer ctx.Info("stopping etcd health monitor") ticker := time.NewTicker(srv.scrapeInterval) defer ticker.Stop() for { @@ -195,24 +193,24 @@ func (srv *EtcdReplicaHealthMonitor) Run(ctx *armadacontext.Context, log *loggin return ctx.Err() case <-ticker.C: t := time.Now() - metrics, err := srv.metricsProvider.Collect(ctx, log) + metrics, err := srv.metricsProvider.Collect(ctx) srv.mu.Lock() srv.timeOfMostRecentCollectionAttempt = time.Now() if err != nil { - log.WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) + ctx.Logger().WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) } else { success := true if err := srv.setSizeInUseBytesFromMetrics(metrics); err != nil { success = false - log.WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) + ctx.Logger().WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) } if err := srv.setSizeBytesFromMetrics(metrics); err != nil { success = false - log.WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) + ctx.Logger().WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) } if err := srv.setCapacityBytesFromMetrics(metrics); err != nil { success = false - log.WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) + ctx.Logger().WithStacktrace(err).Errorf("failed to scrape etcd metrics from %s", srv.name) } if success { srv.timeOfMostRecentSuccessfulCollectionAttempt = srv.timeOfMostRecentCollectionAttempt diff --git a/internal/common/etcdhealth/etcdhealth_test.go b/internal/common/etcdhealth/etcdhealth_test.go index cc81b7e0e18..038277e4227 100644 --- a/internal/common/etcdhealth/etcdhealth_test.go +++ b/internal/common/etcdhealth/etcdhealth_test.go @@ -8,7 +8,6 @@ import ( "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/healthmonitor" - "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/metrics" ) @@ -26,7 +25,7 @@ func TestEtcdReplicaHealthMonitor(t *testing.T) { ctx, cancel := armadacontext.WithCancel(armadacontext.Background()) defer cancel() g, ctx := armadacontext.ErrGroup(ctx) - g.Go(func() error { return hm.Run(ctx, logging.StdLogger()) }) + g.Go(func() error { return hm.Run(ctx) }) // Should still be unavailable due to missing metrics. hm.BlockUntilNextMetricsCollection(ctx) diff --git a/internal/common/healthmonitor/healthmonitor.go b/internal/common/healthmonitor/healthmonitor.go index e81059fff58..2bf7d817b6c 100644 --- a/internal/common/healthmonitor/healthmonitor.go +++ b/internal/common/healthmonitor/healthmonitor.go @@ -4,7 +4,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/logging" ) const ( @@ -25,5 +24,5 @@ type HealthMonitor interface { // Run initialises and starts the health checker. // Run may be blocking and should be run within a separate goroutine. // Must be called before IsHealthy() or any prometheus.Collector interface methods. - Run(*armadacontext.Context, *logging.Logger) error + Run(*armadacontext.Context) error } diff --git a/internal/common/healthmonitor/manualhealthmonitor.go b/internal/common/healthmonitor/manualhealthmonitor.go index 163e8566656..46797493df0 100644 --- a/internal/common/healthmonitor/manualhealthmonitor.go +++ b/internal/common/healthmonitor/manualhealthmonitor.go @@ -6,7 +6,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/logging" ) // ManualHealthMonitor is a manually controlled health monitor. @@ -47,7 +46,7 @@ func (srv *ManualHealthMonitor) IsHealthy() (bool, string, error) { } } -func (srv *ManualHealthMonitor) Run(_ *armadacontext.Context, _ *logging.Logger) error { +func (srv *ManualHealthMonitor) Run(_ *armadacontext.Context) error { return nil } diff --git a/internal/common/healthmonitor/multihealthmonitor.go b/internal/common/healthmonitor/multihealthmonitor.go index 3c4629ca8c4..cf6197918f9 100644 --- a/internal/common/healthmonitor/multihealthmonitor.go +++ b/internal/common/healthmonitor/multihealthmonitor.go @@ -9,7 +9,6 @@ import ( "golang.org/x/exp/maps" "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/logging" ) // MultiHealthMonitor wraps multiple HealthMonitors and itself implements the HealthMonitor interface. @@ -100,11 +99,11 @@ func (srv *MultiHealthMonitor) IsHealthy() (ok bool, reason string, err error) { } // Run initialises prometheus metrics and starts any child health checkers. -func (srv *MultiHealthMonitor) Run(ctx *armadacontext.Context, log *logging.Logger) error { +func (srv *MultiHealthMonitor) Run(ctx *armadacontext.Context) error { g, ctx := armadacontext.ErrGroup(ctx) for _, healthMonitor := range srv.healthMonitorsByName { healthMonitor := healthMonitor - g.Go(func() error { return healthMonitor.Run(ctx, log) }) + g.Go(func() error { return healthMonitor.Run(ctx) }) } return g.Wait() } diff --git a/internal/common/metrics/provider.go b/internal/common/metrics/provider.go index 63d82021086..28fc08a64ef 100644 --- a/internal/common/metrics/provider.go +++ b/internal/common/metrics/provider.go @@ -8,12 +8,10 @@ import ( "strings" "sync" "time" - - "github.com/armadaproject/armada/internal/common/logging" ) type MetricsProvider interface { - Collect(context.Context, *logging.Logger) (map[string]float64, error) + Collect(context.Context) (map[string]float64, error) } type ManualMetricsProvider struct { @@ -36,7 +34,7 @@ func (srv *ManualMetricsProvider) WithCollectionDelay(d time.Duration) *ManualMe return srv } -func (srv *ManualMetricsProvider) Collect(_ context.Context, _ *logging.Logger) (map[string]float64, error) { +func (srv *ManualMetricsProvider) Collect(_ context.Context) (map[string]float64, error) { srv.mu.Lock() defer srv.mu.Unlock() if srv.collectionDelay != 0 { @@ -61,7 +59,7 @@ func NewHttpMetricsProvider(url string, client *http.Client) *HttpMetricsProvide } } -func (srv *HttpMetricsProvider) Collect(ctx context.Context, _ *logging.Logger) (map[string]float64, error) { +func (srv *HttpMetricsProvider) Collect(ctx context.Context) (map[string]float64, error) { req, err := http.NewRequestWithContext(ctx, "GET", srv.url, nil) if err != nil { return nil, err diff --git a/internal/executor/application.go b/internal/executor/application.go index b6d46d1005d..a65cdde84f5 100644 --- a/internal/executor/application.go +++ b/internal/executor/application.go @@ -19,7 +19,6 @@ import ( "github.com/armadaproject/armada/internal/common/cluster" "github.com/armadaproject/armada/internal/common/etcdhealth" "github.com/armadaproject/armada/internal/common/healthmonitor" - "github.com/armadaproject/armada/internal/common/logging" common_metrics "github.com/armadaproject/armada/internal/common/metrics" "github.com/armadaproject/armada/internal/common/task" "github.com/armadaproject/armada/internal/common/util" @@ -39,10 +38,10 @@ import ( "github.com/armadaproject/armada/pkg/executorapi" ) -func StartUp(ctx *armadacontext.Context, log *logging.Logger, config configuration.ExecutorConfiguration) (func(), *sync.WaitGroup) { +func StartUp(ctx *armadacontext.Context, config configuration.ExecutorConfiguration) (func(), *sync.WaitGroup) { err := validateConfig(config) if err != nil { - log.Errorf("Invalid config: %s", err) + ctx.Errorf("Invalid config: %s", err) os.Exit(-1) } @@ -52,7 +51,7 @@ func StartUp(ctx *armadacontext.Context, log *logging.Logger, config configurati config.Kubernetes.Burst, ) if err != nil { - log.Errorf("Failed to connect to kubernetes because %s", err) + ctx.Errorf("Failed to connect to kubernetes because %s", err) os.Exit(-1) } @@ -87,17 +86,17 @@ func StartUp(ctx *armadacontext.Context, log *logging.Logger, config configurati } var etcdClustersHealthMonitoring healthmonitor.HealthMonitor if len(etcdClusterHealthMonitoringByName) > 0 { - log.Info("etcd URLs provided; monitoring etcd health enabled") + ctx.Info("etcd URLs provided; monitoring etcd health enabled") etcdClustersHealthMonitoring = healthmonitor.NewMultiHealthMonitor( "overall_etcd", etcdClusterHealthMonitoringByName, ).WithMetricsPrefix( metrics.ArmadaExecutorMetricsPrefix, ) - g.Go(func() error { return etcdClustersHealthMonitoring.Run(ctx, log) }) + g.Go(func() error { return etcdClustersHealthMonitoring.Run(ctx) }) prometheus.MustRegister(etcdClustersHealthMonitoring) } else { - log.Info("no etcd URLs provided; etcd health isn't monitored") + ctx.Info("no etcd URLs provided; etcd health isn't monitored") } clusterContext := executor_context.NewClusterContext( @@ -113,11 +112,11 @@ func StartUp(ctx *armadacontext.Context, log *logging.Logger, config configurati taskManager := task.NewBackgroundTaskManager(metrics.ArmadaExecutorMetricsPrefix) taskManager.Register(clusterContext.ProcessPodsToDelete, config.Task.PodDeletionInterval, "pod_deletion") - return StartUpWithContext(log, config, clusterContext, etcdClustersHealthMonitoring, taskManager, wg) + return StartUpWithContext(ctx, config, clusterContext, etcdClustersHealthMonitoring, taskManager, wg) } func StartUpWithContext( - log *logging.Logger, + ctx *armadacontext.Context, config configuration.ExecutorConfiguration, clusterContext executor_context.ClusterContext, clusterHealthMonitor healthmonitor.HealthMonitor, @@ -133,21 +132,18 @@ func StartUpWithContext( ) if config.Kubernetes.PendingPodChecks == nil { - log.Error("Config error: Missing pending pod checks") - os.Exit(-1) + ctx.Fatalf("Config error: Missing pending pod checks") } pendingPodChecker, err := podchecks.NewPodChecks(*config.Kubernetes.PendingPodChecks) if err != nil { - log.Errorf("Config error in pending pod checks: %s", err) - os.Exit(-1) + ctx.Fatalf("Config error in pending pod checks: %s", err) } - stopExecutorApiComponents := setupExecutorApiComponents(log, config, clusterContext, clusterHealthMonitor, taskManager, pendingPodChecker, nodeInfoService, podUtilisationService) + stopExecutorApiComponents := setupExecutorApiComponents(ctx, config, clusterContext, clusterHealthMonitor, taskManager, pendingPodChecker, nodeInfoService, podUtilisationService) resourceCleanupService, err := service.NewResourceCleanupService(clusterContext, config.Kubernetes) if err != nil { - log.Errorf("Error creating resource cleanup service: %s", err) - os.Exit(-1) + ctx.Fatalf("Error creating resource cleanup service: %s", err) } taskManager.Register(resourceCleanupService.CleanupResources, config.Task.ResourceCleanupInterval, "resource_cleanup") @@ -159,15 +155,15 @@ func StartUpWithContext( clusterContext.Stop() stopExecutorApiComponents() if taskManager.StopAll(10 * time.Second) { - log.Warnf("Graceful shutdown timed out") + ctx.Warnf("Graceful shutdown timed out") } - log.Infof("Shutdown complete") + ctx.Infof("Shutdown complete") wg.Done() }, wg } func setupExecutorApiComponents( - log *logging.Logger, + ctx *armadacontext.Context, config configuration.ExecutorConfiguration, clusterContext executor_context.ClusterContext, clusterHealthMonitor healthmonitor.HealthMonitor, @@ -178,8 +174,7 @@ func setupExecutorApiComponents( ) func() { conn, err := createConnectionToApi(config.ExecutorApiConnection, config.Client.MaxMessageSizeBytes, config.GRPC) if err != nil { - log.Errorf("Failed to connect to Executor API because: %s", err) - os.Exit(-1) + ctx.Fatalf("Failed to connect to Executor API because: %s", err) } executorApiClient := executorapi.NewExecutorApiClient(conn) @@ -203,8 +198,7 @@ func setupExecutorApiComponents( clock.RealClock{}, 200) if err != nil { - log.Errorf("Failed to create job event reporter: %s", err) - os.Exit(-1) + ctx.Fatalf("Failed to create job event reporter: %s", err) } submitter := job.NewSubmitter( @@ -243,8 +237,7 @@ func setupExecutorApiComponents( config.Kubernetes.StuckTerminatingPodExpiry, ) if err != nil { - log.Errorf("Failed to create pod issue service: %s", err) - os.Exit(-1) + ctx.Fatalf("Failed to create pod issue service: %s", err) } taskManager.Register(podIssueService.HandlePodIssues, config.Task.PodIssueHandlingInterval, "pod_issue_handling") @@ -255,8 +248,7 @@ func setupExecutorApiComponents( taskManager.Register(eventReporter.ReportMissingJobEvents, config.Task.MissingJobEventReconciliationInterval, "event_reconciliation") _, err = pod_metrics.ExposeClusterContextMetrics(clusterContext, clusterUtilisationService, podUtilisationService, nodeInfoService) if err != nil { - log.Errorf("Failed to setup cluster context metrics: %s", err) - os.Exit(-1) + ctx.Fatalf("Failed to setup cluster context metrics: %s", err) } runStateMetricsCollector := runstate.NewJobRunStateStoreMetricsCollector(jobRunState) prometheus.MustRegister(runStateMetricsCollector) @@ -268,8 +260,7 @@ func setupExecutorApiComponents( eventReporter, config.Task.UtilisationEventReportingInterval) if err != nil { - log.Errorf("Failed to pod utilisation reporter: %s", err) - os.Exit(-1) + ctx.Fatalf("Failed to pod utilisation reporter: %s", err) } taskManager.Register( podUtilisationReporter.ReportUtilisationEvents, diff --git a/internal/executor/fake/application.go b/internal/executor/fake/application.go index e02200a5219..d01fe10c396 100644 --- a/internal/executor/fake/application.go +++ b/internal/executor/fake/application.go @@ -1,9 +1,8 @@ package fake import ( - "sync" + "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/task" "github.com/armadaproject/armada/internal/executor" @@ -12,11 +11,11 @@ import ( "github.com/armadaproject/armada/internal/executor/metrics" ) -func StartUp(config configuration.ExecutorConfiguration, nodes []*context.NodeSpec) (func(), *sync.WaitGroup) { +func StartUp(ctx *armadacontext.Context, config configuration.ExecutorConfiguration, nodes []*context.NodeSpec) (func(), *sync.WaitGroup) { wg := &sync.WaitGroup{} wg.Add(1) return executor.StartUpWithContext( - logging.StdLogger(), + ctx, config, context.NewFakeClusterContext(config.Application, config.Kubernetes.NodeIdLabel, nodes), nil, From 1ccd7b1199ce41dc3df9ed0760b9b049ed908196 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 18:06:55 +0000 Subject: [PATCH 15/58] wip --- internal/common/startup.go | 9 ++------- internal/executor/fake/application.go | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/internal/common/startup.go b/internal/common/startup.go index 038ea20f3e6..e4f2e10d582 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -25,14 +25,10 @@ import ( const baseConfigFileName = "config" -// RFC3339Millis -const logTimestampFormat = "2006-01-02T15:04:05.999Z07:00" - func BindCommandlineArguments() { err := viper.BindPFlags(pflag.CommandLine) if err != nil { - log.Error(err.Error()) - os.Exit(-1) + log.Fatalf(err.Error()) } } @@ -109,8 +105,7 @@ func ConfigureLogging() { consoleEncoder := zapcore.NewConsoleEncoder(pe) pe.EncodeCaller = ShortCallerEncoder - level := zap.InfoLevel - core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), level) + core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), readEnvironmentLogLevel()) l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) log.SetDefaultLogger(log.FromZap(l)) } diff --git a/internal/executor/fake/application.go b/internal/executor/fake/application.go index d01fe10c396..ed34153c8e0 100644 --- a/internal/executor/fake/application.go +++ b/internal/executor/fake/application.go @@ -1,9 +1,9 @@ package fake import ( - "github.com/armadaproject/armada/internal/common/armadacontext" - + "sync" + "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/task" "github.com/armadaproject/armada/internal/executor" "github.com/armadaproject/armada/internal/executor/configuration" From 4e83b6157aa486eed2481d330ad48b4a6ebc8d50 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 18:12:20 +0000 Subject: [PATCH 16/58] wip --- internal/common/startup.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/common/startup.go b/internal/common/startup.go index e4f2e10d582..1ff225b1b83 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -32,7 +32,6 @@ func BindCommandlineArguments() { } } -// TODO Move code relating to config out of common into a new package internal/serverconfig func LoadConfig(config commonconfig.Config, defaultPath string, overrideConfigs []string) *viper.Viper { v := viper.NewWithOptions(viper.KeyDelimiter("::")) v.SetConfigName(baseConfigFileName) @@ -102,8 +101,8 @@ func ConfigureLogging() { pe.EncodeTime = zapcore.ISO8601TimeEncoder pe.ConsoleSeparator = " " pe.EncodeLevel = zapcore.CapitalLevelEncoder - consoleEncoder := zapcore.NewConsoleEncoder(pe) pe.EncodeCaller = ShortCallerEncoder + consoleEncoder := zapcore.NewConsoleEncoder(pe) core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), readEnvironmentLogLevel()) l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) From 94de1dd4815b4ff02c55e8e575b1ad31c313960f Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 18:16:44 +0000 Subject: [PATCH 17/58] remove logrus --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index bc4c4388e9b..3de4bb169da 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,6 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.20.5 github.com/renstrom/shortuuid v3.0.0+incompatible - github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 @@ -81,6 +80,7 @@ require ( github.com/redis/go-redis/extra/redisprometheus/v9 v9.0.5 github.com/redis/go-redis/v9 v9.7.0 github.com/segmentio/fasthash v1.0.3 + github.com/sirupsen/logrus v1.9.3 github.com/xitongsys/parquet-go v1.6.2 go.uber.org/mock v0.5.0 go.uber.org/zap v1.27.0 From 4e5450d732756e9382a6a49e18e316be7cbe6c2f Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 18:21:33 +0000 Subject: [PATCH 18/58] remove logrus --- cmd/armada-load-tester/cmd/loadtest.go | 4 ++-- cmd/armada-load-tester/cmd/root.go | 6 +++--- cmd/binoculars/main.go | 2 +- cmd/fakeexecutor/main.go | 2 +- cmd/lookoutingesterv2/dbloadtester/main.go | 2 +- cmd/lookoutingesterv2/main.go | 2 +- cmd/lookoutv2/main.go | 4 ++-- cmd/scheduler/cmd/migrate_database.go | 2 +- cmd/simulator/cmd/root.go | 9 ++++----- go.mod | 2 +- 10 files changed, 17 insertions(+), 18 deletions(-) diff --git a/cmd/armada-load-tester/cmd/loadtest.go b/cmd/armada-load-tester/cmd/loadtest.go index 5a2e301490e..f6c09622bcd 100644 --- a/cmd/armada-load-tester/cmd/loadtest.go +++ b/cmd/armada-load-tester/cmd/loadtest.go @@ -6,10 +6,10 @@ import ( "strings" "time" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/pkg/client" "github.com/armadaproject/armada/pkg/client/domain" "github.com/armadaproject/armada/pkg/client/util" @@ -72,7 +72,7 @@ var loadtestCmd = &cobra.Command{ loadTestSpec := &domain.LoadTestSpecification{} err := util.BindJsonOrYaml(filePath, loadTestSpec) if err != nil { - log.Error(err) + log.Error(err.Error()) os.Exit(1) } diff --git a/cmd/armada-load-tester/cmd/root.go b/cmd/armada-load-tester/cmd/root.go index 5f2bcfb59c3..0803042410d 100644 --- a/cmd/armada-load-tester/cmd/root.go +++ b/cmd/armada-load-tester/cmd/root.go @@ -3,9 +3,9 @@ package cmd import ( "os" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/pkg/client" ) @@ -28,7 +28,7 @@ The location of this file can be passed in using --config argument or picked fro // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := rootCmd.Execute(); err != nil { - log.Error(err) + log.Error(err.Error()) os.Exit(1) } } @@ -37,7 +37,7 @@ var cfgFile string func initConfig() { if err := client.LoadCommandlineArgsFromConfigFile(cfgFile); err != nil { - log.Error(err) + log.Error(err.Error()) os.Exit(1) } } diff --git a/cmd/binoculars/main.go b/cmd/binoculars/main.go index 86389fbc91b..5c0558aeb3a 100644 --- a/cmd/binoculars/main.go +++ b/cmd/binoculars/main.go @@ -8,7 +8,6 @@ import ( "syscall" "github.com/grpc-ecosystem/grpc-gateway/runtime" - log "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" "google.golang.org/grpc" @@ -19,6 +18,7 @@ import ( "github.com/armadaproject/armada/internal/common/armadacontext" gateway "github.com/armadaproject/armada/internal/common/grpc" "github.com/armadaproject/armada/internal/common/health" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" api "github.com/armadaproject/armada/pkg/api/binoculars" ) diff --git a/cmd/fakeexecutor/main.go b/cmd/fakeexecutor/main.go index d24ffce0e62..69d9ce03c33 100644 --- a/cmd/fakeexecutor/main.go +++ b/cmd/fakeexecutor/main.go @@ -5,12 +5,12 @@ import ( "os/signal" "syscall" - log "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/internal/executor/configuration" "github.com/armadaproject/armada/internal/executor/fake" diff --git a/cmd/lookoutingesterv2/dbloadtester/main.go b/cmd/lookoutingesterv2/dbloadtester/main.go index 8daf0960f2f..87f91fa1f73 100644 --- a/cmd/lookoutingesterv2/dbloadtester/main.go +++ b/cmd/lookoutingesterv2/dbloadtester/main.go @@ -4,13 +4,13 @@ import ( "fmt" "time" - log "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" "sigs.k8s.io/yaml" "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/app" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/lookoutingesterv2/configuration" "github.com/armadaproject/armada/internal/lookoutingesterv2/dbloadtester" ) diff --git a/cmd/lookoutingesterv2/main.go b/cmd/lookoutingesterv2/main.go index b60e58f6241..a0e48a6374f 100644 --- a/cmd/lookoutingesterv2/main.go +++ b/cmd/lookoutingesterv2/main.go @@ -1,11 +1,11 @@ package main import ( - log "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" "github.com/armadaproject/armada/internal/common" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/lookoutingesterv2" "github.com/armadaproject/armada/internal/lookoutingesterv2/benchmark" "github.com/armadaproject/armada/internal/lookoutingesterv2/configuration" diff --git a/cmd/lookoutv2/main.go b/cmd/lookoutv2/main.go index 48d48bc97b5..825b939d904 100644 --- a/cmd/lookoutv2/main.go +++ b/cmd/lookoutv2/main.go @@ -5,7 +5,6 @@ import ( "os/signal" "syscall" - log "github.com/sirupsen/logrus" "github.com/spf13/pflag" "github.com/spf13/viper" "k8s.io/utils/clock" @@ -13,6 +12,7 @@ import ( "github.com/armadaproject/armada/internal/common" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/database" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/common/profiling" "github.com/armadaproject/armada/internal/lookoutv2" "github.com/armadaproject/armada/internal/lookoutv2/configuration" @@ -148,7 +148,7 @@ func main() { restapi.UIConfig = config.UIConfig if err := lookoutv2.Serve(config); err != nil { - log.Error(err) + log.Error(err.Error()) os.Exit(1) } } diff --git a/cmd/scheduler/cmd/migrate_database.go b/cmd/scheduler/cmd/migrate_database.go index 9e48f5fba5f..407048e31dc 100644 --- a/cmd/scheduler/cmd/migrate_database.go +++ b/cmd/scheduler/cmd/migrate_database.go @@ -4,11 +4,11 @@ import ( "time" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/armadaproject/armada/internal/common/armadacontext" "github.com/armadaproject/armada/internal/common/database" + log "github.com/armadaproject/armada/internal/common/logging" schedulerdb "github.com/armadaproject/armada/internal/scheduler/database" ) diff --git a/cmd/simulator/cmd/root.go b/cmd/simulator/cmd/root.go index a1c815633c5..1e758e23afb 100644 --- a/cmd/simulator/cmd/root.go +++ b/cmd/simulator/cmd/root.go @@ -6,13 +6,12 @@ import ( "runtime/pprof" "time" - "github.com/armadaproject/armada/internal/scheduler/simulator/sink" - - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/armadaproject/armada/internal/common/armadacontext" + log "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/scheduler/simulator" + "github.com/armadaproject/armada/internal/scheduler/simulator/sink" "github.com/armadaproject/armada/internal/scheduler/testfixtures" ) @@ -139,11 +138,11 @@ func runSimulations(cmd *cobra.Command, args []string) error { log.Infof("Will write profiling information to %s", profilingFile) f, err := os.Create(profilingFile) if err != nil { - log.Fatal(err) + log.Fatal(err.Error()) } err = pprof.StartCPUProfile(f) if err != nil { - log.Fatal(err) + log.Fatal(err.Error()) } defer pprof.StopCPUProfile() } diff --git a/go.mod b/go.mod index 3de4bb169da..776a0d75590 100644 --- a/go.mod +++ b/go.mod @@ -80,7 +80,6 @@ require ( github.com/redis/go-redis/extra/redisprometheus/v9 v9.0.5 github.com/redis/go-redis/v9 v9.7.0 github.com/segmentio/fasthash v1.0.3 - github.com/sirupsen/logrus v1.9.3 github.com/xitongsys/parquet-go v1.6.2 go.uber.org/mock v0.5.0 go.uber.org/zap v1.27.0 @@ -183,6 +182,7 @@ require ( github.com/rivo/uniseg v0.4.2 // indirect github.com/sagikazarmark/locafero v0.6.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/spf13/afero v1.11.0 // indirect From 59d3e9b014a807f523cbed7a9385f317693491bf Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 18:32:07 +0000 Subject: [PATCH 19/58] fix test --- internal/common/logging/logging.go | 2 +- internal/lookoutingesterv2/instructions/instructions.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/common/logging/logging.go b/internal/common/logging/logging.go index 1f5bf2bcb75..da44802bee3 100644 --- a/internal/common/logging/logging.go +++ b/internal/common/logging/logging.go @@ -76,7 +76,7 @@ func (l *Logger) Fatalf(format string, args ...interface{}) { } func (l *Logger) WithError(err error) *Logger { - return l.WithField("error", err.Error()) + return l.WithField("error", err) } // WithStacktrace returns a new Logger obtained by adding error information and, if available, a stack trace diff --git a/internal/lookoutingesterv2/instructions/instructions.go b/internal/lookoutingesterv2/instructions/instructions.go index 6bfc0d2347a..04e85cc13e6 100644 --- a/internal/lookoutingesterv2/instructions/instructions.go +++ b/internal/lookoutingesterv2/instructions/instructions.go @@ -81,7 +81,7 @@ func (c *InstructionConverter) convertSequence( var err error if event.Created == nil { c.metrics.RecordPulsarMessageError(metrics.PulsarMessageErrorProcessing) - log.WithError(err).Warnf("Missing timestamp for event at index %d.", idx) + log.Warnf("Missing timestamp for event at index %d.", idx) continue } ts := protoutil.ToStdTime(event.Created) From b660df06c62edb32bd1aaa83fdf6ceef55714931 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 19:11:04 +0000 Subject: [PATCH 20/58] fix armadactl --- internal/common/startup.go | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/internal/common/startup.go b/internal/common/startup.go index 1ff225b1b83..47ef02f051a 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -1,6 +1,7 @@ package common import ( + "bytes" "crypto/tls" "fmt" "net/http" @@ -91,9 +92,41 @@ func UnmarshalKey(v *viper.Viper, key string, item interface{}) error { return v.UnmarshalKey(key, item, commonconfig.CustomHooks...) } +type CommandLineEncoder struct{} + +func (e *CommandLineEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*bytes.Buffer, error) { + var buf bytes.Buffer + buf.WriteString(entry.Message) + buf.WriteString("\n") + return &buf, nil +} + // TODO Move logging-related code out of common into a new package internal/logging func ConfigureCommandLineLogging() { - panic("not implemented") + // Define an encoder configuration that only includes the message. + encoderConfig := zapcore.EncoderConfig{ + MessageKey: "message", // Only include the message. + LineEnding: "\n", // Newline after each log entry. + // Ignore everything else by leaving their keys empty. + LevelKey: "", + TimeKey: "", + NameKey: "", + CallerKey: "", + FunctionKey: "", + StacktraceKey: "", + } + // Use the console encoder with the custom configuration. + encoder := zapcore.NewConsoleEncoder(encoderConfig) + + // Create a core that writes to stdout. + core := zapcore.NewCore( + encoder, + zapcore.AddSync(os.Stdout), + zapcore.InfoLevel, + ) + + l := zap.New(core) + log.SetDefaultLogger(log.FromZap(l)) } func ConfigureLogging() { @@ -198,8 +231,7 @@ func serveHttp(port uint16, mux http.Handler, useTls bool, certFile, keyFile str } } -// ShortCallerEncoder serializes a caller in package/file:line format, trimming -// all but the final directory from the full path. +// ShortCallerEncoder serializes a caller in to just file:line format. func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { trimmed := caller.TrimmedPath() lastSlash := strings.LastIndexByte(trimmed, '/') From 42baeae7962bb4061b89e74f5f442bfce7034627 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 22:38:28 +0000 Subject: [PATCH 21/58] clean up interfaces --- .../common/armadacontext/armada_context.go | 2 +- internal/common/grpc/grpc.go | 8 +- internal/common/logging/global.go | 93 +++++++++ internal/common/logging/logger.go | 120 +++++++++++ internal/common/logging/logging.go | 192 ------------------ internal/common/logging/null_logger.go | 1 + internal/common/logging/pulsar_adapter.go | 10 +- internal/common/logging/user_input.go | 8 - internal/common/startup.go | 24 +-- internal/lookoutv2/repository/util.go | 6 +- pkg/client/auth/oidc/device.go | 9 +- 11 files changed, 243 insertions(+), 230 deletions(-) create mode 100644 internal/common/logging/global.go create mode 100644 internal/common/logging/logger.go delete mode 100644 internal/common/logging/logging.go delete mode 100644 internal/common/logging/user_input.go diff --git a/internal/common/armadacontext/armada_context.go b/internal/common/armadacontext/armada_context.go index 3b3c7f19fb7..256fb3247e8 100644 --- a/internal/common/armadacontext/armada_context.go +++ b/internal/common/armadacontext/armada_context.go @@ -133,7 +133,7 @@ func WithTimeout(parent *Context, timeout time.Duration) (*Context, context.Canc } // WithLogField returns a copy of parent with the supplied key-value added to the logger -func WithLogField(parent *Context, key string, val interface{}) *Context { +func WithLogField(parent *Context, key string, val any) *Context { return &Context{ Context: parent.Context, logger: parent.logger.WithField(key, val), diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index b7240e9d86a..1c5e9c62773 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -147,7 +147,13 @@ func panicRecoveryHandler(p interface{}) (err error) { func InterceptorLogger() grpc_logging.Logger { return grpc_logging.LoggerFunc(func(ctx context.Context, lvl grpc_logging.Level, msg string, fields ...any) { armadaCtx := armadacontext.FromGrpcCtx(ctx) - l := armadaCtx.Logger().WithField(fields...) + logFields := make(map[string]any, len(fields)/2) + i := grpc_logging.Fields(fields).Iterator() + for i.Next() { + k, v := i.At() + logFields[k] = v + } + l := armadacontext.WithLogFields(armadaCtx, logFields) switch lvl { case grpc_logging.LevelDebug: l.Debug(msg) diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go new file mode 100644 index 00000000000..c88dab4b63d --- /dev/null +++ b/internal/common/logging/global.go @@ -0,0 +1,93 @@ +package logging + +import ( + "go.uber.org/zap" +) + +// The default Logger +var stdLogger = &Logger{zap.S()} + +// ReplaceStdLogger Replaces the defualt logger. This should be called once at app startup! +func ReplaceStdLogger(l *Logger) { + stdLogger = l +} + +// StdLogger Returns the default logger +func StdLogger() *Logger { + return stdLogger +} + +// Debug logs a message at level Debug. +func Debug(args ...any) { + stdLogger.Debug(args...) +} + +// Info logs a message at level Info. +func Info(args ...any) { + stdLogger.Info(args...) +} + +// Warn logs a message at level Warn on the standard logger. +func Warn(args ...any) { + stdLogger.Warn(args...) +} + +// Error logs a message at level Error on the standard logger. +func Error(args ...any) { + stdLogger.Error(args...) +} + +// Panic logs a message at level Panic on the standard logger. +func Panic(args ...any) { + stdLogger.Panic(args...) +} + +// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1. +func Fatal(args ...any) { + stdLogger.Fatal(args...) +} + +// Debugf logs a message at level Debug on the standard logger. +func Debugf(format string, args ...any) { + stdLogger.Debugf(format, args...) +} + +// Infof logs a message at level Info on the standard logger. +func Infof(format string, args ...any) { + stdLogger.Infof(format, args...) +} + +// Warnf logs a message at level Warn on the standard logger. +func Warnf(format string, args ...any) { + stdLogger.Warnf(format, args...) +} + +// Errorf logs a message at level Error on the standard logger. +func Errorf(format string, args ...any) { + stdLogger.Errorf(format, args...) +} + +// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1. +func Fatalf(format string, args ...any) { + stdLogger.Fatalf(format, args...) +} + +// WithField returns a new Logger with the key-value pair added as a new field +func WithField(key string, value any) *Logger { + return stdLogger.WithField(key, value) +} + +// WithFields returns a new Logger with all key-value pairs in the map added as new fields +func WithFields(args map[string]any) *Logger { + return stdLogger.WithFields(args) +} + +// WithError returns a new Logger with the error added as a field +func WithError(err error) *Logger { + return stdLogger.WithError(err) +} + +// WithStacktrace returns a new Logger with the error and (if available) the stacktrace added as fields +func WithStacktrace(err error) *Logger { + return stdLogger.WithStacktrace(err) +} diff --git a/internal/common/logging/logger.go b/internal/common/logging/logger.go new file mode 100644 index 00000000000..d058fc89fb6 --- /dev/null +++ b/internal/common/logging/logger.go @@ -0,0 +1,120 @@ +package logging + +import ( + "github.com/pkg/errors" + "go.uber.org/zap" +) + +// Logger wraps a *zap.SugaredLogger so that the rest of the code doesn't depend directly on Zap +type Logger struct { + undlerlying *zap.SugaredLogger +} + +// FromZap returns a New Logger backed by the supplied *zap.SugaredLogger +func FromZap(l *zap.Logger) *Logger { + return &Logger{ + undlerlying: l.Sugar(), + } +} + +// Debug logs a message at level Debug +func (l *Logger) Debug(args ...any) { + l.undlerlying.Debug(args...) +} + +// Info logs a message at level Info +func (l *Logger) Info(args ...any) { + l.undlerlying.Info(args...) +} + +// Warn logs a message at level Warn +func (l *Logger) Warn(args ...any) { + l.undlerlying.Warn(args...) +} + +// Error logs a message at level Error +func (l *Logger) Error(args ...any) { + l.undlerlying.Error(args...) +} + +// Panic logs a message at level Panic +func (l *Logger) Panic(args ...any) { + l.undlerlying.Panic(args...) +} + +// Fatal logs a message at level Fatal then the process will exit with status set to 1. +func (l *Logger) Fatal(args ...any) { + l.undlerlying.Fatal(args...) +} + +// Debugf logs a message at level Debug. +func (l *Logger) Debugf(format string, args ...interface{}) { + l.undlerlying.Debugf(format, args...) +} + +// Infof logs a message at level Info. +func (l *Logger) Infof(format string, args ...interface{}) { + l.undlerlying.Infof(format, args...) +} + +// Warnf logs a message at level Warn. +func (l *Logger) Warnf(format string, args ...interface{}) { + l.undlerlying.Warnf(format, args...) +} + +// Errorf logs a message at level Error. +func (l *Logger) Errorf(format string, args ...interface{}) { + l.undlerlying.Errorf(format, args...) +} + +// Fatalf logs a message at level Fatal. +func (l *Logger) Fatalf(format string, args ...interface{}) { + l.undlerlying.Fatalf(format, args...) +} + +// WithError returns a new Logger with the error added as a field +func (l *Logger) WithError(err error) *Logger { + return l.WithField("error", err) +} + +// WithStacktrace returns a new Logger with the error and (if available) the stacktrace added as fields +func (l *Logger) WithStacktrace(err error) *Logger { + logger := l.WithError(err) + if stackErr, ok := err.(stackTracer); ok { + return logger.WithField("stacktrace", stackErr.StackTrace()) + } else { + return logger + } +} + +// WithField returns a new Logger with the key-value pair added as a new field +func (l *Logger) WithField(key string, value any) *Logger { + return &Logger{ + undlerlying: l.undlerlying.With(key, value), + } +} + +// WithFields returns a new Logger with all key-value pairs in the map added as new fields +func (l *Logger) WithFields(args map[string]any) *Logger { + fields := make([]any, 0, len(args)) + for key, value := range args { + fields = append(fields, zap.Any(key, value)) + } + return &Logger{ + undlerlying: l.undlerlying.With(fields...), + } +} + +// WithCallerSkip returns a new Logger with the number of callers skipped increased by the skip amount. +// This is needed when building wrappers around the Logger so as to prevent us from always reporting the +// wrapper code as the caller. +func (l *Logger) WithCallerSkip(skip int) *Logger { + return &Logger{ + undlerlying: l.undlerlying.WithOptions(zap.AddCallerSkip(skip)), + } +} + +// Unexported but considered part of the stable interface of pkg/errors. +type stackTracer interface { + StackTrace() errors.StackTrace +} diff --git a/internal/common/logging/logging.go b/internal/common/logging/logging.go deleted file mode 100644 index da44802bee3..00000000000 --- a/internal/common/logging/logging.go +++ /dev/null @@ -1,192 +0,0 @@ -package logging - -import ( - "github.com/pkg/errors" - "go.uber.org/zap" -) - -// Unexported but considered part of the stable interface of pkg/errors. -type stackTracer interface { - StackTrace() errors.StackTrace -} - -type Logger struct { - undlerlying *zap.SugaredLogger -} - -func FromZap(l *zap.Logger) *Logger { - return &Logger{ - undlerlying: l.Sugar(), - } -} - -// Debug logs a message at level Debug -func (l *Logger) Debug(args ...any) { - l.undlerlying.Debug(args) -} - -// Info logs a message at level Info -func (l *Logger) Info(args ...any) { - l.undlerlying.Info(args) -} - -// Warn logs a message at level Warn -func (l *Logger) Warn(args ...any) { - l.undlerlying.Warn(args) -} - -// Error logs a message at level Error -func (l *Logger) Error(args ...any) { - l.undlerlying.Error(args) -} - -// Panic logs a message at level Panic -func (l *Logger) Panic(args ...any) { - l.undlerlying.Panic(args) -} - -// Fatal logs a message at level Fatal then the process will exit with status set to 1. -func (l *Logger) Fatal(args string) { - l.undlerlying.Fatal(args) -} - -// Debugf logs a message at level Debug. -func (l *Logger) Debugf(format string, args ...interface{}) { - l.undlerlying.Debugf(format, args...) -} - -// Infof logs a message at level Info. -func (l *Logger) Infof(format string, args ...interface{}) { - l.undlerlying.Infof(format, args...) -} - -// Warnf logs a message at level Warn. -func (l *Logger) Warnf(format string, args ...interface{}) { - l.undlerlying.Warnf(format, args...) -} - -// Errorf logs a message at level Error. -func (l *Logger) Errorf(format string, args ...interface{}) { - l.undlerlying.Errorf(format, args...) -} - -// Fatalf logs a message at level Fatal. -func (l *Logger) Fatalf(format string, args ...interface{}) { - l.undlerlying.Fatalf(format, args...) -} - -func (l *Logger) WithError(err error) *Logger { - return l.WithField("error", err) -} - -// WithStacktrace returns a new Logger obtained by adding error information and, if available, a stack trace -// as fields -func (l *Logger) WithStacktrace(err error) *Logger { - logger := l.WithError(err) - if stackErr, ok := err.(stackTracer); ok { - return logger.WithField("stacktrace", stackErr.StackTrace()) - } else { - return logger - } -} - -func (l *Logger) WithField(args ...any) *Logger { - return &Logger{ - undlerlying: l.undlerlying.With(args...), - } -} - -func (l *Logger) WithFields(args map[string]any) *Logger { - fields := make([]any, 0, len(args)) - for key, value := range args { - fields = append(fields, zap.Any(key, value)) - } - return With(fields...) -} - -func (l *Logger) WithCallerSkip(skip int) *Logger { - return &Logger{ - undlerlying: l.undlerlying.WithOptions(zap.AddCallerSkip(skip)), - } -} - -var stdLogger = &Logger{zap.S()} - -func StdLogger() *Logger { - return stdLogger -} - -func SetDefaultLogger(l *Logger) { - stdLogger = l -} - -// Debug logs a message at level Debug. -func Debug(msg string) { - stdLogger.Debug(msg) -} - -// Info logs a message at level Info. -func Info(msg string) { - stdLogger.Info(msg) -} - -// Warn logs a message at level Warn on the standard logger. -func Warn(msg string) { - stdLogger.Warn(msg) -} - -// Error logs a message at level Error on the standard logger. -func Error(msg string) { - stdLogger.Error(msg) -} - -// Panic logs a message at level Panic on the standard logger. -func Panic(msg string) { - stdLogger.Panic(msg) -} - -// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1. -func Fatal(msg string) { - stdLogger.Fatal(msg) -} - -// Debugf logs a message at level Debug on the standard logger. -func Debugf(format string, args ...interface{}) { - stdLogger.Debugf(format, args...) -} - -// Infof logs a message at level Info on the standard logger. -func Infof(format string, args ...interface{}) { - stdLogger.Infof(format, args...) -} - -// Warnf logs a message at level Warn on the standard logger. -func Warnf(format string, args ...interface{}) { - stdLogger.Warnf(format, args...) -} - -// Errorf logs a message at level Error on the standard logger. -func Errorf(format string, args ...interface{}) { - stdLogger.Errorf(format, args...) -} - -// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1. -func Fatalf(format string, args ...interface{}) { - stdLogger.Fatalf(format, args...) -} - -func With(args ...any) *Logger { - return stdLogger.WithField(args...) -} - -func WithMany(args map[string]any) *Logger { - return stdLogger.WithField(args) -} - -func WithError(err error) *Logger { - return stdLogger.WithError(err) -} - -func WithStacktrace(err error) *Logger { - return stdLogger.WithStacktrace(err) -} diff --git a/internal/common/logging/null_logger.go b/internal/common/logging/null_logger.go index 5cb5370902f..de52f95a2a2 100644 --- a/internal/common/logging/null_logger.go +++ b/internal/common/logging/null_logger.go @@ -5,6 +5,7 @@ import ( "go.uber.org/zap/zapcore" ) +// NullLogger is Logger that sends a log lines into the ether var NullLogger = &Logger{ undlerlying: zap.New(zapcore.NewNopCore()).Sugar(), } diff --git a/internal/common/logging/pulsar_adapter.go b/internal/common/logging/pulsar_adapter.go index 8d6f298a85f..3e3c3e48426 100644 --- a/internal/common/logging/pulsar_adapter.go +++ b/internal/common/logging/pulsar_adapter.go @@ -4,10 +4,12 @@ import ( pulsarlog "github.com/apache/pulsar-client-go/pulsar/log" ) +// Wrapper to adapt Logger to the logger interface expected by the pulsar client type pulsarWrapper struct { l *Logger } +// NewPulsarLogger returns a Logger that can be used by the pulsar client func NewPulsarLogger() pulsarlog.Logger { return &pulsarWrapper{ l: StdLogger(), @@ -39,19 +41,19 @@ func (p pulsarWrapper) WithError(err error) pulsarlog.Entry { } func (p pulsarWrapper) Debug(args ...any) { - p.l.Debug(args) + p.l.Debug(args...) } func (p pulsarWrapper) Info(args ...any) { - p.l.Info(args) + p.l.Info(args...) } func (p pulsarWrapper) Warn(args ...any) { - p.l.Warn(args) + p.l.Warn(args...) } func (p pulsarWrapper) Error(args ...any) { - p.l.Error(args) + p.l.Error(args...) } func (p pulsarWrapper) Debugf(format string, args ...any) { diff --git a/internal/common/logging/user_input.go b/internal/common/logging/user_input.go deleted file mode 100644 index 3fb161040b9..00000000000 --- a/internal/common/logging/user_input.go +++ /dev/null @@ -1,8 +0,0 @@ -package logging - -import "strings" - -func SanitizeUserInput(str string) string { - safeStr := strings.Replace(str, "\n", "", -1) - return strings.Replace(safeStr, "\r", "", -1) -} diff --git a/internal/common/startup.go b/internal/common/startup.go index 47ef02f051a..38e08d53a1b 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -1,7 +1,6 @@ package common import ( - "bytes" "crypto/tls" "fmt" "net/http" @@ -63,8 +62,7 @@ func LoadConfig(config commonconfig.Config, defaultPath string, overrideConfigs var metadata mapstructure.Metadata customHooks := append(slices.Clone(commonconfig.CustomHooks), func(c *mapstructure.DecoderConfig) { c.Metadata = &metadata }) if err := v.Unmarshal(config, customHooks...); err != nil { - log.Error(err.Error()) - os.Exit(-1) + log.Fatal(err) } // Log a warning if there are config keys that don't match a config item in the struct the yaml is decoded into. @@ -92,22 +90,12 @@ func UnmarshalKey(v *viper.Viper, key string, item interface{}) error { return v.UnmarshalKey(key, item, commonconfig.CustomHooks...) } -type CommandLineEncoder struct{} - -func (e *CommandLineEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*bytes.Buffer, error) { - var buf bytes.Buffer - buf.WriteString(entry.Message) - buf.WriteString("\n") - return &buf, nil -} - -// TODO Move logging-related code out of common into a new package internal/logging func ConfigureCommandLineLogging() { // Define an encoder configuration that only includes the message. encoderConfig := zapcore.EncoderConfig{ - MessageKey: "message", // Only include the message. - LineEnding: "\n", // Newline after each log entry. - // Ignore everything else by leaving their keys empty. + MessageKey: "message", + LineEnding: "\n", + // Ignore everything other than the message by leaving their keys empty. LevelKey: "", TimeKey: "", NameKey: "", @@ -126,7 +114,7 @@ func ConfigureCommandLineLogging() { ) l := zap.New(core) - log.SetDefaultLogger(log.FromZap(l)) + log.ReplaceStdLogger(log.FromZap(l)) } func ConfigureLogging() { @@ -139,7 +127,7 @@ func ConfigureLogging() { core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), readEnvironmentLogLevel()) l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) - log.SetDefaultLogger(log.FromZap(l)) + log.ReplaceStdLogger(log.FromZap(l)) } func readEnvironmentLogLevel() zapcore.Level { diff --git a/internal/lookoutv2/repository/util.go b/internal/lookoutv2/repository/util.go index 6768262b0a4..6c136532626 100644 --- a/internal/lookoutv2/repository/util.go +++ b/internal/lookoutv2/repository/util.go @@ -706,14 +706,14 @@ func prefixAnnotations(prefix string, annotations map[string]string) map[string] func logQueryDebug(query *Query, description string) { log. - With("query", removeNewlinesAndTabs(query.Sql)). + WithField("query", removeNewlinesAndTabs(query.Sql)). WithField("values", query.Args). Debug(description) } func logQueryError(query *Query, description string, duration time.Duration) { log. - With("query", removeNewlinesAndTabs(query.Sql)). + WithField("query", removeNewlinesAndTabs(query.Sql)). WithField("values", query.Args). WithField("duration", duration). Errorf("Error executing %s query", description) @@ -722,7 +722,7 @@ func logQueryError(query *Query, description string, duration time.Duration) { func logSlowQuery(query *Query, description string, duration time.Duration) { if duration > 5*time.Second { log. - With("query", removeNewlinesAndTabs(query.Sql)). + WithField("query", removeNewlinesAndTabs(query.Sql)). WithField("values", query.Args). WithField("duration", duration). Infof("Slow %s query detected", description) diff --git a/pkg/client/auth/oidc/device.go b/pkg/client/auth/oidc/device.go index e6f9ae63474..3abee6473da 100644 --- a/pkg/client/auth/oidc/device.go +++ b/pkg/client/auth/oidc/device.go @@ -13,8 +13,6 @@ import ( openId "github.com/coreos/go-oidc" "golang.org/x/oauth2" - - "github.com/armadaproject/armada/internal/common/logging" ) type DeviceDetails struct { @@ -152,6 +150,11 @@ func makeErrorForHTTPResponse(resp *http.Response) error { if err != nil { return err } - safeURL := logging.SanitizeUserInput(resp.Request.URL.String()) + safeURL := sanitize(resp.Request.URL.String()) return fmt.Errorf("%s %s returned HTTP %s; \n\n %#q", resp.Request.Method, safeURL, resp.Status, bodyBytes) } + +func sanitize(str string) string { + safeStr := strings.ReplaceAll(str, "\n", "") + return strings.ReplaceAll(safeStr, "\r", "") +} From b882e6abeb25716ad211de9ad3cfc0417a657fb6 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sat, 4 Jan 2025 23:24:18 +0000 Subject: [PATCH 22/58] clean up interfaces --- .../armadacontext/armada_context_test.go | 141 +++++++++--------- internal/common/logging/global.go | 23 ++- internal/common/logging/logger.go | 32 ++-- internal/common/logging/logger_test.go | 94 ++++++++++++ internal/common/logging/null_logger.go | 2 +- 5 files changed, 201 insertions(+), 91 deletions(-) create mode 100644 internal/common/logging/logger_test.go diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index 5cb5621b43c..2e74304ce72 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -1,81 +1,80 @@ package armadacontext -// -//import ( -// "context" -// "testing" -// "time" -// -// "github.com/sirupsen/logrus" -// "github.com/stretchr/testify/require" -//) -// -//var defaultLogger = logrus.WithField("foo", "bar") -// -//func TestNew(t *testing.T) { -// ctx := New(context.Background(), defaultLogger) -// require.Equal(t, defaultLogger, ctx.FieldLogger) -// require.Equal(t, context.Background(), ctx.Context) -//} -// -//func TestBackground(t *testing.T) { -// ctx := Background() -// require.Equal(t, ctx.Context, context.Background()) -//} -// -//func TestTODO(t *testing.T) { -// ctx := TODO() -// require.Equal(t, ctx.Context, context.TODO()) -//} -// +import ( + "context" + "github.com/armadaproject/armada/internal/common/logging" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +var defaultLogger = logging.StdLogger().WithField("foo", "bar") + +func TestNew(t *testing.T) { + ctx := New(context.Background(), defaultLogger) + require.Equal(t, defaultLogger, ctx.Logger()) + require.Equal(t, context.Background(), ctx.Context) +} + +func TestBackground(t *testing.T) { + ctx := Background() + require.Equal(t, ctx.Context, context.Background()) +} + +func TestTODO(t *testing.T) { + ctx := TODO() + require.Equal(t, ctx.Context, context.TODO()) +} + //func TestWithLogField(t *testing.T) { // ctx := WithLogField(Background(), "fish", "chips") // require.Equal(t, context.Background(), ctx.Context) -// require.Equal(t, logrus.Fields{"fish": "chips"}, ctx.FieldLogger.(*logrus.Entry).Data) +// require.Equal(t, map[string]any{"fish": "chips"}, ctx.Logger().(*logrus.Entry).Data) //} // //func TestWithLogFields(t *testing.T) { -// ctx := WithLogFields(Background(), logrus.Fields{"fish": "chips", "salt": "pepper"}) +// ctx := WithLogFields(Background(), map[string]any{"fish": "chips", "salt": "pepper"}) // require.Equal(t, context.Background(), ctx.Context) -// require.Equal(t, logrus.Fields{"fish": "chips", "salt": "pepper"}, ctx.FieldLogger.(*logrus.Entry).Data) -//} -// -//func TestWithTimeout(t *testing.T) { -// ctx, _ := WithTimeout(Background(), 100*time.Millisecond) -// testDeadline(t, ctx) -//} -// -//func TestWithDeadline(t *testing.T) { -// ctx, _ := WithDeadline(Background(), time.Now().Add(100*time.Millisecond)) -// testDeadline(t, ctx) -//} -// -//func TestWithValue(t *testing.T) { -// ctx := WithValue(Background(), "foo", "bar") -// require.Equal(t, "bar", ctx.Value("foo")) -//} -// -//func testDeadline(t *testing.T, c *Context) { -// t.Helper() -// d := quiescent(t) -// timer := time.NewTimer(d) -// defer timer.Stop() -// select { -// case <-timer.C: -// t.Fatalf("context not timed out after %v", d) -// case <-c.Done(): -// } -// if e := c.Err(); e != context.DeadlineExceeded { -// t.Errorf("c.Err() == %v; want %v", e, context.DeadlineExceeded) -// } -//} -// -//func quiescent(t *testing.T) time.Duration { -// deadline, ok := t.Deadline() -// if !ok { -// return 5 * time.Second -// } -// -// const arbitraryCleanupMargin = 1 * time.Second -// return time.Until(deadline) - arbitraryCleanupMargin +// require.Equal(t, map[string]any{"fish": "chips", "salt": "pepper"}, ctx.FieldLogger.(*logrus.Entry).Data) //} + +func TestWithTimeout(t *testing.T) { + ctx, _ := WithTimeout(Background(), 100*time.Millisecond) + testDeadline(t, ctx) +} + +func TestWithDeadline(t *testing.T) { + ctx, _ := WithDeadline(Background(), time.Now().Add(100*time.Millisecond)) + testDeadline(t, ctx) +} + +func TestWithValue(t *testing.T) { + ctx := WithValue(Background(), "foo", "bar") + require.Equal(t, "bar", ctx.Value("foo")) +} + +func testDeadline(t *testing.T, c *Context) { + t.Helper() + d := quiescent(t) + timer := time.NewTimer(d) + defer timer.Stop() + select { + case <-timer.C: + t.Fatalf("context not timed out after %v", d) + case <-c.Done(): + } + if e := c.Err(); e != context.DeadlineExceeded { + t.Errorf("c.Err() == %v; want %v", e, context.DeadlineExceeded) + } +} + +func quiescent(t *testing.T) time.Duration { + deadline, ok := t.Deadline() + if !ok { + return 5 * time.Second + } + + const arbitraryCleanupMargin = 1 * time.Second + return time.Until(deadline) - arbitraryCleanupMargin +} diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go index c88dab4b63d..4b222e2b7ce 100644 --- a/internal/common/logging/global.go +++ b/internal/common/logging/global.go @@ -2,12 +2,15 @@ package logging import ( "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "os" ) -// The default Logger -var stdLogger = &Logger{zap.S()} +// The global Logger. Comes configured with some sensible defaults for e.g. unit tests, but applications should +// generally configure their own logging config via ReplaceStdLogger +var stdLogger = &Logger{underlying: createDefaultLogger()} -// ReplaceStdLogger Replaces the defualt logger. This should be called once at app startup! +// ReplaceStdLogger Replaces the global logger. This should be called once at app startup! func ReplaceStdLogger(l *Logger) { stdLogger = l } @@ -91,3 +94,17 @@ func WithError(err error) *Logger { func WithStacktrace(err error) *Logger { return stdLogger.WithStacktrace(err) } + +// Default logging options +func createDefaultLogger() *zap.SugaredLogger { + pe := zap.NewProductionEncoderConfig() + pe.EncodeTime = zapcore.ISO8601TimeEncoder + pe.ConsoleSeparator = " " + pe.EncodeLevel = zapcore.CapitalLevelEncoder + consoleEncoder := zapcore.NewConsoleEncoder(pe) + core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel) + return zap. + New(core, zap.AddCaller()). + WithOptions(zap.AddCallerSkip(2)). + Sugar() +} diff --git a/internal/common/logging/logger.go b/internal/common/logging/logger.go index d058fc89fb6..2eb64137e28 100644 --- a/internal/common/logging/logger.go +++ b/internal/common/logging/logger.go @@ -7,69 +7,69 @@ import ( // Logger wraps a *zap.SugaredLogger so that the rest of the code doesn't depend directly on Zap type Logger struct { - undlerlying *zap.SugaredLogger + underlying *zap.SugaredLogger } // FromZap returns a New Logger backed by the supplied *zap.SugaredLogger func FromZap(l *zap.Logger) *Logger { return &Logger{ - undlerlying: l.Sugar(), + underlying: l.Sugar(), } } // Debug logs a message at level Debug func (l *Logger) Debug(args ...any) { - l.undlerlying.Debug(args...) + l.underlying.Debug(args...) } // Info logs a message at level Info func (l *Logger) Info(args ...any) { - l.undlerlying.Info(args...) + l.underlying.Info(args...) } // Warn logs a message at level Warn func (l *Logger) Warn(args ...any) { - l.undlerlying.Warn(args...) + l.underlying.Warn(args...) } // Error logs a message at level Error func (l *Logger) Error(args ...any) { - l.undlerlying.Error(args...) + l.underlying.Error(args...) } // Panic logs a message at level Panic func (l *Logger) Panic(args ...any) { - l.undlerlying.Panic(args...) + l.underlying.Panic(args...) } // Fatal logs a message at level Fatal then the process will exit with status set to 1. func (l *Logger) Fatal(args ...any) { - l.undlerlying.Fatal(args...) + l.underlying.Fatal(args...) } // Debugf logs a message at level Debug. func (l *Logger) Debugf(format string, args ...interface{}) { - l.undlerlying.Debugf(format, args...) + l.underlying.Debugf(format, args...) } // Infof logs a message at level Info. func (l *Logger) Infof(format string, args ...interface{}) { - l.undlerlying.Infof(format, args...) + l.underlying.Infof(format, args...) } // Warnf logs a message at level Warn. func (l *Logger) Warnf(format string, args ...interface{}) { - l.undlerlying.Warnf(format, args...) + l.underlying.Warnf(format, args...) } // Errorf logs a message at level Error. func (l *Logger) Errorf(format string, args ...interface{}) { - l.undlerlying.Errorf(format, args...) + l.underlying.Errorf(format, args...) } // Fatalf logs a message at level Fatal. func (l *Logger) Fatalf(format string, args ...interface{}) { - l.undlerlying.Fatalf(format, args...) + l.underlying.Fatalf(format, args...) } // WithError returns a new Logger with the error added as a field @@ -90,7 +90,7 @@ func (l *Logger) WithStacktrace(err error) *Logger { // WithField returns a new Logger with the key-value pair added as a new field func (l *Logger) WithField(key string, value any) *Logger { return &Logger{ - undlerlying: l.undlerlying.With(key, value), + underlying: l.underlying.With(key, value), } } @@ -101,7 +101,7 @@ func (l *Logger) WithFields(args map[string]any) *Logger { fields = append(fields, zap.Any(key, value)) } return &Logger{ - undlerlying: l.undlerlying.With(fields...), + underlying: l.underlying.With(fields...), } } @@ -110,7 +110,7 @@ func (l *Logger) WithFields(args map[string]any) *Logger { // wrapper code as the caller. func (l *Logger) WithCallerSkip(skip int) *Logger { return &Logger{ - undlerlying: l.undlerlying.WithOptions(zap.AddCallerSkip(skip)), + underlying: l.underlying.WithOptions(zap.AddCallerSkip(skip)), } } diff --git a/internal/common/logging/logger_test.go b/internal/common/logging/logger_test.go new file mode 100644 index 00000000000..bcd596cc00d --- /dev/null +++ b/internal/common/logging/logger_test.go @@ -0,0 +1,94 @@ +package logging + +import ( + "testing" + + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest/observer" +) + +func TestWithField(t *testing.T) { + + logger, observedLogs := testLogger() + + newLogger := logger.WithField("foo", "bar") + newLogger.Info("test message") + + // Check the captured log entries + entries := observedLogs.All() + require.Len(t, entries, 1, "Expected exactly one log entry") + + // Verify the fields + logEntry := entries[0] + assert.Equal(t, "test message", logEntry.Message) + assert.Equal(t, "bar", logEntry.ContextMap()["foo"]) +} + +func TestWithFields(t *testing.T) { + + logger, observedLogs := testLogger() + + newLogger := logger.WithFields(map[string]any{ + "user": "test_user", + "action": "test_action", + }) + newLogger.Info("test message") + + // Check the captured log entries + entries := observedLogs.All() + require.Len(t, entries, 1, "Expected exactly one log entry") + + // Verify the fields + logEntry := entries[0] + assert.Equal(t, "test message", logEntry.Message) + assert.Equal(t, "test_user", logEntry.ContextMap()["user"]) + assert.Equal(t, "test_action", logEntry.ContextMap()["action"]) +} + +func TestWithError(t *testing.T) { + logger, observedLogs := testLogger() + + err := errors.New("test error") + + newLogger := logger.WithError(err) + newLogger.Info("test message") + + // Check the captured log entries + entries := observedLogs.All() + require.Len(t, entries, 1, "Expected exactly one log entry") + + // Verify the fields + logEntry := entries[0] + assert.Equal(t, "test message", logEntry.Message) + assert.Equal(t, "test error", logEntry.ContextMap()["error"]) +} + +func TestWithStacktrace(t *testing.T) { + logger, observedLogs := testLogger() + + err := errors.WithStack(errors.New("test error")) + + newLogger := logger.WithStacktrace(err) + newLogger.Info("test message") + + // Check the captured log entries + entries := observedLogs.All() + require.Len(t, entries, 1, "Expected exactly one log entry") + + // Verify the fields + logEntry := entries[0] + assert.Equal(t, "test message", logEntry.Message) + assert.Equal(t, "test error", logEntry.ContextMap()["error"]) + assert.Equal(t, err.(stackTracer).StackTrace(), logEntry.ContextMap()["stacktrace"]) +} + +func testLogger() (*Logger, *observer.ObservedLogs) { + core, observedLogs := observer.New(zapcore.DebugLevel) + baseLogger := zap.New(core).Sugar() + logger := &Logger{underlying: baseLogger} + return logger, observedLogs +} diff --git a/internal/common/logging/null_logger.go b/internal/common/logging/null_logger.go index de52f95a2a2..7bafa1e0a39 100644 --- a/internal/common/logging/null_logger.go +++ b/internal/common/logging/null_logger.go @@ -7,5 +7,5 @@ import ( // NullLogger is Logger that sends a log lines into the ether var NullLogger = &Logger{ - undlerlying: zap.New(zapcore.NewNopCore()).Sugar(), + underlying: zap.New(zapcore.NewNopCore()).Sugar(), } From b708709cf86d9a9824ecd2b27a4ce654a54cfd1b Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 5 Jan 2025 12:53:18 +0000 Subject: [PATCH 23/58] wip --- .../common/armadacontext/armada_context.go | 5 ++- internal/common/auth/common.go | 2 + .../common/grpc/armadacontext/interceptors.go | 37 ------------------- internal/common/grpc/grpc.go | 10 ++--- internal/common/requestid/interceptors.go | 1 + 5 files changed, 11 insertions(+), 44 deletions(-) delete mode 100644 internal/common/grpc/armadacontext/interceptors.go diff --git a/internal/common/armadacontext/armada_context.go b/internal/common/armadacontext/armada_context.go index 5515ebe1f14..30fb2b466c9 100644 --- a/internal/common/armadacontext/armada_context.go +++ b/internal/common/armadacontext/armada_context.go @@ -38,7 +38,10 @@ func FromGrpcCtx(ctx context.Context) *Context { if ok { return armadaCtx } - return New(ctx, logrus.NewEntry(logrus.StandardLogger())) + logger := logrus.NewEntry(logrus.StandardLogger()). + WithField("user", ctx.Value("user")). + WithField("requestId", ctx.Value("requestId")) + return New(ctx, logger) } // New returns an armada context that encapsulates both a go context and a logger diff --git a/internal/common/auth/common.go b/internal/common/auth/common.go index 62a22949b93..604487bdffd 100644 --- a/internal/common/auth/common.go +++ b/internal/common/auth/common.go @@ -123,6 +123,8 @@ func CreateGrpcMiddlewareAuthFunction(authService AuthService) func(ctx context. if err != nil { return nil, err } + // record username for request logging + ctx = context.WithValue(ctx, "user", principal.GetName()) return WithPrincipal(ctx, principal), nil } } diff --git a/internal/common/grpc/armadacontext/interceptors.go b/internal/common/grpc/armadacontext/interceptors.go deleted file mode 100644 index 9622e6791a5..00000000000 --- a/internal/common/grpc/armadacontext/interceptors.go +++ /dev/null @@ -1,37 +0,0 @@ -package armadacontext - -import ( - "context" - - grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware/v2" - "github.com/sirupsen/logrus" - "google.golang.org/grpc" - - "github.com/armadaproject/armada/internal/common/armadacontext" - "github.com/armadaproject/armada/internal/common/auth" - "github.com/armadaproject/armada/internal/common/requestid" -) - -func UnaryServerInterceptor() grpc.UnaryServerInterceptor { - return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { - armadaCtx := createArmadaCtx(ctx) - return handler(armadaCtx, req) - } -} - -func StreamServerInterceptor() grpc.StreamServerInterceptor { - return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { - armadaCtx := createArmadaCtx(stream.Context()) - wrapped := grpc_middleware.WrapServerStream(stream) - wrapped.WrappedContext = armadaCtx - return handler(srv, wrapped) - } -} - -func createArmadaCtx(ctx context.Context) *armadacontext.Context { - log := logrus.WithFields(map[string]interface{}{ - "requestId": requestid.FromContextOrMissing(ctx), - "user": auth.GetPrincipal(ctx).GetName(), - }) - return armadacontext.New(ctx, log) -} diff --git a/internal/common/grpc/grpc.go b/internal/common/grpc/grpc.go index 9c63757c5a0..1139e04a427 100644 --- a/internal/common/grpc/grpc.go +++ b/internal/common/grpc/grpc.go @@ -27,7 +27,6 @@ import ( "github.com/armadaproject/armada/internal/common/armadaerrors" "github.com/armadaproject/armada/internal/common/auth" "github.com/armadaproject/armada/internal/common/certs" - grpc_armadacontext "github.com/armadaproject/armada/internal/common/grpc/armadacontext" "github.com/armadaproject/armada/internal/common/grpc/configuration" "github.com/armadaproject/armada/internal/common/requestid" ) @@ -56,7 +55,6 @@ func CreateGrpcServer( srvMetrics.UnaryServerInterceptor(), requestid.UnaryServerInterceptor(false), grpc_auth.UnaryServerInterceptor(authFunction), - grpc_armadacontext.UnaryServerInterceptor(), grpc_logging.UnaryServerInterceptor(InterceptorLogger(), loggerOpts...), armadaerrors.UnaryServerInterceptor(2000), grpc_recovery.UnaryServerInterceptor(grpc_recovery.WithRecoveryHandler(panicRecoveryHandler)), @@ -65,7 +63,6 @@ func CreateGrpcServer( srvMetrics.StreamServerInterceptor(), requestid.StreamServerInterceptor(false), grpc_auth.StreamServerInterceptor(authFunction), - grpc_armadacontext.StreamServerInterceptor(), grpc_logging.StreamServerInterceptor(InterceptorLogger(), loggerOpts...), armadaerrors.StreamServerInterceptor(2000), grpc_recovery.StreamServerInterceptor(grpc_recovery.WithRecoveryHandler(panicRecoveryHandler)), @@ -146,14 +143,15 @@ func panicRecoveryHandler(p interface{}) (err error) { func InterceptorLogger() grpc_logging.Logger { return grpc_logging.LoggerFunc(func(ctx context.Context, lvl grpc_logging.Level, msg string, fields ...any) { - armadaCtx := armadacontext.FromGrpcCtx(ctx) - logFields := make(map[string]any, len(fields)/2) + logFields := make(map[string]any, len(fields)/2+2) + logFields["user"] = ctx.Value("user") + logFields["requestId"] = ctx.Value("requestId") i := grpc_logging.Fields(fields).Iterator() for i.Next() { k, v := i.At() logFields[k] = v } - l := armadaCtx.WithFields(logFields) + l := log.WithFields(logFields) switch lvl { case grpc_logging.LevelDebug: l.Debug(msg) diff --git a/internal/common/requestid/interceptors.go b/internal/common/requestid/interceptors.go index 2e775545319..bd7dd1e7202 100644 --- a/internal/common/requestid/interceptors.go +++ b/internal/common/requestid/interceptors.go @@ -43,6 +43,7 @@ func FromContextOrMissing(ctx context.Context) string { // The second return value is true if the operation was successful. func AddToIncomingContext(ctx context.Context, id string) (context.Context, bool) { if md, ok := metadata.FromIncomingContext(ctx); ok { + ctx = context.WithValue(ctx, "requestId", id) md.Set(MetadataKey, id) return metadata.NewIncomingContext(ctx, md), true } From 05f175067d9b2784ad977bce8c1089fa57e38f8e Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 5 Jan 2025 13:17:10 +0000 Subject: [PATCH 24/58] added back json logging --- internal/common/startup.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/common/startup.go b/internal/common/startup.go index 38e08d53a1b..aaf42c254c7 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -123,13 +123,31 @@ func ConfigureLogging() { pe.ConsoleSeparator = " " pe.EncodeLevel = zapcore.CapitalLevelEncoder pe.EncodeCaller = ShortCallerEncoder - consoleEncoder := zapcore.NewConsoleEncoder(pe) + encoder := readEnvironmentLogFormat(pe) - core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), readEnvironmentLogLevel()) + core := zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), readEnvironmentLogLevel()) l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) log.ReplaceStdLogger(log.FromZap(l)) } +func readEnvironmentLogFormat(pe zapcore.EncoderConfig) zapcore.Encoder { + + formatStr, ok := os.LookupEnv("LOG_FORMAT") + if !ok { + formatStr = "text" + } + + switch strings.ToLower(formatStr) { + case "json": + return zapcore.NewJSONEncoder(pe) + case "text": + return zapcore.NewConsoleEncoder(pe) + default: + println(os.Stderr, fmt.Sprintf("Unknown log format %s, defaulting to text format", formatStr)) + return zapcore.NewConsoleEncoder(pe) + } +} + func readEnvironmentLogLevel() zapcore.Level { level, ok := os.LookupEnv("LOG_LEVEL") if ok { From edd97342ccd37939d342dd28b17b08ed24f8a886 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 5 Jan 2025 13:19:01 +0000 Subject: [PATCH 25/58] lint --- internal/common/armadacontext/armada_context_test.go | 3 ++- internal/common/logging/global.go | 3 ++- internal/common/logging/logger_test.go | 2 -- internal/common/startup.go | 1 - 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index 2e74304ce72..b1a28d49160 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -2,11 +2,12 @@ package armadacontext import ( "context" - "github.com/armadaproject/armada/internal/common/logging" "testing" "time" "github.com/stretchr/testify/require" + + "github.com/armadaproject/armada/internal/common/logging" ) var defaultLogger = logging.StdLogger().WithField("foo", "bar") diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go index 4b222e2b7ce..b5cb747c1f1 100644 --- a/internal/common/logging/global.go +++ b/internal/common/logging/global.go @@ -1,9 +1,10 @@ package logging import ( + "os" + "go.uber.org/zap" "go.uber.org/zap/zapcore" - "os" ) // The global Logger. Comes configured with some sensible defaults for e.g. unit tests, but applications should diff --git a/internal/common/logging/logger_test.go b/internal/common/logging/logger_test.go index bcd596cc00d..349aaf623b0 100644 --- a/internal/common/logging/logger_test.go +++ b/internal/common/logging/logger_test.go @@ -12,7 +12,6 @@ import ( ) func TestWithField(t *testing.T) { - logger, observedLogs := testLogger() newLogger := logger.WithField("foo", "bar") @@ -29,7 +28,6 @@ func TestWithField(t *testing.T) { } func TestWithFields(t *testing.T) { - logger, observedLogs := testLogger() newLogger := logger.WithFields(map[string]any{ diff --git a/internal/common/startup.go b/internal/common/startup.go index aaf42c254c7..c9fcc490fb8 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -131,7 +131,6 @@ func ConfigureLogging() { } func readEnvironmentLogFormat(pe zapcore.EncoderConfig) zapcore.Encoder { - formatStr, ok := os.LookupEnv("LOG_FORMAT") if !ok { formatStr = "text" From 3d45d6ce17cff38121d16bc0f552b9d276445527 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 5 Jan 2025 21:29:34 +0000 Subject: [PATCH 26/58] fix test --- internal/common/armadacontext/armada_context_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index b1a28d49160..31ff956e1a9 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -14,7 +14,7 @@ var defaultLogger = logging.StdLogger().WithField("foo", "bar") func TestNew(t *testing.T) { ctx := New(context.Background(), defaultLogger) - require.Equal(t, defaultLogger, ctx.Logger()) + require.Equal(t, defaultLogger, ctx.logger) require.Equal(t, context.Background(), ctx.Context) } From dd94cb019b506d92afc082df09db691820857871 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 5 Jan 2025 21:34:24 +0000 Subject: [PATCH 27/58] add back log tests --- .../armadacontext/armada_context_test.go | 56 +++++++++++++++---- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index 31ff956e1a9..fcdb0828024 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -2,6 +2,10 @@ package armadacontext import ( "context" + "github.com/stretchr/testify/assert" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "go.uber.org/zap/zaptest/observer" "testing" "time" @@ -28,17 +32,40 @@ func TestTODO(t *testing.T) { require.Equal(t, ctx.Context, context.TODO()) } -//func TestWithLogField(t *testing.T) { -// ctx := WithLogField(Background(), "fish", "chips") -// require.Equal(t, context.Background(), ctx.Context) -// require.Equal(t, map[string]any{"fish": "chips"}, ctx.Logger().(*logrus.Entry).Data) -//} -// -//func TestWithLogFields(t *testing.T) { -// ctx := WithLogFields(Background(), map[string]any{"fish": "chips", "salt": "pepper"}) -// require.Equal(t, context.Background(), ctx.Context) -// require.Equal(t, map[string]any{"fish": "chips", "salt": "pepper"}, ctx.FieldLogger.(*logrus.Entry).Data) -//} +func TestWithLogField(t *testing.T) { + logger, observedLogs := testLogger() + ctx := WithLogField(New(context.Background(), logger), "fish", "chips") + require.Equal(t, context.Background(), ctx.Context) + + ctx.Info("test message") + + // Check the captured log entries + entries := observedLogs.All() + require.Len(t, entries, 1, "Expected exactly one log entry") + + // Verify the fields + logEntry := entries[0] + assert.Equal(t, "test message", logEntry.Message) + assert.Equal(t, "chips", logEntry.ContextMap()["fish"]) +} + +func TestWithLogFields(t *testing.T) { + logger, observedLogs := testLogger() + ctx := WithLogFields(New(context.Background(), logger), map[string]any{"fish": "chips", "salt": "pepper"}) + require.Equal(t, context.Background(), ctx.Context) + + ctx.Info("test message") + + // Check the captured log entries + entries := observedLogs.All() + require.Len(t, entries, 1, "Expected exactly one log entry") + + // Verify the fields + logEntry := entries[0] + assert.Equal(t, "test message", logEntry.Message) + assert.Equal(t, "chips", logEntry.ContextMap()["fish"]) + assert.Equal(t, "pepper", logEntry.ContextMap()["salt"]) +} func TestWithTimeout(t *testing.T) { ctx, _ := WithTimeout(Background(), 100*time.Millisecond) @@ -79,3 +106,10 @@ func quiescent(t *testing.T) time.Duration { const arbitraryCleanupMargin = 1 * time.Second return time.Until(deadline) - arbitraryCleanupMargin } + +func testLogger() (*logging.Logger, *observer.ObservedLogs) { + core, observedLogs := observer.New(zapcore.DebugLevel) + baseLogger := zap.New(core) + logger := logging.FromZap(baseLogger) + return logger, observedLogs +} From 94ad2069829f526d8cb5f9c28cfc2a3a98e996ed Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Mon, 6 Jan 2025 07:15:36 +0000 Subject: [PATCH 28/58] wip --- cmd/binoculars/main.go | 2 +- cmd/eventingester/main.go | 3 +- cmd/executor/main.go | 2 +- cmd/fakeexecutor/main.go | 2 +- cmd/lookoutingesterv2/dbloadtester/main.go | 2 +- cmd/lookoutingesterv2/main.go | 2 +- cmd/lookoutv2/main.go | 2 +- cmd/scheduler/main.go | 3 +- cmd/scheduleringester/main.go | 4 +- cmd/server/main.go | 2 +- config/logging.yaml | 13 ++ go.mod | 1 + go.sum | 2 + .../armadacontext/armada_context_test.go | 5 +- internal/common/logging/config.go | 170 ++++++++++++++++++ internal/common/logging/global.go | 4 +- internal/common/startup.go | 66 ------- 17 files changed, 206 insertions(+), 79 deletions(-) create mode 100644 config/logging.yaml create mode 100644 internal/common/logging/config.go diff --git a/cmd/binoculars/main.go b/cmd/binoculars/main.go index 5c0558aeb3a..cd4e0221766 100644 --- a/cmd/binoculars/main.go +++ b/cmd/binoculars/main.go @@ -35,7 +35,7 @@ func init() { } func main() { - common.ConfigureLogging() + log.MustConfigureApplicationLogging() common.BindCommandlineArguments() var config configuration.BinocularsConfig diff --git a/cmd/eventingester/main.go b/cmd/eventingester/main.go index 93690f33b4d..c6cca9d2791 100644 --- a/cmd/eventingester/main.go +++ b/cmd/eventingester/main.go @@ -1,6 +1,7 @@ package main import ( + "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/eventingester" "github.com/spf13/pflag" @@ -24,7 +25,7 @@ func init() { } func main() { - common.ConfigureLogging() + logging.MustConfigureApplicationLogging() common.BindCommandlineArguments() var config configuration.EventIngesterConfiguration diff --git a/cmd/executor/main.go b/cmd/executor/main.go index a91c0304f49..75567416a1f 100644 --- a/cmd/executor/main.go +++ b/cmd/executor/main.go @@ -31,7 +31,7 @@ func init() { } func main() { - common.ConfigureLogging() + log.MustConfigureApplicationLogging() common.BindCommandlineArguments() var config configuration.ExecutorConfiguration diff --git a/cmd/fakeexecutor/main.go b/cmd/fakeexecutor/main.go index 69d9ce03c33..61b97a38d7e 100644 --- a/cmd/fakeexecutor/main.go +++ b/cmd/fakeexecutor/main.go @@ -29,7 +29,7 @@ func init() { } func main() { - common.ConfigureLogging() + log.MustConfigureApplicationLogging() common.BindCommandlineArguments() var config configuration.ExecutorConfiguration diff --git a/cmd/lookoutingesterv2/dbloadtester/main.go b/cmd/lookoutingesterv2/dbloadtester/main.go index 87f91fa1f73..95e4b834673 100644 --- a/cmd/lookoutingesterv2/dbloadtester/main.go +++ b/cmd/lookoutingesterv2/dbloadtester/main.go @@ -45,7 +45,7 @@ const ReportTemplate string = ` ` func main() { - common.ConfigureLogging() + log.MustConfigureApplicationLogging() common.BindCommandlineArguments() var config configuration.LookoutIngesterV2Configuration diff --git a/cmd/lookoutingesterv2/main.go b/cmd/lookoutingesterv2/main.go index a0e48a6374f..70be8d713f2 100644 --- a/cmd/lookoutingesterv2/main.go +++ b/cmd/lookoutingesterv2/main.go @@ -27,7 +27,7 @@ func init() { } func main() { - common.ConfigureLogging() + log.MustConfigureApplicationLogging() common.BindCommandlineArguments() var config configuration.LookoutIngesterV2Configuration diff --git a/cmd/lookoutv2/main.go b/cmd/lookoutv2/main.go index 825b939d904..584aac3edf3 100644 --- a/cmd/lookoutv2/main.go +++ b/cmd/lookoutv2/main.go @@ -117,7 +117,7 @@ func prune(ctx *armadacontext.Context, config configuration.LookoutV2Config) { } func main() { - common.ConfigureLogging() + log.MustConfigureApplicationLogging() common.BindCommandlineArguments() var config configuration.LookoutV2Config diff --git a/cmd/scheduler/main.go b/cmd/scheduler/main.go index f895b3301cd..7249a2c54d9 100644 --- a/cmd/scheduler/main.go +++ b/cmd/scheduler/main.go @@ -6,10 +6,11 @@ import ( "github.com/armadaproject/armada/cmd/scheduler/cmd" "github.com/armadaproject/armada/internal/common" + "github.com/armadaproject/armada/internal/common/logging" ) func main() { - common.ConfigureLogging() + logging.MustConfigureApplicationLogging() common.BindCommandlineArguments() if err := cmd.RootCmd().Execute(); err != nil { os.Exit(1) diff --git a/cmd/scheduleringester/main.go b/cmd/scheduleringester/main.go index 9aa5207a2ce..527d9d2d627 100644 --- a/cmd/scheduleringester/main.go +++ b/cmd/scheduleringester/main.go @@ -4,6 +4,8 @@ import ( "fmt" "os" + "github.com/armadaproject/armada/internal/common/logging" + "github.com/spf13/pflag" "github.com/spf13/viper" @@ -24,7 +26,7 @@ func init() { } func main() { - common.ConfigureLogging() + logging.MustConfigureApplicationLogging() common.BindCommandlineArguments() var config scheduleringester.Configuration diff --git a/cmd/server/main.go b/cmd/server/main.go index 55aaf59e2c7..df909f5649d 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -35,7 +35,7 @@ func init() { } func main() { - common.ConfigureLogging() + logging.MustConfigureApplicationLogging() common.BindCommandlineArguments() // TODO Load relevant config in one place: don't use viper here and in LoadConfig. diff --git a/config/logging.yaml b/config/logging.yaml new file mode 100644 index 00000000000..30f04cac185 --- /dev/null +++ b/config/logging.yaml @@ -0,0 +1,13 @@ +console: + level: INFO + format: text +file: + enabled: true + level: INFO + format: json + logfile: app.log + rotation: + maxSizeMb: 5 + maxBackups: 3 + maxAgeDays: 7 + compress: false diff --git a/go.mod b/go.mod index 776a0d75590..665ec23c7c0 100644 --- a/go.mod +++ b/go.mod @@ -205,6 +205,7 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect google.golang.org/protobuf v1.35.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/square/go-jose.v2 v2.6.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/cli-runtime v0.26.15 // indirect diff --git a/go.sum b/go.sum index 476a4ddf187..904d5da9953 100644 --- a/go.sum +++ b/go.sum @@ -870,6 +870,8 @@ gopkg.in/jcmturner/goidentity.v3 v3.0.0/go.mod h1:oG2kH0IvSYNIu80dVAyu/yoefjq1mN gopkg.in/jcmturner/gokrb5.v7 v7.3.0/go.mod h1:l8VISx+WGYp+Fp7KRbsiUuXTTOnxIc3Tuvyavf11/WM= gopkg.in/jcmturner/rpc.v1 v1.1.0/go.mod h1:YIdkC4XfD6GXbzje11McwsDuOlZQSb9W4vfLvuNnlv8= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/square/go-jose.v2 v2.4.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI= gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index fcdb0828024..2f9e9f1caad 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -2,12 +2,13 @@ package armadacontext import ( "context" + "testing" + "time" + "github.com/stretchr/testify/assert" "go.uber.org/zap" "go.uber.org/zap/zapcore" "go.uber.org/zap/zaptest/observer" - "testing" - "time" "github.com/stretchr/testify/require" diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go new file mode 100644 index 00000000000..a8c659ad969 --- /dev/null +++ b/internal/common/logging/config.go @@ -0,0 +1,170 @@ +package logging + +import ( + "fmt" + "os" + "strings" + + "github.com/pkg/errors" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" + + "sigs.k8s.io/yaml" +) + +type Config struct { + Console struct { + Level string `yaml:"level"` + Format string `yaml:"format"` + } `yaml:"console"` + File struct { + Enabled bool `yaml:"enabled"` + Level string `yaml:"level"` + Format string `yaml:"format"` + LogFile string `yaml:"logfile"` + Rotation struct { + MaxSizeMb int `yaml:"maxSizeMb"` + MaxBackups int `yaml:"maxBackups"` + MaxAgeDays int `yaml:"maxAgeDays"` + Compress bool `yaml:"compress"` + } `yaml:"rotation"` + } `yaml:"file"` +} + +func MustConfigureApplicationLogging() { + err := ConfigureApplicationLogging() + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, "Error initialising logging"+err.Error()) + os.Exit(1) + } +} + +func ConfigureApplicationLogging() error { + configPath := getEnv("ARMADA_LOG_CONFIG", "config/logging.yaml") + + logConfig, err := readConfig(configPath) + if err != nil { + return err + } + + encoderConfig := zapcore.EncoderConfig{ + TimeKey: "ts", + LevelKey: "level", + NameKey: "logger", + CallerKey: "caller", + FunctionKey: zapcore.OmitKey, + MessageKey: "msg", + StacktraceKey: "stacktrace", + LineEnding: zapcore.DefaultLineEnding, + EncodeLevel: zapcore.CapitalLevelEncoder, + EncodeTime: zapcore.ISO8601TimeEncoder, + EncodeDuration: zapcore.SecondsDurationEncoder, + EncodeCaller: shortCallerEncoder, + ConsoleSeparator: " ", + } + + var cores []zapcore.Core + + // Console logging + consoleEncoder, err := createEncoder(logConfig.Console.Format, encoderConfig) + if err != nil { + return errors.Wrap(err, "error creating console logger") + } + consoleLevel, err := parseLogLevel(logConfig.Console.Level) + if err != nil { + return errors.Wrap(err, "error creating console logger") + } + cores = append(cores, zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), consoleLevel)) + + // File logging + if logConfig.File.Enabled { + fileEncoder, err := createEncoder(logConfig.File.Format, encoderConfig) + if err != nil { + return errors.Wrap(err, "error creating file logger") + } + w := zapcore.AddSync(&lumberjack.Logger{ + Filename: "app.log", + MaxSize: logConfig.File.Rotation.MaxSizeMb, + MaxBackups: logConfig.File.Rotation.MaxBackups, + MaxAge: logConfig.File.Rotation.MaxAgeDays, // days + Compress: logConfig.File.Rotation.Compress, + }) + fileLevel, err := parseLogLevel(logConfig.Console.Level) + if err != nil { + return errors.Wrap(err, "error creating file logger") + } + cores = append(cores, zapcore.NewCore(fileEncoder, w, fileLevel)) + } + + core := zapcore.NewTee(cores...) + + // Create logger + l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) + ReplaceStdLogger(FromZap(l)) + + return nil +} + +func readConfig(configFilePath string) (Config, error) { + yamlConfig, err := os.ReadFile(configFilePath) + if err != nil { + return Config{}, errors.Wrap(err, "failed to read log config file") + } + + var config Config + err = yaml.Unmarshal(yamlConfig, &config) + if err != nil { + return Config{}, errors.Wrap(err, "failed to unmarshall log config file") + } + return config, nil +} + +func createEncoder(format string, encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) { + switch strings.ToLower(format) { + case "json": + return zapcore.NewJSONEncoder(encoderConfig), nil + case "text": + return zapcore.NewConsoleEncoder(encoderConfig), nil + default: + return nil, errors.Errorf("unknown format: %s", format) + } +} + +func parseLogLevel(level string) (zapcore.Level, error) { + switch strings.ToLower(level) { + case "debug": + return zapcore.DebugLevel, nil + case "info": + return zapcore.InfoLevel, nil + case "warn", "warning": + return zapcore.WarnLevel, nil + case "error": + return zapcore.ErrorLevel, nil + case "panic": + return zapcore.PanicLevel, nil + case "fatal": + return zapcore.FatalLevel, nil + default: + return zapcore.InfoLevel, errors.Errorf("unknown level: %s", level) + } +} + +// ShortCallerEncoder serializes a caller in to just file:line format. +func shortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { + trimmed := caller.TrimmedPath() + lastSlash := strings.LastIndexByte(trimmed, '/') + if lastSlash != -1 && lastSlash != len(trimmed)-1 { + fileName := trimmed[lastSlash+1:] + enc.AppendString(fileName) + } else { + enc.AppendString(trimmed) + } +} + +func getEnv(key, defaultValue string) string { + if value, exists := os.LookupEnv(key); exists { + return value + } + return defaultValue +} diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go index b5cb747c1f1..abfdb86ff79 100644 --- a/internal/common/logging/global.go +++ b/internal/common/logging/global.go @@ -9,7 +9,9 @@ import ( // The global Logger. Comes configured with some sensible defaults for e.g. unit tests, but applications should // generally configure their own logging config via ReplaceStdLogger -var stdLogger = &Logger{underlying: createDefaultLogger()} +var ( + stdLogger = &Logger{underlying: createDefaultLogger()} +) // ReplaceStdLogger Replaces the global logger. This should be called once at app startup! func ReplaceStdLogger(l *Logger) { diff --git a/internal/common/startup.go b/internal/common/startup.go index c9fcc490fb8..6a6207ad566 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -117,60 +117,6 @@ func ConfigureCommandLineLogging() { log.ReplaceStdLogger(log.FromZap(l)) } -func ConfigureLogging() { - pe := zap.NewProductionEncoderConfig() - pe.EncodeTime = zapcore.ISO8601TimeEncoder - pe.ConsoleSeparator = " " - pe.EncodeLevel = zapcore.CapitalLevelEncoder - pe.EncodeCaller = ShortCallerEncoder - encoder := readEnvironmentLogFormat(pe) - - core := zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), readEnvironmentLogLevel()) - l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) - log.ReplaceStdLogger(log.FromZap(l)) -} - -func readEnvironmentLogFormat(pe zapcore.EncoderConfig) zapcore.Encoder { - formatStr, ok := os.LookupEnv("LOG_FORMAT") - if !ok { - formatStr = "text" - } - - switch strings.ToLower(formatStr) { - case "json": - return zapcore.NewJSONEncoder(pe) - case "text": - return zapcore.NewConsoleEncoder(pe) - default: - println(os.Stderr, fmt.Sprintf("Unknown log format %s, defaulting to text format", formatStr)) - return zapcore.NewConsoleEncoder(pe) - } -} - -func readEnvironmentLogLevel() zapcore.Level { - level, ok := os.LookupEnv("LOG_LEVEL") - if ok { - // Parse the log level - switch level { - case "debug": - return zapcore.DebugLevel - case "info": - return zapcore.InfoLevel - case "warn", "warning": - return zapcore.WarnLevel - case "error": - return zapcore.ErrorLevel - case "panic": - return zapcore.PanicLevel - case "fatal": - return zapcore.FatalLevel - default: - println(fmt.Sprintf("Unknown log level %s", level)) - } - } - return zapcore.InfoLevel -} - func ServeMetrics(port uint16) (shutdown func()) { return ServeMetricsFor(port, prometheus.DefaultGatherer) } @@ -235,15 +181,3 @@ func serveHttp(port uint16, mux http.Handler, useTls bool, certFile, keyFile str } } } - -// ShortCallerEncoder serializes a caller in to just file:line format. -func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - trimmed := caller.TrimmedPath() - lastSlash := strings.LastIndexByte(trimmed, '/') - if lastSlash != -1 && lastSlash != len(trimmed)-1 { - fileName := trimmed[lastSlash+1:] - enc.AppendString(fileName) - } else { - enc.AppendString(trimmed) - } -} From 9f10d8e42349d181f5cfe42ec0913cd6c544b279 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Mon, 6 Jan 2025 07:31:10 +0000 Subject: [PATCH 29/58] wip --- internal/common/logging/application.go | 161 ++++++++++++++++++++++ internal/common/logging/config.go | 182 ++++--------------------- 2 files changed, 184 insertions(+), 159 deletions(-) create mode 100644 internal/common/logging/application.go diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go new file mode 100644 index 00000000000..30bbe267829 --- /dev/null +++ b/internal/common/logging/application.go @@ -0,0 +1,161 @@ +package logging + +import ( + "fmt" + "os" + + "strings" + + "github.com/pkg/errors" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "gopkg.in/natefinch/lumberjack.v2" + "sigs.k8s.io/yaml" +) + +var ( + defaultLogConfigPath = "config/logging.yaml" + logConfigPathEnvVar = "ARMADA_LOG_CONFIG" +) + +// MustConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from +// a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. +// Note that this function will immediately shut do the application if it fails. +func MustConfigureApplicationLogging() { + err := ConfigureApplicationLogging() + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, "Error initialising logging"+err.Error()) + os.Exit(1) + } +} + +// ConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from +// a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. +func ConfigureApplicationLogging() error { + configPath := getEnv(logConfigPathEnvVar, defaultLogConfigPath) + + logConfig, err := readConfig(configPath) + if err != nil { + return err + } + + encoderConfig := zapcore.EncoderConfig{ + TimeKey: "ts", + LevelKey: "level", + NameKey: "logger", + CallerKey: "caller", + FunctionKey: zapcore.OmitKey, + MessageKey: "msg", + StacktraceKey: "stacktrace", + LineEnding: zapcore.DefaultLineEnding, + EncodeLevel: zapcore.CapitalLevelEncoder, + EncodeTime: zapcore.ISO8601TimeEncoder, + EncodeDuration: zapcore.SecondsDurationEncoder, + EncodeCaller: shortCallerEncoder, + ConsoleSeparator: " ", + } + + var cores []zapcore.Core + + // Console logging + consoleEncoder, err := createEncoder(logConfig.Console.Format, encoderConfig) + if err != nil { + return errors.Wrap(err, "error creating console logger") + } + consoleLevel, err := parseLogLevel(logConfig.Console.Level) + if err != nil { + return errors.Wrap(err, "error creating console logger") + } + cores = append(cores, zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), consoleLevel)) + + // File logging + if logConfig.File.Enabled { + fileEncoder, err := createEncoder(logConfig.File.Format, encoderConfig) + if err != nil { + return errors.Wrap(err, "error creating file logger") + } + w := zapcore.AddSync(&lumberjack.Logger{ + Filename: "app.log", + MaxSize: logConfig.File.Rotation.MaxSizeMb, + MaxBackups: logConfig.File.Rotation.MaxBackups, + MaxAge: logConfig.File.Rotation.MaxAgeDays, // days + Compress: logConfig.File.Rotation.Compress, + }) + fileLevel, err := parseLogLevel(logConfig.Console.Level) + if err != nil { + return errors.Wrap(err, "error creating file logger") + } + cores = append(cores, zapcore.NewCore(fileEncoder, w, fileLevel)) + } + + core := zapcore.NewTee(cores...) + + // Create logger + l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) + ReplaceStdLogger(FromZap(l)) + + return nil +} + +func readConfig(configFilePath string) (Config, error) { + yamlConfig, err := os.ReadFile(configFilePath) + if err != nil { + return Config{}, errors.Wrap(err, "failed to read log config file") + } + + var config Config + err = yaml.Unmarshal(yamlConfig, &config) + if err != nil { + return Config{}, errors.Wrap(err, "failed to unmarshall log config file") + } + return config, nil +} + +func createEncoder(format string, encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) { + switch strings.ToLower(format) { + case "json": + return zapcore.NewJSONEncoder(encoderConfig), nil + case "text": + return zapcore.NewConsoleEncoder(encoderConfig), nil + default: + return nil, errors.Errorf("unknown format: %s", format) + } +} + +func parseLogLevel(level string) (zapcore.Level, error) { + switch strings.ToLower(level) { + case "debug": + return zapcore.DebugLevel, nil + case "info": + return zapcore.InfoLevel, nil + case "warn", "warning": + return zapcore.WarnLevel, nil + case "error": + return zapcore.ErrorLevel, nil + case "panic": + return zapcore.PanicLevel, nil + case "fatal": + return zapcore.FatalLevel, nil + default: + return zapcore.InfoLevel, errors.Errorf("unknown level: %s", level) + } +} + +// ShortCallerEncoder serializes a caller in to just file:line format. +func shortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { + trimmed := caller.TrimmedPath() + lastSlash := strings.LastIndexByte(trimmed, '/') + if lastSlash != -1 && lastSlash != len(trimmed)-1 { + fileName := trimmed[lastSlash+1:] + enc.AppendString(fileName) + } else { + enc.AppendString(trimmed) + } +} + +func getEnv(key, defaultValue string) string { + if value, exists := os.LookupEnv(key); exists { + return value + } + return defaultValue +} diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go index a8c659ad969..cf156f43fe8 100644 --- a/internal/common/logging/config.go +++ b/internal/common/logging/config.go @@ -1,170 +1,34 @@ package logging -import ( - "fmt" - "os" - "strings" - - "github.com/pkg/errors" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "gopkg.in/natefinch/lumberjack.v2" - - "sigs.k8s.io/yaml" -) - +// Config defines Armada logging configuration. type Config struct { + // Defines configuration for console logging on stdout Console struct { - Level string `yaml:"level"` + // Log level, e.g. INFO, ERROR etc + Level string `yaml:"level"` + // Logging format, either text or json Format string `yaml:"format"` } `yaml:"console"` + // Defines configuration for file logging File struct { - Enabled bool `yaml:"enabled"` - Level string `yaml:"level"` - Format string `yaml:"format"` - LogFile string `yaml:"logfile"` + // Whether file logging is enabled. + Enabled bool `yaml:"enabled"` + // Log level, e.g. INFO, ERROR etc + Level string `yaml:"level"` + // Logging format, either text or json + Format string `yaml:"format"` + // The Location of the logfile on disk + LogFile string `yaml:"logfile"` + // Log Rotation Options Rotation struct { - MaxSizeMb int `yaml:"maxSizeMb"` - MaxBackups int `yaml:"maxBackups"` - MaxAgeDays int `yaml:"maxAgeDays"` - Compress bool `yaml:"compress"` + // Maximum size in megabytes of the log file before it gets rotated + MaxSizeMb int `yaml:"maxSizeMb"` + // Maximum number of old log files to retain + MaxBackups int `yaml:"maxBackups"` + // Maximum number of days to retain old log files + MaxAgeDays int `yaml:"maxAgeDays"` + // Whether to compress rotated log files + Compress bool `yaml:"compress"` } `yaml:"rotation"` } `yaml:"file"` } - -func MustConfigureApplicationLogging() { - err := ConfigureApplicationLogging() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Error initialising logging"+err.Error()) - os.Exit(1) - } -} - -func ConfigureApplicationLogging() error { - configPath := getEnv("ARMADA_LOG_CONFIG", "config/logging.yaml") - - logConfig, err := readConfig(configPath) - if err != nil { - return err - } - - encoderConfig := zapcore.EncoderConfig{ - TimeKey: "ts", - LevelKey: "level", - NameKey: "logger", - CallerKey: "caller", - FunctionKey: zapcore.OmitKey, - MessageKey: "msg", - StacktraceKey: "stacktrace", - LineEnding: zapcore.DefaultLineEnding, - EncodeLevel: zapcore.CapitalLevelEncoder, - EncodeTime: zapcore.ISO8601TimeEncoder, - EncodeDuration: zapcore.SecondsDurationEncoder, - EncodeCaller: shortCallerEncoder, - ConsoleSeparator: " ", - } - - var cores []zapcore.Core - - // Console logging - consoleEncoder, err := createEncoder(logConfig.Console.Format, encoderConfig) - if err != nil { - return errors.Wrap(err, "error creating console logger") - } - consoleLevel, err := parseLogLevel(logConfig.Console.Level) - if err != nil { - return errors.Wrap(err, "error creating console logger") - } - cores = append(cores, zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), consoleLevel)) - - // File logging - if logConfig.File.Enabled { - fileEncoder, err := createEncoder(logConfig.File.Format, encoderConfig) - if err != nil { - return errors.Wrap(err, "error creating file logger") - } - w := zapcore.AddSync(&lumberjack.Logger{ - Filename: "app.log", - MaxSize: logConfig.File.Rotation.MaxSizeMb, - MaxBackups: logConfig.File.Rotation.MaxBackups, - MaxAge: logConfig.File.Rotation.MaxAgeDays, // days - Compress: logConfig.File.Rotation.Compress, - }) - fileLevel, err := parseLogLevel(logConfig.Console.Level) - if err != nil { - return errors.Wrap(err, "error creating file logger") - } - cores = append(cores, zapcore.NewCore(fileEncoder, w, fileLevel)) - } - - core := zapcore.NewTee(cores...) - - // Create logger - l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) - ReplaceStdLogger(FromZap(l)) - - return nil -} - -func readConfig(configFilePath string) (Config, error) { - yamlConfig, err := os.ReadFile(configFilePath) - if err != nil { - return Config{}, errors.Wrap(err, "failed to read log config file") - } - - var config Config - err = yaml.Unmarshal(yamlConfig, &config) - if err != nil { - return Config{}, errors.Wrap(err, "failed to unmarshall log config file") - } - return config, nil -} - -func createEncoder(format string, encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) { - switch strings.ToLower(format) { - case "json": - return zapcore.NewJSONEncoder(encoderConfig), nil - case "text": - return zapcore.NewConsoleEncoder(encoderConfig), nil - default: - return nil, errors.Errorf("unknown format: %s", format) - } -} - -func parseLogLevel(level string) (zapcore.Level, error) { - switch strings.ToLower(level) { - case "debug": - return zapcore.DebugLevel, nil - case "info": - return zapcore.InfoLevel, nil - case "warn", "warning": - return zapcore.WarnLevel, nil - case "error": - return zapcore.ErrorLevel, nil - case "panic": - return zapcore.PanicLevel, nil - case "fatal": - return zapcore.FatalLevel, nil - default: - return zapcore.InfoLevel, errors.Errorf("unknown level: %s", level) - } -} - -// ShortCallerEncoder serializes a caller in to just file:line format. -func shortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - trimmed := caller.TrimmedPath() - lastSlash := strings.LastIndexByte(trimmed, '/') - if lastSlash != -1 && lastSlash != len(trimmed)-1 { - fileName := trimmed[lastSlash+1:] - enc.AppendString(fileName) - } else { - enc.AppendString(trimmed) - } -} - -func getEnv(key, defaultValue string) string { - if value, exists := os.LookupEnv(key); exists { - return value - } - return defaultValue -} From 1fc7ead529603b0239dec9030b6d804b00b28d4a Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Mon, 6 Jan 2025 07:50:20 +0000 Subject: [PATCH 30/58] wip --- internal/common/logging/application.go | 23 ++------ internal/common/logging/config.go | 82 ++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 19 deletions(-) diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 30bbe267829..f3cfe390722 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -108,6 +108,10 @@ func readConfig(configFilePath string) (Config, error) { if err != nil { return Config{}, errors.Wrap(err, "failed to unmarshall log config file") } + err = validate(config) + if err != nil { + return Config{}, errors.Wrap(err, "invalid log configuration") + } return config, nil } @@ -122,25 +126,6 @@ func createEncoder(format string, encoderConfig zapcore.EncoderConfig) (zapcore. } } -func parseLogLevel(level string) (zapcore.Level, error) { - switch strings.ToLower(level) { - case "debug": - return zapcore.DebugLevel, nil - case "info": - return zapcore.InfoLevel, nil - case "warn", "warning": - return zapcore.WarnLevel, nil - case "error": - return zapcore.ErrorLevel, nil - case "panic": - return zapcore.PanicLevel, nil - case "fatal": - return zapcore.FatalLevel, nil - default: - return zapcore.InfoLevel, errors.Errorf("unknown level: %s", level) - } -} - // ShortCallerEncoder serializes a caller in to just file:line format. func shortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { trimmed := caller.TrimmedPath() diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go index cf156f43fe8..e00b2ea6740 100644 --- a/internal/common/logging/config.go +++ b/internal/common/logging/config.go @@ -1,5 +1,18 @@ package logging +import ( + "golang.org/x/exp/maps" + "strings" + + "github.com/pkg/errors" + "go.uber.org/zap/zapcore" +) + +var validLogFormats = map[string]bool{ + "text": true, + "json": true, +} + // Config defines Armada logging configuration. type Config struct { // Defines configuration for console logging on stdout @@ -21,6 +34,8 @@ type Config struct { LogFile string `yaml:"logfile"` // Log Rotation Options Rotation struct { + // Whether Log Rotation is enabled + Enabled bool `yaml:"enabled"` // Maximum size in megabytes of the log file before it gets rotated MaxSizeMb int `yaml:"maxSizeMb"` // Maximum number of old log files to retain @@ -32,3 +47,70 @@ type Config struct { } `yaml:"rotation"` } `yaml:"file"` } + +func validate(c Config) error { + _, err := parseLogLevel(c.Console.Level) + if err != nil { + return err + } + + err = validateLogFormat(c.Console.Format) + if err != nil { + return err + } + + if c.File.Enabled { + _, err := parseLogLevel(c.File.Level) + if err != nil { + return err + } + + err = validateLogFormat(c.File.Format) + if err != nil { + return err + } + + rotation := c.File.Rotation + if rotation.Enabled { + if rotation.MaxSizeMb <= 0 { + return errors.New("rotation.maxSizeMb must be greater than zero") + } + if rotation.MaxBackups <= 0 { + return errors.New("rotation.maxBackups must be greater than zero") + } + if rotation.MaxAgeDays <= 0 { + return errors.New("rotation.maxAgeDays must be greater than zero") + } + } + } + + return nil +} + +func validateLogFormat(f string) error { + _, ok := validLogFormats[f] + if !ok { + err := errors.Errorf("unknown log format: %s. Valid formats are %s", f, maps.Keys(validLogFormats)) + return err + } + return nil +} + +func parseLogLevel(level string) (zapcore.Level, error) { + switch strings.ToLower(level) { + case "debug": + return zapcore.DebugLevel, nil + case "info": + return zapcore.InfoLevel, nil + case "warn", "warning": + return zapcore.WarnLevel, nil + case "error": + return zapcore.ErrorLevel, nil + case "panic": + return zapcore.PanicLevel, nil + case "fatal": + return zapcore.FatalLevel, nil + default: + return zapcore.InfoLevel, errors.Errorf("unknown level: %s", level) + } +} From 04ebd1a844327b6b8d2b6122a215cbcb03cd9bb7 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Mon, 6 Jan 2025 07:51:07 +0000 Subject: [PATCH 31/58] lint --- internal/common/logging/application.go | 1 - internal/common/logging/config.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index f3cfe390722..cbc969a224a 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -3,7 +3,6 @@ package logging import ( "fmt" "os" - "strings" "github.com/pkg/errors" diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go index e00b2ea6740..eb69b51a7a8 100644 --- a/internal/common/logging/config.go +++ b/internal/common/logging/config.go @@ -1,11 +1,11 @@ package logging import ( - "golang.org/x/exp/maps" "strings" "github.com/pkg/errors" "go.uber.org/zap/zapcore" + "golang.org/x/exp/maps" ) var validLogFormats = map[string]bool{ From 951ab70b58bfce5c8a8ac96e798af469d19bd499 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 7 Jan 2025 18:13:47 +0000 Subject: [PATCH 32/58] fix skip frames --- go.mod | 4 +- go.sum | 12 ++ .../common/armadacontext/armada_context.go | 8 +- internal/common/logging/application.go | 166 +++++++++------- internal/common/logging/global.go | 37 ++-- internal/common/logging/logger.go | 77 ++++---- internal/common/logging/logger_test.go | 181 +++++++++--------- internal/common/logging/null_logger.go | 5 +- internal/common/logging/pulsar_adapter.go | 36 ++-- internal/common/startup.go | 50 +++-- 10 files changed, 317 insertions(+), 259 deletions(-) diff --git a/go.mod b/go.mod index 665ec23c7c0..e7014313526 100644 --- a/go.mod +++ b/go.mod @@ -160,7 +160,8 @@ require ( github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mailru/easyjson v0.7.7 // indirect - github.com/mattn/go-isatty v0.0.18 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/microcosm-cc/bluemonday v1.0.25 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect @@ -180,6 +181,7 @@ require ( github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/rivo/uniseg v0.4.2 // indirect + github.com/rs/zerolog v1.33.0 // indirect github.com/sagikazarmark/locafero v0.6.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect diff --git a/go.sum b/go.sum index 904d5da9953..bc215fa4e43 100644 --- a/go.sum +++ b/go.sum @@ -114,6 +114,7 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= github.com/coreos/go-oidc v2.2.1+incompatible h1:mh48q/BqXqgjVHpy2ZY7WnWAbenxRjsz9N1i1YxjHAk= github.com/coreos/go-oidc v2.2.1+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E= github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -209,6 +210,7 @@ github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a h1:dR8+Q0uO5S2ZBcs2IH6VBKYwSxPo2vYCYq0ot0mu7xA= github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= @@ -393,8 +395,13 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= @@ -503,6 +510,9 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk= github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0= @@ -727,7 +737,9 @@ golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/internal/common/armadacontext/armada_context.go b/internal/common/armadacontext/armada_context.go index f77a413c1d0..b96f7cf197f 100644 --- a/internal/common/armadacontext/armada_context.go +++ b/internal/common/armadacontext/armada_context.go @@ -75,7 +75,7 @@ func (ctx *Context) Fatalf(format string, args ...interface{}) { func Background() *Context { return &Context{ Context: context.Background(), - logger: logging.StdLogger().WithCallerSkip(1), + logger: logging.StdLogger(), } } @@ -83,7 +83,7 @@ func Background() *Context { func TODO() *Context { return &Context{ Context: context.TODO(), - logger: logging.StdLogger().WithCallerSkip(1), + logger: logging.StdLogger(), } } @@ -93,7 +93,7 @@ func FromGrpcCtx(ctx context.Context) *Context { if ok { return armadaCtx } - logger := logging.StdLogger().WithCallerSkip(1). + logger := logging.StdLogger(). WithField("user", ctx.Value("user")). WithField("requestId", ctx.Value("requestId")) return New(ctx, logger) @@ -108,7 +108,7 @@ func New(ctx context.Context, log *logging.Logger) *Context { } func (ctx *Context) Logger() *logging.Logger { - return ctx.logger.WithCallerSkip(-1) + return ctx.logger.WithCallerSkip(logging.StdSkipFrames - 1) } // WithCancel returns a copy of parent with a new Done channel. It is analogous to context.WithCancel() diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index cbc969a224a..ad469696549 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -2,98 +2,116 @@ package logging import ( "fmt" + "io" "os" - "strings" + "time" "github.com/pkg/errors" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + "github.com/rs/zerolog" "gopkg.in/natefinch/lumberjack.v2" + + "path/filepath" "sigs.k8s.io/yaml" + "strconv" + "strings" ) var ( defaultLogConfigPath = "config/logging.yaml" logConfigPathEnvVar = "ARMADA_LOG_CONFIG" + logTimestampFormat = "2006-01-02T15:04:05.999Z07:00" ) -// MustConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from +// MustConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from // a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. -// Note that this function will immediately shut do the application if it fails. +// Note that this function will immediately shut down the application if it fails. func MustConfigureApplicationLogging() { err := ConfigureApplicationLogging() if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Error initialising logging"+err.Error()) + _, _ = fmt.Fprintln(os.Stderr, "Error initializing logging: "+err.Error()) os.Exit(1) } } -// ConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from +// ConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from // a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. func ConfigureApplicationLogging() error { - configPath := getEnv(logConfigPathEnvVar, defaultLogConfigPath) + // Set some global logging properties + zerolog.TimeFieldFormat = time.RFC3339Nano + zerolog.CallerMarshalFunc = shortCallerEncoder + + // Load config file + configPath := getEnv(logConfigPathEnvVar, defaultLogConfigPath) logConfig, err := readConfig(configPath) if err != nil { return err } - encoderConfig := zapcore.EncoderConfig{ - TimeKey: "ts", - LevelKey: "level", - NameKey: "logger", - CallerKey: "caller", - FunctionKey: zapcore.OmitKey, - MessageKey: "msg", - StacktraceKey: "stacktrace", - LineEnding: zapcore.DefaultLineEnding, - EncodeLevel: zapcore.CapitalLevelEncoder, - EncodeTime: zapcore.ISO8601TimeEncoder, - EncodeDuration: zapcore.SecondsDurationEncoder, - EncodeCaller: shortCallerEncoder, - ConsoleSeparator: " ", - } - - var cores []zapcore.Core - // Console logging - consoleEncoder, err := createEncoder(logConfig.Console.Format, encoderConfig) - if err != nil { - return errors.Wrap(err, "error creating console logger") - } - consoleLevel, err := parseLogLevel(logConfig.Console.Level) + var writers []io.Writer + consoleLogger, err := createConsoleLogger(logConfig) if err != nil { - return errors.Wrap(err, "error creating console logger") + return err } - cores = append(cores, zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), consoleLevel)) + writers = append(writers, consoleLogger) // File logging if logConfig.File.Enabled { - fileEncoder, err := createEncoder(logConfig.File.Format, encoderConfig) + fileLogger, err := createFileLogger(logConfig) if err != nil { - return errors.Wrap(err, "error creating file logger") + return err } - w := zapcore.AddSync(&lumberjack.Logger{ + writers = append(writers, fileLogger) + } + + // Combine loggers + multiWriter := zerolog.MultiLevelWriter(writers...) + logger := zerolog.New(multiWriter).With().Timestamp().Logger() + + // Set our new logger to be the default + ReplaceStdLogger(FromZerolog(logger)) + return nil +} + +func createFileLogger(logConfig Config) (*FilteredLevelWriter, error) { + level, err := zerolog.ParseLevel(logConfig.Console.Level) + if err != nil { + return nil, err + } + + return &FilteredLevelWriter{ + level: level, + writer: &lumberjack.Logger{ Filename: "app.log", MaxSize: logConfig.File.Rotation.MaxSizeMb, MaxBackups: logConfig.File.Rotation.MaxBackups, - MaxAge: logConfig.File.Rotation.MaxAgeDays, // days + MaxAge: logConfig.File.Rotation.MaxAgeDays, Compress: logConfig.File.Rotation.Compress, - }) - fileLevel, err := parseLogLevel(logConfig.Console.Level) - if err != nil { - return errors.Wrap(err, "error creating file logger") - } - cores = append(cores, zapcore.NewCore(fileEncoder, w, fileLevel)) - } - - core := zapcore.NewTee(cores...) + }, + }, nil +} - // Create logger - l := zap.New(core, zap.AddCaller()).WithOptions(zap.AddCallerSkip(2)) - ReplaceStdLogger(FromZap(l)) +func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { + level, err := zerolog.ParseLevel(logConfig.Console.Level) + if err != nil { + return nil, err + } - return nil + return &FilteredLevelWriter{ + level: level, + writer: zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: logTimestampFormat, + FormatLevel: func(i interface{}) string { + return strings.ToUpper(fmt.Sprintf("%s", i)) + }, + FormatCaller: func(i interface{}) string { + return filepath.Base(fmt.Sprintf("%s", i)) + }, + NoColor: true, + }, + }, nil } func readConfig(configFilePath string) (Config, error) { @@ -114,27 +132,16 @@ func readConfig(configFilePath string) (Config, error) { return config, nil } -func createEncoder(format string, encoderConfig zapcore.EncoderConfig) (zapcore.Encoder, error) { - switch strings.ToLower(format) { - case "json": - return zapcore.NewJSONEncoder(encoderConfig), nil - case "text": - return zapcore.NewConsoleEncoder(encoderConfig), nil - default: - return nil, errors.Errorf("unknown format: %s", format) - } -} - -// ShortCallerEncoder serializes a caller in to just file:line format. -func shortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { - trimmed := caller.TrimmedPath() - lastSlash := strings.LastIndexByte(trimmed, '/') - if lastSlash != -1 && lastSlash != len(trimmed)-1 { - fileName := trimmed[lastSlash+1:] - enc.AppendString(fileName) - } else { - enc.AppendString(trimmed) +func shortCallerEncoder(_ uintptr, file string, line int) string { + short := file + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + short = file[i+1:] + break + } } + file = short + return file + ":" + strconv.Itoa(line) } func getEnv(key, defaultValue string) string { @@ -143,3 +150,22 @@ func getEnv(key, defaultValue string) string { } return defaultValue } + +type FilteredLevelWriter struct { + writer io.Writer + level zerolog.Level +} + +// Write writes to the underlying Writer. +func (w *FilteredLevelWriter) Write(p []byte) (int, error) { + return w.writer.Write(p) +} + +// WriteLevel calls WriteLevel of the underlying Writer only if the level is equal +// or above the Level. +func (w *FilteredLevelWriter) WriteLevel(level zerolog.Level, p []byte) (int, error) { + if level >= w.level { + return w.writer.Write(p) + } + return len(p), nil +} diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go index abfdb86ff79..281ba0ad5bf 100644 --- a/internal/common/logging/global.go +++ b/internal/common/logging/global.go @@ -1,21 +1,21 @@ package logging import ( + "github.com/rs/zerolog" "os" - - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + "time" ) // The global Logger. Comes configured with some sensible defaults for e.g. unit tests, but applications should // generally configure their own logging config via ReplaceStdLogger var ( - stdLogger = &Logger{underlying: createDefaultLogger()} + StdSkipFrames = 4 + stdLogger = createDefaultLogger() ) // ReplaceStdLogger Replaces the global logger. This should be called once at app startup! func ReplaceStdLogger(l *Logger) { - stdLogger = l + stdLogger = l.WithCallerSkip(StdSkipFrames) } // StdLogger Returns the default logger @@ -98,16 +98,19 @@ func WithStacktrace(err error) *Logger { return stdLogger.WithStacktrace(err) } -// Default logging options -func createDefaultLogger() *zap.SugaredLogger { - pe := zap.NewProductionEncoderConfig() - pe.EncodeTime = zapcore.ISO8601TimeEncoder - pe.ConsoleSeparator = " " - pe.EncodeLevel = zapcore.CapitalLevelEncoder - consoleEncoder := zapcore.NewConsoleEncoder(pe) - core := zapcore.NewCore(consoleEncoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel) - return zap. - New(core, zap.AddCaller()). - WithOptions(zap.AddCallerSkip(2)). - Sugar() +// createDefaultLogger returns a new Logger configured with default settings using zerolog. +func createDefaultLogger() *Logger { + consoleWriter := zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: time.RFC3339, + NoColor: true, + } + zerologLogger := zerolog.New(consoleWriter). + Level(zerolog.InfoLevel). + With(). + CallerWithSkipFrameCount(StdSkipFrames). + Timestamp(). + Logger() + + return FromZerolog(zerologLogger) } diff --git a/internal/common/logging/logger.go b/internal/common/logging/logger.go index 2eb64137e28..799c44aa47b 100644 --- a/internal/common/logging/logger.go +++ b/internal/common/logging/logger.go @@ -1,107 +1,117 @@ package logging import ( + "fmt" "github.com/pkg/errors" - "go.uber.org/zap" + "github.com/rs/zerolog" ) -// Logger wraps a *zap.SugaredLogger so that the rest of the code doesn't depend directly on Zap +// Logger wraps a zerolog.Logger so that the rest of the code doesn't depend directly on zerolog type Logger struct { - underlying *zap.SugaredLogger + underlying zerolog.Logger } -// FromZap returns a New Logger backed by the supplied *zap.SugaredLogger -func FromZap(l *zap.Logger) *Logger { +// FromZerolog returns a new Logger backed by the supplied zerolog.Logger +func FromZerolog(l zerolog.Logger) *Logger { return &Logger{ - underlying: l.Sugar(), + underlying: l, } } // Debug logs a message at level Debug func (l *Logger) Debug(args ...any) { - l.underlying.Debug(args...) + l.underlying.Debug().Msg(fmt.Sprint(args...)) } // Info logs a message at level Info func (l *Logger) Info(args ...any) { - l.underlying.Info(args...) + l.underlying.Info().Msg(fmt.Sprint(args...)) } // Warn logs a message at level Warn func (l *Logger) Warn(args ...any) { - l.underlying.Warn(args...) + l.underlying.Warn().Msg(fmt.Sprint(args...)) } // Error logs a message at level Error func (l *Logger) Error(args ...any) { - l.underlying.Error(args...) + l.underlying.Error().Msg(fmt.Sprint(args...)) } -// Panic logs a message at level Panic +// Panic logs a message at level Panic and panics func (l *Logger) Panic(args ...any) { - l.underlying.Panic(args...) + l.underlying.Panic().Msg(fmt.Sprint(args...)) } -// Fatal logs a message at level Fatal then the process will exit with status set to 1. +// Fatal logs a message at level Fatal and exits the application func (l *Logger) Fatal(args ...any) { - l.underlying.Fatal(args...) + l.underlying.Fatal().Msg(fmt.Sprint(args...)) } -// Debugf logs a message at level Debug. +// Debugf logs a formatted message at level Debug func (l *Logger) Debugf(format string, args ...interface{}) { - l.underlying.Debugf(format, args...) + l.underlying.Debug().Msgf(format, args...) } -// Infof logs a message at level Info. +// Infof logs a formatted message at level Info func (l *Logger) Infof(format string, args ...interface{}) { - l.underlying.Infof(format, args...) + l.underlying.Info().Msgf(format, args...) } -// Warnf logs a message at level Warn. +// Warnf logs a formatted message at level Warn func (l *Logger) Warnf(format string, args ...interface{}) { - l.underlying.Warnf(format, args...) + l.underlying.Warn().Msgf(format, args...) } -// Errorf logs a message at level Error. +// Errorf logs a formatted message at level Error func (l *Logger) Errorf(format string, args ...interface{}) { - l.underlying.Errorf(format, args...) + l.underlying.Error().Msgf(format, args...) } -// Fatalf logs a message at level Fatal. +// Fatalf logs a formatted message at level Fatal and exits the application func (l *Logger) Fatalf(format string, args ...interface{}) { - l.underlying.Fatalf(format, args...) + l.underlying.Fatal().Msgf(format, args...) } // WithError returns a new Logger with the error added as a field func (l *Logger) WithError(err error) *Logger { - return l.WithField("error", err) + return &Logger{ + underlying: l.underlying. + With(). + AnErr("error", err). + CallerWithSkipFrameCount(StdSkipFrames - 1). + Logger(), + } } // WithStacktrace returns a new Logger with the error and (if available) the stacktrace added as fields func (l *Logger) WithStacktrace(err error) *Logger { logger := l.WithError(err) if stackErr, ok := err.(stackTracer); ok { - return logger.WithField("stacktrace", stackErr.StackTrace()) - } else { - return logger + return logger.WithField("stacktrace", fmt.Sprintf("%+v", stackErr.StackTrace())) } + return logger } // WithField returns a new Logger with the key-value pair added as a new field func (l *Logger) WithField(key string, value any) *Logger { return &Logger{ - underlying: l.underlying.With(key, value), + underlying: l.underlying. + With(). + Interface(key, value). + CallerWithSkipFrameCount(StdSkipFrames - 1). + Logger(), } } // WithFields returns a new Logger with all key-value pairs in the map added as new fields func (l *Logger) WithFields(args map[string]any) *Logger { - fields := make([]any, 0, len(args)) + event := l.underlying.With() for key, value := range args { - fields = append(fields, zap.Any(key, value)) + event = event.Interface(key, value) } return &Logger{ - underlying: l.underlying.With(fields...), + underlying: event.CallerWithSkipFrameCount(StdSkipFrames - 1).Logger(), } } @@ -109,8 +119,9 @@ func (l *Logger) WithFields(args map[string]any) *Logger { // This is needed when building wrappers around the Logger so as to prevent us from always reporting the // wrapper code as the caller. func (l *Logger) WithCallerSkip(skip int) *Logger { + return &Logger{ - underlying: l.underlying.WithOptions(zap.AddCallerSkip(skip)), + underlying: l.underlying.With().CallerWithSkipFrameCount(skip).Logger(), } } diff --git a/internal/common/logging/logger_test.go b/internal/common/logging/logger_test.go index 349aaf623b0..2d858d395b9 100644 --- a/internal/common/logging/logger_test.go +++ b/internal/common/logging/logger_test.go @@ -1,92 +1,93 @@ package logging -import ( - "testing" - - "github.com/pkg/errors" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "go.uber.org/zap/zaptest/observer" -) - -func TestWithField(t *testing.T) { - logger, observedLogs := testLogger() - - newLogger := logger.WithField("foo", "bar") - newLogger.Info("test message") - - // Check the captured log entries - entries := observedLogs.All() - require.Len(t, entries, 1, "Expected exactly one log entry") - - // Verify the fields - logEntry := entries[0] - assert.Equal(t, "test message", logEntry.Message) - assert.Equal(t, "bar", logEntry.ContextMap()["foo"]) -} - -func TestWithFields(t *testing.T) { - logger, observedLogs := testLogger() - - newLogger := logger.WithFields(map[string]any{ - "user": "test_user", - "action": "test_action", - }) - newLogger.Info("test message") - - // Check the captured log entries - entries := observedLogs.All() - require.Len(t, entries, 1, "Expected exactly one log entry") - - // Verify the fields - logEntry := entries[0] - assert.Equal(t, "test message", logEntry.Message) - assert.Equal(t, "test_user", logEntry.ContextMap()["user"]) - assert.Equal(t, "test_action", logEntry.ContextMap()["action"]) -} - -func TestWithError(t *testing.T) { - logger, observedLogs := testLogger() - - err := errors.New("test error") - - newLogger := logger.WithError(err) - newLogger.Info("test message") - - // Check the captured log entries - entries := observedLogs.All() - require.Len(t, entries, 1, "Expected exactly one log entry") - - // Verify the fields - logEntry := entries[0] - assert.Equal(t, "test message", logEntry.Message) - assert.Equal(t, "test error", logEntry.ContextMap()["error"]) -} - -func TestWithStacktrace(t *testing.T) { - logger, observedLogs := testLogger() - - err := errors.WithStack(errors.New("test error")) - - newLogger := logger.WithStacktrace(err) - newLogger.Info("test message") - - // Check the captured log entries - entries := observedLogs.All() - require.Len(t, entries, 1, "Expected exactly one log entry") - - // Verify the fields - logEntry := entries[0] - assert.Equal(t, "test message", logEntry.Message) - assert.Equal(t, "test error", logEntry.ContextMap()["error"]) - assert.Equal(t, err.(stackTracer).StackTrace(), logEntry.ContextMap()["stacktrace"]) -} - -func testLogger() (*Logger, *observer.ObservedLogs) { - core, observedLogs := observer.New(zapcore.DebugLevel) - baseLogger := zap.New(core).Sugar() - logger := &Logger{underlying: baseLogger} - return logger, observedLogs -} +// +//import ( +// "testing" +// +// "github.com/pkg/errors" +// "github.com/stretchr/testify/assert" +// "github.com/stretchr/testify/require" +// "go.uber.org/zap" +// "go.uber.org/zap/zapcore" +// "go.uber.org/zap/zaptest/observer" +//) +// +//func TestWithField(t *testing.T) { +// logger, observedLogs := testLogger() +// +// newLogger := logger.WithField("foo", "bar") +// newLogger.Info("test message") +// +// // Check the captured log entries +// entries := observedLogs.All() +// require.Len(t, entries, 1, "Expected exactly one log entry") +// +// // Verify the fields +// logEntry := entries[0] +// assert.Equal(t, "test message", logEntry.Message) +// assert.Equal(t, "bar", logEntry.ContextMap()["foo"]) +//} +// +//func TestWithFields(t *testing.T) { +// logger, observedLogs := testLogger() +// +// newLogger := logger.WithFields(map[string]any{ +// "user": "test_user", +// "action": "test_action", +// }) +// newLogger.Info("test message") +// +// // Check the captured log entries +// entries := observedLogs.All() +// require.Len(t, entries, 1, "Expected exactly one log entry") +// +// // Verify the fields +// logEntry := entries[0] +// assert.Equal(t, "test message", logEntry.Message) +// assert.Equal(t, "test_user", logEntry.ContextMap()["user"]) +// assert.Equal(t, "test_action", logEntry.ContextMap()["action"]) +//} +// +//func TestWithError(t *testing.T) { +// logger, observedLogs := testLogger() +// +// err := errors.New("test error") +// +// newLogger := logger.WithError(err) +// newLogger.Info("test message") +// +// // Check the captured log entries +// entries := observedLogs.All() +// require.Len(t, entries, 1, "Expected exactly one log entry") +// +// // Verify the fields +// logEntry := entries[0] +// assert.Equal(t, "test message", logEntry.Message) +// assert.Equal(t, "test error", logEntry.ContextMap()["error"]) +//} +// +//func TestWithStacktrace(t *testing.T) { +// logger, observedLogs := testLogger() +// +// err := errors.WithStack(errors.New("test error")) +// +// newLogger := logger.WithStacktrace(err) +// newLogger.Info("test message") +// +// // Check the captured log entries +// entries := observedLogs.All() +// require.Len(t, entries, 1, "Expected exactly one log entry") +// +// // Verify the fields +// logEntry := entries[0] +// assert.Equal(t, "test message", logEntry.Message) +// assert.Equal(t, "test error", logEntry.ContextMap()["error"]) +// assert.Equal(t, err.(stackTracer).StackTrace(), logEntry.ContextMap()["stacktrace"]) +//} +// +//func testLogger() (*Logger, *observer.ObservedLogs) { +// core, observedLogs := observer.New(zapcore.DebugLevel) +// baseLogger := zap.New(core).Sugar() +// logger := &Logger{underlying: baseLogger} +// return logger, observedLogs +//} diff --git a/internal/common/logging/null_logger.go b/internal/common/logging/null_logger.go index 7bafa1e0a39..01264831839 100644 --- a/internal/common/logging/null_logger.go +++ b/internal/common/logging/null_logger.go @@ -1,11 +1,10 @@ package logging import ( - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + "github.com/rs/zerolog" ) // NullLogger is Logger that sends a log lines into the ether var NullLogger = &Logger{ - underlying: zap.New(zapcore.NewNopCore()).Sugar(), + underlying: zerolog.Nop(), } diff --git a/internal/common/logging/pulsar_adapter.go b/internal/common/logging/pulsar_adapter.go index 3e3c3e48426..adb7066a0b2 100644 --- a/internal/common/logging/pulsar_adapter.go +++ b/internal/common/logging/pulsar_adapter.go @@ -4,6 +4,8 @@ import ( pulsarlog "github.com/apache/pulsar-client-go/pulsar/log" ) +var pulsarAdapterSkipFrames = StdSkipFrames + 1 + // Wrapper to adapt Logger to the logger interface expected by the pulsar client type pulsarWrapper struct { l *Logger @@ -11,33 +13,31 @@ type pulsarWrapper struct { // NewPulsarLogger returns a Logger that can be used by the pulsar client func NewPulsarLogger() pulsarlog.Logger { - return &pulsarWrapper{ - l: StdLogger(), - } + return newPulsarLogger(StdLogger()) } func (p pulsarWrapper) SubLogger(fs pulsarlog.Fields) pulsarlog.Logger { - return &pulsarWrapper{ - l: p.l.WithFields(fs), - } + return newPulsarLogger( + p.l.WithFields(fs), + ) } func (p pulsarWrapper) WithFields(fs pulsarlog.Fields) pulsarlog.Entry { - return &pulsarWrapper{ - l: p.l.WithFields(fs), - } + return newPulsarLogger( + p.l.WithFields(fs), + ) } func (p pulsarWrapper) WithField(name string, value interface{}) pulsarlog.Entry { - return &pulsarWrapper{ - l: p.l.WithField(name, value), - } + return newPulsarLogger( + p.l.WithField(name, value), + ) } func (p pulsarWrapper) WithError(err error) pulsarlog.Entry { - return &pulsarWrapper{ - l: p.l.WithError(err), - } + return newPulsarLogger( + p.l.WithError(err), + ) } func (p pulsarWrapper) Debug(args ...any) { @@ -71,3 +71,9 @@ func (p pulsarWrapper) Warnf(format string, args ...any) { func (p pulsarWrapper) Errorf(format string, args ...any) { p.l.Errorf(format, args) } + +func newPulsarLogger(l *Logger) pulsarlog.Logger { + return &pulsarWrapper{ + l: l.WithCallerSkip(pulsarAdapterSkipFrames), + } +} diff --git a/internal/common/startup.go b/internal/common/startup.go index 6a6207ad566..ff170247cfb 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -13,8 +13,6 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/spf13/pflag" "github.com/spf13/viper" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" "golang.org/x/exp/slices" "github.com/armadaproject/armada/internal/common/armadacontext" @@ -91,30 +89,30 @@ func UnmarshalKey(v *viper.Viper, key string, item interface{}) error { } func ConfigureCommandLineLogging() { - // Define an encoder configuration that only includes the message. - encoderConfig := zapcore.EncoderConfig{ - MessageKey: "message", - LineEnding: "\n", - // Ignore everything other than the message by leaving their keys empty. - LevelKey: "", - TimeKey: "", - NameKey: "", - CallerKey: "", - FunctionKey: "", - StacktraceKey: "", - } - // Use the console encoder with the custom configuration. - encoder := zapcore.NewConsoleEncoder(encoderConfig) - - // Create a core that writes to stdout. - core := zapcore.NewCore( - encoder, - zapcore.AddSync(os.Stdout), - zapcore.InfoLevel, - ) - - l := zap.New(core) - log.ReplaceStdLogger(log.FromZap(l)) + //// Define an encoder configuration that only includes the message. + //encoderConfig := zapcore.EncoderConfig{ + // MessageKey: "message", + // LineEnding: "\n", + // // Ignore everything other than the message by leaving their keys empty. + // LevelKey: "", + // TimeKey: "", + // NameKey: "", + // CallerKey: "", + // FunctionKey: "", + // StacktraceKey: "", + //} + //// Use the console encoder with the custom configuration. + //encoder := zapcore.NewConsoleEncoder(encoderConfig) + // + //// Create a core that writes to stdout. + //core := zapcore.NewCore( + // encoder, + // zapcore.AddSync(os.Stdout), + // zapcore.InfoLevel, + //) + // + //l := zap.New(core) + //log.ReplaceStdLogger(log.FromZap(l)) } func ServeMetrics(port uint16) (shutdown func()) { From 10f48132e18b3df3b0d3a21dfa4846eaa7a675a4 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 7 Jan 2025 18:27:49 +0000 Subject: [PATCH 33/58] fixed milli time --- internal/common/logging/application.go | 4 ++-- internal/common/logging/global.go | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index ad469696549..b8c47be6c0c 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -19,7 +19,7 @@ import ( var ( defaultLogConfigPath = "config/logging.yaml" logConfigPathEnvVar = "ARMADA_LOG_CONFIG" - logTimestampFormat = "2006-01-02T15:04:05.999Z07:00" + RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" ) // MustConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from @@ -102,7 +102,7 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { level: level, writer: zerolog.ConsoleWriter{ Out: os.Stdout, - TimeFormat: logTimestampFormat, + TimeFormat: RFC3339Milli, FormatLevel: func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%s", i)) }, diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go index 281ba0ad5bf..48ddfeb75ff 100644 --- a/internal/common/logging/global.go +++ b/internal/common/logging/global.go @@ -3,7 +3,6 @@ package logging import ( "github.com/rs/zerolog" "os" - "time" ) // The global Logger. Comes configured with some sensible defaults for e.g. unit tests, but applications should @@ -102,7 +101,7 @@ func WithStacktrace(err error) *Logger { func createDefaultLogger() *Logger { consoleWriter := zerolog.ConsoleWriter{ Out: os.Stdout, - TimeFormat: time.RFC3339, + TimeFormat: RFC3339Milli, NoColor: true, } zerologLogger := zerolog.New(consoleWriter). From faf182ea8513fd8585a27712be46a70720f879de Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 7 Jan 2025 18:32:56 +0000 Subject: [PATCH 34/58] formatting --- internal/common/logging/application.go | 4 ++-- internal/common/logging/logger.go | 7 ++++++- internal/common/logging/null_logger.go | 10 ---------- 3 files changed, 8 insertions(+), 13 deletions(-) delete mode 100644 internal/common/logging/null_logger.go diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index b8c47be6c0c..64aede94189 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -38,7 +38,7 @@ func MustConfigureApplicationLogging() { func ConfigureApplicationLogging() error { // Set some global logging properties - zerolog.TimeFieldFormat = time.RFC3339Nano + zerolog.TimeFieldFormat = time.RFC3339Nano // needs to be higher or greater precision than the writer format. zerolog.CallerMarshalFunc = shortCallerEncoder // Load config file @@ -83,7 +83,7 @@ func createFileLogger(logConfig Config) (*FilteredLevelWriter, error) { return &FilteredLevelWriter{ level: level, writer: &lumberjack.Logger{ - Filename: "app.log", + Filename: logConfig.File.LogFile, MaxSize: logConfig.File.Rotation.MaxSizeMb, MaxBackups: logConfig.File.Rotation.MaxBackups, MaxAge: logConfig.File.Rotation.MaxAgeDays, diff --git a/internal/common/logging/logger.go b/internal/common/logging/logger.go index 799c44aa47b..86778239365 100644 --- a/internal/common/logging/logger.go +++ b/internal/common/logging/logger.go @@ -2,6 +2,7 @@ package logging import ( "fmt" + "github.com/pkg/errors" "github.com/rs/zerolog" ) @@ -11,6 +12,11 @@ type Logger struct { underlying zerolog.Logger } +// NullLogger is Logger that discards all log lines +var NullLogger = &Logger{ + underlying: zerolog.Nop(), +} + // FromZerolog returns a new Logger backed by the supplied zerolog.Logger func FromZerolog(l zerolog.Logger) *Logger { return &Logger{ @@ -119,7 +125,6 @@ func (l *Logger) WithFields(args map[string]any) *Logger { // This is needed when building wrappers around the Logger so as to prevent us from always reporting the // wrapper code as the caller. func (l *Logger) WithCallerSkip(skip int) *Logger { - return &Logger{ underlying: l.underlying.With().CallerWithSkipFrameCount(skip).Logger(), } diff --git a/internal/common/logging/null_logger.go b/internal/common/logging/null_logger.go deleted file mode 100644 index 01264831839..00000000000 --- a/internal/common/logging/null_logger.go +++ /dev/null @@ -1,10 +0,0 @@ -package logging - -import ( - "github.com/rs/zerolog" -) - -// NullLogger is Logger that sends a log lines into the ether -var NullLogger = &Logger{ - underlying: zerolog.Nop(), -} From ba90268d59e11f10dcdc2f887de939daeeb08c0c Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Wed, 8 Jan 2025 07:57:25 +0000 Subject: [PATCH 35/58] formatting --- .../armadacontext/armada_context_test.go | 165 +++++++++++++++--- internal/common/logging/application.go | 8 +- 2 files changed, 140 insertions(+), 33 deletions(-) diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index 2f9e9f1caad..2a8d276da62 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -1,20 +1,26 @@ package armadacontext import ( + "bytes" "context" + "encoding/json" + "github.com/rs/zerolog" "testing" "time" "github.com/stretchr/testify/assert" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "go.uber.org/zap/zaptest/observer" - "github.com/stretchr/testify/require" "github.com/armadaproject/armada/internal/common/logging" ) +type testLogEntry struct { + Level string `json:"level"` + Message string `json:"message"` + CustomField1 string `json:"customField1,omitempty"` + CustomField2 string `json:"customField2,omitempty"` +} + var defaultLogger = logging.StdLogger().WithField("foo", "bar") func TestNew(t *testing.T) { @@ -33,39 +39,130 @@ func TestTODO(t *testing.T) { require.Equal(t, ctx.Context, context.TODO()) } +func TestLogAtLevel(t *testing.T) { + tests := map[string]struct { + logFunction func(ctx *Context) + expectedLevel string + expectedMsg string + }{ + "Debug": { + logFunction: func(ctx *Context) { + ctx.Debug("test message") + }, + expectedMsg: "test message", + expectedLevel: "debug", + }, + "Debugf": { + logFunction: func(ctx *Context) { + ctx.Debugf("test message %d", 1) + }, + expectedMsg: "test message 1", + expectedLevel: "debug", + }, + "Info": { + logFunction: func(ctx *Context) { + ctx.Info("test message") + }, + expectedMsg: "test message", + expectedLevel: "info", + }, + "Infof": { + logFunction: func(ctx *Context) { + ctx.Infof("test message %d", 1) + }, + expectedMsg: "test message 1", + expectedLevel: "info", + }, + "Warn": { + logFunction: func(ctx *Context) { + ctx.Warn("test message") + }, + expectedMsg: "test message", + expectedLevel: "warn", + }, + "Warnf": { + logFunction: func(ctx *Context) { + ctx.Warnf("test message %d", 1) + }, + expectedMsg: "test message 1", + expectedLevel: "warn", + }, + "Error": { + logFunction: func(ctx *Context) { + ctx.Errorf("test message") + }, + expectedMsg: "test message", + expectedLevel: "error", + }, + "Errorf": { + logFunction: func(ctx *Context) { + ctx.Errorf("test message %d", 1) + }, + expectedMsg: "test message 1", + expectedLevel: "error", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + logger, buf := testLogger() + ctx := New(context.Background(), logger) + tc.logFunction(ctx) + assertLogLineExpected( + t, + &testLogEntry{ + Level: tc.expectedLevel, + Message: tc.expectedMsg, + }, + buf, + ) + }) + } +} + func TestWithLogField(t *testing.T) { - logger, observedLogs := testLogger() - ctx := WithLogField(New(context.Background(), logger), "fish", "chips") + logger, buf := testLogger() + ctx := WithLogField(New(context.Background(), logger), "customField1", "foo") + + // Ensure the context is correctly set require.Equal(t, context.Background(), ctx.Context) ctx.Info("test message") + require.NotEmpty(t, buf.Bytes(), "Expected log entry in buffer") - // Check the captured log entries - entries := observedLogs.All() - require.Len(t, entries, 1, "Expected exactly one log entry") + var entry testLogEntry + err := json.Unmarshal(buf.Bytes(), &entry) + require.NoError(t, err, "Failed to unmarshal log entry") - // Verify the fields - logEntry := entries[0] - assert.Equal(t, "test message", logEntry.Message) - assert.Equal(t, "chips", logEntry.ContextMap()["fish"]) + assert.Equal(t, "test message", entry.Message) + assert.Equal(t, "foo", entry.CustomField1) + assert.Equal(t, "info", entry.Level) } func TestWithLogFields(t *testing.T) { - logger, observedLogs := testLogger() - ctx := WithLogFields(New(context.Background(), logger), map[string]any{"fish": "chips", "salt": "pepper"}) + logger, buf := testLogger() + + ctx := WithLogFields( + New(context.Background(), logger), + map[string]interface{}{ + "customField1": "bar", + "customField2": "baz", + }, + ) + + // Ensure the context is correctly set require.Equal(t, context.Background(), ctx.Context) ctx.Info("test message") + require.NotEmpty(t, buf.Bytes(), "Expected log entry in buffer") - // Check the captured log entries - entries := observedLogs.All() - require.Len(t, entries, 1, "Expected exactly one log entry") + var entry testLogEntry + err := json.Unmarshal(buf.Bytes(), &entry) + require.NoError(t, err, "Failed to unmarshal log entry") - // Verify the fields - logEntry := entries[0] - assert.Equal(t, "test message", logEntry.Message) - assert.Equal(t, "chips", logEntry.ContextMap()["fish"]) - assert.Equal(t, "pepper", logEntry.ContextMap()["salt"]) + assert.Equal(t, "test message", entry.Message) + assert.Equal(t, "bar", entry.CustomField1) + assert.Equal(t, "baz", entry.CustomField2) + assert.Equal(t, "info", entry.Level) } func TestWithTimeout(t *testing.T) { @@ -108,9 +205,21 @@ func quiescent(t *testing.T) time.Duration { return time.Until(deadline) - arbitraryCleanupMargin } -func testLogger() (*logging.Logger, *observer.ObservedLogs) { - core, observedLogs := observer.New(zapcore.DebugLevel) - baseLogger := zap.New(core) - logger := logging.FromZap(baseLogger) - return logger, observedLogs +// testLogger sets up a Zerolog logger that writes to a buffer for testing +func testLogger() (*logging.Logger, *bytes.Buffer) { + var buf bytes.Buffer + baseLogger := zerolog.New(&buf).Level(zerolog.DebugLevel).With().Timestamp().Logger() + logger := logging.FromZerolog(baseLogger) + return logger, &buf +} + +func assertLogLineExpected(t *testing.T, expected *testLogEntry, logOutput *bytes.Buffer) { + var entry testLogEntry + err := json.Unmarshal(logOutput.Bytes(), &entry) + require.NoError(t, err, "Failed to unmarshal log entry") + + assert.Equal(t, expected.Message, entry.Message) + assert.Equal(t, expected.Level, entry.Level) + assert.Equal(t, expected.CustomField1, entry.CustomField1) + assert.Equal(t, expected.CustomField2, entry.CustomField2) } diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 64aede94189..e940a4e18a4 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -4,16 +4,15 @@ import ( "fmt" "io" "os" + "path/filepath" + "strconv" + "strings" "time" "github.com/pkg/errors" "github.com/rs/zerolog" "gopkg.in/natefinch/lumberjack.v2" - - "path/filepath" "sigs.k8s.io/yaml" - "strconv" - "strings" ) var ( @@ -36,7 +35,6 @@ func MustConfigureApplicationLogging() { // ConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from // a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. func ConfigureApplicationLogging() error { - // Set some global logging properties zerolog.TimeFieldFormat = time.RFC3339Nano // needs to be higher or greater precision than the writer format. zerolog.CallerMarshalFunc = shortCallerEncoder From 498789b70aa1c118a0ab51d3221b2e741d40a44b Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Wed, 8 Jan 2025 08:18:46 +0000 Subject: [PATCH 36/58] more tests --- .../armadacontext/armada_context_test.go | 44 ++- internal/common/logging/logger.go | 3 +- internal/common/logging/logger_test.go | 285 ++++++++++++------ internal/common/logging/pulsar_adapter.go | 8 +- 4 files changed, 221 insertions(+), 119 deletions(-) diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index 2a8d276da62..36e5457d991 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -123,19 +123,18 @@ func TestWithLogField(t *testing.T) { logger, buf := testLogger() ctx := WithLogField(New(context.Background(), logger), "customField1", "foo") - // Ensure the context is correctly set - require.Equal(t, context.Background(), ctx.Context) - ctx.Info("test message") - require.NotEmpty(t, buf.Bytes(), "Expected log entry in buffer") - - var entry testLogEntry - err := json.Unmarshal(buf.Bytes(), &entry) - require.NoError(t, err, "Failed to unmarshal log entry") - assert.Equal(t, "test message", entry.Message) - assert.Equal(t, "foo", entry.CustomField1) - assert.Equal(t, "info", entry.Level) + require.Equal(t, context.Background(), ctx.Context) + assertLogLineExpected( + t, + &testLogEntry{ + Level: "info", + Message: "test message", + CustomField1: "foo", + }, + buf, + ) } func TestWithLogFields(t *testing.T) { @@ -149,20 +148,19 @@ func TestWithLogFields(t *testing.T) { }, ) - // Ensure the context is correctly set - require.Equal(t, context.Background(), ctx.Context) - ctx.Info("test message") - require.NotEmpty(t, buf.Bytes(), "Expected log entry in buffer") - - var entry testLogEntry - err := json.Unmarshal(buf.Bytes(), &entry) - require.NoError(t, err, "Failed to unmarshal log entry") - assert.Equal(t, "test message", entry.Message) - assert.Equal(t, "bar", entry.CustomField1) - assert.Equal(t, "baz", entry.CustomField2) - assert.Equal(t, "info", entry.Level) + require.Equal(t, context.Background(), ctx.Context) + assertLogLineExpected( + t, + &testLogEntry{ + Level: "info", + Message: "test message", + CustomField1: "bar", + CustomField2: "baz", + }, + buf, + ) } func TestWithTimeout(t *testing.T) { diff --git a/internal/common/logging/logger.go b/internal/common/logging/logger.go index 86778239365..19eff97915b 100644 --- a/internal/common/logging/logger.go +++ b/internal/common/logging/logger.go @@ -94,7 +94,7 @@ func (l *Logger) WithError(err error) *Logger { func (l *Logger) WithStacktrace(err error) *Logger { logger := l.WithError(err) if stackErr, ok := err.(stackTracer); ok { - return logger.WithField("stacktrace", fmt.Sprintf("%+v", stackErr.StackTrace())) + return logger.WithField("stacktrace", fmt.Sprintf("%v", stackErr.StackTrace())) } return logger } @@ -125,6 +125,7 @@ func (l *Logger) WithFields(args map[string]any) *Logger { // This is needed when building wrappers around the Logger so as to prevent us from always reporting the // wrapper code as the caller. func (l *Logger) WithCallerSkip(skip int) *Logger { + return &Logger{ underlying: l.underlying.With().CallerWithSkipFrameCount(skip).Logger(), } diff --git a/internal/common/logging/logger_test.go b/internal/common/logging/logger_test.go index 2d858d395b9..1f8d9b76d5d 100644 --- a/internal/common/logging/logger_test.go +++ b/internal/common/logging/logger_test.go @@ -1,93 +1,196 @@ package logging -// -//import ( -// "testing" -// -// "github.com/pkg/errors" -// "github.com/stretchr/testify/assert" -// "github.com/stretchr/testify/require" -// "go.uber.org/zap" -// "go.uber.org/zap/zapcore" -// "go.uber.org/zap/zaptest/observer" -//) -// -//func TestWithField(t *testing.T) { -// logger, observedLogs := testLogger() -// -// newLogger := logger.WithField("foo", "bar") -// newLogger.Info("test message") -// -// // Check the captured log entries -// entries := observedLogs.All() -// require.Len(t, entries, 1, "Expected exactly one log entry") -// -// // Verify the fields -// logEntry := entries[0] -// assert.Equal(t, "test message", logEntry.Message) -// assert.Equal(t, "bar", logEntry.ContextMap()["foo"]) -//} -// -//func TestWithFields(t *testing.T) { -// logger, observedLogs := testLogger() -// -// newLogger := logger.WithFields(map[string]any{ -// "user": "test_user", -// "action": "test_action", -// }) -// newLogger.Info("test message") -// -// // Check the captured log entries -// entries := observedLogs.All() -// require.Len(t, entries, 1, "Expected exactly one log entry") -// -// // Verify the fields -// logEntry := entries[0] -// assert.Equal(t, "test message", logEntry.Message) -// assert.Equal(t, "test_user", logEntry.ContextMap()["user"]) -// assert.Equal(t, "test_action", logEntry.ContextMap()["action"]) -//} -// -//func TestWithError(t *testing.T) { -// logger, observedLogs := testLogger() -// -// err := errors.New("test error") -// -// newLogger := logger.WithError(err) -// newLogger.Info("test message") -// -// // Check the captured log entries -// entries := observedLogs.All() -// require.Len(t, entries, 1, "Expected exactly one log entry") -// -// // Verify the fields -// logEntry := entries[0] -// assert.Equal(t, "test message", logEntry.Message) -// assert.Equal(t, "test error", logEntry.ContextMap()["error"]) -//} -// -//func TestWithStacktrace(t *testing.T) { -// logger, observedLogs := testLogger() -// -// err := errors.WithStack(errors.New("test error")) -// -// newLogger := logger.WithStacktrace(err) -// newLogger.Info("test message") -// -// // Check the captured log entries -// entries := observedLogs.All() -// require.Len(t, entries, 1, "Expected exactly one log entry") -// -// // Verify the fields -// logEntry := entries[0] -// assert.Equal(t, "test message", logEntry.Message) -// assert.Equal(t, "test error", logEntry.ContextMap()["error"]) -// assert.Equal(t, err.(stackTracer).StackTrace(), logEntry.ContextMap()["stacktrace"]) -//} -// -//func testLogger() (*Logger, *observer.ObservedLogs) { -// core, observedLogs := observer.New(zapcore.DebugLevel) -// baseLogger := zap.New(core).Sugar() -// logger := &Logger{underlying: baseLogger} -// return logger, observedLogs -//} +import ( + "bytes" + "encoding/json" + "fmt" + "testing" + + "github.com/pkg/errors" + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type testLogEntry struct { + Level string `json:"level"` + Message string `json:"message"` + CustomField1 string `json:"customField1,omitempty"` + CustomField2 string `json:"customField2,omitempty"` + Error string `json:"error,omitempty"` + Stacktrace string `json:"stacktrace,omitempty"` +} + +func TestWithField(t *testing.T) { + logger, buf := testLogger() + logger = logger.WithField("customField1", "foo") + + logger.Info("test message") + + assertLogLineExpected( + t, + &testLogEntry{ + Level: "info", + Message: "test message", + CustomField1: "foo", + }, + buf, + ) +} + +func TestWithFields(t *testing.T) { + logger, buf := testLogger() + logger = logger.WithFields( + map[string]any{"customField1": "bar", "customField2": "baz"}, + ) + + logger.Info("test message") + + assertLogLineExpected( + t, + &testLogEntry{ + Level: "info", + Message: "test message", + CustomField1: "bar", + CustomField2: "baz", + }, + buf, + ) +} + +func TestWithError(t *testing.T) { + logger, buf := testLogger() + err := errors.New("test error") + logger = logger.WithError(err) + + logger.Info("test message") + + assertLogLineExpected( + t, + &testLogEntry{ + Level: "info", + Message: "test message", + Error: "test error", + }, + buf, + ) +} + +func TestWithStacktrace(t *testing.T) { + logger, buf := testLogger() + err := errors.New("test error") + logger = logger.WithStacktrace(err) + + logger.Info("test message") + + assertLogLineExpected( + t, + &testLogEntry{ + Level: "info", + Message: "test message", + Error: "test error", + Stacktrace: fmt.Sprintf("%v", err.(stackTracer).StackTrace()), + }, + buf, + ) +} + +func TestLogAtLevel(t *testing.T) { + tests := map[string]struct { + logFunction func(logger *Logger) + expectedLevel string + expectedMsg string + }{ + "Debug": { + logFunction: func(l *Logger) { + l.Debug("test message") + }, + expectedMsg: "test message", + expectedLevel: "debug", + }, + "Debugf": { + logFunction: func(l *Logger) { + l.Debugf("test message %d", 1) + }, + expectedMsg: "test message 1", + expectedLevel: "debug", + }, + "Info": { + logFunction: func(l *Logger) { + l.Info("test message") + }, + expectedMsg: "test message", + expectedLevel: "info", + }, + "Infof": { + logFunction: func(l *Logger) { + l.Infof("test message %d", 1) + }, + expectedMsg: "test message 1", + expectedLevel: "info", + }, + "Warn": { + logFunction: func(l *Logger) { + l.Warn("test message") + }, + expectedMsg: "test message", + expectedLevel: "warn", + }, + "Warnf": { + logFunction: func(l *Logger) { + l.Warnf("test message %d", 1) + }, + expectedMsg: "test message 1", + expectedLevel: "warn", + }, + "Error": { + logFunction: func(l *Logger) { + l.Errorf("test message") + }, + expectedMsg: "test message", + expectedLevel: "error", + }, + "Errorf": { + logFunction: func(l *Logger) { + l.Errorf("test message %d", 1) + }, + expectedMsg: "test message 1", + expectedLevel: "error", + }, + } + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + logger, buf := testLogger() + tc.logFunction(logger) + assertLogLineExpected( + t, + &testLogEntry{ + Level: tc.expectedLevel, + Message: tc.expectedMsg, + }, + buf, + ) + }) + } +} + +// testLogger sets up a Zerolog logger that writes to a buffer for testing +func testLogger() (*Logger, *bytes.Buffer) { + var buf bytes.Buffer + baseLogger := zerolog.New(&buf).Level(zerolog.DebugLevel).With().Timestamp().Logger() + logger := FromZerolog(baseLogger) + return logger, &buf +} + +func assertLogLineExpected(t *testing.T, expected *testLogEntry, logOutput *bytes.Buffer) { + var entry testLogEntry + err := json.Unmarshal(logOutput.Bytes(), &entry) + require.NoError(t, err, "Failed to unmarshal log entry") + + assert.Equal(t, expected.Message, entry.Message) + assert.Equal(t, expected.Level, entry.Level) + assert.Equal(t, expected.CustomField1, entry.CustomField1) + assert.Equal(t, expected.CustomField2, entry.CustomField2) + assert.Equal(t, expected.Error, entry.Error) + assert.Equal(t, expected.Stacktrace, entry.Stacktrace) +} diff --git a/internal/common/logging/pulsar_adapter.go b/internal/common/logging/pulsar_adapter.go index adb7066a0b2..1ff8490dad7 100644 --- a/internal/common/logging/pulsar_adapter.go +++ b/internal/common/logging/pulsar_adapter.go @@ -57,19 +57,19 @@ func (p pulsarWrapper) Error(args ...any) { } func (p pulsarWrapper) Debugf(format string, args ...any) { - p.l.Debugf(format, args) + p.l.Debugf(format, args...) } func (p pulsarWrapper) Infof(format string, args ...any) { - p.l.Infof(format, args) + p.l.Infof(format, args...) } func (p pulsarWrapper) Warnf(format string, args ...any) { - p.l.Warnf(format, args) + p.l.Warnf(format, args...) } func (p pulsarWrapper) Errorf(format string, args ...any) { - p.l.Errorf(format, args) + p.l.Errorf(format, args...) } func newPulsarLogger(l *Logger) pulsarlog.Logger { From f0ab4e789cca70c3aa0d0fb245157bed70e79331 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Wed, 8 Jan 2025 08:19:39 +0000 Subject: [PATCH 37/58] lint --- internal/common/armadacontext/armada_context_test.go | 2 +- internal/common/logging/global.go | 3 ++- internal/common/logging/logger.go | 1 - 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/common/armadacontext/armada_context_test.go b/internal/common/armadacontext/armada_context_test.go index 36e5457d991..60c94fdc441 100644 --- a/internal/common/armadacontext/armada_context_test.go +++ b/internal/common/armadacontext/armada_context_test.go @@ -4,10 +4,10 @@ import ( "bytes" "context" "encoding/json" - "github.com/rs/zerolog" "testing" "time" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go index 48ddfeb75ff..c34b8cf9529 100644 --- a/internal/common/logging/global.go +++ b/internal/common/logging/global.go @@ -1,8 +1,9 @@ package logging import ( - "github.com/rs/zerolog" "os" + + "github.com/rs/zerolog" ) // The global Logger. Comes configured with some sensible defaults for e.g. unit tests, but applications should diff --git a/internal/common/logging/logger.go b/internal/common/logging/logger.go index 19eff97915b..b6e5605f6ea 100644 --- a/internal/common/logging/logger.go +++ b/internal/common/logging/logger.go @@ -125,7 +125,6 @@ func (l *Logger) WithFields(args map[string]any) *Logger { // This is needed when building wrappers around the Logger so as to prevent us from always reporting the // wrapper code as the caller. func (l *Logger) WithCallerSkip(skip int) *Logger { - return &Logger{ underlying: l.underlying.With().CallerWithSkipFrameCount(skip).Logger(), } From 7901e4b0e0ee877113be15dd57402e242cdf732b Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Wed, 8 Jan 2025 17:46:13 +0000 Subject: [PATCH 38/58] colorful --- go.mod | 4 ++-- go.sum | 22 ++++++++++++++++++---- internal/common/logging/application.go | 1 - internal/common/logging/config.go | 9 +++++---- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index e7014313526..c44581115ca 100644 --- a/go.mod +++ b/go.mod @@ -79,6 +79,7 @@ require ( github.com/prometheus/common v0.60.0 github.com/redis/go-redis/extra/redisprometheus/v9 v9.0.5 github.com/redis/go-redis/v9 v9.7.0 + github.com/rs/zerolog v1.33.0 github.com/segmentio/fasthash v1.0.3 github.com/xitongsys/parquet-go v1.6.2 go.uber.org/mock v0.5.0 @@ -87,6 +88,7 @@ require ( golang.org/x/time v0.5.0 google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 gopkg.in/inf.v0 v0.9.1 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( @@ -181,7 +183,6 @@ require ( github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/procfs v0.15.1 // indirect github.com/rivo/uniseg v0.4.2 // indirect - github.com/rs/zerolog v1.33.0 // indirect github.com/sagikazarmark/locafero v0.6.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect @@ -207,7 +208,6 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect google.golang.org/protobuf v1.35.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect - gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/square/go-jose.v2 v2.6.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/cli-runtime v0.26.15 // indirect diff --git a/go.sum b/go.sum index 1491cc31df8..74b927914a6 100644 --- a/go.sum +++ b/go.sum @@ -114,6 +114,7 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= github.com/coreos/go-oidc v2.2.1+incompatible h1:mh48q/BqXqgjVHpy2ZY7WnWAbenxRjsz9N1i1YxjHAk= github.com/coreos/go-oidc v2.2.1+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/dockercfg v0.3.1 h1:/FpZ+JaygUR/lZP2NlFI2DVfrOEMAIKP5wWEJdoYe9E= github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -209,6 +210,7 @@ github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 h1:ZpnhV/YsD2/4cESfV5+Hoeu/iUR3ruzNvZ+yQfO03a0= github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a h1:dR8+Q0uO5S2ZBcs2IH6VBKYwSxPo2vYCYq0ot0mu7xA= github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFGgqEef3s= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= @@ -393,8 +395,11 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0 github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= -github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= -github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= @@ -503,6 +508,9 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= +github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagikazarmark/locafero v0.6.0 h1:ON7AQg37yzcRPU69mt7gwhFEBwxI6P9T4Qu3N51bwOk= github.com/sagikazarmark/locafero v0.6.0/go.mod h1:77OmuIc6VTraTXKXIs/uvUxKGUXjE1GbemJYHqdNjX0= @@ -560,8 +568,6 @@ github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFA github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI= github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk= github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= -github.com/weaveworks/promrus v1.2.0 h1:jOLf6pe6/vss4qGHjXmGz4oDJQA+AOCqEL3FvvZGz7M= -github.com/weaveworks/promrus v1.2.0/go.mod h1:SaE82+OJ91yqjrE1rsvBWVzNZKcHYFtMUyS1+Ogs/KA= github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= @@ -604,10 +610,14 @@ go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee33 go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -725,7 +735,9 @@ golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -868,6 +880,8 @@ gopkg.in/jcmturner/goidentity.v3 v3.0.0/go.mod h1:oG2kH0IvSYNIu80dVAyu/yoefjq1mN gopkg.in/jcmturner/gokrb5.v7 v7.3.0/go.mod h1:l8VISx+WGYp+Fp7KRbsiUuXTTOnxIc3Tuvyavf11/WM= gopkg.in/jcmturner/rpc.v1 v1.1.0/go.mod h1:YIdkC4XfD6GXbzje11McwsDuOlZQSb9W4vfLvuNnlv8= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/square/go-jose.v2 v2.4.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI= gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index e940a4e18a4..146ef62b5bc 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -95,7 +95,6 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { if err != nil { return nil, err } - return &FilteredLevelWriter{ level: level, writer: zerolog.ConsoleWriter{ diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go index eb69b51a7a8..c8f844318df 100644 --- a/internal/common/logging/config.go +++ b/internal/common/logging/config.go @@ -9,8 +9,9 @@ import ( ) var validLogFormats = map[string]bool{ - "text": true, - "json": true, + "text": true, + "json": true, + "colorful": true, } // Config defines Armada logging configuration. @@ -19,7 +20,7 @@ type Config struct { Console struct { // Log level, e.g. INFO, ERROR etc Level string `yaml:"level"` - // Logging format, either text or json + // Logging format, either text, json or colorful Format string `yaml:"format"` } `yaml:"console"` // Defines configuration for file logging @@ -28,7 +29,7 @@ type Config struct { Enabled bool `yaml:"enabled"` // Log level, e.g. INFO, ERROR etc Level string `yaml:"level"` - // Logging format, either text or json + // Logging format, either text, json or or colorful Format string `yaml:"format"` // The Location of the logfile on disk LogFile string `yaml:"logfile"` From a83400905768d5de7e0ce4d744aef6a03d55ca65 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Wed, 8 Jan 2025 18:28:12 +0000 Subject: [PATCH 39/58] fixes --- config/logging.yaml | 2 +- internal/common/logging/application.go | 59 +++++++++++++++-------- internal/common/logging/pulsar_adapter.go | 2 +- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/config/logging.yaml b/config/logging.yaml index 30f04cac185..3c7eeab868c 100644 --- a/config/logging.yaml +++ b/config/logging.yaml @@ -1,6 +1,6 @@ console: level: INFO - format: text + format: colorful file: enabled: true level: INFO diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 146ef62b5bc..279dbbc174d 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -2,17 +2,15 @@ package logging import ( "fmt" + "github.com/pkg/errors" + "github.com/rs/zerolog" + "gopkg.in/natefinch/lumberjack.v2" "io" "os" "path/filepath" + "sigs.k8s.io/yaml" "strconv" "strings" - "time" - - "github.com/pkg/errors" - "github.com/rs/zerolog" - "gopkg.in/natefinch/lumberjack.v2" - "sigs.k8s.io/yaml" ) var ( @@ -36,7 +34,7 @@ func MustConfigureApplicationLogging() { // a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. func ConfigureApplicationLogging() error { // Set some global logging properties - zerolog.TimeFieldFormat = time.RFC3339Nano // needs to be higher or greater precision than the writer format. + zerolog.TimeFieldFormat = RFC3339Milli // needs to be higher or greater precision than the writer format. zerolog.CallerMarshalFunc = shortCallerEncoder // Load config file @@ -73,21 +71,25 @@ func ConfigureApplicationLogging() error { } func createFileLogger(logConfig Config) (*FilteredLevelWriter, error) { - level, err := zerolog.ParseLevel(logConfig.Console.Level) + level, err := zerolog.ParseLevel(logConfig.File.Level) if err != nil { return nil, err } - return &FilteredLevelWriter{ - level: level, - writer: &lumberjack.Logger{ - Filename: logConfig.File.LogFile, - MaxSize: logConfig.File.Rotation.MaxSizeMb, - MaxBackups: logConfig.File.Rotation.MaxBackups, - MaxAge: logConfig.File.Rotation.MaxAgeDays, - Compress: logConfig.File.Rotation.Compress, - }, - }, nil + // Set up lumberjack for log rotation + lumberjackLogger := &lumberjack.Logger{ + Filename: logConfig.File.LogFile, + MaxSize: logConfig.File.Rotation.MaxSizeMb, + MaxBackups: logConfig.File.Rotation.MaxBackups, + MaxAge: logConfig.File.Rotation.MaxAgeDays, + Compress: logConfig.File.Rotation.Compress, + } + + if strings.ToLower(logConfig.File.Format) == "text" || strings.ToLower(logConfig.File.Format) == "colorful" { + return createConsoleWriter(lumberjackLogger, level, logConfig.File.Format), nil + } else { + return createJsonWriter(lumberjackLogger, level), nil + } } func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { @@ -95,10 +97,25 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { if err != nil { return nil, err } + if strings.ToLower(logConfig.Console.Format) == "text" || strings.ToLower(logConfig.Console.Format) == "colorful" { + return createConsoleWriter(os.Stdout, level, logConfig.Console.Format), nil + } else { + return createJsonWriter(os.Stdout, level), nil + } +} + +func createJsonWriter(out io.Writer, level zerolog.Level) *FilteredLevelWriter { + return &FilteredLevelWriter{ + level: level, + writer: out, + } +} + +func createConsoleWriter(out io.Writer, level zerolog.Level, format string) *FilteredLevelWriter { return &FilteredLevelWriter{ level: level, writer: zerolog.ConsoleWriter{ - Out: os.Stdout, + Out: out, TimeFormat: RFC3339Milli, FormatLevel: func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%s", i)) @@ -106,9 +123,9 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { FormatCaller: func(i interface{}) string { return filepath.Base(fmt.Sprintf("%s", i)) }, - NoColor: true, + NoColor: strings.ToLower(format) == "text", }, - }, nil + } } func readConfig(configFilePath string) (Config, error) { diff --git a/internal/common/logging/pulsar_adapter.go b/internal/common/logging/pulsar_adapter.go index 1ff8490dad7..d4e13d04b59 100644 --- a/internal/common/logging/pulsar_adapter.go +++ b/internal/common/logging/pulsar_adapter.go @@ -4,7 +4,7 @@ import ( pulsarlog "github.com/apache/pulsar-client-go/pulsar/log" ) -var pulsarAdapterSkipFrames = StdSkipFrames + 1 +var pulsarAdapterSkipFrames = StdSkipFrames // Wrapper to adapt Logger to the logger interface expected by the pulsar client type pulsarWrapper struct { From e8cb59ad6aca0d8ff1177ea0e0a37f982f4a2b04 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 12 Jan 2025 20:52:06 +0000 Subject: [PATCH 40/58] fixes --- internal/common/logging/application.go | 4 +-- internal/common/logging/config.go | 40 ++++++++------------------ 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 279dbbc174d..e6da71ca89f 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -85,7 +85,7 @@ func createFileLogger(logConfig Config) (*FilteredLevelWriter, error) { Compress: logConfig.File.Rotation.Compress, } - if strings.ToLower(logConfig.File.Format) == "text" || strings.ToLower(logConfig.File.Format) == "colorful" { + if strings.ToLower(logConfig.File.Format) == "text" || strings.ToLower(logConfig.File.Format) == "colourful" { return createConsoleWriter(lumberjackLogger, level, logConfig.File.Format), nil } else { return createJsonWriter(lumberjackLogger, level), nil @@ -97,7 +97,7 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { if err != nil { return nil, err } - if strings.ToLower(logConfig.Console.Format) == "text" || strings.ToLower(logConfig.Console.Format) == "colorful" { + if strings.ToLower(logConfig.Console.Format) == "text" || strings.ToLower(logConfig.Console.Format) == "colourful" { return createConsoleWriter(os.Stdout, level, logConfig.Console.Format), nil } else { return createJsonWriter(os.Stdout, level), nil diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go index c8f844318df..db4c6987439 100644 --- a/internal/common/logging/config.go +++ b/internal/common/logging/config.go @@ -1,17 +1,16 @@ package logging import ( - "strings" - "github.com/pkg/errors" - "go.uber.org/zap/zapcore" + "github.com/rs/zerolog" "golang.org/x/exp/maps" + "strings" ) var validLogFormats = map[string]bool{ - "text": true, - "json": true, - "colorful": true, + "text": true, + "json": true, + "colourful": true, } // Config defines Armada logging configuration. @@ -50,7 +49,7 @@ type Config struct { } func validate(c Config) error { - _, err := parseLogLevel(c.Console.Level) + _, err := zerolog.ParseLevel(c.Console.Level) if err != nil { return err } @@ -61,7 +60,7 @@ func validate(c Config) error { } if c.File.Enabled { - _, err := parseLogLevel(c.File.Level) + _, err := zerolog.ParseLevel(c.File.Level) if err != nil { return err } @@ -91,27 +90,12 @@ func validate(c Config) error { func validateLogFormat(f string) error { _, ok := validLogFormats[f] if !ok { - err := errors.Errorf("unknown log format: %s. Valid formats are %s", f, maps.Keys(validLogFormats)) + err := errors.Errorf( + "unknown log format: %s. Valid formats are %s", + f, + strings.Join(maps.Keys(validLogFormats), ","), + ) return err } return nil } - -func parseLogLevel(level string) (zapcore.Level, error) { - switch strings.ToLower(level) { - case "debug": - return zapcore.DebugLevel, nil - case "info": - return zapcore.InfoLevel, nil - case "warn", "warning": - return zapcore.WarnLevel, nil - case "error": - return zapcore.ErrorLevel, nil - case "panic": - return zapcore.PanicLevel, nil - case "fatal": - return zapcore.FatalLevel, nil - default: - return zapcore.InfoLevel, errors.Errorf("unknown level: %s", level) - } -} From 6e950b9da6d1370e92ae3c8bb63d4b40122f4e7a Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 12 Jan 2025 21:06:38 +0000 Subject: [PATCH 41/58] fixes --- config/logging.yaml | 2 +- internal/common/logging/application.go | 54 +++---------------- internal/common/logging/config.go | 75 +++++++++++++++----------- internal/common/logging/levelwriter.go | 26 +++++++++ 4 files changed, 80 insertions(+), 77 deletions(-) create mode 100644 internal/common/logging/levelwriter.go diff --git a/config/logging.yaml b/config/logging.yaml index 3c7eeab868c..8faf43615dc 100644 --- a/config/logging.yaml +++ b/config/logging.yaml @@ -1,6 +1,6 @@ console: level: INFO - format: colorful + format: colourful file: enabled: true level: INFO diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index e6da71ca89f..601dcefc140 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -2,18 +2,17 @@ package logging import ( "fmt" - "github.com/pkg/errors" - "github.com/rs/zerolog" - "gopkg.in/natefinch/lumberjack.v2" "io" "os" "path/filepath" - "sigs.k8s.io/yaml" "strconv" "strings" + + "github.com/rs/zerolog" + "gopkg.in/natefinch/lumberjack.v2" ) -var ( +const ( defaultLogConfigPath = "config/logging.yaml" logConfigPathEnvVar = "ARMADA_LOG_CONFIG" RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" @@ -85,7 +84,7 @@ func createFileLogger(logConfig Config) (*FilteredLevelWriter, error) { Compress: logConfig.File.Rotation.Compress, } - if strings.ToLower(logConfig.File.Format) == "text" || strings.ToLower(logConfig.File.Format) == "colourful" { + if logConfig.Console.Format == FormatText || logConfig.Console.Format == FormatColourful { return createConsoleWriter(lumberjackLogger, level, logConfig.File.Format), nil } else { return createJsonWriter(lumberjackLogger, level), nil @@ -97,7 +96,7 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { if err != nil { return nil, err } - if strings.ToLower(logConfig.Console.Format) == "text" || strings.ToLower(logConfig.Console.Format) == "colourful" { + if logConfig.Console.Format == FormatText || logConfig.Console.Format == FormatColourful { return createConsoleWriter(os.Stdout, level, logConfig.Console.Format), nil } else { return createJsonWriter(os.Stdout, level), nil @@ -111,7 +110,7 @@ func createJsonWriter(out io.Writer, level zerolog.Level) *FilteredLevelWriter { } } -func createConsoleWriter(out io.Writer, level zerolog.Level, format string) *FilteredLevelWriter { +func createConsoleWriter(out io.Writer, level zerolog.Level, format LogFormat) *FilteredLevelWriter { return &FilteredLevelWriter{ level: level, writer: zerolog.ConsoleWriter{ @@ -123,29 +122,11 @@ func createConsoleWriter(out io.Writer, level zerolog.Level, format string) *Fil FormatCaller: func(i interface{}) string { return filepath.Base(fmt.Sprintf("%s", i)) }, - NoColor: strings.ToLower(format) == "text", + NoColor: format == FormatText, }, } } -func readConfig(configFilePath string) (Config, error) { - yamlConfig, err := os.ReadFile(configFilePath) - if err != nil { - return Config{}, errors.Wrap(err, "failed to read log config file") - } - - var config Config - err = yaml.Unmarshal(yamlConfig, &config) - if err != nil { - return Config{}, errors.Wrap(err, "failed to unmarshall log config file") - } - err = validate(config) - if err != nil { - return Config{}, errors.Wrap(err, "invalid log configuration") - } - return config, nil -} - func shortCallerEncoder(_ uintptr, file string, line int) string { short := file for i := len(file) - 1; i > 0; i-- { @@ -164,22 +145,3 @@ func getEnv(key, defaultValue string) string { } return defaultValue } - -type FilteredLevelWriter struct { - writer io.Writer - level zerolog.Level -} - -// Write writes to the underlying Writer. -func (w *FilteredLevelWriter) Write(p []byte) (int, error) { - return w.writer.Write(p) -} - -// WriteLevel calls WriteLevel of the underlying Writer only if the level is equal -// or above the Level. -func (w *FilteredLevelWriter) WriteLevel(level zerolog.Level, p []byte) (int, error) { - if level >= w.level { - return w.writer.Write(p) - } - return len(p), nil -} diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go index db4c6987439..87cff50e808 100644 --- a/internal/common/logging/config.go +++ b/internal/common/logging/config.go @@ -1,17 +1,22 @@ package logging import ( + "fmt" + "os" + "github.com/pkg/errors" "github.com/rs/zerolog" - "golang.org/x/exp/maps" - "strings" + "gopkg.in/yaml.v3" ) -var validLogFormats = map[string]bool{ - "text": true, - "json": true, - "colourful": true, -} +type LogFormat string + +// Allowed values for LogFormat. +const ( + FormatText LogFormat = "text" + FormatJSON LogFormat = "json" + FormatColourful LogFormat = "colourful" +) // Config defines Armada logging configuration. type Config struct { @@ -19,8 +24,8 @@ type Config struct { Console struct { // Log level, e.g. INFO, ERROR etc Level string `yaml:"level"` - // Logging format, either text, json or colorful - Format string `yaml:"format"` + // Logging format, either text, json or colourful + Format LogFormat `yaml:"format"` } `yaml:"console"` // Defines configuration for file logging File struct { @@ -28,8 +33,8 @@ type Config struct { Enabled bool `yaml:"enabled"` // Log level, e.g. INFO, ERROR etc Level string `yaml:"level"` - // Logging format, either text, json or or colorful - Format string `yaml:"format"` + // Logging format, either text, json or or colourful + Format LogFormat `yaml:"format"` // The Location of the logfile on disk LogFile string `yaml:"logfile"` // Log Rotation Options @@ -54,22 +59,12 @@ func validate(c Config) error { return err } - err = validateLogFormat(c.Console.Format) - if err != nil { - return err - } - if c.File.Enabled { _, err := zerolog.ParseLevel(c.File.Level) if err != nil { return err } - err = validateLogFormat(c.File.Format) - if err != nil { - return err - } - rotation := c.File.Rotation if rotation.Enabled { if rotation.MaxSizeMb <= 0 { @@ -87,15 +82,35 @@ func validate(c Config) error { return nil } -func validateLogFormat(f string) error { - _, ok := validLogFormats[f] - if !ok { - err := errors.Errorf( - "unknown log format: %s. Valid formats are %s", - f, - strings.Join(maps.Keys(validLogFormats), ","), - ) +func readConfig(configFilePath string) (Config, error) { + yamlConfig, err := os.ReadFile(configFilePath) + if err != nil { + return Config{}, errors.Wrap(err, "failed to read log config file") + } + + var config Config + err = yaml.Unmarshal(yamlConfig, &config) + if err != nil { + return Config{}, errors.Wrap(err, "failed to unmarshall log config file") + } + err = validate(config) + if err != nil { + return Config{}, errors.Wrap(err, "invalid log configuration") + } + return config, nil +} + +func (lf *LogFormat) UnmarshalYAML(value *yaml.Node) error { + var s string + if err := value.Decode(&s); err != nil { return err } - return nil + + switch LogFormat(s) { + case FormatText, FormatJSON, FormatColourful: + *lf = LogFormat(s) + return nil + default: + return fmt.Errorf("invalid log format %q: valid values are %q, %q, or %q", s, FormatText, FormatJSON, FormatColourful) + } } diff --git a/internal/common/logging/levelwriter.go b/internal/common/logging/levelwriter.go new file mode 100644 index 00000000000..edf63a298ba --- /dev/null +++ b/internal/common/logging/levelwriter.go @@ -0,0 +1,26 @@ +package logging + +import ( + "io" + + "github.com/rs/zerolog" +) + +type FilteredLevelWriter struct { + writer io.Writer + level zerolog.Level +} + +// Write writes to the underlying Writer. +func (w *FilteredLevelWriter) Write(p []byte) (int, error) { + return w.writer.Write(p) +} + +// WriteLevel calls WriteLevel of the underlying Writer only if the level is equal +// or above the Level. +func (w *FilteredLevelWriter) WriteLevel(level zerolog.Level, p []byte) (int, error) { + if level >= w.level { + return w.writer.Write(p) + } + return len(p), nil +} From cb8891f4a28a63196c1c4b211cfa916201dead46 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 12 Jan 2025 21:27:42 +0000 Subject: [PATCH 42/58] fixes --- internal/common/logging/application.go | 103 ++++++++++++++++--------- internal/common/logging/config.go | 7 ++ 2 files changed, 72 insertions(+), 38 deletions(-) diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 601dcefc140..413d2e2d218 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -2,6 +2,7 @@ package logging import ( "fmt" + "github.com/pkg/errors" "io" "os" "path/filepath" @@ -13,9 +14,13 @@ import ( ) const ( - defaultLogConfigPath = "config/logging.yaml" - logConfigPathEnvVar = "ARMADA_LOG_CONFIG" - RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" + defaultConfigPath = "config/logging.yaml" + configPathEnvVar = "ARMADA_LOG_CONFIG" + RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" + + // legacy env vars- will be removedin a future release + legacyLogFormatEnvVar = "LOG_FORMAT" + legacyLogLevelEnvVar = "LOG_LEVEL" ) // MustConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from @@ -37,7 +42,7 @@ func ConfigureApplicationLogging() error { zerolog.CallerMarshalFunc = shortCallerEncoder // Load config file - configPath := getEnv(logConfigPathEnvVar, defaultLogConfigPath) + configPath := getEnv(configPathEnvVar, defaultConfigPath) logConfig, err := readConfig(configPath) if err != nil { return err @@ -84,11 +89,7 @@ func createFileLogger(logConfig Config) (*FilteredLevelWriter, error) { Compress: logConfig.File.Rotation.Compress, } - if logConfig.Console.Format == FormatText || logConfig.Console.Format == FormatColourful { - return createConsoleWriter(lumberjackLogger, level, logConfig.File.Format), nil - } else { - return createJsonWriter(lumberjackLogger, level), nil - } + return createWriter(lumberjackLogger, level, logConfig.File.Format) } func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { @@ -96,35 +97,38 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { if err != nil { return nil, err } - if logConfig.Console.Format == FormatText || logConfig.Console.Format == FormatColourful { - return createConsoleWriter(os.Stdout, level, logConfig.Console.Format), nil - } else { - return createJsonWriter(os.Stdout, level), nil - } + return createWriter(os.Stdout, level, logConfig.Console.Format) } -func createJsonWriter(out io.Writer, level zerolog.Level) *FilteredLevelWriter { - return &FilteredLevelWriter{ - level: level, - writer: out, - } -} - -func createConsoleWriter(out io.Writer, level zerolog.Level, format LogFormat) *FilteredLevelWriter { - return &FilteredLevelWriter{ - level: level, - writer: zerolog.ConsoleWriter{ - Out: out, - TimeFormat: RFC3339Milli, - FormatLevel: func(i interface{}) string { - return strings.ToUpper(fmt.Sprintf("%s", i)) - }, - FormatCaller: func(i interface{}) string { - return filepath.Base(fmt.Sprintf("%s", i)) +func createWriter(out io.Writer, level zerolog.Level, format LogFormat) (*FilteredLevelWriter, error) { + + level = overrideLevelFromEnv(level) + format = overrideFormatFromEnv(format) + + switch format { + case FormatJSON: + return &FilteredLevelWriter{ + level: level, + writer: out, + }, nil + case FormatColourful: + case FormatText: + return &FilteredLevelWriter{ + level: level, + writer: zerolog.ConsoleWriter{ + Out: out, + TimeFormat: RFC3339Milli, + FormatLevel: func(i interface{}) string { + return strings.ToUpper(fmt.Sprintf("%s", i)) + }, + FormatCaller: func(i interface{}) string { + return filepath.Base(fmt.Sprintf("%s", i)) + }, + NoColor: format == FormatText, }, - NoColor: format == FormatText, - }, + }, nil } + return nil, errors.Errorf("unknown log format: %s", format) } func shortCallerEncoder(_ uintptr, file string, line int) string { @@ -139,9 +143,32 @@ func shortCallerEncoder(_ uintptr, file string, line int) string { return file + ":" + strconv.Itoa(line) } -func getEnv(key, defaultValue string) string { - if value, exists := os.LookupEnv(key); exists { - return value +func overrideLevelFromEnv(level zerolog.Level) zerolog.Level { + levelOverrideStr, ok := os.LookupEnv(legacyLogLevelEnvVar) + if !ok { + return level + } + if ok { + levelOverride, err := zerolog.ParseLevel(levelOverrideStr) + if err == nil { + return levelOverride + } + } + return level +} + +func overrideFormatFromEnv(format LogFormat) LogFormat { + levelFormatStr, ok := os.LookupEnv(legacyLogFormatEnvVar) + if !ok { + return format + } + switch strings.ToLower(levelFormatStr) { + case "json": + return FormatJSON + case "colourful": + return FormatColourful + case "text": + return FormatText } - return defaultValue + return format } diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go index 87cff50e808..41e2f426fed 100644 --- a/internal/common/logging/config.go +++ b/internal/common/logging/config.go @@ -114,3 +114,10 @@ func (lf *LogFormat) UnmarshalYAML(value *yaml.Node) error { return fmt.Errorf("invalid log format %q: valid values are %q, %q, or %q", s, FormatText, FormatJSON, FormatColourful) } } + +func getEnv(key, defaultValue string) string { + if value, exists := os.LookupEnv(key); exists { + return value + } + return defaultValue +} From 9800c79740f6c15115010d3000c8344fa338ab2e Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 12 Jan 2025 21:29:00 +0000 Subject: [PATCH 43/58] wip --- internal/common/logging/config.go | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go index 41e2f426fed..94a9fb3c167 100644 --- a/internal/common/logging/config.go +++ b/internal/common/logging/config.go @@ -3,6 +3,7 @@ package logging import ( "fmt" "os" + "strings" "github.com/pkg/errors" "github.com/rs/zerolog" @@ -105,7 +106,7 @@ func (lf *LogFormat) UnmarshalYAML(value *yaml.Node) error { if err := value.Decode(&s); err != nil { return err } - + s = strings.ToLower(s) switch LogFormat(s) { case FormatText, FormatJSON, FormatColourful: *lf = LogFormat(s) @@ -114,10 +115,3 @@ func (lf *LogFormat) UnmarshalYAML(value *yaml.Node) error { return fmt.Errorf("invalid log format %q: valid values are %q, %q, or %q", s, FormatText, FormatJSON, FormatColourful) } } - -func getEnv(key, defaultValue string) string { - if value, exists := os.LookupEnv(key); exists { - return value - } - return defaultValue -} From 0fe501710c014fb4ce595564498fefed652aeceb Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 12 Jan 2025 21:30:52 +0000 Subject: [PATCH 44/58] lint --- internal/common/logging/application.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 413d2e2d218..8ba81a4d67c 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -2,13 +2,13 @@ package logging import ( "fmt" - "github.com/pkg/errors" "io" "os" "path/filepath" "strconv" "strings" + "github.com/pkg/errors" "github.com/rs/zerolog" "gopkg.in/natefinch/lumberjack.v2" ) @@ -101,7 +101,6 @@ func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { } func createWriter(out io.Writer, level zerolog.Level, format LogFormat) (*FilteredLevelWriter, error) { - level = overrideLevelFromEnv(level) format = overrideFormatFromEnv(format) @@ -143,6 +142,13 @@ func shortCallerEncoder(_ uintptr, file string, line int) string { return file + ":" + strconv.Itoa(line) } +func getEnv(key, defaultValue string) string { + if value, exists := os.LookupEnv(key); exists { + return value + } + return defaultValue +} + func overrideLevelFromEnv(level zerolog.Level) zerolog.Level { levelOverrideStr, ok := os.LookupEnv(legacyLogLevelEnvVar) if !ok { From 34b97dc7dfd88075358d1971c2a2f521a1297fc9 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 12 Jan 2025 21:37:39 +0000 Subject: [PATCH 45/58] lint --- cmd/armada-load-tester/main.go | 4 ++-- cmd/armadactl/main.go | 4 ++-- cmd/testsuite/main.go | 4 ++-- internal/common/logging/cli.go | 23 +++++++++++++++++++++++ internal/common/startup.go | 27 --------------------------- 5 files changed, 29 insertions(+), 33 deletions(-) create mode 100644 internal/common/logging/cli.go diff --git a/cmd/armada-load-tester/main.go b/cmd/armada-load-tester/main.go index c025b3830c2..85bb12b862c 100644 --- a/cmd/armada-load-tester/main.go +++ b/cmd/armada-load-tester/main.go @@ -2,10 +2,10 @@ package main import ( "github.com/armadaproject/armada/cmd/armada-load-tester/cmd" - "github.com/armadaproject/armada/internal/common" + "github.com/armadaproject/armada/internal/common/logging" ) func main() { - common.ConfigureCommandLineLogging() + logging.ConfigureCliLogging() cmd.Execute() } diff --git a/cmd/armadactl/main.go b/cmd/armadactl/main.go index b90993fc2ff..2168fe69c66 100644 --- a/cmd/armadactl/main.go +++ b/cmd/armadactl/main.go @@ -4,12 +4,12 @@ import ( "os" "github.com/armadaproject/armada/cmd/armadactl/cmd" - "github.com/armadaproject/armada/internal/common" + "github.com/armadaproject/armada/internal/common/logging" ) // Config is handled by cmd/params.go func main() { - common.ConfigureCommandLineLogging() + logging.ConfigureCliLogging() root := cmd.RootCmd() if err := root.Execute(); err != nil { // We don't need to log the error here because cobra has already done this for us diff --git a/cmd/testsuite/main.go b/cmd/testsuite/main.go index 2c069956574..f6961a60f71 100644 --- a/cmd/testsuite/main.go +++ b/cmd/testsuite/main.go @@ -4,12 +4,12 @@ import ( "os" "github.com/armadaproject/armada/cmd/testsuite/cmd" - "github.com/armadaproject/armada/internal/common" + "github.com/armadaproject/armada/internal/common/logging" ) // Config is handled by cmd/params.go func main() { - common.ConfigureCommandLineLogging() + logging.ConfigureCliLogging() err := cmd.RootCmd().Execute() if err != nil { os.Exit(1) diff --git a/internal/common/logging/cli.go b/internal/common/logging/cli.go new file mode 100644 index 00000000000..f923c7db8bc --- /dev/null +++ b/internal/common/logging/cli.go @@ -0,0 +1,23 @@ +package logging + +import ( + "os" + + "github.com/rs/zerolog" +) + +func ConfigureCliLogging() { + zerolog.TimestampFieldName = "" + zerolog.LevelFieldName = "" + + // Create a ConsoleWriter that writes to stdout. + // Since we’ve cleared out the field names above, only the message will be printed. + consoleWriter := zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: "", // No timestamp + } + + // Create a logger that logs at InfoLevel and above. + l := zerolog.New(consoleWriter).Level(zerolog.InfoLevel).With().Logger() + ReplaceStdLogger(FromZerolog(l)) +} diff --git a/internal/common/startup.go b/internal/common/startup.go index ff170247cfb..2cde6a2ecb8 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -88,33 +88,6 @@ func UnmarshalKey(v *viper.Viper, key string, item interface{}) error { return v.UnmarshalKey(key, item, commonconfig.CustomHooks...) } -func ConfigureCommandLineLogging() { - //// Define an encoder configuration that only includes the message. - //encoderConfig := zapcore.EncoderConfig{ - // MessageKey: "message", - // LineEnding: "\n", - // // Ignore everything other than the message by leaving their keys empty. - // LevelKey: "", - // TimeKey: "", - // NameKey: "", - // CallerKey: "", - // FunctionKey: "", - // StacktraceKey: "", - //} - //// Use the console encoder with the custom configuration. - //encoder := zapcore.NewConsoleEncoder(encoderConfig) - // - //// Create a core that writes to stdout. - //core := zapcore.NewCore( - // encoder, - // zapcore.AddSync(os.Stdout), - // zapcore.InfoLevel, - //) - // - //l := zap.New(core) - //log.ReplaceStdLogger(log.FromZap(l)) -} - func ServeMetrics(port uint16) (shutdown func()) { return ServeMetricsFor(port, prometheus.DefaultGatherer) } From 7273f2ee93c0c59d941b32ef20e955bde975718a Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 12 Jan 2025 21:40:42 +0000 Subject: [PATCH 46/58] wip --- internal/common/logging/application.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 8ba81a4d67c..904efb1166a 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -110,8 +110,7 @@ func createWriter(out io.Writer, level zerolog.Level, format LogFormat) (*Filter level: level, writer: out, }, nil - case FormatColourful: - case FormatText: + case FormatColourful, FormatText: return &FilteredLevelWriter{ level: level, writer: zerolog.ConsoleWriter{ From fc8b26e5d1318285ea1741875cf43a10a5ecfbf3 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Sun, 12 Jan 2025 21:41:12 +0000 Subject: [PATCH 47/58] wip --- internal/common/logging/application.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go index 904efb1166a..53c25a059ea 100644 --- a/internal/common/logging/application.go +++ b/internal/common/logging/application.go @@ -18,7 +18,7 @@ const ( configPathEnvVar = "ARMADA_LOG_CONFIG" RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" - // legacy env vars- will be removedin a future release + // legacy env vars: will be removed in a future release legacyLogFormatEnvVar = "LOG_FORMAT" legacyLogLevelEnvVar = "LOG_LEVEL" ) From 314e5a588242bdb7ae886d86e975c1a0fb6ed902 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Mon, 13 Jan 2025 07:40:46 +0000 Subject: [PATCH 48/58] update docker --- .goreleaser.yml | 12 ++++++++++++ build/binoculars/Dockerfile | 1 + build/bundles/armada/Dockerfile | 3 +++ build/bundles/lookout/Dockerfile | 3 +++ build/eventingester/Dockerfile | 2 ++ build/executor/Dockerfile | 1 + build/fakeexecutor/Dockerfile | 1 + build/lookoutingesterv2/Dockerfile | 1 + build/lookoutv2/Dockerfile | 1 + build/scheduler/Dockerfile | 1 + build/scheduleringester/Dockerfile | 1 + build/server/Dockerfile | 1 + 12 files changed, 28 insertions(+) diff --git a/.goreleaser.yml b/.goreleaser.yml index 9172b64c6b0..e50580f99e0 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -197,6 +197,7 @@ dockers: - scheduler - scheduleringester extra_files: + - config/logging.yaml - config/server/config.yaml - config/executor/config.yaml - config/binoculars/config.yaml @@ -217,6 +218,7 @@ dockers: - lookoutv2 - lookoutingesterv2 extra_files: + - config/logging.yaml - config/lookoutv2/config.yaml - config/lookoutingesterv2/config.yaml - internal/lookout/ui @@ -244,6 +246,7 @@ dockers: - lookoutingesterv2 - fakeexecutor extra_files: + - config/logging.yaml - config/server/config.yaml - config/executor/config.yaml - config/binoculars/config.yaml @@ -269,6 +272,7 @@ dockers: - server extra_files: - config/server/config.yaml + - config/logging.yaml dockerfile: ./build/server/Dockerfile - id: executor @@ -283,6 +287,7 @@ dockers: - executor extra_files: - config/executor/config.yaml + - config/logging.yaml dockerfile: ./build/executor/Dockerfile - id: fakeexecutor @@ -297,6 +302,7 @@ dockers: - fakeexecutor extra_files: - config/executor/config.yaml + - config/logging.yaml dockerfile: ./build/fakeexecutor/Dockerfile - id: armadaloadtester @@ -335,6 +341,7 @@ dockers: - lookoutingesterv2 extra_files: - config/lookoutingesterv2/config.yaml + - config/logging.yaml dockerfile: ./build/lookoutingesterv2/Dockerfile - id: lookoutv2 @@ -354,6 +361,7 @@ dockers: - pkg/api/binoculars/api.swagger.json - config/lookoutv2/config.yaml - config/lookoutingesterv2/config.yaml + - config/logging.yaml dockerfile: ./build/lookoutv2/Dockerfile - id: eventingester @@ -368,6 +376,7 @@ dockers: - eventingester extra_files: - config/eventingester/config.yaml + - config/logging.yaml dockerfile: ./build/eventingester/Dockerfile - id: scheduler @@ -382,6 +391,7 @@ dockers: - scheduler extra_files: - config/scheduler/config.yaml + - config/logging.yaml dockerfile: ./build/scheduler/Dockerfile - id: scheduleringester @@ -396,6 +406,7 @@ dockers: - scheduleringester extra_files: - config/scheduleringester/config.yaml + - config/logging.yaml dockerfile: ./build/scheduleringester/Dockerfile - id: binoculars @@ -410,6 +421,7 @@ dockers: - binoculars extra_files: - config/binoculars/config.yaml + - config/logging.yaml dockerfile: ./build/binoculars/Dockerfile - id: armadactl diff --git a/build/binoculars/Dockerfile b/build/binoculars/Dockerfile index 8d58cc1d228..dda83838f48 100644 --- a/build/binoculars/Dockerfile +++ b/build/binoculars/Dockerfile @@ -9,6 +9,7 @@ USER armada COPY binoculars /app/ COPY config/binoculars/config.yaml /app/config/binoculars/config.yaml +COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/build/bundles/armada/Dockerfile b/build/bundles/armada/Dockerfile index 0955bd18d75..d4b870bd8ea 100644 --- a/build/bundles/armada/Dockerfile +++ b/build/bundles/armada/Dockerfile @@ -7,6 +7,9 @@ LABEL org.opencontainers.image.url=https://hub.docker.com/r/gresearch/armada RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada +# Logging (share between all components) +COPY config/logging.yaml /app/config/logging.yaml + # Server COPY config/server/config.yaml /app/config/server/config.yaml diff --git a/build/bundles/lookout/Dockerfile b/build/bundles/lookout/Dockerfile index c01574341f8..5d5354bafc9 100644 --- a/build/bundles/lookout/Dockerfile +++ b/build/bundles/lookout/Dockerfile @@ -8,6 +8,9 @@ LABEL org.opencontainers.image.url=https://hub.docker.com/r/gresearch/armada-loo RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada +# Logging (shared between both components) +COPY config/logging.yaml /app/config/logging.yaml + COPY config/lookoutingesterv2/config.yaml /app/config/lookoutingesterv2/config.yaml COPY config/lookoutv2/config.yaml /app/config/lookoutv2/config.yaml diff --git a/build/eventingester/Dockerfile b/build/eventingester/Dockerfile index 2b1a8bb8d0b..236bbd21ec7 100644 --- a/build/eventingester/Dockerfile +++ b/build/eventingester/Dockerfile @@ -9,5 +9,7 @@ RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada COPY eventingester /app/ COPY config/eventingester/config.yaml /app/config/eventingester/config.yaml +COPY config/logging.yaml /app/config/logging.yaml + WORKDIR /app ENTRYPOINT ["./eventingester"] diff --git a/build/executor/Dockerfile b/build/executor/Dockerfile index 343896ad9d7..b1fdfded70b 100644 --- a/build/executor/Dockerfile +++ b/build/executor/Dockerfile @@ -10,6 +10,7 @@ USER armada COPY executor /app/ COPY config/executor/config.yaml /app/config/executor/config.yaml +COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/build/fakeexecutor/Dockerfile b/build/fakeexecutor/Dockerfile index efdb47f30aa..db31d788344 100644 --- a/build/fakeexecutor/Dockerfile +++ b/build/fakeexecutor/Dockerfile @@ -9,6 +9,7 @@ USER armada COPY fakeexecutor /app/ COPY config/executor/config.yaml /app/config/executor/config.yaml +COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/build/lookoutingesterv2/Dockerfile b/build/lookoutingesterv2/Dockerfile index c2f953facd2..e5c70002b22 100644 --- a/build/lookoutingesterv2/Dockerfile +++ b/build/lookoutingesterv2/Dockerfile @@ -10,6 +10,7 @@ USER armada COPY lookoutingesterv2 /app/ COPY config/lookoutingesterv2/config.yaml /app/config/lookoutingesterv2/config.yaml +COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/build/lookoutv2/Dockerfile b/build/lookoutv2/Dockerfile index 79b259acc98..a75154b7358 100644 --- a/build/lookoutv2/Dockerfile +++ b/build/lookoutv2/Dockerfile @@ -26,5 +26,6 @@ COPY lookoutv2 /app/ COPY config/lookoutv2/config.yaml /app/config/lookoutv2/config.yaml COPY lookoutingesterv2 /app/ COPY config/lookoutingesterv2/ /app/config/lookoutingesterv2 +COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app ENTRYPOINT ["./lookoutv2"] diff --git a/build/scheduler/Dockerfile b/build/scheduler/Dockerfile index 6aa5524bb7f..8713880e0fa 100644 --- a/build/scheduler/Dockerfile +++ b/build/scheduler/Dockerfile @@ -9,5 +9,6 @@ RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada COPY scheduler /app/ COPY config/scheduler/config.yaml /app/config/scheduler/config.yaml +COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app ENTRYPOINT ["./scheduler"] diff --git a/build/scheduleringester/Dockerfile b/build/scheduleringester/Dockerfile index cf86787e726..233445a40c4 100644 --- a/build/scheduleringester/Dockerfile +++ b/build/scheduleringester/Dockerfile @@ -9,5 +9,6 @@ RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada COPY scheduleringester /app/ COPY config/scheduleringester/config.yaml /app/config/scheduleringester/config.yaml +COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app ENTRYPOINT ["./scheduleringester"] diff --git a/build/server/Dockerfile b/build/server/Dockerfile index 4453057c381..fa8def55ef2 100644 --- a/build/server/Dockerfile +++ b/build/server/Dockerfile @@ -10,6 +10,7 @@ USER armada COPY server /app/ COPY config/server/config.yaml /app/config/server/config.yaml +COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app From d89d12fe825c9dfe04157720364b8e2765f74fef Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Mon, 13 Jan 2025 08:14:42 +0000 Subject: [PATCH 49/58] wip --- .goreleaser.yml | 12 -- build/binoculars/Dockerfile | 1 - build/bundles/armada/Dockerfile | 3 - build/bundles/lookout/Dockerfile | 3 - build/eventingester/Dockerfile | 2 - build/executor/Dockerfile | 1 - build/fakeexecutor/Dockerfile | 1 - build/lookoutingesterv2/Dockerfile | 1 - build/lookoutv2/Dockerfile | 1 - build/scheduler/Dockerfile | 1 - build/scheduleringester/Dockerfile | 1 - build/server/Dockerfile | 1 - cmd/armada-load-tester/main.go | 4 +- cmd/armadactl/main.go | 4 +- cmd/binoculars/main.go | 2 +- cmd/eventingester/main.go | 3 +- cmd/executor/main.go | 4 +- cmd/fakeexecutor/main.go | 2 +- cmd/lookoutingesterv2/dbloadtester/main.go | 2 +- cmd/lookoutingesterv2/main.go | 2 +- cmd/lookoutv2/main.go | 4 +- cmd/scheduler/main.go | 3 +- cmd/scheduleringester/main.go | 4 +- cmd/server/main.go | 2 +- cmd/simulator/cmd/root.go | 4 +- cmd/testsuite/main.go | 4 +- config/logging.yaml | 13 -- internal/common/logging/application.go | 179 --------------------- internal/common/logging/cli.go | 23 --- internal/common/logging/config.go | 117 -------------- internal/common/logging/global.go | 2 +- internal/common/startup.go | 80 +++++++++ 32 files changed, 101 insertions(+), 385 deletions(-) delete mode 100644 config/logging.yaml delete mode 100644 internal/common/logging/application.go delete mode 100644 internal/common/logging/cli.go delete mode 100644 internal/common/logging/config.go diff --git a/.goreleaser.yml b/.goreleaser.yml index e50580f99e0..9172b64c6b0 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -197,7 +197,6 @@ dockers: - scheduler - scheduleringester extra_files: - - config/logging.yaml - config/server/config.yaml - config/executor/config.yaml - config/binoculars/config.yaml @@ -218,7 +217,6 @@ dockers: - lookoutv2 - lookoutingesterv2 extra_files: - - config/logging.yaml - config/lookoutv2/config.yaml - config/lookoutingesterv2/config.yaml - internal/lookout/ui @@ -246,7 +244,6 @@ dockers: - lookoutingesterv2 - fakeexecutor extra_files: - - config/logging.yaml - config/server/config.yaml - config/executor/config.yaml - config/binoculars/config.yaml @@ -272,7 +269,6 @@ dockers: - server extra_files: - config/server/config.yaml - - config/logging.yaml dockerfile: ./build/server/Dockerfile - id: executor @@ -287,7 +283,6 @@ dockers: - executor extra_files: - config/executor/config.yaml - - config/logging.yaml dockerfile: ./build/executor/Dockerfile - id: fakeexecutor @@ -302,7 +297,6 @@ dockers: - fakeexecutor extra_files: - config/executor/config.yaml - - config/logging.yaml dockerfile: ./build/fakeexecutor/Dockerfile - id: armadaloadtester @@ -341,7 +335,6 @@ dockers: - lookoutingesterv2 extra_files: - config/lookoutingesterv2/config.yaml - - config/logging.yaml dockerfile: ./build/lookoutingesterv2/Dockerfile - id: lookoutv2 @@ -361,7 +354,6 @@ dockers: - pkg/api/binoculars/api.swagger.json - config/lookoutv2/config.yaml - config/lookoutingesterv2/config.yaml - - config/logging.yaml dockerfile: ./build/lookoutv2/Dockerfile - id: eventingester @@ -376,7 +368,6 @@ dockers: - eventingester extra_files: - config/eventingester/config.yaml - - config/logging.yaml dockerfile: ./build/eventingester/Dockerfile - id: scheduler @@ -391,7 +382,6 @@ dockers: - scheduler extra_files: - config/scheduler/config.yaml - - config/logging.yaml dockerfile: ./build/scheduler/Dockerfile - id: scheduleringester @@ -406,7 +396,6 @@ dockers: - scheduleringester extra_files: - config/scheduleringester/config.yaml - - config/logging.yaml dockerfile: ./build/scheduleringester/Dockerfile - id: binoculars @@ -421,7 +410,6 @@ dockers: - binoculars extra_files: - config/binoculars/config.yaml - - config/logging.yaml dockerfile: ./build/binoculars/Dockerfile - id: armadactl diff --git a/build/binoculars/Dockerfile b/build/binoculars/Dockerfile index dda83838f48..8d58cc1d228 100644 --- a/build/binoculars/Dockerfile +++ b/build/binoculars/Dockerfile @@ -9,7 +9,6 @@ USER armada COPY binoculars /app/ COPY config/binoculars/config.yaml /app/config/binoculars/config.yaml -COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/build/bundles/armada/Dockerfile b/build/bundles/armada/Dockerfile index d4b870bd8ea..0955bd18d75 100644 --- a/build/bundles/armada/Dockerfile +++ b/build/bundles/armada/Dockerfile @@ -7,9 +7,6 @@ LABEL org.opencontainers.image.url=https://hub.docker.com/r/gresearch/armada RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada -# Logging (share between all components) -COPY config/logging.yaml /app/config/logging.yaml - # Server COPY config/server/config.yaml /app/config/server/config.yaml diff --git a/build/bundles/lookout/Dockerfile b/build/bundles/lookout/Dockerfile index 5d5354bafc9..c01574341f8 100644 --- a/build/bundles/lookout/Dockerfile +++ b/build/bundles/lookout/Dockerfile @@ -8,9 +8,6 @@ LABEL org.opencontainers.image.url=https://hub.docker.com/r/gresearch/armada-loo RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada -# Logging (shared between both components) -COPY config/logging.yaml /app/config/logging.yaml - COPY config/lookoutingesterv2/config.yaml /app/config/lookoutingesterv2/config.yaml COPY config/lookoutv2/config.yaml /app/config/lookoutv2/config.yaml diff --git a/build/eventingester/Dockerfile b/build/eventingester/Dockerfile index 236bbd21ec7..2b1a8bb8d0b 100644 --- a/build/eventingester/Dockerfile +++ b/build/eventingester/Dockerfile @@ -9,7 +9,5 @@ RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada COPY eventingester /app/ COPY config/eventingester/config.yaml /app/config/eventingester/config.yaml -COPY config/logging.yaml /app/config/logging.yaml - WORKDIR /app ENTRYPOINT ["./eventingester"] diff --git a/build/executor/Dockerfile b/build/executor/Dockerfile index b1fdfded70b..343896ad9d7 100644 --- a/build/executor/Dockerfile +++ b/build/executor/Dockerfile @@ -10,7 +10,6 @@ USER armada COPY executor /app/ COPY config/executor/config.yaml /app/config/executor/config.yaml -COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/build/fakeexecutor/Dockerfile b/build/fakeexecutor/Dockerfile index db31d788344..efdb47f30aa 100644 --- a/build/fakeexecutor/Dockerfile +++ b/build/fakeexecutor/Dockerfile @@ -9,7 +9,6 @@ USER armada COPY fakeexecutor /app/ COPY config/executor/config.yaml /app/config/executor/config.yaml -COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/build/lookoutingesterv2/Dockerfile b/build/lookoutingesterv2/Dockerfile index e5c70002b22..c2f953facd2 100644 --- a/build/lookoutingesterv2/Dockerfile +++ b/build/lookoutingesterv2/Dockerfile @@ -10,7 +10,6 @@ USER armada COPY lookoutingesterv2 /app/ COPY config/lookoutingesterv2/config.yaml /app/config/lookoutingesterv2/config.yaml -COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/build/lookoutv2/Dockerfile b/build/lookoutv2/Dockerfile index a75154b7358..79b259acc98 100644 --- a/build/lookoutv2/Dockerfile +++ b/build/lookoutv2/Dockerfile @@ -26,6 +26,5 @@ COPY lookoutv2 /app/ COPY config/lookoutv2/config.yaml /app/config/lookoutv2/config.yaml COPY lookoutingesterv2 /app/ COPY config/lookoutingesterv2/ /app/config/lookoutingesterv2 -COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app ENTRYPOINT ["./lookoutv2"] diff --git a/build/scheduler/Dockerfile b/build/scheduler/Dockerfile index 8713880e0fa..6aa5524bb7f 100644 --- a/build/scheduler/Dockerfile +++ b/build/scheduler/Dockerfile @@ -9,6 +9,5 @@ RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada COPY scheduler /app/ COPY config/scheduler/config.yaml /app/config/scheduler/config.yaml -COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app ENTRYPOINT ["./scheduler"] diff --git a/build/scheduleringester/Dockerfile b/build/scheduleringester/Dockerfile index 233445a40c4..cf86787e726 100644 --- a/build/scheduleringester/Dockerfile +++ b/build/scheduleringester/Dockerfile @@ -9,6 +9,5 @@ RUN addgroup -S -g 2000 armada && adduser -S -u 1000 armada -G armada USER armada COPY scheduleringester /app/ COPY config/scheduleringester/config.yaml /app/config/scheduleringester/config.yaml -COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app ENTRYPOINT ["./scheduleringester"] diff --git a/build/server/Dockerfile b/build/server/Dockerfile index fa8def55ef2..4453057c381 100644 --- a/build/server/Dockerfile +++ b/build/server/Dockerfile @@ -10,7 +10,6 @@ USER armada COPY server /app/ COPY config/server/config.yaml /app/config/server/config.yaml -COPY config/logging.yaml /app/config/logging.yaml WORKDIR /app diff --git a/cmd/armada-load-tester/main.go b/cmd/armada-load-tester/main.go index 85bb12b862c..c025b3830c2 100644 --- a/cmd/armada-load-tester/main.go +++ b/cmd/armada-load-tester/main.go @@ -2,10 +2,10 @@ package main import ( "github.com/armadaproject/armada/cmd/armada-load-tester/cmd" - "github.com/armadaproject/armada/internal/common/logging" + "github.com/armadaproject/armada/internal/common" ) func main() { - logging.ConfigureCliLogging() + common.ConfigureCommandLineLogging() cmd.Execute() } diff --git a/cmd/armadactl/main.go b/cmd/armadactl/main.go index 2168fe69c66..b90993fc2ff 100644 --- a/cmd/armadactl/main.go +++ b/cmd/armadactl/main.go @@ -4,12 +4,12 @@ import ( "os" "github.com/armadaproject/armada/cmd/armadactl/cmd" - "github.com/armadaproject/armada/internal/common/logging" + "github.com/armadaproject/armada/internal/common" ) // Config is handled by cmd/params.go func main() { - logging.ConfigureCliLogging() + common.ConfigureCommandLineLogging() root := cmd.RootCmd() if err := root.Execute(); err != nil { // We don't need to log the error here because cobra has already done this for us diff --git a/cmd/binoculars/main.go b/cmd/binoculars/main.go index cd4e0221766..5c0558aeb3a 100644 --- a/cmd/binoculars/main.go +++ b/cmd/binoculars/main.go @@ -35,7 +35,7 @@ func init() { } func main() { - log.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() var config configuration.BinocularsConfig diff --git a/cmd/eventingester/main.go b/cmd/eventingester/main.go index c6cca9d2791..93690f33b4d 100644 --- a/cmd/eventingester/main.go +++ b/cmd/eventingester/main.go @@ -1,7 +1,6 @@ package main import ( - "github.com/armadaproject/armada/internal/common/logging" "github.com/armadaproject/armada/internal/eventingester" "github.com/spf13/pflag" @@ -25,7 +24,7 @@ func init() { } func main() { - logging.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() var config configuration.EventIngesterConfiguration diff --git a/cmd/executor/main.go b/cmd/executor/main.go index 75567416a1f..fc51ad93efa 100644 --- a/cmd/executor/main.go +++ b/cmd/executor/main.go @@ -31,7 +31,7 @@ func init() { } func main() { - log.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() var config configuration.ExecutorConfiguration @@ -61,7 +61,7 @@ func main() { ) defer shutdownMetricServer() - shutdown, wg := executor.StartUp(armadacontext.Background(), config) + shutdown, wg := executor.StartUp(armadacontext.Background(), log.NewEntry(log.StandardLogger()), config) go func() { <-shutdownChannel shutdown() diff --git a/cmd/fakeexecutor/main.go b/cmd/fakeexecutor/main.go index 61b97a38d7e..69d9ce03c33 100644 --- a/cmd/fakeexecutor/main.go +++ b/cmd/fakeexecutor/main.go @@ -29,7 +29,7 @@ func init() { } func main() { - log.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() var config configuration.ExecutorConfiguration diff --git a/cmd/lookoutingesterv2/dbloadtester/main.go b/cmd/lookoutingesterv2/dbloadtester/main.go index 95e4b834673..87f91fa1f73 100644 --- a/cmd/lookoutingesterv2/dbloadtester/main.go +++ b/cmd/lookoutingesterv2/dbloadtester/main.go @@ -45,7 +45,7 @@ const ReportTemplate string = ` ` func main() { - log.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() var config configuration.LookoutIngesterV2Configuration diff --git a/cmd/lookoutingesterv2/main.go b/cmd/lookoutingesterv2/main.go index 70be8d713f2..a0e48a6374f 100644 --- a/cmd/lookoutingesterv2/main.go +++ b/cmd/lookoutingesterv2/main.go @@ -27,7 +27,7 @@ func init() { } func main() { - log.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() var config configuration.LookoutIngesterV2Configuration diff --git a/cmd/lookoutv2/main.go b/cmd/lookoutv2/main.go index 584aac3edf3..da8bb63e57c 100644 --- a/cmd/lookoutv2/main.go +++ b/cmd/lookoutv2/main.go @@ -117,7 +117,7 @@ func prune(ctx *armadacontext.Context, config configuration.LookoutV2Config) { } func main() { - log.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() var config configuration.LookoutV2Config @@ -148,7 +148,7 @@ func main() { restapi.UIConfig = config.UIConfig if err := lookoutv2.Serve(config); err != nil { - log.Error(err.Error()) + log.Error(err) os.Exit(1) } } diff --git a/cmd/scheduler/main.go b/cmd/scheduler/main.go index 7249a2c54d9..f895b3301cd 100644 --- a/cmd/scheduler/main.go +++ b/cmd/scheduler/main.go @@ -6,11 +6,10 @@ import ( "github.com/armadaproject/armada/cmd/scheduler/cmd" "github.com/armadaproject/armada/internal/common" - "github.com/armadaproject/armada/internal/common/logging" ) func main() { - logging.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() if err := cmd.RootCmd().Execute(); err != nil { os.Exit(1) diff --git a/cmd/scheduleringester/main.go b/cmd/scheduleringester/main.go index 527d9d2d627..9aa5207a2ce 100644 --- a/cmd/scheduleringester/main.go +++ b/cmd/scheduleringester/main.go @@ -4,8 +4,6 @@ import ( "fmt" "os" - "github.com/armadaproject/armada/internal/common/logging" - "github.com/spf13/pflag" "github.com/spf13/viper" @@ -26,7 +24,7 @@ func init() { } func main() { - logging.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() var config scheduleringester.Configuration diff --git a/cmd/server/main.go b/cmd/server/main.go index df909f5649d..55aaf59e2c7 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -35,7 +35,7 @@ func init() { } func main() { - logging.MustConfigureApplicationLogging() + common.ConfigureLogging() common.BindCommandlineArguments() // TODO Load relevant config in one place: don't use viper here and in LoadConfig. diff --git a/cmd/simulator/cmd/root.go b/cmd/simulator/cmd/root.go index 1e758e23afb..1ddd0e6ba34 100644 --- a/cmd/simulator/cmd/root.go +++ b/cmd/simulator/cmd/root.go @@ -138,11 +138,11 @@ func runSimulations(cmd *cobra.Command, args []string) error { log.Infof("Will write profiling information to %s", profilingFile) f, err := os.Create(profilingFile) if err != nil { - log.Fatal(err.Error()) + log.Fatal(err) } err = pprof.StartCPUProfile(f) if err != nil { - log.Fatal(err.Error()) + log.Fatal(err) } defer pprof.StopCPUProfile() } diff --git a/cmd/testsuite/main.go b/cmd/testsuite/main.go index f6961a60f71..2c069956574 100644 --- a/cmd/testsuite/main.go +++ b/cmd/testsuite/main.go @@ -4,12 +4,12 @@ import ( "os" "github.com/armadaproject/armada/cmd/testsuite/cmd" - "github.com/armadaproject/armada/internal/common/logging" + "github.com/armadaproject/armada/internal/common" ) // Config is handled by cmd/params.go func main() { - logging.ConfigureCliLogging() + common.ConfigureCommandLineLogging() err := cmd.RootCmd().Execute() if err != nil { os.Exit(1) diff --git a/config/logging.yaml b/config/logging.yaml deleted file mode 100644 index 8faf43615dc..00000000000 --- a/config/logging.yaml +++ /dev/null @@ -1,13 +0,0 @@ -console: - level: INFO - format: colourful -file: - enabled: true - level: INFO - format: json - logfile: app.log - rotation: - maxSizeMb: 5 - maxBackups: 3 - maxAgeDays: 7 - compress: false diff --git a/internal/common/logging/application.go b/internal/common/logging/application.go deleted file mode 100644 index 53c25a059ea..00000000000 --- a/internal/common/logging/application.go +++ /dev/null @@ -1,179 +0,0 @@ -package logging - -import ( - "fmt" - "io" - "os" - "path/filepath" - "strconv" - "strings" - - "github.com/pkg/errors" - "github.com/rs/zerolog" - "gopkg.in/natefinch/lumberjack.v2" -) - -const ( - defaultConfigPath = "config/logging.yaml" - configPathEnvVar = "ARMADA_LOG_CONFIG" - RFC3339Milli = "2006-01-02T15:04:05.000Z07:00" - - // legacy env vars: will be removed in a future release - legacyLogFormatEnvVar = "LOG_FORMAT" - legacyLogLevelEnvVar = "LOG_LEVEL" -) - -// MustConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from -// a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. -// Note that this function will immediately shut down the application if it fails. -func MustConfigureApplicationLogging() { - err := ConfigureApplicationLogging() - if err != nil { - _, _ = fmt.Fprintln(os.Stderr, "Error initializing logging: "+err.Error()) - os.Exit(1) - } -} - -// ConfigureApplicationLogging sets up logging suitable for an application. Logging configuration is loaded from -// a filepath given by the ARMADA_LOG_CONFIG environmental variable or from config/logging.yaml if this var is unset. -func ConfigureApplicationLogging() error { - // Set some global logging properties - zerolog.TimeFieldFormat = RFC3339Milli // needs to be higher or greater precision than the writer format. - zerolog.CallerMarshalFunc = shortCallerEncoder - - // Load config file - configPath := getEnv(configPathEnvVar, defaultConfigPath) - logConfig, err := readConfig(configPath) - if err != nil { - return err - } - - // Console logging - var writers []io.Writer - consoleLogger, err := createConsoleLogger(logConfig) - if err != nil { - return err - } - writers = append(writers, consoleLogger) - - // File logging - if logConfig.File.Enabled { - fileLogger, err := createFileLogger(logConfig) - if err != nil { - return err - } - writers = append(writers, fileLogger) - } - - // Combine loggers - multiWriter := zerolog.MultiLevelWriter(writers...) - logger := zerolog.New(multiWriter).With().Timestamp().Logger() - - // Set our new logger to be the default - ReplaceStdLogger(FromZerolog(logger)) - return nil -} - -func createFileLogger(logConfig Config) (*FilteredLevelWriter, error) { - level, err := zerolog.ParseLevel(logConfig.File.Level) - if err != nil { - return nil, err - } - - // Set up lumberjack for log rotation - lumberjackLogger := &lumberjack.Logger{ - Filename: logConfig.File.LogFile, - MaxSize: logConfig.File.Rotation.MaxSizeMb, - MaxBackups: logConfig.File.Rotation.MaxBackups, - MaxAge: logConfig.File.Rotation.MaxAgeDays, - Compress: logConfig.File.Rotation.Compress, - } - - return createWriter(lumberjackLogger, level, logConfig.File.Format) -} - -func createConsoleLogger(logConfig Config) (*FilteredLevelWriter, error) { - level, err := zerolog.ParseLevel(logConfig.Console.Level) - if err != nil { - return nil, err - } - return createWriter(os.Stdout, level, logConfig.Console.Format) -} - -func createWriter(out io.Writer, level zerolog.Level, format LogFormat) (*FilteredLevelWriter, error) { - level = overrideLevelFromEnv(level) - format = overrideFormatFromEnv(format) - - switch format { - case FormatJSON: - return &FilteredLevelWriter{ - level: level, - writer: out, - }, nil - case FormatColourful, FormatText: - return &FilteredLevelWriter{ - level: level, - writer: zerolog.ConsoleWriter{ - Out: out, - TimeFormat: RFC3339Milli, - FormatLevel: func(i interface{}) string { - return strings.ToUpper(fmt.Sprintf("%s", i)) - }, - FormatCaller: func(i interface{}) string { - return filepath.Base(fmt.Sprintf("%s", i)) - }, - NoColor: format == FormatText, - }, - }, nil - } - return nil, errors.Errorf("unknown log format: %s", format) -} - -func shortCallerEncoder(_ uintptr, file string, line int) string { - short := file - for i := len(file) - 1; i > 0; i-- { - if file[i] == '/' { - short = file[i+1:] - break - } - } - file = short - return file + ":" + strconv.Itoa(line) -} - -func getEnv(key, defaultValue string) string { - if value, exists := os.LookupEnv(key); exists { - return value - } - return defaultValue -} - -func overrideLevelFromEnv(level zerolog.Level) zerolog.Level { - levelOverrideStr, ok := os.LookupEnv(legacyLogLevelEnvVar) - if !ok { - return level - } - if ok { - levelOverride, err := zerolog.ParseLevel(levelOverrideStr) - if err == nil { - return levelOverride - } - } - return level -} - -func overrideFormatFromEnv(format LogFormat) LogFormat { - levelFormatStr, ok := os.LookupEnv(legacyLogFormatEnvVar) - if !ok { - return format - } - switch strings.ToLower(levelFormatStr) { - case "json": - return FormatJSON - case "colourful": - return FormatColourful - case "text": - return FormatText - } - return format -} diff --git a/internal/common/logging/cli.go b/internal/common/logging/cli.go deleted file mode 100644 index f923c7db8bc..00000000000 --- a/internal/common/logging/cli.go +++ /dev/null @@ -1,23 +0,0 @@ -package logging - -import ( - "os" - - "github.com/rs/zerolog" -) - -func ConfigureCliLogging() { - zerolog.TimestampFieldName = "" - zerolog.LevelFieldName = "" - - // Create a ConsoleWriter that writes to stdout. - // Since we’ve cleared out the field names above, only the message will be printed. - consoleWriter := zerolog.ConsoleWriter{ - Out: os.Stdout, - TimeFormat: "", // No timestamp - } - - // Create a logger that logs at InfoLevel and above. - l := zerolog.New(consoleWriter).Level(zerolog.InfoLevel).With().Logger() - ReplaceStdLogger(FromZerolog(l)) -} diff --git a/internal/common/logging/config.go b/internal/common/logging/config.go deleted file mode 100644 index 94a9fb3c167..00000000000 --- a/internal/common/logging/config.go +++ /dev/null @@ -1,117 +0,0 @@ -package logging - -import ( - "fmt" - "os" - "strings" - - "github.com/pkg/errors" - "github.com/rs/zerolog" - "gopkg.in/yaml.v3" -) - -type LogFormat string - -// Allowed values for LogFormat. -const ( - FormatText LogFormat = "text" - FormatJSON LogFormat = "json" - FormatColourful LogFormat = "colourful" -) - -// Config defines Armada logging configuration. -type Config struct { - // Defines configuration for console logging on stdout - Console struct { - // Log level, e.g. INFO, ERROR etc - Level string `yaml:"level"` - // Logging format, either text, json or colourful - Format LogFormat `yaml:"format"` - } `yaml:"console"` - // Defines configuration for file logging - File struct { - // Whether file logging is enabled. - Enabled bool `yaml:"enabled"` - // Log level, e.g. INFO, ERROR etc - Level string `yaml:"level"` - // Logging format, either text, json or or colourful - Format LogFormat `yaml:"format"` - // The Location of the logfile on disk - LogFile string `yaml:"logfile"` - // Log Rotation Options - Rotation struct { - // Whether Log Rotation is enabled - Enabled bool `yaml:"enabled"` - // Maximum size in megabytes of the log file before it gets rotated - MaxSizeMb int `yaml:"maxSizeMb"` - // Maximum number of old log files to retain - MaxBackups int `yaml:"maxBackups"` - // Maximum number of days to retain old log files - MaxAgeDays int `yaml:"maxAgeDays"` - // Whether to compress rotated log files - Compress bool `yaml:"compress"` - } `yaml:"rotation"` - } `yaml:"file"` -} - -func validate(c Config) error { - _, err := zerolog.ParseLevel(c.Console.Level) - if err != nil { - return err - } - - if c.File.Enabled { - _, err := zerolog.ParseLevel(c.File.Level) - if err != nil { - return err - } - - rotation := c.File.Rotation - if rotation.Enabled { - if rotation.MaxSizeMb <= 0 { - return errors.New("rotation.maxSizeMb must be greater than zero") - } - if rotation.MaxBackups <= 0 { - return errors.New("rotation.maxBackups must be greater than zero") - } - if rotation.MaxAgeDays <= 0 { - return errors.New("rotation.maxAgeDays must be greater than zero") - } - } - } - - return nil -} - -func readConfig(configFilePath string) (Config, error) { - yamlConfig, err := os.ReadFile(configFilePath) - if err != nil { - return Config{}, errors.Wrap(err, "failed to read log config file") - } - - var config Config - err = yaml.Unmarshal(yamlConfig, &config) - if err != nil { - return Config{}, errors.Wrap(err, "failed to unmarshall log config file") - } - err = validate(config) - if err != nil { - return Config{}, errors.Wrap(err, "invalid log configuration") - } - return config, nil -} - -func (lf *LogFormat) UnmarshalYAML(value *yaml.Node) error { - var s string - if err := value.Decode(&s); err != nil { - return err - } - s = strings.ToLower(s) - switch LogFormat(s) { - case FormatText, FormatJSON, FormatColourful: - *lf = LogFormat(s) - return nil - default: - return fmt.Errorf("invalid log format %q: valid values are %q, %q, or %q", s, FormatText, FormatJSON, FormatColourful) - } -} diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go index c34b8cf9529..1c537f9a18a 100644 --- a/internal/common/logging/global.go +++ b/internal/common/logging/global.go @@ -102,7 +102,7 @@ func WithStacktrace(err error) *Logger { func createDefaultLogger() *Logger { consoleWriter := zerolog.ConsoleWriter{ Out: os.Stdout, - TimeFormat: RFC3339Milli, + TimeFormat: "2006-01-02T15:04:05.000Z07:00", NoColor: true, } zerologLogger := zerolog.New(consoleWriter). diff --git a/internal/common/startup.go b/internal/common/startup.go index 2cde6a2ecb8..62e225541f8 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -3,14 +3,17 @@ package common import ( "crypto/tls" "fmt" + "io" "net/http" "os" + "path/filepath" "strings" "time" "github.com/mitchellh/mapstructure" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/rs/zerolog" "github.com/spf13/pflag" "github.com/spf13/viper" "golang.org/x/exp/slices" @@ -23,6 +26,9 @@ import ( const baseConfigFileName = "config" +// RFC3339Millis +const logTimestampFormat = "2006-01-02T15:04:05.000Z07:00" + func BindCommandlineArguments() { err := viper.BindPFlags(pflag.CommandLine) if err != nil { @@ -88,6 +94,80 @@ func UnmarshalKey(v *viper.Viper, key string, item interface{}) error { return v.UnmarshalKey(key, item, commonconfig.CustomHooks...) } +// TODO Move logging-related code out of common into a new package internal/logging +func ConfigureCommandLineLogging() { + zerolog.TimestampFieldName = "" + zerolog.LevelFieldName = "" + + // Create a ConsoleWriter that writes to stdout. + // Since we’ve cleared out the field names above, only the message will be printed. + consoleWriter := zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: "", // No timestamp + } + + l := zerolog.New(consoleWriter).Level(zerolog.InfoLevel).With().Logger() + log.ReplaceStdLogger(log.FromZerolog(l)) +} + +func ConfigureLogging() { + + // needs to be higher or greater precision than the writer format. + zerolog.TimeFieldFormat = logTimestampFormat + + level := readEnvironmentLogLevel() + format := readEnvironmentLogFormat() + + var outStream io.Writer = os.Stdout + + if format != "json" { + outStream = zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: logTimestampFormat, + FormatLevel: func(i interface{}) string { + return strings.ToUpper(fmt.Sprintf("%s", i)) + }, + FormatCaller: func(i interface{}) string { + return filepath.Base(fmt.Sprintf("%s", i)) + }, + NoColor: format == "text", + } + } + + zerologLogger := zerolog.New(outStream). + Level(level). + With(). + CallerWithSkipFrameCount(log.StdSkipFrames). + Timestamp(). + Logger() + + log.ReplaceStdLogger(log.FromZerolog(zerologLogger)) +} + +func readEnvironmentLogLevel() zerolog.Level { + level, ok := os.LookupEnv("LOG_LEVEL") + if ok { + logLevel, err := zerolog.ParseLevel(level) + if err == nil { + return logLevel + } + } + return zerolog.InfoLevel +} + +func readEnvironmentLogFormat() string { + format, ok := os.LookupEnv("LOG_FORMAT") + if ok { + format = strings.ToLower(format) + if format == "text" || format == "colourful" || format == "json" { + return format + } else { + _, _ = fmt.Fprintf(os.Stderr, "Invalid log format %s\n", format) + } + } + return "colourful" +} + func ServeMetrics(port uint16) (shutdown func()) { return ServeMetricsFor(port, prometheus.DefaultGatherer) } From bda5f3376c8807862aaa5faa8fc32c85bf6bd0a4 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 07:30:45 +0000 Subject: [PATCH 50/58] add prometheus hook --- internal/common/logging/prometheus.go | 41 +++++++++++++++++++++++++++ internal/common/startup.go | 1 + 2 files changed, 42 insertions(+) create mode 100644 internal/common/logging/prometheus.go diff --git a/internal/common/logging/prometheus.go b/internal/common/logging/prometheus.go new file mode 100644 index 00000000000..9b7c09a6ad5 --- /dev/null +++ b/internal/common/logging/prometheus.go @@ -0,0 +1,41 @@ +package logging + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/rs/zerolog" +) + +// implements zerolog.Hook +type prometheusHook struct { + counters map[zerolog.Level]prometheus.Counter +} + +// NewPrometheusHook creates and registers Prometheus counters for each log level. +func NewPrometheusHook() *prometheusHook { + counters := make(map[zerolog.Level]prometheus.Counter) + + for _, level := range []zerolog.Level{ + zerolog.DebugLevel, + zerolog.InfoLevel, + zerolog.WarnLevel, + zerolog.ErrorLevel, + } { + counter := prometheus.NewCounter(prometheus.CounterOpts{ + Name: "log_messages", + Help: "Total number of log lines logged by level", + ConstLabels: prometheus.Labels{ + "level": level.String(), + }, + }) + // Register the counter with Prometheus. + prometheus.MustRegister(counter) + counters[level] = counter + } + return &prometheusHook{counters: counters} +} + +func (h *prometheusHook) Run(_ *zerolog.Event, level zerolog.Level, _ string) { + if counter, ok := h.counters[level]; ok { + counter.Inc() + } +} diff --git a/internal/common/startup.go b/internal/common/startup.go index 62e225541f8..355c8285f25 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -136,6 +136,7 @@ func ConfigureLogging() { zerologLogger := zerolog.New(outStream). Level(level). + Hook(log.NewPrometheusHook()). With(). CallerWithSkipFrameCount(log.StdSkipFrames). Timestamp(). From 5833afb132b65bf1ee056c325ef4645d7f22d0c9 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 07:31:33 +0000 Subject: [PATCH 51/58] add prometheus hook --- internal/common/logging/prometheus.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/common/logging/prometheus.go b/internal/common/logging/prometheus.go index 9b7c09a6ad5..7e5fa3d1814 100644 --- a/internal/common/logging/prometheus.go +++ b/internal/common/logging/prometheus.go @@ -5,13 +5,13 @@ import ( "github.com/rs/zerolog" ) -// implements zerolog.Hook -type prometheusHook struct { +// PrometheusHook implements zerolog.Hook +type PrometheusHook struct { counters map[zerolog.Level]prometheus.Counter } // NewPrometheusHook creates and registers Prometheus counters for each log level. -func NewPrometheusHook() *prometheusHook { +func NewPrometheusHook() *PrometheusHook { counters := make(map[zerolog.Level]prometheus.Counter) for _, level := range []zerolog.Level{ @@ -31,10 +31,10 @@ func NewPrometheusHook() *prometheusHook { prometheus.MustRegister(counter) counters[level] = counter } - return &prometheusHook{counters: counters} + return &PrometheusHook{counters: counters} } -func (h *prometheusHook) Run(_ *zerolog.Event, level zerolog.Level, _ string) { +func (h *PrometheusHook) Run(_ *zerolog.Event, level zerolog.Level, _ string) { if counter, ok := h.counters[level]; ok { counter.Inc() } From 69328c5d755f6cb15a407bbd30e1e883f30a1141 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 07:32:48 +0000 Subject: [PATCH 52/58] add prometheus hook --- cmd/executor/main.go | 2 +- internal/common/startup.go | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/executor/main.go b/cmd/executor/main.go index fc51ad93efa..a91c0304f49 100644 --- a/cmd/executor/main.go +++ b/cmd/executor/main.go @@ -61,7 +61,7 @@ func main() { ) defer shutdownMetricServer() - shutdown, wg := executor.StartUp(armadacontext.Background(), log.NewEntry(log.StandardLogger()), config) + shutdown, wg := executor.StartUp(armadacontext.Background(), config) go func() { <-shutdownChannel shutdown() diff --git a/internal/common/startup.go b/internal/common/startup.go index 355c8285f25..87b05f068de 100644 --- a/internal/common/startup.go +++ b/internal/common/startup.go @@ -111,7 +111,6 @@ func ConfigureCommandLineLogging() { } func ConfigureLogging() { - // needs to be higher or greater precision than the writer format. zerolog.TimeFieldFormat = logTimestampFormat From 3c4541994421ef97690b6596297a96458b904072 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 07:35:03 +0000 Subject: [PATCH 53/58] go mod tidy --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index c44581115ca..7dcc6047a5e 100644 --- a/go.mod +++ b/go.mod @@ -88,7 +88,6 @@ require ( golang.org/x/time v0.5.0 google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 gopkg.in/inf.v0 v0.9.1 - gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( diff --git a/go.sum b/go.sum index 74b927914a6..b6aaf5eeb5f 100644 --- a/go.sum +++ b/go.sum @@ -880,8 +880,6 @@ gopkg.in/jcmturner/goidentity.v3 v3.0.0/go.mod h1:oG2kH0IvSYNIu80dVAyu/yoefjq1mN gopkg.in/jcmturner/gokrb5.v7 v7.3.0/go.mod h1:l8VISx+WGYp+Fp7KRbsiUuXTTOnxIc3Tuvyavf11/WM= gopkg.in/jcmturner/rpc.v1 v1.1.0/go.mod h1:YIdkC4XfD6GXbzje11McwsDuOlZQSb9W4vfLvuNnlv8= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= -gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= -gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/square/go-jose.v2 v2.4.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI= gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= From f85c39e00d6d62ace79b794a8dce86dcc7ee0c18 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 07:42:34 +0000 Subject: [PATCH 54/58] unit test for prometheus --- internal/common/logging/prometheus_test.go | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 internal/common/logging/prometheus_test.go diff --git a/internal/common/logging/prometheus_test.go b/internal/common/logging/prometheus_test.go new file mode 100644 index 00000000000..bc09b31f861 --- /dev/null +++ b/internal/common/logging/prometheus_test.go @@ -0,0 +1,41 @@ +package logging + +import ( + "testing" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/rs/zerolog" + "github.com/stretchr/testify/assert" +) + +// TestPrometheusHook_IncrementsCounter verifies that calling Run on monitored levels +// properly increments the corresponding counter. +func TestPrometheusHook_IncrementsCounter(t *testing.T) { + hook := NewPrometheusHook() + + // Clean up: Unregister the counters to avoid polluting the global registry in further tests. + t.Cleanup(func() { + for _, counter := range hook.counters { + prometheus.Unregister(counter) + } + }) + + // Define the log levels we are monitoring. + levels := []zerolog.Level{ + zerolog.DebugLevel, + zerolog.InfoLevel, + zerolog.WarnLevel, + zerolog.ErrorLevel, + } + + for _, level := range levels { + n := 3 + for i := 0; i < n; i++ { + hook.Run(nil, level, "dummy message") + } + // Verify the counter value. + value := testutil.ToFloat64(hook.counters[level]) + assert.Equal(t, float64(n), value, "expected counter for level %s to be %d, got %f", level, n, value) + } +} From 1d6f9f5a9adb7756569ce6714d2448ca8e9ec305 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 07:54:22 +0000 Subject: [PATCH 55/58] go mod tidy --- go.mod | 1 - go.sum | 4 ---- 2 files changed, 5 deletions(-) diff --git a/go.mod b/go.mod index 7dcc6047a5e..f1b9d7a05de 100644 --- a/go.mod +++ b/go.mod @@ -83,7 +83,6 @@ require ( github.com/segmentio/fasthash v1.0.3 github.com/xitongsys/parquet-go v1.6.2 go.uber.org/mock v0.5.0 - go.uber.org/zap v1.27.0 golang.org/x/term v0.25.0 golang.org/x/time v0.5.0 google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 diff --git a/go.sum b/go.sum index b6aaf5eeb5f..789178833e1 100644 --- a/go.sum +++ b/go.sum @@ -610,14 +610,10 @@ go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 h1:+FNtrFTmVw0YZGpBGX56XDee33 go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0Hg7FvpRQsQh5OSqIylirxKC7o= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= -go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= -go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= -go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= -go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= From 3b985c7593fc73ab54e398b49722c99aacb56565 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 08:18:19 +0000 Subject: [PATCH 56/58] fix --- internal/common/logging/global.go | 28 +++++++++++------------ internal/common/resource/resource_test.go | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/internal/common/logging/global.go b/internal/common/logging/global.go index 1c537f9a18a..507e1d49478 100644 --- a/internal/common/logging/global.go +++ b/internal/common/logging/global.go @@ -6,11 +6,11 @@ import ( "github.com/rs/zerolog" ) -// The global Logger. Comes configured with some sensible defaults for e.g. unit tests, but applications should -// generally configure their own logging config via ReplaceStdLogger var ( StdSkipFrames = 4 - stdLogger = createDefaultLogger() + // The global Logger. Comes configured with some sensible defaults for e.g. unit tests, but applications should + // generally configure their own logging config via ReplaceStdLogger + stdLogger = createDefaultLogger() ) // ReplaceStdLogger Replaces the global logger. This should be called once at app startup! @@ -23,57 +23,57 @@ func StdLogger() *Logger { return stdLogger } -// Debug logs a message at level Debug. +// Debug logs a message at level Debug on the default logger. func Debug(args ...any) { stdLogger.Debug(args...) } -// Info logs a message at level Info. +// Info logs a message at level Info on the default logger. func Info(args ...any) { stdLogger.Info(args...) } -// Warn logs a message at level Warn on the standard logger. +// Warn logs a message at level Warn on the default logger. func Warn(args ...any) { stdLogger.Warn(args...) } -// Error logs a message at level Error on the standard logger. +// Error logs a message at level Error on the default logger. func Error(args ...any) { stdLogger.Error(args...) } -// Panic logs a message at level Panic on the standard logger. +// Panic logs a message at level Panic on the default logger. func Panic(args ...any) { stdLogger.Panic(args...) } -// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1. +// Fatal logs a message at level Fatal on the default logger then the process will exit with status set to 1. func Fatal(args ...any) { stdLogger.Fatal(args...) } -// Debugf logs a message at level Debug on the standard logger. +// Debugf logs a message at level Debug on the default logger. func Debugf(format string, args ...any) { stdLogger.Debugf(format, args...) } -// Infof logs a message at level Info on the standard logger. +// Infof logs a message at level Info on the default logger. func Infof(format string, args ...any) { stdLogger.Infof(format, args...) } -// Warnf logs a message at level Warn on the standard logger. +// Warnf logs a message at level Warn on the default logger. func Warnf(format string, args ...any) { stdLogger.Warnf(format, args...) } -// Errorf logs a message at level Error on the standard logger. +// Errorf logs a message at level Error on the default logger. func Errorf(format string, args ...any) { stdLogger.Errorf(format, args...) } -// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1. +// Fatalf logs a message at level Fatal on the default logger then the process will exit with status set to 1. func Fatalf(format string, args ...any) { stdLogger.Fatalf(format, args...) } diff --git a/internal/common/resource/resource_test.go b/internal/common/resource/resource_test.go index 184d1fa9067..80752ca18aa 100644 --- a/internal/common/resource/resource_test.go +++ b/internal/common/resource/resource_test.go @@ -142,7 +142,7 @@ func TestTotalResourceRequest_ShouldSumAllContainers(t *testing.T) { func TestTotalResourceRequestShouldReportMaxInitContainerValues(t *testing.T) { highCpuResource := makeContainerResource(1000, 5) highRamResource := makeContainerResource(100, 500) - // WithField init containers, it should take the max of each individual resource from all init containers + // With init containers, it should take the max of each individual resource from all init containers expectedResult := makeContainerResource(1000, 500) pod := makePodWithResource([]*v1.ResourceList{}, []*v1.ResourceList{&highCpuResource, &highRamResource}) From fd4b712fabbd0a235e107371d3a359ea086448c7 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 08:21:49 +0000 Subject: [PATCH 57/58] fix --- internal/scheduler/nodedb/nodeiteration.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/scheduler/nodedb/nodeiteration.go b/internal/scheduler/nodedb/nodeiteration.go index fda04565657..d8260270fa2 100644 --- a/internal/scheduler/nodedb/nodeiteration.go +++ b/internal/scheduler/nodedb/nodeiteration.go @@ -219,7 +219,7 @@ type NodeTypeIterator struct { priority int32 // Used to index into node.keys to assert that keys are always increasing. // This to detect if the iterator gets stuck. - // TODO(albin): WithField better testing we should be able to remove this. + // TODO(albin): With better testing we should be able to remove this. keyIndex int // Name of the memdb index used for node iteration. // Should correspond to the priority set for this iterator. From ab21792dfca581b2017e68819df82d9247b6a4d2 Mon Sep 17 00:00:00 2001 From: Chris Martin Date: Tue, 14 Jan 2025 08:24:00 +0000 Subject: [PATCH 58/58] fix --- internal/scheduler/scheduling/scheduling_algo.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/scheduler/scheduling/scheduling_algo.go b/internal/scheduler/scheduling/scheduling_algo.go index 47b5921389c..b24bb7b1ff9 100644 --- a/internal/scheduler/scheduling/scheduling_algo.go +++ b/internal/scheduler/scheduling/scheduling_algo.go @@ -130,7 +130,7 @@ func (l *FairSchedulingAlgo) Schedule( start := time.Now() schedulerResult, sctx, err := l.SchedulePool(ctx, fsctx, pool.Name, pool.MarketDriven) - ctx.Infof("Scheduled on executor pool %s in %v with error %v", pool, time.Now().Sub(start), err) + ctx.Infof("Scheduled on executor pool %s in %v with error %v", pool.Name, time.Now().Sub(start), err) if errors.Is(err, context.DeadlineExceeded) { // We've reached the scheduling time limit;