Commit graph

756 commits

Author SHA1 Message Date
Simon Pasquier 7704cde4ea
discovery(k8s): add metadata labels to endpointslices
This commit adds 2 new metadata labels for the endpointslice role:
* `__meta_kubernetes_endpointslice_endpoint_node_name`
* `__meta_kubernetes_endpointslice_endpoint_zone`

The latter is only present when the `discovery.k8s.io/v1` API group is
available.

I also updated the configuration doc and added an entry for the
`__meta_kubernetes_endpointslice_endpoint_hostname` label which was
missing.

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
2024-04-17 11:20:19 +02:00
Matthieu MOREL 6f595c6762
golangci-lint: enable whitespace linter (#13905)
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2024-04-11 09:27:54 +01:00
Bryan Boreham 594b317ecc
Merge pull request #13898 from hanghuge/main
[DOCS] Fix unavailable link to Kubernetes docs
2024-04-10 11:52:12 +01:00
Matthieu MOREL d496687c8e golangci-lint: enable usestdlibvars linter
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2024-04-08 19:26:23 +00:00
hanghuge c14a158d03 Signed-off-by: hanghuge <cmoman@outlook.com>
Fix unavailable link

Signed-off-by: hanghuge <cmoman@outlook.com>
2024-04-08 18:44:22 +08:00
David Ashpole bbfc72b4e2
support unregistering discovery manager metrics (#13896)
Signed-off-by: David Ashpole <dashpole@google.com>
2024-04-05 16:19:07 +02:00
dandrucz 38b75bc0d7
Linode IPv6 Range support, Optional region filtering, Missing fields in Documentation fixed (#13774)
* Add support for discovering Linode IPv6 ranges associated with linodes.
* Add optional but recommended region filtering (faster queries, more relevant information).
* Added missing fields in configuration.md, updated linode test cases.
* Convert to TableDrivenTests as per tjhop request.

Signed-off-by: David Andruczyk <dandrucz@akamai.com>
2024-04-05 09:31:59 +01:00
Bryan Boreham 8799581b24
Merge pull request #13554 from machine424/k8s-failures
discovery(k8s): add metric prometheus_sd_kubernetes_failures_total
2024-04-04 10:43:44 +01:00
Bryan Boreham c6a42f8891
Merge pull request #13741 from bboreham/azure-test-labels
Azure Discovery tests: Add test for mapping VMs to labels
2024-03-27 11:36:17 +01:00
sellskin ff15b17400 remove code that will not be executed
Signed-off-by: sellskin <mydesk@yeah.net>
2024-03-25 12:18:33 +08:00
Daniel Kimsey aa3e58358b consul: Add support for catalog list services filter
This adds support for Consul's Catalog [List Services][^1] API's `filter`
parameter added in 1.14.x. This parameter grants the operator more
flexibility to do server-side filtering of the Catalog, before
Prometheus subscribes for updates. Operators can use this to improve
both the performance of Prometheus's Consul SD and reduce the impact of
enumerating large catalogs.

[^1]: https://developer.hashicorp.com/consul/api-docs/v1.14.x/catalog

Signed-off-by: Daniel Kimsey <dekimsey@protonmail.com>
2024-03-17 20:32:54 -05:00
Jan-Otto Kröpke 302e151de8
{discovery,remote_write}/azure: Support default SDK authentication (#13099)
* discovery/azure: Offer default SDK authentication

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
2024-03-16 11:06:57 +00:00
Bryan Boreham ab9c544ec7 Azure Discovery tests: Add test for VMToLabelSet
Test fails due to bug in code on main.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-03-12 15:04:09 +00:00
Bryan Boreham 5f2c0c5283 Azure Discovery tests: mock the azure client interface
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-03-12 15:04:09 +00:00
Bryan Boreham 4e24e5b1d1 Refactor: Azure Discovery: introduce an interface for the client
So we can mock it.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-03-12 15:04:09 +00:00
Bryan Boreham b8d428b753 Refactor: Azure Discovery: extract function to generate labelSet
This should make it easier to test.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-03-12 15:04:04 +00:00
michaelact eea6ab1cdd
[BUGFIX] Azure SD: Fix 'error: parameter virtualMachineScaleSetName cannot be empty' (#13702)
Erroneous code was introduced during a merge-back-to-main at #13399.

Signed-off-by: michaelact <86778470+michaelact@users.noreply.github.com>
2024-03-08 15:19:39 +00:00
machine424 0e81ab44a2
discovery(k8s): add a metric to track failed requests, failures will still be logged.
Signed-off-by: machine424 <ayoubmrini424@gmail.com>
2024-02-28 19:55:46 +01:00
Björn Rabenstein dbb4055633
Merge pull request #13593 from machine424/no-index
discovery: kubernetes: Avoid creating unnecessary Kubernetes indexers…
2024-02-27 10:48:37 +01:00
Augustin Husson 9738e48a7d
Merge pull request #13629 from prometheus/release-2.50
merge back release 2.50
2024-02-22 11:07:52 +01:00
machine424 92544c00bf
discovery: kubernetes: Avoid creating unnecessary Kubernetes indexers in RoleEndpointSlice
This was due to a missing "return", see https://github.com/prometheus/prometheus/pull/13554#discussion_r1490965817

Signed-off-by: machine424 <ayoubmrini424@gmail.com>
2024-02-16 14:29:28 +01:00
Darshan Chaudhary 0e8a2e44a4
Azure SD: check for nil before accessing and dereferencing (#13578)
Signed-off-by: darshanime <deathbullet@gmail.com>
2024-02-14 17:59:01 +01:00
Bryan Boreham 6005ac6f9d
Merge pull request #9311 from Creatone/creatone/use-testify-3
tests: Move from t.Errorf and others. (Part 3)
2024-02-05 18:48:59 +01:00
Bryan Boreham 857138d3ce review feedback
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-02-04 15:52:50 +01:00
Bryan Boreham b17f88b7fb consul sd tests: don't call FailNow from a background goroutine
This is not allowed by the Go test framework.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-02-01 15:15:48 +00:00
Bryan Boreham 46008fdecd lint
Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
2024-02-01 14:57:11 +00:00
Ayoub Mrini 581d8d86b4
Pod status changes not discovered by Kube Endpoints SD (#13337)
* fix(discovery/kubernetes/endpoints): react to changes on Pods because some modifications can occur on them without triggering an update on the related Endpoints (The Pod phase changing from Pending to Running e.g.).

---------

Signed-off-by: machine424 <ayoubmrini424@gmail.com>
Co-authored-by: Guillermo Sanchez Gavier <gsanchez@newrelic.com>
2024-02-01 12:34:37 +00:00
Paweł Szulik 7f24efccdb Refactor discovery tests to use testify.
Signed-off-by: Paweł Szulik <paul.szulik@gmail.com>
2024-01-31 16:42:11 +00:00
tyltr b538919258 remove redundant code
Signed-off-by: tyltr <tylitianrui@126.com>
2024-01-31 20:27:23 +08:00
Julien Pivotto 960b6266e2
Merge pull request #13447 from ptodev/update-puppetdb-metric-label
Change metric label for Puppet SD from 'http' to 'puppetdb'
2024-01-26 15:40:15 +01:00
Augustin Husson 9a9e73b34a
update all go dependencies (#13438)
Signed-off-by: Augustin Husson <husson.augustin@gmail.com>
2024-01-24 16:53:32 +01:00
Paulin Todev 53589fde4f
Change metric label for Puppetdb from 'http' to 'puppetdb'
Signed-off-by: Paulin Todev <paulin.todev@gmail.com>
2024-01-23 19:24:13 +00:00
Paulin Todev 78411d5e8b
SD Managers taking over responsibility for registration of debug metrics (#13375)
SD Managers take over responsibility for SD metrics registration

---------

Signed-off-by: Paulin Todev <paulin.todev@gmail.com>
Signed-off-by: Björn Rabenstein <github@rabenste.in>
Co-authored-by: Björn Rabenstein <github@rabenste.in>
2024-01-23 16:53:55 +01:00
Bryan Boreham b9952ff571
Merge pull request #13380 from machine424/fixes
fix(discovery tests): allow requireUpdate util to timeout in discovery/file…
2024-01-18 14:11:01 +00:00
machine424 2d01e56695
chore(kubernetes): check preconditions earlier and avoid unnecessary checks or iterations
Signed-off-by: machine424 <ayoubmrini424@gmail.com>
2024-01-16 12:10:35 +01:00
Bartlomiej Plotka db3544c552
Merge branch 'main' into merge-2.49.0-back
Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
2024-01-15 12:38:36 +01:00
machine424 afc05129f1
fix(discovery): allow requireUpdate util to timeout in discovery/file/file_test.go.
The loop ran indefinitely if the condition isn't met.

Before, each iteration created a new timer channel which was always outpaced by
the other timer channel with smaller duration.

minor detail: There was a memory leak: resources of the ~10 previous timers were
constantly kept. With the fix, we may keep the resources of one timer around for defaultWait
but this isn't worth the changes to make it right.

Signed-off-by: machine424 <ayoubmrini424@gmail.com>
2024-01-07 21:18:08 +01:00
daniel-resdiary d79ad810aa Get VM Scale Set NIC (#13283)
Calling `*armnetwork.InterfacesClient.Get()` doesn't work for Scale Set
VM NIC, because these use a different Resource ID format.

Use `*armnetwork.InterfacesClient.GetVirtualMachineScaleSetNetworkInterface()`
instead.  This needs both the scale set name and the instance ID, so
add an `InstanceID` field to the `virtualMachine` struct.  `InstanceID`
is empty for a VM that isn't a ScaleSetVM.

Signed-off-by: Daniel Nicholls <daniel.nicholls@resdiary.com>
2023-12-19 19:20:02 +00:00
Julien Pivotto 5d5303c746
Merge pull request #13285 from daniel-resdiary/DedupGetNic
Dedup code handling getting network interface
2023-12-18 15:27:18 +01:00
Julien Pivotto 7600e6cc05
Merge pull request #13294 from mmorel-35/azure
build(deps): bump github.com/Azure/azure-sdk-for-go/sdk/resourcemanager
2023-12-18 12:13:50 +01:00
Julien Pivotto 10557ed8e0
Merge pull request #13278 from Automaat/feat/extend-kuma-sd-api
kuma_sd: Extend Kuma SD configuration to allow users to specify ClientId
2023-12-18 12:12:07 +01:00
Matthieu MOREL 952cb41373 build(deps): bump github.com/Azure/azure-sdk-for-go/sdk/resourcemanager
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2023-12-15 10:25:07 +00:00
Daniel Nicholls 0704c72548 Dedup code handling getting network interface
Signed-off-by: Daniel Nicholls <daniel.nicholls@resdiary.com>
2023-12-13 14:14:01 +00:00
daniel-resdiary 103133124a
Get VM Scale Set NIC (#13283)
Calling `*armnetwork.InterfacesClient.Get()` doesn't work for Scale Set
VM NIC, because these use a different Resource ID format.

Use `*armnetwork.InterfacesClient.GetVirtualMachineScaleSetNetworkInterface()`
instead.  This needs both the scale set name and the instance ID, so
add an `InstanceID` field to the `virtualMachine` struct.  `InstanceID`
is empty for a VM that isn't a ScaleSetVM.

Signed-off-by: Daniel Nicholls <daniel.nicholls@resdiary.com>
2023-12-13 12:38:16 +00:00
Marcin Skalski e27232614a code review
Signed-off-by: Marcin Skalski <skalskimarcin33@gmail.com>
2023-12-12 08:32:46 +01:00
Marcin Skalski 0af810aa71 fix go lint
Signed-off-by: Marcin Skalski <skalskimarcin33@gmail.com>
2023-12-11 17:01:52 +01:00
Marcin Skalski d12ccf9fa2 kuma_sd: Extend Kuma SD configuration to allow users to specify ClientId
Signed-off-by: Marcin Skalski <skalskimarcin33@gmail.com>
2023-12-11 16:33:42 +01:00
Paulin Todev d2e997030e
Fix linter issues
Signed-off-by: Paulin Todev <paulin.todev@gmail.com>
2023-12-11 14:28:37 +00:00
Paulin Todev 27bb57a37b
Define metric label values in one place
Signed-off-by: Paulin Todev <paulin.todev@gmail.com>
2023-12-11 13:39:01 +00:00
Paulin Todev 108a749a45
Set up labels for counters in advance
Signed-off-by: Paulin Todev <paulin.todev@gmail.com>
2023-12-11 13:39:00 +00:00
Paulin Todev 6a5306a53c
Use const labels for Discovery Manager metrics.
Signed-off-by: Paulin Todev <paulin.todev@gmail.com>
2023-12-11 11:14:27 +00:00
Paulin Todev 6de80d7fb0
Allow non-default registry to be used for metrics of SD components
Signed-off-by: Paulin Todev <paulin.todev@gmail.com>
2023-12-11 11:14:26 +00:00
Gavin Lam d5924f305b
Fix missing public IP addresses in Azure service discovery (#13241)
Fixes #5588, #9293

Signed-off-by: Gavin Lam <gavin.oss@tutamail.com>
2023-12-07 11:42:14 +00:00
Matthieu MOREL 9c4782f1cc
golangci-lint: enable testifylint linter (#13254)
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2023-12-07 11:35:01 +00:00
Julien Pivotto 52b15d2195
Merge pull request #13219 from djcode/main
Discovery: Add support for NS records to DNS Discovery
2023-12-01 16:10:07 +01:00
Daniel Jolly 9ebea5e1ca Removed extra whitespace
Signed-off-by: Daniel Jolly <code@danieljolly.com>
2023-11-29 12:50:09 -05:00
Oleksandr Redko 2a75604f8e
Enable default revive rules (#13068)
Signed-off-by: Oleksandr Redko <Oleksandr_Redko@epam.com>
2023-11-29 17:23:34 +00:00
Daniel Jolly 2c420b518e Add support for NS records in DNS discovery
Signed-off-by: Daniel Jolly <code@danieljolly.com>
2023-11-29 12:21:50 -05:00
Charles Korn 2329fba0e5
Fix linting issues in comments (#13178)
Signed-off-by: Charles Korn <charles.korn@grafana.com>
2023-11-23 09:07:23 +00:00
Etourneau Gwenn b37258c99b
Added Caching of network interface for Azure (#12622)
* Added Caching of network interface for Azure

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>

* Rename Counter for Azure cache

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>

* Format with goimports

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>

* Updated duration comparaison
Enabled cache by default with 5x the default refresh time

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>

* Change random function

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>

* Remove refresh interval

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>

* Remove from config as well

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>

* Reformat config_test
Removed uneeded error

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>

---------

Signed-off-by: Etourneau Gwenn <getourneau@yugabyte.com>
2023-11-21 11:59:17 +00:00
Bryan Boreham 6613195487
Merge pull request #13097 from roidelapluie/gpu
Linode SD: Add __meta_linode_gpus label
2023-11-13 14:22:37 +00:00
Julien Pivotto 222d46d243 Linode: Add GPU label
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2023-11-03 15:34:31 -04:00
Matthieu MOREL fe057fc60d use Go standard errors package
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2023-11-03 07:26:31 +00:00
Oleksandr Redko fa90ca46e5 ci(lint): enable godot; append dot at the end of comments
Signed-off-by: Oleksandr Redko <Oleksandr_Redko@epam.com>
2023-10-31 19:53:38 +02:00
Matthieu MOREL 68e6b4dd34
ci(lint): enable errorlint on discovery (#12918)
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2023-10-31 12:46:55 +01:00
Oleksandr Redko 8e5f0387a2
ci(lint): enable nolintlint and remove redundant comments (#12926)
Signed-off-by: Oleksandr Redko <Oleksandr_Redko@epam.com>
2023-10-31 12:35:13 +01:00
Matthieu MOREL bbdc7d5902 Update Azure SDK For Go
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2023-10-14 03:39:31 +00:00
Julien Pivotto 4b735f02a6
Merge pull request #10569 from zzJinux/discovery-manager-run
Fix discovery managers to be properly cancelled
2023-09-29 12:07:55 +02:00
Julien Pivotto d659e672f1
Merge pull request #12816 from slashpai/lightsail
LIGHTSAIL SD: Validate HTTP config
2023-09-08 15:01:35 +02:00
Julien Pivotto 738e0dbaf0
Merge pull request #12814 from slashpai/puppetdb
PUPPETDB SD: Validate HTTP config
2023-09-08 15:00:21 +02:00
Julien Pivotto a2208b2849
Merge pull request #12815 from slashpai/Uyuni
UYUNI SD: Validate HTTP config
2023-09-08 14:59:50 +02:00
Jayapriya Pai 57988fda75
LIGHTSAIL SD: Validate HTTP config
Related-to #12810

Signed-off-by: Jayapriya Pai <janantha@redhat.com>
2023-09-08 17:54:58 +05:30
Jayapriya Pai 344f9d8d15
PUPPETDB SD: Validate HTTP config
Related-to #12810

Signed-off-by: Jayapriya Pai <janantha@redhat.com>
2023-09-08 17:42:01 +05:30
Jayapriya Pai 34c8dcc73f
UYUNI SD: Validate HTTP config
Related-to #12810

Signed-off-by: Jayapriya Pai <janantha@redhat.com>
2023-09-08 17:34:41 +05:30
Julien Pivotto 75470ba815
Merge pull request #12812 from slashpai/azure
AZURE SD: Validate HTTP config
2023-09-08 14:03:19 +02:00
Jayapriya Pai e44d5dfb3a
AZURE SD: Validate HTTP config
Related-to #12810

Signed-off-by: Jayapriya Pai <janantha@redhat.com>
2023-09-08 13:10:31 +05:30
Jayapriya Pai 7f085aeb6a
EC2 SD: Validate HTTP config
Related-to #12810

Signed-off-by: Jayapriya Pai <janantha@redhat.com>
2023-09-08 11:46:07 +05:30
Julien Pivotto 8498b96a2b HTTP SD: Validate HTTP config
Fixes #12762

Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2023-09-03 23:06:30 +02:00
Julien Pivotto 009017a3fb Revert "Remove deleted target from discovery manager"
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2023-08-14 23:29:39 +02:00
Julien Pivotto 103b8567d6
Merge pull request #12541 from haleyao/main
Remove deleted target from discovery manager
2023-08-14 15:37:59 +02:00
Julien Pivotto ea039fcebf
Merge pull request #11463 from proggga/small_refactor
marathon.go: Simplified conditions in method
2023-08-14 15:09:02 +02:00
Marc Tuduri 98383fdc63
sd: change hetzner role type and constants to be exportable
Signed-off-by: Marc Tuduri <marctc@protonmail.com>
2023-07-31 12:59:50 +02:00
Julien Pivotto 2bdb2e627f
Merge pull request #10914 from wangrzneu/add-endpointslice-label
Add more labels for endpointslice and endpoints role in k8s discovery
2023-07-18 13:35:03 +02:00
Julian Tölle 7aa7965716
build(deps): bump github.com/hetznercloud/hcloud-go to v2.0.0
Version 2 introduced a breaking change in the `id` field of all
resources. They were changed from `int` to `int64` to make sure that all
future numerical IDs are supported on all architectures.

You can learn more about this
[here](https://docs.hetzner.cloud/#deprecation-notices-%E2%9A%A0%EF%B8%8F)

Signed-off-by: Julian Tölle <julian.toelle@hetzner-cloud.de>
2023-07-17 14:25:29 +02:00
Daniel Swarbrick de89a8c827
Linode SD: cast InstanceSpec values to int64 to avoid overflows
InstanceSpec struct members are untyped integers, so they can overflow
on 32-bit arch when bit-shifted left.

Signed-off-by: Daniel Swarbrick <daniel.swarbrick@gmail.com>
2023-07-16 19:23:44 +02:00
Mikhail Fesenko 02e11cc2a7
Fix from discussion
Signed-off-by: Mikhail Fesenko <proggga@gmail.com>
2023-07-13 00:53:19 +02:00
Mikhail Fesenko 17cdfdd79f
maraphon.go: Simplified conditions in method
Signed-off-by: Mikhail Fesenko <proggga@gmail.com>
2023-07-13 00:44:13 +02:00
haleyao c5a37ddad5 Remove deleted target from discovery manager
Signed-off-by: haleyao <haleyao@tencent.com>
2023-07-10 00:09:25 +08:00
Julien Pivotto 076056ccdf
Merge pull request #11642 from zoonage/main
Do not add pods to target group if the PodIP status is not set
2023-07-05 23:10:50 +02:00
Taavi Väänänen 0f4c27e2bf
discovery/openstack: Include instance image ID in labels
Adds a new label to include the ID of the image that an instance is
using. This can be used for example to filter a job to only include
instances using a certain image as that image includes some exporter.

Sometimes the image information isn't available, such as when the image
is private and the user doesn't have the roles required to see it. In
those cases we just don't set the label, as the rest of the information
from the discovery provider can still be used.

Signed-off-by: Taavi Väänänen <hi@taavi.wtf>
2023-07-04 18:50:11 +03:00
Matt Harbison 02277bbe0d Fix path handling in File-SD watcher to allow directory monitoring on Windows
Previously, `d.paths` were normalized to backslashes on Windows, even when if
the config file used Unix style.  The end result meant always watching `./`, so
changes for this config were always ignored:

    scrape_configs:
      - job_name: 'envmsc1'
        file_sd_configs:
        - files:
          - 'targets/envmsc1.d/*.yml'
          - 'targets/envmsc1.d/*.yaml'

Additionally, unlike the other platforms, no warning was emitted on startup
about not being able to install the watch if the directory didn't exist.  Now it
is logged.

Signed-off-by: Matt Harbison <mharbison@atto.com>
2023-06-23 16:37:04 -04:00
renzheng.wang b2c5de2e65 fix lint issue
Signed-off-by: renzheng.wang <wangrzneu@gmail.com>
2023-05-30 20:35:04 +08:00
renzheng.wang 98ffad01b8 update tests and docs
Signed-off-by: renzheng.wang <wangrzneu@gmail.com>
2023-05-30 20:13:52 +08:00
renzheng.wang 866fa25b20 add label and labelpresent for endpointslice role in k8s discovery
Signed-off-by: renzheng.wang <wangrzneu@gmail.com>
2023-05-30 20:13:38 +08:00
Julien Pivotto 0dc31ade41 Add support for consul path_prefix
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
2023-05-17 00:14:58 +02:00
Mickael Carl 2f35619710 discovery/kubernetes: attach node labels when the endpoints TargetRef's kind are Node
Signed-off-by: Mickael Carl <mcarl@apple.com>
2023-05-11 10:11:56 +01:00
cui fliter 276ca6a883 fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2023-04-25 14:19:16 +08:00
Matthieu MOREL bae9a21200
Merge branch 'main' into linter/nilerr
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2023-04-19 19:56:39 +02:00
beorn7 5b53aa1108 style: Replace else if cascades with switch
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.

The exceptions that I have found in our codebase are just these two:

* The `if else` is followed by an additional statement before the next
  condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
  used. In this case, using `switch` would require tagging the `for`
  loop, which probably tips the balance.

Why are `switch` statements more readable?

For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.

I'm sure the aforemention wise coders can list even more reasons.

In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.

Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-19 17:22:31 +02:00
beorn7 c3c7d44d84 lint: Adjust to the lint warnings raised by current versions of golint-ci
We haven't updated golint-ci in our CI yet, but this commit prepares
for that.

There are a lot of new warnings, and it is mostly because the "revive"
linter got updated. I agree with most of the new warnings, mostly
around not naming unused function parameters (although it is justified
in some cases for documentation purposes – while things like mocks are
a good example where not naming the parameter is clearer).

I'm pretty upset about the "empty block" warning to include `for`
loops. It's such a common pattern to do something in the head of the
`for` loop and then have an empty block. There is still an open issue
about this: https://github.com/mgechev/revive/issues/810 I have
disabled "revive" altogether in files where empty blocks are used
excessively, and I have made the effort to add individual
`// nolint:revive` where empty blocks are used just once or twice.
It's borderline noisy, though, but let's go with it for now.

I should mention that none of the "empty block" warnings for `for`
loop bodies were legitimate.

Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-19 17:10:10 +02:00