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

feat: change logging to zap and change how change logs applied #9

Merged
merged 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ RUN go build -o /usr/bin/dgate-cli ./cmd/dgate-cli

FROM alpine:3.19 as runner
WORKDIR /app
COPY --from=builder /usr/bin/dgate-server ./
COPY --from=builder /usr/bin/dgate-cli ./
COPY --from=builder /usr/bin/dgate-server /usr/bin/
COPY --from=builder /usr/bin/dgate-cli /usr/bin/
COPY --from=builder /app/config.dgate.yaml ./
EXPOSE 80 443 9080 9443
CMD [ "./dgate-server" ]
CMD [ "dgate-server" ]
15 changes: 11 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
- other commands (backup, restore, etc.) (low priority)
- replace k6 with wrk for performance tests

## Replace zerolog with slog

## Add Module Tests

- Test multiple modules being used at the same time
Expand Down Expand Up @@ -113,7 +111,7 @@ time based tags
- ?*name:data1,data2
- means that the server must have *one (and only one)* of these tags, for the object to be applied

## Module Permissions
## Module Permissions (using tags?)

- Allow users to define permissions for modules to access certain dgate resources/apis and/or OS resources.
- resource:document:read
Expand Down Expand Up @@ -146,8 +144,17 @@ dgate-cli ns mk my-ns nmae=my-ns
Variable 'nmae' is not recognized. Did you mean 'name'?
```

## DGate CLI - help command show required variables

When the user runs the help command, the CLI should show the required variables for the command. For example, if the user runs `dgate-cli ns mk --help`, the CLI should show the required variables for the `ns mk` command. `name` is a required variable for the `ns mk` command. Also, the CLI should show non-required variables.

## Improve Module Debugability

Make it easier to debug modules by adding more logging and error handling. This can be done by adding more logging to the modules and making it easier to see the logs in the Admin Console.

Add stack tracing for typescript modules.
Add stack tracing for typescript modules.


## Decouple Admin API from Raft Implementation

Currently, Raft Implementation is tightly coupled with the Admin API. This makes it difficult to change the Raft Implementation without changing the Admin API. Decouple the Raft Implementation from the Admin API to make it easier to change the Raft Implementation.
2 changes: 1 addition & 1 deletion cmd/dgate-cli/commands/collection_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func CollectionCommand(client dgclient.DGateClient) *cli.Command {
Aliases: []string{"col"},
Args: true,
ArgsUsage: "<command> <name>",
Usage: "collection commands",
Usage: "collection <action> <args>",
Subcommands: []*cli.Command{
{
Name: "create",
Expand Down
2 changes: 1 addition & 1 deletion cmd/dgate-cli/commands/document_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func DocumentCommand(client dgclient.DGateClient) *cli.Command {
Aliases: []string{"doc"},
Args: true,
ArgsUsage: "<command> <name>",
Usage: "document commands",
Usage: "document <action> <args>",
Subcommands: []*cli.Command{
{
Name: "create",
Expand Down
2 changes: 1 addition & 1 deletion cmd/dgate-cli/commands/domain_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func DomainCommand(client dgclient.DGateClient) *cli.Command {
Aliases: []string{"dom"},
Args: true,
ArgsUsage: "<command> <name>",
Usage: "domain commands",
Usage: "domain <action> <args>",
Subcommands: []*cli.Command{
{
Name: "create",
Expand Down
2 changes: 1 addition & 1 deletion cmd/dgate-cli/commands/module_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func ModuleCommand(client dgclient.DGateClient) *cli.Command {
Aliases: []string{"mod"},
Args: true,
ArgsUsage: "<command> <name>",
Usage: "module commands",
Usage: "module <action> <args>",
Subcommands: []*cli.Command{
{
Name: "create",
Expand Down
8 changes: 5 additions & 3 deletions cmd/dgate-cli/commands/namespace_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (

func NamespaceCommand(client dgclient.DGateClient) *cli.Command {
return &cli.Command{
Name: "namespace",
Aliases: []string{"ns"},
Args: true,
Name: "namespace",
Args: true,
Aliases: []string{"ns"},
ArgsUsage: "<command> <name>",
Usage: "namespace <action> <args>",
Subcommands: []*cli.Command{
{
Name: "create",
Expand Down
2 changes: 1 addition & 1 deletion cmd/dgate-cli/commands/route_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func RouteCommand(client dgclient.DGateClient) *cli.Command {
Aliases: []string{"rt"},
Args: true,
ArgsUsage: "<command> <name>",
Usage: "route commands",
Usage: "route <action> <args>",
Subcommands: []*cli.Command{
{
Name: "create",
Expand Down
2 changes: 1 addition & 1 deletion cmd/dgate-cli/commands/secret_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func SecretCommand(client dgclient.DGateClient) *cli.Command {
Aliases: []string{"sec"},
Args: true,
ArgsUsage: "<command> <name>",
Usage: "secret commands",
Usage: "secret <action> <args>",
Subcommands: []*cli.Command{
{
Name: "create",
Expand Down
2 changes: 1 addition & 1 deletion cmd/dgate-cli/commands/service_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func ServiceCommand(client dgclient.DGateClient) *cli.Command {
Aliases: []string{"svc"},
Args: true,
ArgsUsage: "<command> <name>",
Usage: "service commands",
Usage: "service <action> <args>",
Subcommands: []*cli.Command{
{
Name: "create",
Expand Down
11 changes: 8 additions & 3 deletions cmd/dgate-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@
fmt.Printf("Error loading config: %s\n", err)
os.Exit(1)
} else {
proxyState := proxy.StartProxyGateway(version, dgateConfig)
admin.StartAdminAPI(dgateConfig, proxyState)
logger, err := dgateConfig.GetLogger()
if err != nil {
fmt.Printf("Error setting up logger: %s\n", err)
os.Exit(1)

Check warning on line 66 in cmd/dgate-server/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-server/main.go#L63-L66

Added lines #L63 - L66 were not covered by tests
}
defer logger.Sync()
proxyState := proxy.NewProxyState(logger.Named("proxy"), dgateConfig)
admin.StartAdminAPI(version, dgateConfig, logger.Named("admin"), proxyState)

Check warning on line 70 in cmd/dgate-server/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/dgate-server/main.go#L68-L70

Added lines #L68 - L70 were not covered by tests
if err := proxyState.Start(); err != nil {
fmt.Printf("Error loading config: %s\n", err)
os.Exit(1)
Expand All @@ -75,7 +81,6 @@
)
<-sigchan
proxyState.Stop()
os.Exit(1)
}
}
}
4 changes: 1 addition & 3 deletions config.dgate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_server:
port: 8888
host: 0.0.0.0
global_headers:
X-Node-Id: ${HOSTNAME:-N/A}
X-Test-Header: ${TEST_HEADER:-test}
proxy:
port: ${PORT:-80}
host: 0.0.0.0
Expand Down Expand Up @@ -43,8 +43,6 @@ admin:
port: 9080
host: 0.0.0.0
allow_list:
- "127.0.0.1"
- "127.0.0.1/8"
- "::1"
auth_method: null

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ require (
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ go.opentelemetry.io/otel/sdk/metric v1.26.0 h1:cWSks5tfriHPdWFnl+qpX3P681aAYqlZH
go.opentelemetry.io/otel/sdk/metric v1.26.0/go.mod h1:ClMFFknnThJCksebJwz7KIyEDHO+nTB6gK8obLy8RyE=
go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA=
go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
91 changes: 41 additions & 50 deletions internal/admin/admin_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,67 @@
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"

"github.com/dgate-io/chi-router"
"github.com/dgate-io/chi-router/middleware"
"github.com/dgate-io/dgate/internal/admin/changestate"
"github.com/dgate-io/dgate/internal/config"
"github.com/dgate-io/dgate/internal/proxy"
"github.com/dgate-io/dgate/pkg/util"
"github.com/rs/zerolog"
"go.uber.org/zap"

"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)

func StartAdminAPI(conf *config.DGateConfig, proxyState *proxy.ProxyState) {
func StartAdminAPI(
version string, conf *config.DGateConfig,
logger *zap.Logger, cs changestate.ChangeState,
) {

Check warning on line 25 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L25

Added line #L25 was not covered by tests
if conf.AdminConfig == nil {
proxyState.Logger().Warn().
Msg("Admin API is disabled")
logger.Warn("Admin API is disabled")

Check warning on line 27 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L27

Added line #L27 was not covered by tests
return
}

// Start HTTP Server
mux := chi.NewRouter()
configureRoutes(mux, proxyState, conf)
configureRoutes(mux, version,
logger.Named("routes"), cs, conf)

Check warning on line 33 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L32-L33

Added lines #L32 - L33 were not covered by tests

// Start HTTP Server
go func() {
adminHttpLogger := logger.Named("http")
hostPort := fmt.Sprintf("%s:%d",
conf.AdminConfig.Host, conf.AdminConfig.Port)
logger.Info("Starting admin api on " + hostPort)
server := &http.Server{
Addr: hostPort,
Handler: mux,
ErrorLog: zap.NewStdLog(adminHttpLogger),

Check warning on line 44 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L36-L44

Added lines #L36 - L44 were not covered by tests
}
if err := server.ListenAndServe(); err != nil {
panic(err)

Check warning on line 47 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}
}()

// Start HTTPS Server
go func() {
if conf.AdminConfig.TLS != nil {
adminHttpsLogger := proxyState.Logger(
proxy.WithComponentLogger("admin-https"),
proxy.WithDefaultLevel(zerolog.InfoLevel),
)
adminHttpsLog := logger.Named("https")

Check warning on line 54 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L54

Added line #L54 was not covered by tests
secureHostPort := fmt.Sprintf("%s:%d",
conf.AdminConfig.Host, conf.AdminConfig.TLS.Port)
secureServer := &http.Server{
Addr: secureHostPort,
Handler: mux,
ErrorLog: log.New(adminHttpsLogger, "", 0),
ErrorLog: zap.NewStdLog(adminHttpsLog),

Check warning on line 60 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L60

Added line #L60 was not covered by tests
}
proxyState.Logger().Info().
Msgf("Starting secure admin api on %s", secureHostPort)
proxyState.Logger().Debug().
Msgf("TLS Cert: %s", conf.AdminConfig.TLS.CertFile)
proxyState.Logger().Debug().
Msgf("TLS Key: %s", conf.AdminConfig.TLS.KeyFile)
logger.Info("Starting secure admin api on" + secureHostPort)
logger.Debug("TLS Cert",
zap.String("cert_file", conf.AdminConfig.TLS.CertFile),
zap.String("key_file", conf.AdminConfig.TLS.KeyFile),
)

Check warning on line 66 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L62-L66

Added lines #L62 - L66 were not covered by tests
if err := secureServer.ListenAndServeTLS(
conf.AdminConfig.TLS.CertFile,
conf.AdminConfig.TLS.KeyFile,
Expand All @@ -62,14 +76,13 @@
// Start Test Server
if conf.TestServerConfig != nil {
if !conf.Debug {
proxyState.Logger().Warn().
Msg("Test server is disabled in non-debug mode")
logger.Warn("Test server is disabled in non-debug mode")

Check warning on line 79 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L79

Added line #L79 was not covered by tests
} else {
go func() {
testHostPort := fmt.Sprintf("%s:%d",
conf.TestServerConfig.Host, conf.TestServerConfig.Port)
mux := chi.NewRouter()
mux.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {
testMux := chi.NewRouter()
testMux.HandleFunc("/*", func(w http.ResponseWriter, r *http.Request) {

Check warning on line 85 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L84-L85

Added lines #L84 - L85 were not covered by tests
if strings.HasPrefix(r.URL.Path, "/debug") {
// strip /debug prefix
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/debug")
Expand Down Expand Up @@ -106,14 +119,11 @@
util.JsonResponse(w, http.StatusOK, respMap)
})

testServerLogger := proxyState.Logger(
proxy.WithComponentLogger("test-server-http"),
proxy.WithDefaultLevel(zerolog.InfoLevel),
)
testServerLogger := logger.Named("test-server")

Check warning on line 122 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L122

Added line #L122 was not covered by tests
testServer := &http.Server{
Addr: testHostPort,
Handler: mux,
ErrorLog: log.New(testServerLogger, "", 0),
Handler: testMux,
ErrorLog: zap.NewStdLog(testServerLogger),

Check warning on line 126 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L125-L126

Added lines #L125 - L126 were not covered by tests
}
if conf.TestServerConfig.EnableHTTP2 {
h2Server := &http2.Server{}
Expand All @@ -122,34 +132,15 @@
panic(err)
}
if conf.TestServerConfig.EnableH2C {
testServer.Handler = h2c.NewHandler(mux, h2Server)
testServer.Handler = h2c.NewHandler(testServer.Handler, h2Server)

Check warning on line 135 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L135

Added line #L135 was not covered by tests
}
}
proxyState.Logger().Info().
Msgf("Starting test server on %s", testHostPort)
logger.Info("Starting test server on " + testHostPort)

Check warning on line 138 in internal/admin/admin_api.go

View check run for this annotation

Codecov / codecov/patch

internal/admin/admin_api.go#L138

Added line #L138 was not covered by tests

if err := testServer.ListenAndServe(); err != nil {
panic(err)
}
}()
}
}
go func() {
adminHttpLogger := proxyState.Logger(
proxy.WithComponentLogger("admin-http"),
proxy.WithDefaultLevel(zerolog.InfoLevel),
)
hostPort := fmt.Sprintf("%s:%d",
conf.AdminConfig.Host, conf.AdminConfig.Port)
proxyState.Logger().Info().
Msgf("Starting admin api on %s", hostPort)
server := &http.Server{
Addr: hostPort,
Handler: mux,
ErrorLog: log.New(adminHttpLogger, "", 0),
}
if err := server.ListenAndServe(); err != nil {
panic(err)
}
}()
}
Loading
Loading