Skip to content

Commit

Permalink
feat: access logs in aws-lambda-router (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-braund-cabiri authored Jan 30, 2025
1 parent 3838b9b commit 08cb5a8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions aws-lambda-router/internal/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package internal
import (
"context"
"fmt"
"os"

"github.com/wundergraph/cosmo/router/core"
"github.com/wundergraph/cosmo/router/pkg/authentication"
"github.com/wundergraph/cosmo/router/pkg/config"
"github.com/wundergraph/cosmo/router/pkg/cors"
"github.com/wundergraph/cosmo/router/pkg/execution_config"
"github.com/wundergraph/cosmo/router/pkg/logging"
"github.com/wundergraph/cosmo/router/pkg/metric"
"github.com/wundergraph/cosmo/router/pkg/trace"
"go.uber.org/zap"
Expand Down Expand Up @@ -146,6 +148,57 @@ func NewRouter(opts ...Option) (*core.Router, error) {
core.WithCacheWarmupConfig(&cfg.CacheWarmup),
)

if cfg.AccessLogs.Enabled {
c := &core.AccessLogsConfig{
Attributes: cfg.AccessLogs.Router.Fields,
SubgraphEnabled: cfg.AccessLogs.Subgraphs.Enabled,
SubgraphAttributes: cfg.AccessLogs.Subgraphs.Fields,
}

if cfg.AccessLogs.Output.File.Enabled {
f, err := logging.NewLogFile(cfg.AccessLogs.Output.File.Path)
if err != nil {
return nil, fmt.Errorf("could not create log file: %w", err)
}
if cfg.AccessLogs.Buffer.Enabled {
bl, err := logging.NewJSONZapBufferedLogger(logging.BufferedLoggerOptions{
WS: f,
BufferSize: int(cfg.AccessLogs.Buffer.Size.Uint64()),
FlushInterval: cfg.AccessLogs.Buffer.FlushInterval,
Development: cfg.DevelopmentMode,
Level: zap.InfoLevel,
Pretty: !cfg.JSONLog,
})
if err != nil {
return nil, fmt.Errorf("could not create buffered logger: %w", err)
}
c.Logger = bl.Logger
} else {
c.Logger = logging.NewZapAccessLogger(f, cfg.DevelopmentMode, !cfg.JSONLog)
}
} else if cfg.AccessLogs.Output.Stdout.Enabled {

if cfg.AccessLogs.Buffer.Enabled {
bl, err := logging.NewJSONZapBufferedLogger(logging.BufferedLoggerOptions{
WS: os.Stdout,
BufferSize: int(cfg.AccessLogs.Buffer.Size.Uint64()),
FlushInterval: cfg.AccessLogs.Buffer.FlushInterval,
Development: cfg.DevelopmentMode,
Level: zap.InfoLevel,
Pretty: !cfg.JSONLog,
})
if err != nil {
return nil, fmt.Errorf("could not create buffered logger: %w", err)
}
c.Logger = bl.Logger
} else {
c.Logger = logging.NewZapAccessLogger(os.Stdout, cfg.DevelopmentMode, !cfg.JSONLog)
}
}

routerOpts = append(routerOpts, core.WithAccessLogs(c))
}

var authenticators []authentication.Authenticator
for i, auth := range cfg.Authentication.Providers {
if auth.JWKS != nil {
Expand Down
2 changes: 1 addition & 1 deletion aws-lambda-router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-lambda-router",
"version": "0.4.0",
"version": "0.5.0",
"private": true,
"description": "Placeholder package to simplify versioning and releasing with lerna.",
"keywords": [
Expand Down

0 comments on commit 08cb5a8

Please sign in to comment.