diff --git a/go.mod b/go.mod index d3a578e..09c7819 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( github.com/veraison/go-cose v1.1.0 go.uber.org/automaxprocs v1.5.3 go.uber.org/zap v1.25.0 - golang.org/x/sync v0.1.0 + golang.org/x/sync v0.7.0 google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d google.golang.org/grpc v1.57.1 ) @@ -97,10 +97,10 @@ require ( github.com/sirupsen/logrus v1.8.1 // indirect github.com/stretchr/objx v0.5.2 // indirect go.uber.org/multierr v1.10.0 // indirect - golang.org/x/crypto v0.21.0 // indirect - golang.org/x/net v0.21.0 // indirect - golang.org/x/sys v0.18.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/crypto v0.25.0 // indirect + golang.org/x/net v0.27.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/text v0.16.0 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20230822172742-b8732ec3820d // indirect google.golang.org/protobuf v1.33.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index bcef3bc..0973b70 100644 --- a/go.sum +++ b/go.sum @@ -320,8 +320,9 @@ golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0 golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= +golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= @@ -348,8 +349,9 @@ golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qx golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= -golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -360,8 +362,9 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -386,8 +389,9 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= @@ -401,8 +405,9 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= diff --git a/grpcserver/grpcserver.go b/grpcserver/grpcserver.go index 7a5da06..280d52c 100644 --- a/grpcserver/grpcserver.go +++ b/grpcserver/grpcserver.go @@ -13,7 +13,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/reflection" - env "github.com/datatrails/go-datatrails-common/environment" "github.com/datatrails/go-datatrails-common/grpchealth" grpcHealth "google.golang.org/grpc/health/grpc_health_v1" ) @@ -79,12 +78,11 @@ func tracingFilter(ctx context.Context, fullMethodName string) bool { // New creates a new GRPCServer that is bound to a specific GRPC API. This object complies with // the standard Listener service and can be managed by the startup.Listeners object. -func New(log Logger, name string, opts ...GRPCServerOption) GRPCServer { - listenStr := fmt.Sprintf(":%s", env.GetOrFatal("PORT")) +func New(log Logger, name string, port string, opts ...GRPCServerOption) GRPCServer { g := GRPCServer{ name: strings.ToLower(name), - listenStr: listenStr, + listenStr: fmt.Sprintf(":%s", port), register: defaultRegisterServer, interceptors: []grpc.UnaryServerInterceptor{ grpc_otrace.UnaryServerInterceptor(grpc_otrace.WithFilterFunc(tracingFilter)), diff --git a/startup/run.go b/startup/run.go index 5c3bdfc..16602ec 100644 --- a/startup/run.go +++ b/startup/run.go @@ -11,10 +11,10 @@ import ( "github.com/datatrails/go-datatrails-common/tracing" ) -type Runner func(string, logger.Logger) error +type Runner func(logger.Logger) error // defers do not work in main() because of the os.Exit( -func Run(serviceName string, run Runner) { +func Run(serviceName string, portName string, run Runner) { logger.New(environment.GetLogLevel()) log := logger.Sugar.WithServiceName(serviceName) @@ -31,11 +31,11 @@ func Run(serviceName string, run Runner) { exitCode := func() int { var exitCode int - closer := tracing.NewTracer() + closer := tracing.NewTracer(log, portName) if closer != nil { defer closer.Close() } - err := run(serviceName, log) + err := run(log) if err != nil { log.Infof("Error at startup: %v", err) exitCode = 1 diff --git a/tracing/logger.go b/tracing/logger.go new file mode 100644 index 0000000..4bb4e5e --- /dev/null +++ b/tracing/logger.go @@ -0,0 +1,7 @@ +package tracing + +import ( + "github.com/datatrails/go-datatrails-common/logger" +) + +type Logger = logger.Logger diff --git a/tracing/tracing.go b/tracing/tracing.go index f64e823..b1fe770 100644 --- a/tracing/tracing.go +++ b/tracing/tracing.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "log" "net/http" "net/textproto" "os" @@ -16,7 +15,6 @@ import ( opentracing "github.com/opentracing/opentracing-go" "github.com/datatrails/go-datatrails-common/environment" - "github.com/datatrails/go-datatrails-common/logger" zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing" zipkin "github.com/openzipkin/zipkin-go" @@ -77,13 +75,7 @@ func NewSpanContext(ctx context.Context, operationName string) (opentracing.Span } func StartSpanFromContext(ctx context.Context, name string, options ...opentracing.StartSpanOption) (opentracing.Span, context.Context) { - - log := logger.Sugar.FromContext(ctx) - defer log.Close() - - log.Debugf("tracing.StartSpanFromContext: %s", name) - - tags := make(map[string]interface{}) + tags := make(map[string]any) tags["component"] = "DATATRAILS" options = append(options, opentracing.Tags(tags)) return opentracing.StartSpanFromContext(ctx, name, options...) @@ -129,50 +121,50 @@ func trimPodName(p string) string { return p } -func NewTracer() io.Closer { +func NewTracer(log Logger, portName string) io.Closer { instanceName, _, _ := strings.Cut(environment.GetOrFatal("POD_NAME"), " ") nameSpace := environment.GetOrFatal("POD_NAMESPACE") containerName := environment.GetOrFatal("CONTAINER_NAME") podName := strings.Join([]string{trimPodName(instanceName), nameSpace, containerName}, ".") - listenStr := fmt.Sprintf("localhost:%s", environment.GetOrFatal("PORT")) - return NewFromEnv(strings.TrimSpace(podName), listenStr, "ZIPKIN_ENDPOINT", "DISABLE_ZIPKIN") + listenStr := fmt.Sprintf("localhost:%s", environment.GetOrFatal(portName)) + return NewFromEnv(log, strings.TrimSpace(podName), listenStr, "ZIPKIN_ENDPOINT", "DISABLE_ZIPKIN") } // NewFromEnv initialises tracing and returns a closer if tracing is // configured. If the necessary configuration is not available it is Fatal // unless disableVar is set and is truthy (strconf.ParseBool -> true). If // tracing is disabled returns nil -func NewFromEnv(service string, host string, endpointVar, disableVar string) io.Closer { +func NewFromEnv(log Logger, service string, host string, endpointVar, disableVar string) io.Closer { ze, ok := os.LookupEnv(endpointVar) if !ok { if disabled := environment.GetTruthyOrFatal(disableVar); !disabled { - logger.Sugar.Panicf( + log.Panicf( "'%s' has not been provided and is not disabled by '%s'", endpointVar, disableVar) } - logger.Sugar.Infof("zipkin disabled by '%s'", disableVar) + log.Infof("zipkin disabled by '%s'", disableVar) return nil } // zipkin conf is available, disable it if disableVar is truthy if disabled := environment.GetTruthyOrFatal(disableVar); disabled { - logger.Sugar.Infof("'%s' set, zipkin disabled", disableVar) + log.Infof("'%s' set, zipkin disabled", disableVar) return nil } - return New(service, host, ze) + return New(log, service, host, ze) } // New initialises tracing // uses zipkin client tracer -func New(service string, host string, zipkinEndpoint string) io.Closer { +func New(log Logger, service string, host string, zipkinEndpoint string) io.Closer { // create our local service endpoint localEndpoint, err := zipkin.NewEndpoint(service, host) if err != nil { - logger.Sugar.Panicf("unable to create zipkin local endpoint service '%s' - host '%s': %v", service, host, err) + log.Panicf("unable to create zipkin local endpoint service '%s' - host '%s': %v", service, host, err) } // set up a span reporter - zipkinLogger := log.New(os.Stdout, "zipkin", log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile) + zipkinLogger := newZipkinLogger() reporter := zipkinhttp.NewReporter(zipkinEndpoint, zipkinhttp.Logger(zipkinLogger)) // TODO: One day this should probably be configurable in helm for each service @@ -183,7 +175,7 @@ func New(service string, host string, zipkinEndpoint string) io.Closer { // not recieving messages or presenting callable endpoints, e.g. a cron like service) sampler, err := zipkin.NewBoundarySampler(rate, time.Now().UnixNano()) if err != nil { - logger.Sugar.Panicf("unable to create zipkin sampler: rate %f: %v", rate, err) + log.Panicf("unable to create zipkin sampler: rate %f: %v", rate, err) } // initialise the tracer @@ -194,14 +186,12 @@ func New(service string, host string, zipkinEndpoint string) io.Closer { zipkin.WithSampler(sampler), ) if err != nil { - logger.Sugar.Panicf("unable to create zipkin tracer: %v", err) + log.Panicf("unable to create zipkin tracer: %v", err) } // use zipkin-go-opentracing to wrap our tracer tracer := zipkinot.Wrap(nativeTracer) opentracing.SetGlobalTracer(tracer) - // logger.Plain.Core().With(zap.String("service", cfg.ServiceName),) - return reporter } diff --git a/tracing/zipkin.go b/tracing/zipkin.go new file mode 100644 index 0000000..f9779da --- /dev/null +++ b/tracing/zipkin.go @@ -0,0 +1,10 @@ +package tracing + +import ( + "log" + "os" +) + +func newZipkinLogger() *log.Logger { + return log.New(os.Stdout, "zipkin", log.Ldate|log.Ltime|log.Lmicroseconds|log.Llongfile) +}