govisual

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 25, 2025 License: MIT Imports: 17 Imported by: 2

README

GoVisual

A lightweight, zero-configuration HTTP request visualizer and debugger for Go web applications during local development.

Features

  • Real-time Request Monitoring: Visualize HTTP requests passing through your application
  • Request Inspection: Deep inspection of headers, body, status codes, and timing information
  • Middleware Tracing: Visualize middleware execution flow and identify performance bottlenecks
  • Zero Configuration: Drop-in integration with standard Go HTTP handlers
  • OpenTelemetry Integration: Optional export of telemetry data to OpenTelemetry collectors

Installation

go get github.com/doganarif/govisual

Quick Start

package main

import (
    "net/http"
    "github.com/doganarif/govisual"
)

func main() {
    mux := http.NewServeMux()

    // Add your routes
    mux.HandleFunc("/api/users", userHandler)

    // Wrap with GoVisual
    handler := govisual.Wrap(
        mux,
        govisual.WithRequestBodyLogging(true),
        govisual.WithResponseBodyLogging(true),
    )

    http.ListenAndServe(":8080", handler)
}

Access the dashboard at http://localhost:8080/__viz

Documentation

For detailed documentation, please refer to the DOCS.

Configuration Options

handler := govisual.Wrap(
    mux,
    govisual.WithMaxRequests(100),              // Number of requests to store
    govisual.WithDashboardPath("/__dashboard"), // Custom dashboard path
    govisual.WithRequestBodyLogging(true),      // Log request bodies
    govisual.WithResponseBodyLogging(true),     // Log response bodies
    govisual.WithIgnorePaths("/health"),        // Paths to ignore
    govisual.WithOpenTelemetry(true),           // Enable OpenTelemetry
    govisual.WithServiceName("my-service"),     // Service name for OTel
    govisual.WithServiceVersion("1.0.0"),       // Service version
    govisual.WithOTelEndpoint("localhost:4317"), // OTLP endpoint

    // Storage options (choose one)
    govisual.WithMemoryStorage(),                // In-memory storage (default)
    govisual.WithPostgresStorage(               // PostgreSQL storage
        "postgres://user:password@localhost:5432/database?sslmode=disable",
        "govisual_requests"                     // Table name
    ),
    govisual.WithRedisStorage(                  // Redis storage
        "redis://localhost:6379/0",             // Redis connection string
        86400                                   // TTL in seconds (24 hours)
    ),
)

Storage Backends

GoVisual supports multiple storage backends for storing request logs:

In-Memory Storage (Default)

The default storage keeps all request logs in memory. This is the simplest option but logs will be lost when the application restarts.

handler := govisual.Wrap(
    mux,
    govisual.WithMemoryStorage(), // Optional, this is the default
)
PostgreSQL Storage

For persistent storage, you can use PostgreSQL. This requires the github.com/lib/pq package.

handler := govisual.Wrap(
    mux,
    govisual.WithPostgresStorage(
        "postgres://user:password@localhost:5432/dbname?sslmode=disable", // Connection string
        "govisual_requests"  // Table name (created automatically if it doesn't exist)
    ),
)
Redis Storage

For high-performance storage with automatic expiration, you can use Redis. This requires the github.com/go-redis/redis/v8 package.

handler := govisual.Wrap(
    mux,
    govisual.WithRedisStorage(
        "redis://localhost:6379/0", // Redis connection string
        86400                       // TTL in seconds (24 hours)
    ),
)

SQLite Driver Conflict

If you're already using a SQLite driver in your application (such as github.com/mattn/go-sqlite3), you may experience a conflict when using govisual with SQLite storage:

panic: sql: Register called twice for driver sqlite3

This occurs because both your application and govisual try to register the SQLite driver with the same name.

To resolve this issue, you can pass your existing database connection to govisual:

import (
    "database/sql"

    "github.com/doganarif/govisual"
    _ "github.com/mattn/go-sqlite3" // Your preferred SQLite driver
)

func main() {
    // Create your own database connection
    db, err := sql.Open("sqlite3", "path/to/your/database.db")
    if err != nil {
        // Handle error
    }
    defer db.Close()

    // Pass the existing connection to govisual
    app := gin.New()
    visualHandler := govisual.Wrap(
        app,
        govisual.WithSQLiteStorageDB(db, "govisual_requests"),
    )

    // Use visualHandler as your main handler
    http.ListenAndServe(":8080", visualHandler)
}

This approach allows you to:

  1. Use your preferred SQLite driver
  2. Avoid driver registration conflicts
  3. Reuse your existing connection

Examples

Basic Example

Simple example showing core functionalities:

cd cmd/examples/basic
go run main.go
OpenTelemetry Example

Example showing integration with OpenTelemetry:

cd cmd/examples/otel
docker-compose up -d  # Start Jaeger
go run main.go
Multi-Storage Example

Example showing different storage backends:

cd cmd/examples/multistorage
docker-compose up -d  # Start PostgreSQL and Redis

Modify the environment variables in docker-compose.yml to switch between storage backends.

Visit Multi-Storage Example for detailed instructions.

Dashboard Features

GoVisual Dashboard

  • Request Table: View all captured HTTP requests with method, path, status code, and response time
  • Request Details: One-click access to headers, body content, and timing information
  • Middleware Trace: Interactive visualization of middleware execution flow
  • Request Filtering: Filter by HTTP method, status code, path pattern, or duration
  • Real-time Updates: See new requests appear instantly as they happen

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap

func Wrap(handler http.Handler, opts ...Option) http.Handler

Wrap wraps an http.Handler with request visualization middleware

Types

type Config

type Config struct {
	MaxRequests int

	DashboardPath string

	LogRequestBody bool

	LogResponseBody 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

	// Performance Profiling configuration
	EnableProfiling bool

	ProfileType profiling.ProfileType

	ProfileThreshold time.Duration

	MaxProfileMetrics int
}

func (*Config) ShouldIgnorePath

func (c *Config) ShouldIgnorePath(path string) bool

ShouldIgnorePath checks if a path should be ignored based on the configured patterns ShouldIgnorePath checks if a path should be ignored based on the configured patterns

type Option

type Option func(*Config)

Option is a function that modifies the configuration

func WithDashboardPath

func WithDashboardPath(path string) Option

WithDashboardPath sets the path to access the dashboard

func WithIgnorePaths

func WithIgnorePaths(patterns ...string) Option

WithIgnorePaths sets the path patterns to ignore

func WithMaxProfileMetrics added in v0.2.0

func WithMaxProfileMetrics(max int) Option

WithMaxProfileMetrics sets the maximum number of profile metrics to store

func WithMaxRequests

func WithMaxRequests(max int) Option

WithMaxRequests sets the maximum number of requests to store

func WithMemoryStorage added in v0.1.4

func WithMemoryStorage() Option

WithMemoryStorage configures the application to use in-memory storage

func WithMongoDBStorage added in v0.1.9

func WithMongoDBStorage(uri, databaseName, collectionName string) Option

WithMongoDBStorage configures the application to use MongoDB storage

func WithOTelEndpoint added in v0.1.3

func WithOTelEndpoint(endpoint string) Option

WithOTelEndpoint sets the OTLP endpoint for exporting telemetry data

func WithOpenTelemetry added in v0.1.3

func WithOpenTelemetry(enabled bool) Option

WithOpenTelemetry enables or disables OpenTelemetry instrumentation

func WithPostgresStorage added in v0.1.4

func WithPostgresStorage(connStr string, tableName string) Option

WithPostgresStorage configures the application to use PostgreSQL storage

func WithProfileThreshold added in v0.2.0

func WithProfileThreshold(threshold time.Duration) Option

WithProfileThreshold sets the minimum duration to trigger profiling

func WithProfileType added in v0.2.0

func WithProfileType(profileType profiling.ProfileType) Option

WithProfileType sets the types of profiling to perform

func WithProfiling added in v0.2.0

func WithProfiling(enabled bool) Option

WithProfiling enables or disables performance profiling

func WithRedisStorage added in v0.1.4

func WithRedisStorage(connStr string, ttlSeconds int) Option

WithRedisStorage configures the application to use Redis storage

func WithRequestBodyLogging

func WithRequestBodyLogging(enabled bool) Option

WithRequestBodyLogging enables or disables request body logging

func WithResponseBodyLogging

func WithResponseBodyLogging(enabled bool) Option

WithResponseBodyLogging enables or disables response body logging

func WithSQLiteStorage added in v0.1.5

func WithSQLiteStorage(dbPath string, tableName string) Option

WithSQLiteStorage configures the application to use SQLite storage

func WithSQLiteStorageDB added in v0.1.8

func WithSQLiteStorageDB(db *sql.DB, tableName string) Option

WithSQLiteStorageDB configures the application to use SQLite storage with an existing database connection

func WithServiceName added in v0.1.3

func WithServiceName(name string) Option

WithServiceName sets the service name for OpenTelemetry

func WithServiceVersion added in v0.1.3

func WithServiceVersion(version string) Option

WithServiceVersion sets the service version for OpenTelemetry

Directories

Path Synopsis
cmd
examples/basic command
examples/otel command
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL