core

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package core holds the LogSense client implementation: option wiring, the single background sender goroutine, and the retry/backoff delivery logic. The top-level logsense package is a thin facade over this package.

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 is the LogSense SDK client. Create one with New().

Its state is split into DTO structs: cfg (user settings), stream (the channels the sender goroutine uses), and stats (runtime counters).

A single background goroutine owns delivery: events flow through the bounded stream.Events channel to one sender, so there are never concurrent in-flight batches and memory can't grow without bound if the endpoint is slow or down.

func New

func New(apiKey string, opts ...Option) *Client

New creates a new Client and starts its background sender goroutine.

func (*Client) Capture

func (c *Client) Capture(err error, ctx context.Context, extra ...map[string]any)

Capture enqueues an error for async ingest. Never panics.

The error message is used verbatim as the event Message so repeated occurrences of the same error group together; the stack trace is attached as a structured field rather than embedded in the message.

Note: the stack is captured at the point Capture is called. If you call it from inside a logging hook, the stack reflects the hook, not the error's origin — call Capture at the site where the error is handled for best results.

func (*Client) Dropped

func (c *Client) Dropped() int64

Dropped returns the number of events discarded because the queue was full. A non-zero, growing value means the endpoint can't keep up with your volume.

func (*Client) Flush

func (c *Client) Flush()

Flush sends all currently-buffered events synchronously and waits for the send to complete. Safe to call after Shutdown (returns immediately).

func (*Client) Log

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

Log enqueues a log line at the given level.

func (*Client) Shutdown

func (c *Client) Shutdown()

Shutdown flushes any buffered events and stops the background goroutine. Idempotent. Always call it (or defer it) before your process exits.

func (*Client) StartSpan

func (c *Client) StartSpan(ctx context.Context, name string, opts ...SpanOption) (context.Context, *Span)

StartSpan begins a span named name. If ctx already carries a span, the new one inherits its trace ID and becomes its child; otherwise a new trace is started. The returned context carries the new span so nested StartSpan calls link up.

Always end the span:

ctx, span := client.StartSpan(ctx, "GET /checkout")
defer span.End()

type Option

type Option func(*Client)

Option configures a Client.

func WithBatchSize

func WithBatchSize(n int) Option

WithBatchSize sets how many events accumulate before an early flush. Default 50.

func WithContextEnricher

func WithContextEnricher(fn func(ctx context.Context) (traceID string, fields map[string]any)) Option

WithContextEnricher registers a function that pulls correlation data out of the context.Context passed to Capture/Log — typically an OpenTelemetry trace/span ID. Returning a non-empty traceID sets the event's TraceID; any returned fields are merged into the event's structured data (without overwriting explicit fields). This keeps the SDK dependency-free while still letting logs correlate with traces.

func WithEndpoint

func WithEndpoint(url string) Option

WithEndpoint overrides the default API endpoint.

func WithEnvironment

func WithEnvironment(env string) Option

WithEnvironment sets a default environment (prod/staging/dev).

func WithHTTPClient

func WithHTTPClient(hc *http.Client) Option

WithHTTPClient supplies a custom *http.Client (for tuned pooling, proxies, or tests). By default the SDK uses its own client with a 5s timeout, never the shared http.DefaultClient.

func WithMaxQueue

func WithMaxQueue(n int) Option

WithMaxQueue caps the number of buffered events (logs and spans each get their own queue of this size). When a queue is full new events are dropped (drop-newest) and counted; see Dropped. Default 10000.

func WithOnError

func WithOnError(fn func(error)) Option

WithOnError registers a callback invoked when a batch cannot be delivered (marshal failure, network error, or non-2xx response after retries). Use it to surface delivery problems — a wrong API key or a 5xx-ing endpoint would otherwise lose logs with no signal. The callback must not block.

func WithService

func WithService(name string) Option

WithService sets a default service name for all captured logs.

type Span

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

Span is an in-progress unit of work. Create one with Client.StartSpan, then call End (typically deferred) to record its duration and enqueue it for delivery. A Span is safe to mutate from the goroutine that owns it; End is idempotent.

func (*Span) End

func (s *Span) End()

End records the span's end time and duration and enqueues it for delivery. Idempotent — only the first call takes effect. Safe to call on a nil span.

func (*Span) SetAttributes

func (s *Span) SetAttributes(attrs map[string]any)

SetAttributes merges attributes into the span (last write wins).

func (*Span) SetError

func (s *Span) SetError(err error)

SetError marks the span as failed: status ERROR with err's message, and an "error" attribute. No-op when err is nil.

func (*Span) SetStatus

func (s *Span) SetStatus(code, message string)

SetStatus sets the span's status code (e.g. "OK", "ERROR") and message.

func (*Span) SpanID

func (s *Span) SpanID() string

SpanID returns the span's ID.

func (*Span) TraceID

func (s *Span) TraceID() string

TraceID returns the span's trace ID (useful for correlating logs).

type SpanOption

type SpanOption func(*Span)

SpanOption configures a Span at creation.

func WithSpanAttributes

func WithSpanAttributes(attrs map[string]any) SpanOption

WithSpanAttributes attaches initial attributes to the span.

func WithSpanKind

func WithSpanKind(kind string) SpanOption

WithSpanKind sets the span kind (server|client|producer|consumer|internal).

Jump to

Keyboard shortcuts

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