Documentation
¶
Overview ¶
Package nats provides NATS and JetStream connection utilities with tracing integration.
It offers connection initialization with TLS/mTLS support, health checks, graceful shutdown, and distributed tracing via OpenTelemetry.
Index ¶
- Variables
- func Connect(ctx context.Context, cfg Config) (*nats.Conn, error)
- func ConsumeWithTracing(ctx context.Context, in ConsumeInput) (jetstream.ConsumeContext, error)
- func ExtractTraceContext(ctx context.Context, msg jetstream.Msg) context.Context
- func JetStreamPublish(ctx context.Context, js jetstream.JetStream, in JetStreamPublishInput) (*jetstream.PubAck, error)
- func Publish(ctx context.Context, conn *nats.Conn, in PublishInput) error
- type Config
- type ConsumeInput
- type HealthChecker
- type JetStreamPublishInput
- type PublishInput
- type TLSConfig
Constants ¶
This section is empty.
Variables ¶
var ErrIncompleteMTLS = errors.New("mtls requires both cert_file and key_file")
ErrIncompleteMTLS is returned when mTLS config has cert_file but no key_file or vice versa.
var ErrURLRequired = errors.New("url is required")
ErrURLRequired is returned when URL is empty in Config.
Functions ¶
func Connect ¶
Connect establishes a connection to the NATS server. It registers a shutdown handler to drain the connection gracefully.
func ConsumeWithTracing ¶
func ConsumeWithTracing(ctx context.Context, in ConsumeInput) (jetstream.ConsumeContext, error)
ConsumeWithTracing starts consuming messages with automatic trace extraction. Each message handler receives a context with the extracted trace context.
func ExtractTraceContext ¶
ExtractTraceContext extracts trace context from a JetStream message. Use this when you need manual control over span creation.
func JetStreamPublish ¶
func JetStreamPublish(ctx context.Context, js jetstream.JetStream, in JetStreamPublishInput) (*jetstream.PubAck, error)
JetStreamPublish sends a message to JetStream with trace context injection.
Types ¶
type Config ¶
type Config struct {
// URL is the NATS server URL.
// Required. Example: "nats://localhost:4222"
URL string `koanf:"url"`
// MaxReconnects is the maximum number of reconnection attempts.
// -1 means unlimited reconnects.
MaxReconnects int `koanf:"max_reconnects"`
// ReconnectWait is the time to wait between reconnection attempts.
ReconnectWait time.Duration `koanf:"reconnect_wait"`
// ReconnectJitter is the maximum random jitter added to reconnect wait.
ReconnectJitter time.Duration `koanf:"reconnect_jitter"`
// ConnectTimeout is the timeout for initial connection.
ConnectTimeout time.Duration `koanf:"connect_timeout"`
// DrainTimeout is the timeout for graceful drain during shutdown.
DrainTimeout time.Duration `koanf:"drain_timeout"`
// TLS holds TLS/mTLS configuration. Optional.
TLS *TLSConfig `koanf:"tls"`
}
Config holds configuration for NATS connection.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible default values. URL is required and must be set by the caller.
type ConsumeInput ¶
type ConsumeInput struct {
Consumer jetstream.Consumer
Handler func(ctx context.Context, msg jetstream.Msg) error
Options []jetstream.PullConsumeOpt
}
ConsumeInput holds parameters for consumer operations with tracing.
type HealthChecker ¶
type HealthChecker struct {
// contains filtered or unexported fields
}
HealthChecker checks NATS connection status. Implements grpchealth.HealthChecker interface.
func NewHealthChecker ¶
func NewHealthChecker(conn *nats.Conn) *HealthChecker
NewHealthChecker creates a health checker for the given connection.
type JetStreamPublishInput ¶
type JetStreamPublishInput struct {
Subject string
Data []byte
Headers nats.Header
MsgID string // optional, for deduplication
}
JetStreamPublishInput holds parameters for JetStream publish operations.
type PublishInput ¶
PublishInput holds parameters for Publish operations.
type TLSConfig ¶
type TLSConfig struct {
// CAFile is the path to the CA certificate for server verification.
CAFile string `koanf:"ca_file"`
// SkipVerify disables server certificate verification.
// Default: false (verification enabled).
SkipVerify bool `koanf:"skip_verify"`
// CertFile is the path to the client certificate for mTLS.
CertFile string `koanf:"cert_file"`
// KeyFile is the path to the client private key for mTLS.
KeyFile string `koanf:"key_file"`
}
TLSConfig holds TLS and mTLS configuration.