Skip to content

Commit

Permalink
Handle port correctly
Browse files Browse the repository at this point in the history
Allow the user to specify the name of the port env variable. Some
services do not use the PORT variable when the do not have a
grpcserver.

Add local logger to tracing

AB#8760
  • Loading branch information
eccles committed Jul 17, 2024
1 parent 7de13d4 commit 6ea6f74
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 59 deletions.
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down
6 changes: 2 additions & 4 deletions grpcserver/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)),
Expand Down
43 changes: 22 additions & 21 deletions startup/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,45 @@ 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)

// ensure we configure go max procs and memlimit
// for kubernetes.
k8Config, err := k8sworker.NewK8Config(k8sworker.WithLogger(log.Infof))
if err != nil {
log.Infof("Error configuring go for kubernetes: %v", err)
os.Exit(1)
}

// log the useful kubernetes go configuration
log.Infof("Go Configuration: %+v", k8Config)

exitCode := func() int {
var exitCode int
closer := tracing.NewTracer()
if closer != nil {
defer closer.Close()
var err error
// ensure we configure go max procs and memlimit
// for kubernetes.
k8Config, err := k8sworker.NewK8Config(k8sworker.WithLogger(log.Infof))
if err != nil {
log.Infof("Error configuring go for kubernetes: %v", err)
return 1
}
err := run(serviceName, log)
defer k8sworker.Close()

// log the useful kubernetes go configuration
log.Infof("Go Configuration: %+v", k8Config)

if portName != "" {
closer := tracing.NewTracer(log, portName)
if closer != nil {
defer closer.Close()
}
}
err = run(log)
if err != nil {
log.Infof("Error at startup: %v", err)
exitCode = 1
}
return exitCode
}()

log.Infof("Shutting down gracefully")
log.Infof("Shutting down")
logger.OnExit()

// ensure we reset go configuration back to normal
k8sworker.Close()

os.Exit(exitCode)

}
7 changes: 7 additions & 0 deletions tracing/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package tracing

import (
"github.com/datatrails/go-datatrails-common/logger"
)

type Logger = logger.Logger
38 changes: 14 additions & 24 deletions tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/textproto"
"os"
Expand All @@ -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"
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
10 changes: 10 additions & 0 deletions tracing/zipkin.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6ea6f74

Please sign in to comment.