Skip to content

Commit

Permalink
CORD-16295 | kibana logger fix, change order of multi logger, close a…
Browse files Browse the repository at this point in the history
…nd open conection for each write (#70)
  • Loading branch information
tomez authored Jan 26, 2022
1 parent a4b926d commit 4f8aabc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/consul-registration-hook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,6 @@ func main() {

log.Printf("consul-registration-hook (version: %s)", version)
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
log.Fatalf("Execution failed: %s", err)
}
}
35 changes: 19 additions & 16 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ type Msg struct {
}

type KibanaWriter struct {
pod string
namespace string
variant string
scid string
netWrite net.Conn
pod string
namespace string
variant string
scid string
netWrite net.Conn
loggerAddress string
}

func NewKibanaWriter() (*KibanaWriter, error) {
Expand All @@ -39,19 +40,15 @@ func NewKibanaWriter() (*KibanaWriter, error) {
loggerAddress = defaultLoggerAddress
}

netWrite, err := net.Dial("tcp", loggerAddress)
if err != nil {
return nil, fmt.Errorf("error: %s", err)
}
pod := os.Getenv("KUBERNETES_POD_NAME")
namespace := os.Getenv("KUBERNETES_POD_NAMESPACE")
variant := os.Getenv("VARIANT_NAME")
return &KibanaWriter{
netWrite: netWrite,
pod: pod,
namespace: namespace,
variant: variant,
scid: scid,
loggerAddress: loggerAddress,
pod: pod,
namespace: namespace,
variant: variant,
scid: scid,
}, nil
}

Expand All @@ -63,18 +60,24 @@ func (k KibanaWriter) Write(p []byte) (n int, err error) {
Namespace: k.namespace,
Variant: k.variant,
}
netWrite, err := net.Dial("tcp", k.loggerAddress)
if err != nil {
return 0, fmt.Errorf("error: %s", err)
}
defer netWrite.Close()

writeTemplate, err := json.Marshal(msg)
if err != nil {
fmt.Println(err)
return
}
return k.netWrite.Write(writeTemplate)
return netWrite.Write(writeTemplate)
}

func ConfigureLogger() {
kibanaWriter, err := NewKibanaWriter()
if err != nil {
log.Printf("Unable to initialize kibana logger: %s", err)
}
log.SetOutput(io.MultiWriter(kibanaWriter, os.Stdout))
log.SetOutput(io.MultiWriter(os.Stdout, kibanaWriter))
}

0 comments on commit 4f8aabc

Please sign in to comment.