Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
feat: add logging to package
Browse files Browse the repository at this point in the history
  • Loading branch information
buehler committed Jan 3, 2022
1 parent 4fa2cba commit 6ef1f0c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
17 changes: 15 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/WirePact/go-translator/pki"
"github.com/WirePact/go-translator/translator"
"github.com/WirePact/go-translator/wirepact"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -52,18 +53,30 @@ func NewConfigFromEnvironmentVariables(
egressTranslator translator.EgressTranslation) (TranslatorConfig, error) {
pkiAddress := os.Getenv(TranslatorEnvPkiAddress)
if pkiAddress == "" {
logrus.Error("PKI_ADDRESS env variable is not set.")
return TranslatorConfig{}, errors.New(ErrPkiAddressNotSet)
}

commonName := os.Getenv(TranslatorEnvCommonName)
if commonName == "" {
logrus.Error("COMMON_NAME env variable is not set.")
return TranslatorConfig{}, errors.New(ErrCommonNameNotSet)
}

ingressPort := getIntEnvironment(TranslatorEnvIngressPort, TranslatorDefaultIngressPort)
egressPort := getIntEnvironment(TranslatorEnvEgressPort, TranslatorDefaultEgressPort)

logrus.WithFields(map[string]interface{}{
"COMMON_NAME": commonName,
"PKI_ADDRESS": pkiAddress,
"INGERSS_PORT": ingressPort,
"EGRESS_PORT": egressPort,
}).Info("Create translator config.")

return TranslatorConfig{
IngressPort: getIntEnvironment(TranslatorEnvIngressPort, TranslatorDefaultIngressPort),
IngressPort: ingressPort,
IngressTranslator: ingressTranslator,
EgressPort: getIntEnvironment(TranslatorEnvEgressPort, TranslatorDefaultEgressPort),
EgressPort: egressPort,
EgressTranslator: egressTranslator,
Config: pki.Config{
BaseAddress: pkiAddress,
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.16

require (
github.com/envoyproxy/go-control-plane v0.10.1
github.com/sirupsen/logrus v1.8.1 // indirect
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1
google.golang.org/grpc v1.42.0
gopkg.in/square/go-jose.v2 v2.6.0
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1 h1:zH8ljVhhq7yC0MIeUL/
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -58,8 +59,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -92,6 +96,7 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -101,6 +106,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
7 changes: 7 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package go_translator

import "github.com/sirupsen/logrus"

func SetLogLevel(level logrus.Level) {
logrus.SetLevel(level)
}
13 changes: 10 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/WirePact/go-translator/internal"
"github.com/WirePact/go-translator/pki"
auth "github.com/envoyproxy/go-control-plane/envoy/service/auth/v3"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)

Expand All @@ -36,6 +37,7 @@ func NewTranslator(config *TranslatorConfig) (*Translator, error) {

ingressListen, err := net.Listen("tcp", fmt.Sprintf(":%v", config.IngressPort))
if err != nil {
logrus.WithError(err).Errorf("Could not listen on ingress port %v", config.IngressPort)
return nil, err
}

Expand All @@ -48,6 +50,7 @@ func NewTranslator(config *TranslatorConfig) (*Translator, error) {

egressListen, err := net.Listen("tcp", fmt.Sprintf(":%v", config.EgressPort))
if err != nil {
logrus.WithError(err).Errorf("Could not listen on egress port %v", config.EgressPort)
return nil, err
}

Expand All @@ -65,29 +68,32 @@ func NewTranslator(config *TranslatorConfig) (*Translator, error) {
func (translator *Translator) Start() {
err := pki.EnsureKeyMaterial(&translator.config.Config)
if err != nil {
panic(err)
logrus.WithError(err).Fatal("Could not ensure key material.")
}

translator.close = make(chan bool)

go func() {
logrus.Info("Serving Ingress")
err := translator.ingressServer.Serve(*translator.ingressListen)
if err != nil {
panic(err)
logrus.WithError(err).Fatal("Could not serve ingress.")
}
}()

go func() {
logrus.Info("Serving Egress")
err := translator.egressServer.Serve(*translator.egressListen)
if err != nil {
panic(err)
logrus.WithError(err).Fatal("Could not serve egress.")
}
}()

go func() {
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
<-signalChannel
logrus.Infoln("Graceful shutdown signal received. Closing translator.")
translator.close <- true
}()

Expand All @@ -99,5 +105,6 @@ func (translator *Translator) Start() {

// Stop closes the server and returns the "start" function.
func (translator *Translator) Stop() {
logrus.Infoln("Stop function called. Closing translator.")
translator.close <- true
}

0 comments on commit 6ef1f0c

Please sign in to comment.