Go

Go Audit Logging Guide

Integrate AuditKit into your Go application with the official Go SDK. Zero-allocation hot path, context-aware logging, and native support for middleware patterns.

Overview

Go is the language of choice for high-performance backend systems, infrastructure tools, and cloud-native applications. The AuditKit Go SDK is designed for performance-critical applications with zero-allocation logging on the hot path, context propagation through context.Context, and native support for net/http middleware patterns. The SDK works with any Go HTTP router including chi, gorilla/mux, gin, and the standard library.

Getting started

1

Install the SDK

Add the AuditKit Go module.

Go
go get github.com/auditkit/auditkit-go
2

Initialize the client

Create an AuditKit client with your configuration.

Go
import "github.com/auditkit/auditkit-go"

client, err := auditkit.New(auditkit.Config{
    APIKey:   os.Getenv("AUDITKIT_API_KEY"),
    TenantID: "org_123",
})
if err != nil {
    log.Fatal(err)
}
defer client.Close()
3

Log events

Capture audit events with strongly typed structs.

Go
err := client.Log(ctx, auditkit.Event{
    Action: "user.role_changed",
    Actor: auditkit.Actor{
        ID:    userID,
        Email: user.Email,
    },
    Target: auditkit.Target{
        Type: "user",
        ID:   targetUserID,
    },
    Metadata: map[string]any{
        "previous_role": previousRole,
        "new_role":      newRole,
    },
})
4

Add HTTP middleware

Use the middleware for automatic request logging.

Go
import "github.com/auditkit/auditkit-go/middleware"

mux := http.NewServeMux()
handler := middleware.AuditLog(client)(mux)
http.ListenAndServe(":8080", handler)

Complete example

Here is a complete example showing AuditKit integrated into a Go application with authentication logging, data access tracking, and explicit event capture.

Go
package main

import (
    "log"
    "net/http"
    "os"

    "github.com/auditkit/auditkit-go"
    "github.com/auditkit/auditkit-go/middleware"
)

func main() {
    client, err := auditkit.New(auditkit.Config{
        APIKey:   os.Getenv("AUDITKIT_API_KEY"),
        TenantID: "org_123",
    })
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    mux := http.NewServeMux()

    mux.HandleFunc("POST /api/users/{id}/role", func(w http.ResponseWriter, r *http.Request) {
        userID := r.PathValue("id")
        user := auth.UserFromContext(r.Context())

        previousRole, _ := db.GetUserRole(r.Context(), userID)
        newRole := r.FormValue("role")
        _ = db.UpdateRole(r.Context(), userID, newRole)

        _ = client.Log(r.Context(), auditkit.Event{
            Action: "user.role_changed",
            Actor: auditkit.Actor{
                ID:    user.ID,
                Email: user.Email,
            },
            Target: auditkit.Target{
                Type: "user",
                ID:   userID,
            },
            Metadata: map[string]any{
                "previous_role": previousRole,
                "new_role":      newRole,
                "ip_address":    r.RemoteAddr,
            },
        })

        w.WriteHeader(http.StatusOK)
    })

    // Wrap with audit logging middleware
    handler := middleware.AuditLog(client)(mux)
    log.Fatal(http.ListenAndServe(":8080", handler))
}

Common patterns

net/http middleware for automatic request logging

context.Context propagation for tenant and actor resolution

GORM hooks for automatic database change logging

gRPC interceptors for service-to-service audit trails

Buffered async logging for high-throughput systems

Graceful shutdown with pending event flush

Frequently asked questions

How do I add audit logging to a Go application?

Install auditkit-go, create a client with auditkit.New(), and call client.Log() with structured Event objects. Use the middleware package for automatic HTTP request logging with any Go router.

Does the Go SDK support buffered/async logging?

Yes. The AuditKit Go SDK supports buffered async logging for high-throughput applications. Events are batched and sent in the background with configurable buffer size and flush intervals. Call client.Close() to flush pending events on shutdown.

More integration guides

Related resources

Add audit logging to your Go app

Get started with tamper-proof audit trails in minutes. Open source, from $99/mo.