client

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 10 Imported by: 0

README

eventrelay Go SDK

Go client for sending events to an eventrelay server.

Install

go get github.com/dmoose/eventrelay/client

Usage

import "github.com/dmoose/eventrelay/client"

c := client.New("http://localhost:6060/events", "myapp")

c.Emit("deploy", map[string]any{"env": "prod"})
c.EmitError("crash", map[string]any{"msg": "something broke"})
c.EmitWarn("high_latency", map[string]any{"p99": 450})
c.EmitDebug("cache_miss", nil)

c.Flush() // wait for pending events before exit
Channels
deploy := c.WithChannel("deploy")
deploy.Emit("started", map[string]any{"branch": "main"})
Timed Operations
done := c.Timed("db_query", nil)
rows := doQuery()
done(map[string]any{"rows": len(rows)})
slog Integration

Route Go structured logging to eventrelay:

handler := client.NewSlogHandler(c, "logs")
logger := slog.New(handler)
logger.Info("request handled", "path", "/api/users", "status", 200)
No-Op Mode

When the URL is empty, all operations silently no-op. This lets you embed instrumentation without conditional checks:

c := client.New(os.Getenv("EVENTRELAY_URL"), "myapp")
c.Emit("startup", nil) // safe even if EVENTRELAY_URL is unset

Documentation

Overview

Package client provides a Go SDK for sending events to an eventrelay server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client sends events to an eventrelay server. All methods are safe for concurrent use. If URL is empty, all operations are no-ops.

func New

func New(url, source string) *Client

New creates a Client. If url is empty, returns a no-op client.

func (*Client) Emit

func (c *Client) Emit(action string, data map[string]any)

Emit sends an info-level event. Fire-and-forget — non-blocking, errors silently dropped.

func (*Client) EmitDebug

func (c *Client) EmitDebug(action string, data map[string]any)

EmitDebug sends a debug-level event.

func (*Client) EmitError

func (c *Client) EmitError(action string, data map[string]any)

EmitError sends an error-level event.

func (*Client) EmitWarn

func (c *Client) EmitWarn(action string, data map[string]any)

EmitWarn sends a warn-level event.

func (*Client) EmitWith

func (c *Client) EmitWith(evt Event)

EmitWith sends a fully customized event.

func (*Client) Flush

func (c *Client) Flush()

Flush waits for all pending events to be sent.

func (*Client) Log added in v1.1.0

func (c *Client) Log(level, message string, fields map[string]any)

Log sends a structured log entry to the /log endpoint. Fire-and-forget.

func (*Client) LogDebug added in v1.1.0

func (c *Client) LogDebug(message string, fields map[string]any)

LogDebug sends a debug-level log entry.

func (*Client) LogError added in v1.1.0

func (c *Client) LogError(message string, fields map[string]any)

LogError sends an error-level log entry.

func (*Client) LogInfo added in v1.1.0

func (c *Client) LogInfo(message string, fields map[string]any)

LogInfo sends an info-level log entry.

func (*Client) LogWarn added in v1.1.0

func (c *Client) LogWarn(message string, fields map[string]any)

LogWarn sends a warn-level log entry.

func (*Client) Timed

func (c *Client) Timed(action string, data map[string]any) func(map[string]any)

Timed is a helper for timing operations. Returns a function that emits the event with duration_ms set when called.

done := client.Timed("db_query", nil)
// ... do work ...
done(map[string]any{"rows": 42})

func (*Client) WithChannel

func (c *Client) WithChannel(channel string) *Client

WithChannel returns a new Client that tags all events with the given channel.

type Event

type Event struct {
	Source     string         `json:"source"`
	Channel    string         `json:"channel,omitempty"`
	Action     string         `json:"action,omitempty"`
	Level      string         `json:"level,omitempty"`
	AgentID    string         `json:"agent_id,omitempty"`
	DurationMS *int64         `json:"duration_ms,omitempty"`
	Data       map[string]any `json:"data,omitempty"`
	TS         time.Time      `json:"ts"`
}

Event is the payload sent to the relay.

type LogEntry added in v1.1.0

type LogEntry struct {
	Level   string         `json:"level"`
	Message string         `json:"message"`
	Logger  string         `json:"logger,omitempty"`
	Fields  map[string]any `json:"fields,omitempty"`
	Caller  string         `json:"caller,omitempty"`
	TS      time.Time      `json:"ts"`
}

LogEntry is the payload sent to the /log endpoint.

type SlogHandler

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

SlogHandler implements slog.Handler, sending structured log records as events. Use with slog.New(eventrelay.NewSlogHandler(client, "logs")) to route all structured logging to the event relay as events on a channel.

func NewSlogHandler

func NewSlogHandler(c *Client, channel string) *SlogHandler

NewSlogHandler creates a slog.Handler that sends log records as events.

func (*SlogHandler) Enabled

func (h *SlogHandler) Enabled(_ context.Context, _ slog.Level) bool

func (*SlogHandler) Handle

func (h *SlogHandler) Handle(_ context.Context, r slog.Record) error

func (*SlogHandler) WithAttrs

func (h *SlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SlogHandler) WithGroup

func (h *SlogHandler) WithGroup(name string) slog.Handler

type SlogLogHandler added in v1.1.0

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

SlogLogHandler implements slog.Handler, sending records to the /log endpoint as structured log entries (message, level, logger, fields, caller). Use with slog.New(eventrelay.NewSlogLogHandler(client)) for first-class log integration with the eventrelay Logs tab.

func NewSlogLogHandler added in v1.1.0

func NewSlogLogHandler(c *Client, opts *SlogLogOptions) *SlogLogHandler

NewSlogLogHandler creates a slog.Handler that sends to the /log endpoint. Logger name defaults to the client's source; override with the returned handler's WithLogger method if needed.

func (*SlogLogHandler) Enabled added in v1.1.0

func (h *SlogLogHandler) Enabled(_ context.Context, _ slog.Level) bool

func (*SlogLogHandler) Handle added in v1.1.0

func (h *SlogLogHandler) Handle(_ context.Context, r slog.Record) error

func (*SlogLogHandler) WithAttrs added in v1.1.0

func (h *SlogLogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SlogLogHandler) WithGroup added in v1.1.0

func (h *SlogLogHandler) WithGroup(name string) slog.Handler

type SlogLogOptions added in v1.1.0

type SlogLogOptions struct {
	Logger    string // override logger name (default: client source)
	AddSource bool   // include file:line caller info
}

SlogLogOptions configures the log handler.

Jump to

Keyboard shortcuts

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