Skip to content
Open
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ _testmain.go
/test/garbage/*.out
/test/pass.out
/test/run.out
/test/times.out
/test/times.out

# ignore the vendor directory
vendor
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ handler := govisual.Wrap(
govisual.WithDashboardPath("/__dashboard"), // Custom dashboard path
govisual.WithRequestBodyLogging(true), // Log request bodies
govisual.WithResponseBodyLogging(true), // Log response bodies
govisual.WithConsoleLogging(true), // Log request timing to TTY
govisual.WithIgnorePaths("/health"), // Paths to ignore
govisual.WithOpenTelemetry(true), // Enable OpenTelemetry
govisual.WithServiceName("my-service"), // Service name for OTel
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/golang/snappy v1.0.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGC
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A=
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
Expand Down Expand Up @@ -91,6 +93,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
18 changes: 14 additions & 4 deletions internal/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"time"

"github.com/doganarif/govisual/internal/model"
"github.com/doganarif/govisual/internal/options"
"github.com/doganarif/govisual/internal/store"
"github.com/doganarif/govisual/internal/utils"
)

// PathMatcher defines an interface for checking if a path should be ignored
Expand Down Expand Up @@ -39,7 +41,10 @@ func (w *responseWriter) Write(b []byte) (int, error) {
}

// Wrap wraps an http.Handler with the request visualization middleware
func Wrap(handler http.Handler, store store.Store, logRequestBody, logResponseBody bool, pathMatcher PathMatcher) http.Handler {
func Wrap(handler http.Handler, store store.Store, config *options.Config, pathMatcher PathMatcher) http.Handler {

logger := utils.NewLogger()

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if the path should be ignored
if pathMatcher != nil && pathMatcher.ShouldIgnorePath(r.URL.Path) {
Expand All @@ -51,7 +56,7 @@ func Wrap(handler http.Handler, store store.Store, logRequestBody, logResponseBo
reqLog := model.NewRequestLog(r)

// Capture request body if enabled
if logRequestBody && r.Body != nil {
if config.LogRequestBody && r.Body != nil {
// Read the body
bodyBytes, _ := io.ReadAll(r.Body)
r.Body.Close()
Expand All @@ -65,7 +70,7 @@ func Wrap(handler http.Handler, store store.Store, logRequestBody, logResponseBo

// Create response writer wrapper
var resWriter *responseWriter
if logResponseBody {
if config.LogResponseBody {
resWriter = &responseWriter{
ResponseWriter: w,
statusCode: 200, // Default status code
Expand Down Expand Up @@ -111,10 +116,15 @@ func Wrap(handler http.Handler, store store.Store, logRequestBody, logResponseBo
}

// Capture response body if enabled
if logResponseBody && resWriter.buffer != nil {
if config.LogRequestBody && resWriter.buffer != nil {
reqLog.ResponseBody = resWriter.buffer.String()
}

// Log to console if enabled
if config.LogRequestToConsole {
logger.LogRequest(reqLog)
}

// Store the request log
store.Add(reqLog)
})
Expand Down
9 changes: 8 additions & 1 deletion internal/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/doganarif/govisual/internal/model"
"github.com/doganarif/govisual/internal/options"
)

// mockStore implements store.Store for testing
Expand Down Expand Up @@ -61,7 +62,13 @@ func TestWrapMiddleware(t *testing.T) {
w.Write([]byte("hello world"))
})

wrapped := Wrap(handler, store, true, true, &mockPathMatcher{})
config := &options.Config{
LogRequestBody: true,
LogResponseBody: true,
LogRequestToConsole: false,
}

wrapped := Wrap(handler, store, config, &mockPathMatcher{})

req := httptest.NewRequest("POST", "/test?x=1", strings.NewReader("sample-body"))
req.Header.Set("X-Test", "test")
Expand Down
73 changes: 73 additions & 0 deletions internal/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package options

import (
"database/sql"
"path/filepath"
"strings"

"github.com/doganarif/govisual/internal/store"
)

type Config struct {
MaxRequests int

DashboardPath string

LogRequestBody bool

LogResponseBody bool

LogRequestToConsole bool

IgnorePaths []string

// OpenTelemetry configuration
EnableOpenTelemetry bool

ServiceName string

ServiceVersion string

OTelEndpoint string

// Storage configuration
StorageType store.StorageType

// Connection string for database stores
ConnectionString string

// TableName for SQL database stores
TableName string

// TTL for Redis store in seconds
RedisTTL int

// Existing database connection for SQLite
ExistingDB *sql.DB
}

// ShouldIgnorePath checks if a path should be ignored based on the configured patterns
func (c *Config) ShouldIgnorePath(path string) bool {
// First check if it's the dashboard path which should always be ignored to prevent recursive logging
if path == c.DashboardPath || strings.HasPrefix(path, c.DashboardPath+"/") {
return true
}

// Then check against provided ignore patterns
for _, pattern := range c.IgnorePaths {
matched, err := filepath.Match(pattern, path)
if err == nil && matched {
return true
}

// Special handling for path groups with trailing slash
if len(pattern) > 0 && pattern[len(pattern)-1] == '/' {
// If pattern ends with /, check if path starts with pattern
if len(path) >= len(pattern) && path[:len(pattern)] == pattern {
return true
}
}
}

return false
}
135 changes: 135 additions & 0 deletions internal/utils/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package utils

import (
"fmt"
"net/http"
"os"
"time"

"github.com/doganarif/govisual/internal/model"
"github.com/mattn/go-isatty"
)

var (
green = "\033[32m"
white = "\033[37m"
red = "\033[31m"
blue = "\033[34m"
yellow = "\033[33m"
gray = "\033[90m"
black = "\033[30m"
magenta = "\033[35m"
cyan = "\033[36m"
reset = "\033[0m"
)

type Logger struct{}

func init() {
// Check if the output target is a terminal
// If not, disable color codes
if !isatty.IsTerminal(os.Stdout.Fd()) || !isatty.IsTerminal(os.Stderr.Fd()) {
green = ""
white = ""
red = ""
blue = ""
yellow = ""
gray = ""
black = ""
magenta = ""
cyan = ""
reset = ""
}
}

func NewLogger() *Logger {
// Create a new logger instance
return &Logger{}
}

func colorizeMethod(method string) string {
if method == "" {
return ""
}

var color string
switch method {
case http.MethodGet:
color = blue
case http.MethodPost:
color = green
case http.MethodPut:
color = yellow
case http.MethodDelete:
color = red
case http.MethodPatch:
color = magenta
case http.MethodHead:
color = gray
case http.MethodOptions:
color = cyan
case http.MethodTrace:
color = white
default:
color = black
}

return fmt.Sprintf("[%s%-7s%s]", color, method, reset)
}

func colorizeStatus(status int) string {
if status < 100 || status > 599 {
return fmt.Sprintf("[%s%3d%s]", red, status, reset)
}

var color string
switch {
case status >= http.StatusContinue && status < http.StatusOK:
color = gray
case status >= http.StatusOK && status < http.StatusMultipleChoices:
color = green
case status >= http.StatusMultipleChoices && status < http.StatusBadRequest:
color = blue
case status >= http.StatusBadRequest && status < http.StatusInternalServerError:
color = yellow
default:
color = red
}

return fmt.Sprintf("[%s%3d%s]", color, status, reset)
}

func colorizeDuration(duration time.Duration) string {
if duration < 0 {
return fmt.Sprintf("%s%13v%s", red, duration, reset)
}

var color string
switch {
case duration < 500*time.Millisecond:
color = green
case duration < 1*time.Second:
color = yellow
default:
color = red
}

return fmt.Sprintf("%s%13v%s", color, duration, reset)
}

func (logger *Logger) LogRequest(reqLog *model.RequestLog) {
// This function logs the request details based on the configuration
if reqLog == nil {
fmt.Println("Warning: Attempted to log nil request log, ignoring")
return
}

fmt.Printf(
"[VIZ] %v %s%s %s %#v\n",
reqLog.Timestamp.Format("2006-01-02 15:04:05"),
colorizeMethod(reqLog.Method),
colorizeStatus(reqLog.StatusCode),
colorizeDuration(time.Since(reqLog.Timestamp)),
reqLog.Path,
)
}
Loading