I am using the http://github.com/streadway/amqp package in my application in order to handle connections to a remote RabbitMQ server. Everything is ok and works fine, but I have a question.
The current name for a connection is "ip:port", so when there are multiple connections from the same IP+port, they become hardly distinguishable. It would be great if you can specify a name for each connection.
Is there any way to set a distinct friendly name for each connection?
RabbitMQ 3.6.5 added the facility for the connecting client to report a friendly name string value to identify a connection for management purposes. This is strictly an identifier and, as it is client-reported, it cannot be relied upon for anything other than weak identification of connections. The release notes state:
Clients now can provide a human-readable connection name that will be displayed in the management UI... In order to use this feature, set the
connection_name
key in client properties. Note that this name doesn’t have to be unique and cannot be used as a connection identifier, for example, in HTTP API requests.
Provided you are using a sufficiently new version of RabbitMQ, you can set this parameter when making connections using streadway/amqp
by passing an instance of amqp.Config
when making the initial connection. The Properties
field allows custom properties of the connection to be specified.
The example program below opens a connection using the AMQP URL provided in the environment variable AMQP_URL
, identified using the connection name passed as the first command line argument to the invocation.
package main
import (
"log"
"os"
"github.com/streadway/amqp"
)
func main() {
amqpUrl := os.Getenv("AMQP_URL")
cfg := amqp.Config{
Properties: amqp.Table{
"connection_name": os.Args[1],
},
}
conn, err := amqp.DialConfig(amqpUrl, cfg)
if err != nil {
log.Fatal(err)
}
defer conn.Close()
<-(chan struct{})(nil)
}
Starting multiple instances to connect to a local RabbitMQ instance using the following command line:
AMQP_URL=amqp://admin:password@localhost:5672 go run ./main.go connX
where a numeral is substituted for X
yields the following output in the "Connections" page of the RabbitMQ Management web UI:
and the individual connection detail pages shows the value under the "Client-provided name" detail value: