Repository: prometheus/alertmanager
File: cluster/cluster.go (Go)

Code before:
	ctx, cancel := context.WithTimeout(context.Background(), resolveTimeout)
	defer cancel()
	resolvedPeers, err := resolvePeers(ctx, knownPeers, advertiseAddr, &net.Resolver{}, waitIfEmpty)
	if err != nil {
		return nil, fmt.Errorf("resolve peers: %w", err)
	}
	l.Debug("resolved peers to following addresses", "peers", strings.Join(resolvedPeers, ","))

	// Initial validation of user-specified advertise address.
	addr, err := calculateAdvertiseAddress(bindHost, advertiseHost, allowInsecureAdvertise)
	if err != nil {
		l.Warn("couldn't deduce an advertise address: " + err.Error())
	} else if hasNonlocal(resolvedPeers) && isUnroutable(addr.String()) {
		l.Warn("this node advertises itself on an unroutable address", "addr", addr.String())
		l.Warn("this node will be unreachable in the cluster")
		l.Warn("provide --cluster.advertise-address as a routable IP address or hostname")
	} else if isAny(bindAddr) && advertiseHost == "" {
		// memberlist doesn't advertise properly when the bind address is empty or unspecified.
		l.Info("setting advertise address explicitly", "addr", addr.String(), "port", bindPort)
		advertiseHost = addr.String()
		advertisePort = bindPort
	}

	// Generate a random name if none is provided.
	if name == "" {
		name = ulid.Make().String()
	}

	p := &Peer{
		states:              map[string]State{},
		stopc:               make(chan struct{}),
		readyc:              make(chan struct{}),
		logger:              l,
		peers:               map[string]peer{},
		resolvedPeers:       resolvedPeers,
		resolvePeersTimeout: resolveTimeout,
		knownPeers:          knownPeers,
	}

	p.register(reg, name)

	retransmit := len(knownPeers) / 2
	if retransmit < 3 {
		retransmit = 3
	}
	p.delegate = newDelegate(l, reg, p, retransmit)

	cfg := memberlist.DefaultLANConfig()
	cfg.Name = name
	cfg.BindAddr = bindHost
	cfg.BindPort = bindPort

Review comment (bug):
Is it ok to use the ulid.Make() which does panic on error here?
There is also 
```
func New(ms uint64, entropy io.Reader) (id ULID, err error) {
```

Code after:

	ctx, cancel := context.WithTimeout(context.Background(), resolveTimeout)
	defer cancel()
	resolvedPeers, err := resolvePeers(ctx, knownPeers, advertiseAddr, &net.Resolver{}, waitIfEmpty)
	if err != nil {
		return nil, fmt.Errorf("resolve peers: %w", err)
	}
	l.Debug("resolved peers to following addresses", "peers", strings.Join(resolvedPeers, ","))

	// Initial validation of user-specified advertise address.
	addr, err := calculateAdvertiseAddress(bindHost, advertiseHost, allowInsecureAdvertise)
	if err != nil {
		l.Warn("couldn't deduce an advertise address: " + err.Error())
	} else if hasNonlocal(resolvedPeers) && isUnroutable(addr.String()) {
		l.Warn("this node advertises itself on an unroutable address", "addr", addr.String())
		l.Warn("this node will be unreachable in the cluster")
		l.Warn("provide --cluster.advertise-address as a routable IP address or hostname")
	} else if isAny(bindAddr) && advertiseHost == "" {
		// memberlist doesn't advertise properly when the bind address is empty or unspecified.
		l.Info("setting advertise address explicitly", "addr", addr.String(), "port", bindPort)
		advertiseHost = addr.String()
		advertisePort = bindPort
	}

	// Generate a random name if none is provided.
	if name == "" {
		id, err := ulid.New(ulid.Now(), rand.Reader)
		if err != nil {
			return nil, err
		}
		name = id.String()
	}

	p := &Peer{
		states:              map[string]State{},
		stopc:               make(chan struct{}),
		readyc:              make(chan struct{}),
		logger:              l,
		peers:               map[string]peer{},
		resolvedPeers:       resolvedPeers,
		resolvePeersTimeout: resolveTimeout,
		knownPeers:          knownPeers,
	}

	p.register(reg, name)

	retransmit := len(knownPeers) / 2
	if retransmit < 3 {
		retransmit = 3
	}
	p.delegate = newDelegate(l, reg, p, retransmit)