Complete Go SDK for monitoring AI agents with Trusera's Cedar-based policy engine.
Repository: github.com/Trusera/trusera-sdk-go
- Zero external dependencies (stdlib only)
- HTTP client interception for monitoring outbound requests
- Three enforcement modes: log, warn, block
- Background event batching and flushing
- Thread-safe concurrent operations
- 85.4% test coverage
- Comprehensive examples
Main client implementation:
Clientstruct with mutex-protected event queue- Background flusher goroutine
- Option pattern for configuration
NewClient(),Track(),Flush(),RegisterAgent(),Close()- Default values: 30s flush interval, 100 event batch size
Event types and creation:
- 6 event types:
tool_call,llm_invoke,data_access,api_call,file_write,decision Eventstruct with ID, Type, Name, Payload, Metadata, TimestampNewEvent()with auto-generated ID (crypto/rand hex)- Builder pattern:
WithPayload(),WithMetadata()
HTTP interception logic:
EnforcementMode: Log, Warn, BlockInterceptorOptionswith exclude/block patternsWrapHTTPClient()- wraps http.Client with interceptioninterceptingTransportimplements http.RoundTripper- Header sanitization (redacts Authorization, Cookie, X-Api-Key)
- Request body capture (500 byte limit)
- Convenience helpers:
CreateInterceptedClient(),InterceptDefault(),MustRegisterAndIntercept()
- Client creation and options
- Event tracking and flushing
- Agent registration
- Background flusher
- Auto-flush on batch size
- Thread safety
- Event creation and uniqueness
- Builder pattern
- JSON serialization
- All event types
- HTTP interception
- All enforcement modes
- Exclude/block patterns
- Header sanitization
- Concurrent requests
- Body capture
Test Results: All 19 tests pass, 85.4% coverage
- Client creation
- Tracking tool calls, LLM invocations, data access, decisions
- Manual flushing
- HTTP client wrapping
- Warn mode enforcement
- Exclude/block patterns
- POST requests
- Block mode enforcement
- MustRegisterAndIntercept helper
- Error handling for blocked requests
module github.com/Trusera/trusera-sdk-go
go 1.21
Zero external dependencies.
Enabled linters: errcheck, gosimple, govet, ineffassign, staticcheck, unused, gofmt, goimports, misspell, unconvert, goconst, gocyclo, dupl
- Test on Go 1.21, 1.22, 1.23
- Lint with golangci-lint
- Build verification
- Security scan with gosec
- Coverage upload to Codecov
- Installation
- Quickstart (3 lines)
- HTTP interceptor examples
- Enforcement modes
- Event types
- Configuration options
- API reference
- Thread safety
- 5-minute guide
- Basic tracking example
- HTTP interception example
- Block mode example
- Development setup
- Testing guidelines
- Code style
- PR process
- Project structure
- Version history
- Feature list
- Release notes
func NewClient(apiKey string, opts ...Option) *Client
func (c *Client) Track(event Event)
func (c *Client) Flush() error
func (c *Client) RegisterAgent(name, framework string) (string, error)
func (c *Client) Close() errorfunc WithBaseURL(url string) Option
func WithAgentID(id string) Option
func WithFlushInterval(d time.Duration) Option
func WithBatchSize(n int) Optionfunc NewEvent(eventType EventType, name string) Event
func (e Event) WithPayload(key string, value any) Event
func (e Event) WithMetadata(key string, value any) Eventfunc WrapHTTPClient(client *http.Client, truseraClient *Client, opts InterceptorOptions) *http.Client
func CreateInterceptedClient(truseraClient *Client, opts InterceptorOptions) *http.Client
func InterceptDefault(truseraClient *Client, opts InterceptorOptions)
func MustRegisterAndIntercept(apiKey, agentName, framework string, opts InterceptorOptions) (*Client, *http.Client, error)All checks passing:
- ✓
go test ./...- 19/19 tests pass - ✓
go test -cover ./...- 85.4% coverage - ✓
go fmt ./...- formatted - ✓
go vet ./...- no issues - ✓
go build ./...- compiles - ✓ All examples compile
- ✓ Zero external dependencies
- ✓ Thread-safe operations
| Category | Files | Lines |
|---|---|---|
| Core | 3 | 490 |
| Tests | 3 | 1570 |
| Examples | 3 | ~300 |
| Docs | 5 | ~1000 |
| Config | 3 | ~100 |
| Total | 17 | ~3500 |
// Create and register
client, httpClient, _ := trusera.MustRegisterAndIntercept(
"api-key",
"my-agent",
"langchain",
trusera.InterceptorOptions{
Enforcement: trusera.ModeBlock,
BlockPatterns: []string{"malicious.com"},
},
)
defer client.Close()
// Track events
client.Track(trusera.NewEvent(trusera.EventToolCall, "search").
WithPayload("query", "AI security"))
// HTTP requests auto-monitored
httpClient.Get("https://api.example.com")- Publish to GitHub:
github.com/Trusera/trusera-sdk-go - Tag v0.1.0 release
- Submit to pkg.go.dev
- Add to Trusera docs
- Create integration examples with popular frameworks