Skip to content

Commit

Permalink
ref: export memory stats to logger package
Browse files Browse the repository at this point in the history
  • Loading branch information
thoas committed Sep 25, 2023
1 parent 4281140 commit 1dd240d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
21 changes: 21 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package logger

import (
"context"
"fmt"
"log/slog"
"os"
"runtime"
"strings"
)

Expand Down Expand Up @@ -76,3 +78,22 @@ func New(cfg Config) *slog.Logger {
})

}

func LogMemStats(ctx context.Context, msg string, logger *slog.Logger) {
var m runtime.MemStats
runtime.ReadMemStats(&m)

attributes := []slog.Attr{
slog.String("alloc", fmt.Sprintf("%v MiB", bToMb(m.Alloc))),
slog.String("heap-alloc", fmt.Sprintf("%v MiB", bToMb(m.HeapAlloc))),
slog.String("total-alloc", fmt.Sprintf("%v MiB", bToMb(m.TotalAlloc))),
slog.String("sys", fmt.Sprintf("%v MiB", bToMb(m.Sys))),
slog.Int("numgc", int(m.NumGC)),
slog.Int("total-goroutine", runtime.NumGoroutine()),
}
logger.LogAttrs(ctx, slog.LevelInfo, msg, attributes...)
}

func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
24 changes: 2 additions & 22 deletions middleware/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package middleware

import (
"context"
"fmt"
"log/slog"
"runtime"
"time"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/thoas/picfit/config"
"github.com/thoas/picfit/constants"
loggerpkg "github.com/thoas/picfit/logger"
)

func NewLogger(cfg *config.Config, logger *slog.Logger) gin.HandlerFunc {
Expand Down Expand Up @@ -48,25 +47,6 @@ func NewLogger(cfg *config.Config, logger *slog.Logger) gin.HandlerFunc {
logger.LogAttrs(ctx, slog.LevelInfo, path, attributes...)
}

logMemStats(ctx, logger)
loggerpkg.LogMemStats(ctx, "Memory stats", logger)
}
}

func logMemStats(ctx context.Context, logger *slog.Logger) {
var m runtime.MemStats
runtime.ReadMemStats(&m)

attributes := []slog.Attr{
slog.String("alloc", fmt.Sprintf("%v MiB", bToMb(m.Alloc))),
slog.String("heap-alloc", fmt.Sprintf("%v MiB", bToMb(m.HeapAlloc))),
slog.String("total-alloc", fmt.Sprintf("%v MiB", bToMb(m.TotalAlloc))),
slog.String("sys", fmt.Sprintf("%v MiB", bToMb(m.Sys))),
slog.Int("numgc", int(m.NumGC)),
slog.Int("total-goroutine", runtime.NumGoroutine()),
}
logger.LogAttrs(ctx, slog.LevelInfo, "Memory stats", attributes...)
}

func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
9 changes: 5 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/thoas/picfit"
"github.com/thoas/picfit/config"
loggerpkg "github.com/thoas/picfit/logger"
)

func New(ctx context.Context, cfg *config.Config) (*HTTPServer, error) {
Expand Down Expand Up @@ -38,15 +39,15 @@ func Run(ctx context.Context, path string) error {
}
ctx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer stop()
if err := server.Run(ctx); err != nil {
return err
}

go func() {
for range time.Tick(time.Duration(cfg.Options.FreeMemoryInterval) * time.Second) {
loggerpkg.LogMemStats(ctx, "Force free memory", server.processor.Logger)
debug.FreeOSMemory()
}
}()
if err := server.Run(ctx); err != nil {
return err
}

select { // nolint:gosimple
case <-ctx.Done():
Expand Down

0 comments on commit 1dd240d

Please sign in to comment.