Documentation
¶
Overview ¶
Package activitylogmq provides a Watermill broker factory for publishing and consuming IlonaPay activity-log (audit) messages.
Most application code should use the higher-level github.com/mawarpay/pkg-activitylogmq/messaging package, which wires publisher, subscriber, and the HTTP forwarder to activity-log-service in one Init call. This root package is for services that need direct control over Watermill publishers and subscribers.
Supported brokers ¶
- RabbitMQ (AMQP)
- Amazon MQ for RabbitMQ (AMQPS)
- Google Cloud Pub/Sub
- Apache Kafka
Broker selection is driven by environment variables. Call LoadConfig then check Config.Enabled before constructing clients:
cfg := activitylogmq.LoadConfig()
if !cfg.Enabled() {
// queue disabled; service continues without audit publishing
return
}
pub, err := activitylogmq.NewPublisher(cfg, logger)
sub, err := activitylogmq.NewSubscriber(cfg, logger)
Configuration ¶
See the module README for the full environment-variable reference. Key vars:
- MESSAGE_BROKER — rabbitmq | amazonmq | pubsub | kafka (auto-detected when unset)
- ACTIVITY_LOG_QUEUE — topic / queue name (default activity-log.create)
- RABBITMQ_URL or RABBITMQ_HOST — RabbitMQ connection
- AMAZONMQ_URL or AMAZONMQ_HOST — Amazon MQ for RabbitMQ (AMQPS, port 5671)
- PUBSUB_PROJECT_ID — Google Cloud project
- KAFKA_BROKERS — comma-separated Kafka addresses
Caveats ¶
RabbitMQ and Amazon MQ use Watermill's durable queue config, which names the queue after the topic. Multiple subscribers on the same topic compete for deliveries. NewPublisher and NewSubscriber dial the broker; callers must Close the returned clients when finished.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewPublisher ¶
NewPublisher creates a Watermill message.Publisher for the broker described by cfg.
cfg must be Config.Enabled; otherwise an error is returned and no connection is attempted. A nil logger is replaced with Watermill's standard logger.
The returned publisher dials the broker and must be closed by the caller when it is no longer needed. Errors wrapping dial or configuration failures are returned as-is from the underlying Watermill adapter.
Example ¶
package main
import (
"fmt"
activitylogmq "github.com/mawarpay/pkg-activitylogmq"
)
func main() {
_, err := activitylogmq.NewPublisher(activitylogmq.Config{
Broker: activitylogmq.BrokerRabbitMQ,
}, nil)
fmt.Println(err != nil)
}
Output: true
func NewSubscriber ¶
func NewSubscriber(cfg Config, logger watermill.LoggerAdapter) (message.Subscriber, error)
NewSubscriber creates a Watermill message.Subscriber for the broker described by cfg.
cfg must be Config.Enabled; otherwise an error is returned and no connection is attempted. A nil logger is replaced with Watermill's standard logger.
For RabbitMQ and Amazon MQ, the queue name equals the topic (durable queue topology). For Kafka, cfg.KafkaConsumerGroup is used. The returned subscriber must be closed by the caller when finished.
Types ¶
type Broker ¶
type Broker string
Broker identifies the message transport selected by LoadConfig.
const ( // BrokerRabbitMQ selects RabbitMQ / AMQP. Aliases for MESSAGE_BROKER: // "rabbitmq", "amqp", "rabbit". BrokerRabbitMQ Broker = "rabbitmq" // BrokerAmazonMQ selects Amazon MQ for RabbitMQ over AMQPS (TLS). // It reuses the Watermill AMQP adapter with an amqps:// URI. // Aliases for MESSAGE_BROKER: "amazonmq", "amazon_mq", "amazon-mq", "amq". BrokerAmazonMQ Broker = "amazonmq" // BrokerPubSub selects Google Cloud Pub/Sub. Aliases for MESSAGE_BROKER: // "pubsub", "google", "gcp", "google_pubsub". BrokerPubSub Broker = "pubsub" // BrokerKafka selects Apache Kafka. BrokerKafka Broker = "kafka" )
type Config ¶
type Config struct {
// Broker is the selected transport. Empty when no broker could be detected.
Broker Broker
// Topic is the queue or topic name used for publish and subscribe.
// Defaults to "activity-log.create" when no topic env var is set.
Topic string
// RabbitMQURI is the AMQP/AMQPS connection URI when Broker is
// BrokerRabbitMQ or BrokerAmazonMQ.
RabbitMQURI string
// PubSubProjectID is the GCP project ID when Broker is BrokerPubSub.
PubSubProjectID string
// KafkaBrokers is the list of Kafka bootstrap addresses when Broker is
// BrokerKafka.
KafkaBrokers []string
// KafkaConsumerGroup is the consumer group for Kafka subscribers.
// Defaults to "activity-log-consumer".
KafkaConsumerGroup string
}
Config holds broker connection settings loaded from the environment by LoadConfig. Pass an Enabled config to NewPublisher or NewSubscriber.
func LoadConfig ¶
func LoadConfig() Config
LoadConfig reads MESSAGE_BROKER (or auto-detects the broker) and broker-specific environment variables into a Config.
Topic resolution order: ACTIVITY_LOG_QUEUE, ACTIVITY_LOG_TOPIC, PUBSUB_TOPIC, KAFKA_TOPIC, then "activity-log.create".
Broker auto-detect order when MESSAGE_BROKER is unset: RabbitMQ URI present, then PUBSUB_PROJECT_ID, then KAFKA_BROKERS, then Amazon MQ URI/host.
LoadConfig never returns an error; call Config.Enabled to determine whether publishing and subscribing can proceed.
Example ¶
package main
import (
"fmt"
activitylogmq "github.com/mawarpay/pkg-activitylogmq"
)
func main() {
// LoadConfig reads MESSAGE_BROKER and broker-specific env vars.
// Check Enabled before constructing publishers or subscribers.
cfg := activitylogmq.LoadConfig()
if !cfg.Enabled() {
fmt.Println("broker disabled")
return
}
pub, err := activitylogmq.NewPublisher(cfg, nil)
if err != nil {
fmt.Println("publisher:", err)
return
}
defer pub.Close()
sub, err := activitylogmq.NewSubscriber(cfg, nil)
if err != nil {
fmt.Println("subscriber:", err)
return
}
defer sub.Close()
}
Output:
func (Config) Enabled ¶
Enabled reports whether Broker is set and the corresponding connection settings are present (AMQP URI, Pub/Sub project ID, or Kafka brokers). A disabled config must not be passed to NewPublisher or NewSubscriber.
Example ¶
package main
import (
"fmt"
activitylogmq "github.com/mawarpay/pkg-activitylogmq"
)
func main() {
enabled := activitylogmq.Config{
Broker: activitylogmq.BrokerRabbitMQ,
RabbitMQURI: "amqp://guest:guest@localhost:5672/",
}
disabled := activitylogmq.Config{
Broker: activitylogmq.BrokerRabbitMQ,
}
fmt.Println(enabled.Enabled())
fmt.Println(disabled.Enabled())
}
Output: true false
Directories
¶
| Path | Synopsis |
|---|---|
|
Package clients provides an HTTP client for creating activity-log rows in activity-log-service.
|
Package clients provides an HTTP client for creating activity-log rows in activity-log-service. |
|
Package messaging wires activity-log publishing and consumption for IlonaPay microservices.
|
Package messaging wires activity-log publishing and consumption for IlonaPay microservices. |