From 8848614315e9ab3c173312b0417373c726cdc346 Mon Sep 17 00:00:00 2001 From: Zhu Jiekun Date: Thu, 5 Sep 2024 22:48:09 +0800 Subject: [PATCH] lib/discovery/azure: fix host check in next link in Azure SD (#6915) Previous bugfix at 49f63b2 only partially fixed pagination host validation error. Before this fix it was: ``` unexpected nextLink host \"management.azure.com\", expecting \"https://management.azure.com\" ``` Now we only check the `Host` without schema. However, when Azure respond `nextLink` in `Host:Port` format, the `nextLink` check will fail: ``` unexpected nextLink host \"management.azure.com:443\", expecting \"management.azure.com\" ``` This pull request further relaxes the checks by only checking the `Hostname`. --- related issue: https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6912 --- docs/changelog/CHANGELOG.md | 1 + lib/promscrape/discovery/azure/api.go | 2 +- lib/promscrape/discovery/azure/machine.go | 6 ++++-- lib/promscrape/discovery/azure/machine_test.go | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/changelog/CHANGELOG.md b/docs/changelog/CHANGELOG.md index 1cdb765805..8f914e2c5b 100644 --- a/docs/changelog/CHANGELOG.md +++ b/docs/changelog/CHANGELOG.md @@ -23,6 +23,7 @@ See also [LTS releases](https://docs.victoriametrics.com/lts-releases/). * FEATURE [stream aggregation](https://docs.victoriametrics.com/stream-aggregation/): perform deduplication for all received data when specifying `-streamAggr.dedupInterval` or `-remoteWrite.streamAggr.dedupInterval` command-line flags are set. Previously, if the `-remoteWrite.streamAggr.config` or `-streamAggr.config` is set, only series that matched aggregation config were deduplicated. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/6711#issuecomment-2288361213) for details. * FEATURE: all VictoriaMetrics [enterprise](https://docs.victoriametrics.com/enterprise/) components: add support of hot-reload for license key supplied by `-licenseFile` command-line flag. +* BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent/) fix service discovery of Azure Virtual Machines for response contains `nextLink` in `Host:Port` format. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6912). * BUGFIX: [vmagent dashboard](https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/dashboards/vmagent.json): fix legend captions for stream aggregation related panels. Before they were displaying wrong label names. * BUGFIX: [vmgateway](https://docs.victoriametrics.com/vmgateway/): add missing `datadog`, `newrelic`, `opentelemetry` and `pushgateway` routes to the `JWT` authorization routes. Allows prefixed (`promtheus/graphite`) routes for query requests. * BUGFIX: [Single-node VictoriaMetrics](https://docs.victoriametrics.com/) and `vmstorage` in [VictoriaMetrics cluster](https://docs.victoriametrics.com/cluster-victoriametrics/): fix metric `vm_object_references{type="indexdb"}`. Previously, it was overcounted. diff --git a/lib/promscrape/discovery/azure/api.go b/lib/promscrape/discovery/azure/api.go index e21257d697..a7087deee3 100644 --- a/lib/promscrape/discovery/azure/api.go +++ b/lib/promscrape/discovery/azure/api.go @@ -122,7 +122,7 @@ func newAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) { cfg := &apiConfig{ c: c, - apiServerHost: u.Host, + apiServerHost: u.Hostname(), port: port, resourceGroup: sdc.ResourceGroup, subscriptionID: sdc.SubscriptionID, diff --git a/lib/promscrape/discovery/azure/machine.go b/lib/promscrape/discovery/azure/machine.go index 491d80b365..a46da299da 100644 --- a/lib/promscrape/discovery/azure/machine.go +++ b/lib/promscrape/discovery/azure/machine.go @@ -96,8 +96,10 @@ func visitAllAPIObjects(ac *apiConfig, apiURL string, cb func(data json.RawMessa return fmt.Errorf("cannot parse nextLink from response %q: %w", lar.NextLink, err) } - if nextURL.Host != "" && nextURL.Host != ac.apiServerHost { - return fmt.Errorf("unexpected nextLink host %q, expecting %q", nextURL.Host, ac.apiServerHost) + // Sometimes Azure will respond a host with a port. Since all possible apiServer defined in cloudEnvironments do not include a port, + // it is best to check the host without the port. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/6912 + if nextURL.Host != "" && nextURL.Hostname() != ac.apiServerHost { + return fmt.Errorf("unexpected nextLink host %q, expecting %q", nextURL.Hostname(), ac.apiServerHost) } nextLinkURI = nextURL.RequestURI() diff --git a/lib/promscrape/discovery/azure/machine_test.go b/lib/promscrape/discovery/azure/machine_test.go index 06710c192b..dccc371525 100644 --- a/lib/promscrape/discovery/azure/machine_test.go +++ b/lib/promscrape/discovery/azure/machine_test.go @@ -91,7 +91,7 @@ func TestGetVirtualMachinesSuccess(t *testing.T) { defer c.Stop() ac := &apiConfig{ c: c, - apiServerHost: u.Host, + apiServerHost: u.Hostname(), subscriptionID: "some-id", refreshToken: func() (string, time.Duration, error) { return "auth-token", 0, nil