kohi

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 27 Imported by: 0

README

kohi-go

Go SDK for Kohi.

Install

go get github.com/kohicorp/kohi-go

Quick Start

monitor, err := kohi.New(kohi.Config{
    ProjectKey: os.Getenv("KOHI_PROJECT_KEY"),
    SecretKey:  os.Getenv("KOHI_SECRET_KEY"),
})
if err != nil {
    log.Fatal(err)
}
defer monitor.Close()

http.ListenAndServe(":8080", kohi.NetHTTPMiddleware(monitor)(mux))

kohi.FastHTTPMiddleware is also available for fasthttp.

Behavior

  • Buffers events in memory and flushes on batch size or interval
  • Retries retryable 408, 429, and 5xx responses with backoff
  • Drops events when the queue is full instead of blocking request handlers
  • Redacts common secret fields and masks email addresses before send
  • Captures request/response metadata for net/http and fasthttp

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalHeaderMap

func CanonicalHeaderMap(headers map[string]string) map[string]string

func CanonicalHeaders

func CanonicalHeaders(h http.Header) map[string]string

func DecodeResponseBody

func DecodeResponseBody(raw []byte, headers map[string]string) any

func FastHTTPMiddleware

func FastHTTPMiddleware(monitor *Monitor) func(fasthttp.RequestHandler) fasthttp.RequestHandler

func GzipBatch added in v0.1.3

func GzipBatch(events []Event) ([]byte, error)

func IsRetryableError

func IsRetryableError(err error) bool

func NetHTTPMiddleware

func NetHTTPMiddleware(monitor *Monitor) func(http.Handler) http.Handler

func ParseJSONBody

func ParseJSONBody(raw []byte) any

func RedactValue

func RedactValue(value any) any

func Sign

func Sign(secret, body []byte) string

func ValidateConfig

func ValidateConfig(projectKey, secretKey string) error

func VersionHeaderValue

func VersionHeaderValue() string

Types

type Config

type Config struct {
	ProjectKey         string
	SecretKey          string
	Endpoint           string
	Enabled            *bool
	MaxConcurrentSends int
	QueueSize          int
	BatchSize          int
	FlushInterval      time.Duration
	HTTPClient         *http.Client
	Logger             *log.Logger
}

func (Config) String

func (c Config) String() string

type Event

type Event struct {
	URL             string            `json:"url"`
	Endpoint        string            `json:"endpoint"`
	Method          string            `json:"method"`
	StatusCode      int               `json:"status_code"`
	RequestHeaders  map[string]string `json:"request_headers"`
	RequestBody     any               `json:"request_body"`
	ResponseHeaders map[string]string `json:"response_headers"`
	ResponseBody    any               `json:"response_body"`
	DurationMS      int64             `json:"duration_ms"`
	ClientIP        string            `json:"client_ip,omitempty"`
}

func RedactEvent

func RedactEvent(evt Event) Event

type Monitor

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

func New

func New(cfg Config) (*Monitor, error)

func NewNoop

func NewNoop() *Monitor

func (*Monitor) AddEvent

func (m *Monitor) AddEvent(evt Event)

func (*Monitor) Close

func (m *Monitor) Close() error

func (*Monitor) Enabled

func (m *Monitor) Enabled() bool

func (*Monitor) Shutdown

func (m *Monitor) Shutdown(ctx context.Context) error

type ResponseCapture

type ResponseCapture struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

func NewResponseCapture

func NewResponseCapture(w http.ResponseWriter) *ResponseCapture

func (*ResponseCapture) EnsureStatus

func (rw *ResponseCapture) EnsureStatus(code int)

func (*ResponseCapture) Flush

func (rw *ResponseCapture) Flush()

func (*ResponseCapture) Hijack

func (rw *ResponseCapture) Hijack() (net.Conn, *bufio.ReadWriter, error)

func (*ResponseCapture) StatusCode

func (rw *ResponseCapture) StatusCode() int

func (*ResponseCapture) Write

func (rw *ResponseCapture) Write(b []byte) (int, error)

func (*ResponseCapture) WriteHeader

func (rw *ResponseCapture) WriteHeader(code int)

Jump to

Keyboard shortcuts

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