Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development add log format #35

Merged
merged 16 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ build
*.key
*.pem
*.csr
dist
dist
ngonx-log
observability
15 changes: 8 additions & 7 deletions cmd/cli/grpcxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package cli
import (
"context"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"time"

"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/logger"
"github.com/spf13/cobra"
"github.com/talos-systems/grpc-proxy/proxy"
Expand All @@ -29,7 +29,7 @@ var grpcCmd = &cobra.Command{

lis, err := net.Listen("tcp", configFromYaml.GrpcProxy.Listener)
if err != nil {
log.Fatalf("failed to listen: %v", err)
logger.LogError(errors.Errorf("grpc: failed to listen: %v", err).Error())
}

logger.LogInfo(fmt.Sprintf("Proxy running at %q\n", configFromYaml.GrpcProxy.Listener))
Expand All @@ -43,7 +43,7 @@ var grpcCmd = &cobra.Command{
creds, sslErr := credentials.NewClientTLSFromFile(
configFromYaml.GrpcClientCert, "")
if sslErr != nil {
log.Fatalf("Failed to parse credentials: %v", sslErr)
logger.LogError(errors.Errorf("grpc: failed to parse credentials: %v", sslErr).Error())
}
conn, err := grpc.DialContext(
ctx,
Expand Down Expand Up @@ -94,7 +94,7 @@ var grpcCmd = &cobra.Command{
configFromYaml.GrpcSSL.KeyFile,
)
if sslErr != nil {
log.Fatalf("Failed to parse credentials: %v", sslErr)
logger.LogError(errors.Errorf("grpc: failed to parse credentials: %v", sslErr).Error())
return
}
opts = append(opts, grpc.Creds(creds))
Expand All @@ -104,7 +104,7 @@ var grpcCmd = &cobra.Command{

go gracefulShutdown(server)
if err := server.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
logger.LogError(errors.Errorf("grpc: failed to serve: %v", err).Error())
}
},
}
Expand All @@ -119,11 +119,12 @@ func gracefulShutdown(server *grpc.Server) {

signal.Notify(quit, os.Interrupt)
sig := <-quit
logger.LogInfo(fmt.Sprintf("server is shutting down %s", sig.String()))
logger.LogWarn(fmt.Sprintf("grpc: server is shutting down %s", sig.String()))

_, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

server.GracefulStop()
logger.LogInfo("server stopped")
logger.LogWarn(fmt.Sprintf("grpc: server is shutting down %s", sig.String()))

}
17 changes: 9 additions & 8 deletions cmd/cli/lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

handlers "github.com/kenriortega/ngonx/internal/proxy/handlers"
"github.com/kenriortega/ngonx/pkg/backoff"
"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/logger"
"github.com/spf13/cobra"
)
Expand All @@ -30,27 +31,27 @@ var lbCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
port, err := cmd.Flags().GetInt(flagPort)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("lb: %v", err).Error())
}
serverList, err := cmd.Flags().GetString(flagServerList)
if err != nil {
log.Fatalf(err.Error())
}
if len(serverList) == 0 {
log.Fatal("Please provide one or more backends to load balance")
logger.LogError(errors.Errorf("lb: provide one or more backends to load balance %v", err).Error())
}

// parse servers
tokens := strings.Split(serverList, ",")
for _, tok := range tokens {
serverUrl, err := url.Parse(tok)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("lb: %v", err).Error())
}

proxy := httputil.NewSingleHostReverseProxy(serverUrl)
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
logger.LogInfo(fmt.Sprintf("[%s] %s\n", serverUrl.Host, e.Error()))
logger.LogInfo(fmt.Sprintf("lb: %s %s\n", serverUrl.Host, e.Error()))
retry := handlers.GetRetryFromContext(request)

if retry < 3 {
Expand All @@ -66,7 +67,7 @@ var lbCmd = &cobra.Command{

// if the same request routing for few attempts with different backends, increase the count
attempts := handlers.GetAttemptsFromContext(request)
logger.LogInfo(fmt.Sprintf("%s(%s) Attempting retry %d\n", request.RemoteAddr, request.URL.Path, attempts))
logger.LogInfo(fmt.Sprintf("lb: %s(%s) Attempting retry %d\n", request.RemoteAddr, request.URL.Path, attempts))
ctx := context.WithValue(request.Context(), domain.ATTEMPTS, attempts+1)
handlers.Lbalancer(writer, request.WithContext(ctx))
}
Expand All @@ -76,7 +77,7 @@ var lbCmd = &cobra.Command{
Alive: true,
ReverseProxy: proxy,
})
logger.LogInfo(fmt.Sprintf("Configured server: %s\n", serverUrl))
logger.LogInfo(fmt.Sprintf("lb: configured server: %s\n", serverUrl))
}

// create http server
Expand All @@ -88,9 +89,9 @@ var lbCmd = &cobra.Command{
// start health checking
go handlers.HealthCheck()

logger.LogInfo(fmt.Sprintf("Load Balancer started at :%d\n", port))
logger.LogInfo(fmt.Sprintf("lb: Load Balancer started at :%d\n", port))
if err := server.ListenAndServe(); err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("lb: %v", err).Error())
}

},
Expand Down
15 changes: 8 additions & 7 deletions cmd/cli/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
handlers "github.com/kenriortega/ngonx/internal/proxy/handlers"
services "github.com/kenriortega/ngonx/internal/proxy/services"
"github.com/kenriortega/ngonx/pkg/badgerdb"
"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/genkey"
"github.com/kenriortega/ngonx/pkg/httpsrv"
"github.com/kenriortega/ngonx/pkg/logger"
Expand All @@ -18,15 +19,15 @@ var proxyCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
port, err := cmd.Flags().GetInt(flagPort)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("proxy: %v", err).Error())
}
generateApiKey, err := cmd.Flags().GetBool(flagGenApiKey)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("proxy: %v", err).Error())
}
prevKey, err := cmd.Flags().GetString(flagPrevKey)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("proxy: %v", err).Error())
}
// Exporter Metrics
go metric.ExposeMetricServer(configFromYaml.ProxyGateway.PortExporterProxy)
Expand All @@ -47,16 +48,16 @@ var proxyCmd = &cobra.Command{
apiKey := genkey.ApiKeyGenerator(word)
_, err := h.Service.SaveSecretKEY(engine, key, apiKey)
if err != nil {
logger.LogError("genkey: Failed " + err.Error())
logger.LogError(errors.Errorf("proxy: failed genkey cmd %v", err).Error())
}
logger.LogInfo("genkey: Susscefull")
logger.LogInfo("proxy: genkey cmd was susscefull")
}
if prevKey != "" {
_, err := h.Service.SaveSecretKEY(engine, key, prevKey)
if err != nil {
logger.LogError("prevKey: Failed " + err.Error())
logger.LogError(errors.Errorf("proxy: failed prevKey cmd %v", err).Error())
}
logger.LogInfo("prevKey: Susscefull")
logger.LogInfo("proxy: prevKey cmd was Susscefull")
}

for _, endpoints := range configFromYaml.ProxyGateway.EnpointsProxy {
Expand Down
9 changes: 5 additions & 4 deletions cmd/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
handlers "github.com/kenriortega/ngonx/internal/mngt/handlers"
services "github.com/kenriortega/ngonx/internal/mngt/services"
"github.com/kenriortega/ngonx/pkg/config"
"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/healthcheck"
"github.com/kenriortega/ngonx/pkg/httpsrv"
"github.com/kenriortega/ngonx/pkg/logger"
Expand Down Expand Up @@ -70,7 +71,7 @@ func initConfig() {
configFromYaml, errConfig = config.LoadConfig(cfgPath, cfgFile)

if errConfig != nil {
logger.LogError("Yaml file not found please run command setup " + errConfig.Error())
logger.LogError(errors.Errorf("ngonx: Yaml file not found please run command setup :%v", errConfig).Error())
}
go StartMngt(configFromYaml)

Expand All @@ -80,7 +81,7 @@ func StartMngt(config config.Config) {

stripped, err := fs.Sub(frontend, "ui")
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("ngonx: :%v", err).Error())
}

frontendFS := http.FileServer(http.FS(stripped))
Expand Down Expand Up @@ -125,12 +126,12 @@ func StartMngt(config config.Config) {

endpoints, err := service.ListEndpoints()
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("ngonx: :%v", err).Error())
}
for _, it := range endpoints {
u, err := url.Parse(it.PathUrl)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("ngonx: :%v", err).Error())
}
status := healthcheck.IsBackendAlive(u)
if status {
Expand Down
7 changes: 4 additions & 3 deletions cmd/cli/setup.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cli

import (
"log"

"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/logger"
"github.com/spf13/cobra"
)

Expand All @@ -12,7 +12,8 @@ var setupCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
settingsFile, err := cmd.Flags().GetString(flagCfgFile)
if err != nil {
log.Fatalf(err.Error())
logger.LogError(errors.Errorf("ngonx: :%v", err).Error())

}
configFromYaml.CreateSettingFile(settingsFile)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ require (
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20211031064116-611d5d643895 // indirect
google.golang.org/grpc v1.40.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,8 @@ gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8
gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ=
gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
7 changes: 5 additions & 2 deletions internal/mngt/domain/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mngt
import (
"encoding/json"

"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/logger"
uuid "github.com/satori/go.uuid"
)
Expand Down Expand Up @@ -31,12 +32,14 @@ func NewEnpoint(pathUrl, status string) Endpoint {
func (ed *Endpoint) FromMapToJSON(data map[string]interface{}) {
b, err := json.Marshal(data)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("ngonx mngt: :%v", err).Error())

}

err = json.Unmarshal(b, &ed)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("ngonx mngt: :%v", err).Error())

}
}
func (ed *Endpoint) ToMAP() (toHashMap map[string]interface{}, err error) {
Expand Down
24 changes: 12 additions & 12 deletions internal/mngt/handlers/mngt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package mngt

import (
"encoding/json"
"log"
"net/http"
"time"

"github.com/gorilla/websocket"
domain "github.com/kenriortega/ngonx/internal/mngt/domain"
services "github.com/kenriortega/ngonx/internal/mngt/services"
"github.com/kenriortega/ngonx/pkg/errors"
"github.com/kenriortega/ngonx/pkg/logger"
)

Expand Down Expand Up @@ -36,7 +36,8 @@ func (mh MngtHandler) GetAllEndpoints(w http.ResponseWriter, r *http.Request) {
endpoints, err := mh.service.ListEndpoints()

if err != nil {
logger.LogError("handler: " + err.Error())
logger.LogError(errors.Errorf("ngonx mngt: :%v", err).Error())

writeResponse(w, http.StatusInternalServerError, err)

}
Expand All @@ -53,15 +54,15 @@ func (mh MngtHandler) RegisterEndpoint(data map[string]interface{}) {
endpoint.FromMapToJSON(data)
err := mh.service.RegisterEndpoint(endpoint)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("ngonx mngt: :%v", err).Error())
}
}

func (mh MngtHandler) UpdateEndpoint(endpoint domain.Endpoint) {

err := mh.service.UpdateEndpoint(endpoint)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("ngonx mngt: :%v", err).Error())
}
}

Expand All @@ -71,7 +72,7 @@ func writeResponse(w http.ResponseWriter, code int, data interface{}) {
w.Header().Add("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(data)
if err != nil {
logger.LogError(err.Error())
logger.LogError(errors.Errorf("ngonx mngt: :%v", err).Error())
}

}
Expand All @@ -85,7 +86,7 @@ func (mh MngtHandler) WssocketHandler(w http.ResponseWriter, r *http.Request) {
}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
logger.LogError(errors.Errorf("ngonx mngt: upgrade ws :%v", err).Error())
return
}
defer c.Close()
Expand All @@ -94,7 +95,7 @@ func (mh MngtHandler) WssocketHandler(w http.ResponseWriter, r *http.Request) {

mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
logger.LogError(errors.Errorf("ngonx mngt: read ws :%v", err).Error())
break
}
switch string(message) {
Expand All @@ -104,18 +105,17 @@ func (mh MngtHandler) WssocketHandler(w http.ResponseWriter, r *http.Request) {
endpoints, _ := mh.service.ListEndpoints()
bytes, err := json.Marshal(endpoints)
if err != nil {
log.Fatal(err)
logger.LogError(errors.Errorf("ngonx mngt: ws :%v", err).Error())
}
err = c.WriteMessage(mt, bytes)
if err != nil {
log.Println("write endpoints:", err)

logger.LogError(errors.Errorf("ngonx mngt: write endpoints ws :%v", err).Error())
}
durations, err := time.ParseDuration(ds)
if err != nil {
err = c.WriteMessage(mt, []byte(err.Error()))
if err != nil {
log.Println("write durations:", err)
logger.LogError(errors.Errorf("ngonx mngt: write durations ws :%v", err).Error())
}
}
time.Sleep(durations)
Expand All @@ -124,7 +124,7 @@ func (mh MngtHandler) WssocketHandler(w http.ResponseWriter, r *http.Request) {
default:
err = c.WriteMessage(mt, []byte("CMD not found"))
if err != nil {
log.Println("write default:", err)
logger.LogError(errors.Errorf("ngonx mngt: write default ws :%v", err).Error())
}
}
}
Expand Down
Loading