Repository: prometheus/prometheus (language: go)

Issue:
AlertmanagerFromGroup does not use port added to address
### What did you do?

The code creates a `LabelBuilder` here:
https://github.com/prometheus/prometheus/blob/72a48321dabed43f2cd9e510dda96c50d4a48a58/notifier/notifier.go#L730

then does various steps which might add a port number, and delete labels beginning `__meta_`, but none of this work has any effect.
At the end of processing it uses `lset` which was the result of relabelling 40 lines earlier:
https://github.com/prometheus/prometheus/blob/72a48321dabed43f2cd9e510dda96c50d4a48a58/notifier/notifier.go#L771

### What did you expect to see?

I guess, since this code has no effect and hasn't changed in 5 years, we don't need it.


### What did you see instead? Under which circumstances?

-

### System information

_No response_

### Prometheus version

```text
Commit 72a48321dabed43f2cd9e510dda96c50d4a48a58
```


### Prometheus configuration file

_No response_

### Alertmanager version

_No response_

### Alertmanager configuration file

_No response_

### Logs

_No response_


Discussion / hints:

Fix patch:
diff --git a/notifier/notifier.go b/notifier/notifier.go
index 99886bd4bc3..fd89a029c7d 100644
--- a/notifier/notifier.go
+++ b/notifier/notifier.go
@@ -19,11 +19,9 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
-	"net"
 	"net/http"
 	"net/url"
 	"path"
-	"strings"
 	"sync"
 	"time"
 
@@ -727,47 +725,11 @@ func AlertmanagerFromGroup(tg *targetgroup.Group, cfg *config.AlertmanagerConfig
 			continue
 		}
 
-		lb := labels.NewBuilder(lset)
-
-		// addPort checks whether we should add a default port to the address.
-		// If the address is not valid, we don't append a port either.
-		addPort := func(s string) bool {
-			// If we can split, a port exists and we don't have to add one.
-			if _, _, err := net.SplitHostPort(s); err == nil {
-				return false
-			}
-			// If adding a port makes it valid, the previous error
-			// was not due to an invalid address and we can append a port.
-			_, _, err := net.SplitHostPort(s + ":1234")
-			return err == nil
-		}
 		addr := lset.Get(model.AddressLabel)
-		// If it's an address with no trailing port, infer it based on the used scheme.
-		if addPort(addr) {
-			// Addresses reaching this point are already wrapped in [] if necessary.
-			switch lset.Get(model.SchemeLabel) {
-			case "http", "":
-				addr = addr + ":80"
-			case "https":
-				addr = addr + ":443"
-			default:
-				return nil, nil, fmt.Errorf("invalid scheme: %q", cfg.Scheme)
-			}
-			lb.Set(model.AddressLabel, addr)
-		}
-
 		if err := config.CheckTargetAddress(model.LabelValue(addr)); err != nil {
 			return nil, nil, err
 		}
 
-		// Meta labels are deleted after relabelling. Other internal labels propagate to
-		// the target which decides whether they will be part of their label set.
-		for _, l := range lset {
-			if strings.HasPrefix(l.Name, model.MetaLabelPrefix) {
-				lb.Del(l.Name)
-			}
-		}
-
 		res = append(res, alertmanagerLabels{lset})
 	}
 	return res, droppedAlertManagers, nil


Test patch: