codeskop

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 26 Imported by: 0

README

Codeskop for Go

Errors, panics, incoming requests and outgoing HTTP calls from your Go services, in Codeskop. Go 1.23+, standard library only (Gin and Echo integrations are separate packages).

go get github.com/Codeskop-io/codeskop-go@v0.1.1   # or @latest
codeskop.Init(codeskop.Options{APIKey: os.Getenv("CODESKOP_API_KEY")}) // cs_live_pk_… (public key)
defer codeskop.Close(2 * time.Second)

net/http

mux := http.NewServeMux()
mux.HandleFunc("GET /orders/{id}", getOrder)
http.ListenAndServe(":8080", codeskop.Middleware(mux)) // routes, panics (answered with 500), API Trust

Gin and Echo

r.Use(codeskopgin.Middleware())   // github.com/Codeskop-io/codeskop-go/integrations/gin (use instead of gin.Recovery())
e.Use(codeskopecho.Middleware())  // github.com/Codeskop-io/codeskop-go/integrations/echo

Errors, users and outgoing calls

codeskop.CaptureError(ctx, err, map[string]string{"provider": "stripe"})
codeskop.SetUser(r.Context(), user.ID)            // inside a wrapped handler
defer codeskop.Recover(ctx)                        // in your own goroutines
client := &http.Client{Transport: codeskop.Transport(http.DefaultTransport)}

Options

APIKey, Endpoint, Environment, Release (auto-detected from common CI/host variables), DisableRequests, DisableOutgoing, IgnoreRoutes, IgnoreErrors, BeforeSend, DisableUserID, Debug. Environment variables: CODESKOP_API_KEY, CODESKOP_ENDPOINT, CODESKOP_ENVIRONMENT, CODESKOP_RELEASE.

Never captured: request or response bodies, cookies, Authorization headers, query strings.

Releasing

Go modules are published by tagging: merge to main, update CHANGELOG.md and Version in client.go, then push a tag vX.Y.Z. The repository must be public for proxy.golang.org and pkg.go.dev to serve it; request indexing with GOPROXY=https://proxy.golang.org go list -m github.com/Codeskop-io/codeskop-go@vX.Y.Z.

License

MIT

Documentation

Overview

Package codeskop is the Codeskop server SDK for Go: errors, panics, incoming requests and outgoing HTTP calls (docs/10 in the backend repo).

codeskop.Init(codeskop.Options{APIKey: os.Getenv("CODESKOP_API_KEY")})
defer codeskop.Close(2 * time.Second)
http.ListenAndServe(":8080", codeskop.Middleware(mux))

Every function is safe to call before Init (it does nothing) and never panics.

Index

Constants

View Source
const BlockedBody = `{"error":"consumer_blocked"}`

BlockedBody is the JSON body sent to consumers blocked by API Trust.

View Source
const Version = "0.1.1"

Version of the SDK.

Variables

This section is empty.

Functions

func CaptureError

func CaptureError(ctx context.Context, err error, tags map[string]string)

CaptureError records an error with the stack of the caller.

func CaptureMessage

func CaptureMessage(ctx context.Context, message string)

CaptureMessage records a message as an issue.

func Close

func Close(timeout time.Duration)

Close flushes and stops the SDK; call it (or defer it) before main returns.

func Flush

func Flush(timeout time.Duration) bool

Flush sends everything queued, waiting up to timeout.

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware records every request (by ServeMux pattern on Go 1.23+), reports panics (answering 500) and handles API Trust. Wrap your mux: http.ListenAndServe(":8080", codeskop.Middleware(mux)).

func NormalizePath

func NormalizePath(path string) string

NormalizePath applies the server's templating: no query; numeric, UUID and long-hex segments become {id}.

func Recover

func Recover(ctx context.Context)

Recover reports a panic in the current goroutine and re-panics: `defer codeskop.Recover(ctx)`.

func SetFramework

func SetFramework(name string)

SetFramework names the framework on events (integrations call it).

func SetUser

func SetUser(ctx context.Context, userID string)

SetUser attributes the current request's events to userID (inside an integration-wrapped handler).

func TemplateRoute

func TemplateRoute(route string) string

TemplateRoute turns a router pattern into our {name} form: "GET /users/{id}", "/users/:id", "/files/{path...}".

func Transport

func Transport(base http.RoundTripper) http.RoundTripper

Transport wraps an http.RoundTripper so outgoing calls become api_timing / api_error events: client := &http.Client{Transport: codeskop.Transport(http.DefaultTransport)}.

func WithUser

func WithUser(ctx context.Context, userID string) context.Context

WithUser returns a context whose events are attributed to userID (your own ID, never an email). The HTTP integrations put a mutable holder in each request context, so SetUser works too.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client holds the queue and sends events in the background.

func Init

func Init(o Options) *Client

Init starts the SDK. Calling it again replaces the previous client.

func (*Client) Close

func (c *Client) Close(timeout time.Duration)

Close flushes and stops the background sender.

func (*Client) Flush

func (c *Client) Flush(timeout time.Duration) bool

Flush blocks until queued events are sent or the timeout passes.

func (*Client) Ready

func (c *Client) Ready(timeout time.Duration) bool

Ready blocks until the first remote config fetch finished (or the timeout passes).

type Event

type Event map[string]any

Event is one event in the ingest envelope (docs/10 §10.6).

type Options

type Options struct {
	APIKey           string
	Endpoint         string
	Environment      string
	Release          string
	DisableRequests  bool
	DisableOutgoing  bool
	SampleRates      map[string]float64
	IgnoreRoutes     []string
	IgnoreErrors     []string
	BeforeSend       func(Event) Event
	DisableUserID    bool
	MaxQueueEvents   int
	FlushInterval    time.Duration
	Debug            bool
	Disabled         bool
	APITrustResolver func(r *http.Request) string
	TrustProxy       *bool
}

Options configure the SDK (docs/10 §10.3). Zero values read CODESKOP_* environment variables.

type Recorder

type Recorder struct {
	Method    string
	RequestID string
	// contains filtered or unexported fields
}

Recorder tracks one incoming request; framework integrations (net/http, Gin, Echo) use it.

func StartRequest

func StartRequest(r *http.Request) (*Recorder, context.Context)

StartRequest begins recording r and returns the recorder plus a context carrying the request's user holder.

func (*Recorder) Error

func (rec *Recorder) Error(ctx context.Context, err error, route, mechanism string)

Error reports err as an unhandled error of this request.

func (*Recorder) Finish

func (rec *Recorder) Finish(route string, status int, reqBytes, resBytes int64)

Finish records the request (once).

func (*Recorder) Panic

func (rec *Recorder) Panic(ctx context.Context, p any, route, mechanism string)

Panic reports a recovered panic of this request.

func (*Recorder) Trust

func (rec *Recorder) Trust(r *http.Request) (blocked bool)

Trust runs API Trust capture; true means the request must be refused (opt-in blocking, fails open).

Directories

Path Synopsis
integrations
echo
Package codeskopecho integrates Codeskop with Echo: e.Use(codeskopecho.Middleware()).
Package codeskopecho integrates Codeskop with Echo: e.Use(codeskopecho.Middleware()).
gin
Package codeskopgin integrates Codeskop with Gin: router.Use(codeskopgin.Middleware()).
Package codeskopgin integrates Codeskop with Gin: router.Use(codeskopgin.Middleware()).
internal
mockingest
Package mockingest is a mock Codeskop ingest API for tests (docs/10 §10.10).
Package mockingest is a mock Codeskop ingest API for tests (docs/10 §10.10).

Jump to

Keyboard shortcuts

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