Documentation
¶
Overview ¶
Package yclogslog provides a log/slog.Handler implementation that sends log entries to Yandex Cloud Logging via the gRPC LogIngestionService/Write API.
The handler buffers entries in memory, batches them, and sends asynchronously with retry on transient errors. It is designed for long-running services running on Yandex Cloud Compute / COI instances.
Basic usage:
sdk, err := ycsdk.Build(ctx,
options.WithCredentials(credentials.InstanceServiceAccount()),
)
// handle err
handler, err := yclogslog.NewHandler(ctx, sdk, yclogslog.Options{
FolderID: "b1gxxxxxxxxxx",
Service: "my-service",
})
// handle err
defer handler.Close(context.Background())
slog.SetDefault(slog.New(handler))
slog.Info("server started", "port", 8080)
Index ¶
- type DropPolicy
- type Handler
- func (h *Handler) Close(ctx context.Context) error
- func (h *Handler) Enabled(_ context.Context, level slog.Level) bool
- func (h *Handler) Handle(_ context.Context, record slog.Record) error
- func (h *Handler) Stats() StatsSnapshot
- func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *Handler) WithGroup(name string) slog.Handler
- type Options
- type Stats
- type StatsSnapshot
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DropPolicy ¶
type DropPolicy int
DropPolicy defines the behavior when the internal queue is full.
const ( // DropNewest discards the incoming entry when the queue is full. DropNewest DropPolicy = iota // DropOldest discards the oldest entry in the queue to make room for the new one. DropOldest )
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler implements slog.Handler and sends log entries to Yandex Cloud Logging.
func NewHandler ¶
NewHandler creates a new Handler that sends logs to Yandex Cloud Logging using the provided SDK instance. The caller owns the SDK and is responsible for closing it. The context is used to resolve the gRPC connection.
func (*Handler) Close ¶
Close flushes pending entries, stops the background worker, and closes the gRPC connection. The provided context controls the maximum time to wait for the flush. Close does NOT close the underlying SDK.
func (*Handler) Stats ¶
func (h *Handler) Stats() StatsSnapshot
Stats returns a snapshot of the handler's internal counters.
type Options ¶
type Options struct {
// Destination: exactly one of FolderID or LogGroupID must be set.
FolderID string
LogGroupID string
// Service is a required label identifying the application.
Service string
// InstanceID is an optional identifier for the running instance.
InstanceID string
// DefaultPayload is a set of key-value pairs merged into json_payload of every log entry.
// Conflicts are resolved in favor of the entry's own attributes.
DefaultPayload map[string]string
// MinLevel sets the minimum log level. Entries below this level are discarded.
// Default: slog.LevelInfo (zero value).
MinLevel slog.Level
// QueueSize is the maximum number of entries buffered in memory.
// Default: 10000.
QueueSize int
// BatchMaxEntries is the maximum number of entries per batch sent to Cloud Logging.
// Default: 100.
BatchMaxEntries int
// FlushInterval is the maximum time to wait before sending a non-full batch.
// Default: 1s.
FlushInterval time.Duration
// ShutdownFlushTimeout is the maximum time to wait for pending entries to be sent on Close.
// Default: 5s.
ShutdownFlushTimeout time.Duration
// RetryMaxElapsed is the maximum total time spent retrying a single batch.
// Default: 30s.
RetryMaxElapsed time.Duration
// RetryInitialBackoff is the initial delay before the first retry.
// Default: 100ms.
RetryInitialBackoff time.Duration
// RetryMaxBackoff is the maximum delay between retries.
// Default: 5s.
RetryMaxBackoff time.Duration
// DropPolicy defines the behavior when the queue is full.
// Default: DropNewest.
DropPolicy DropPolicy
// GRPCTimeout is the timeout for a single Write RPC call.
// Default: 10s.
GRPCTimeout time.Duration
// DebugLog is an optional logger for internal diagnostics (send errors,
// retries, dropped entries, etc.). If nil, a no-op logger is used.
DebugLog *slog.Logger
// IngestionEndpoint overrides the gRPC endpoint for LogIngestionService.
// Default: "ingester.logging.yandexcloud.net:443".
IngestionEndpoint string
}
Options configures the Handler.
type Stats ¶
type Stats struct {
SentEntries atomic.Int64
DroppedEntries atomic.Int64
FailedBatches atomic.Int64
RetriedBatches atomic.Int64
}
Stats holds atomic counters for observability.
func (*Stats) Snapshot ¶
func (s *Stats) Snapshot() StatsSnapshot
Snapshot returns a point-in-time copy of the stats.