Skip to content

Commit

Permalink
feat: add private ips to labels
Browse files Browse the repository at this point in the history
  • Loading branch information
tboerger committed Dec 18, 2023
1 parent 479f0f1 commit 691eb1e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
7 changes: 7 additions & 0 deletions changelog/1.1.0_2023-12-18/private-ips.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Enhancement: Add private IPs as labels

We have added a list of all attached private networks per server, that way you
are now also able to connect optionally through the private address instead of
using the public address.

https://github.com/promhippie/prometheus-hcloud-sd/pull/261
1 change: 1 addition & 0 deletions docs/partials/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* `__meta_hcloud_name`
* `__meta_hcloud_os_flavor`
* `__meta_hcloud_os_version`
* `__meta_hcloud_private_ipv4_<name>`
* `__meta_hcloud_project`
* `__meta_hcloud_public_ipv4`
* `__meta_hcloud_public_ipv6`
Expand Down
31 changes: 30 additions & 1 deletion pkg/action/discoverer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
"osFlavor": providerPrefix + "os_flavor",
"osVersion": providerPrefix + "os_version",
"project": providerPrefix + "project",
"privateIPv4": providerPrefix + "ipv4_",
"publicIPv4": providerPrefix + "public_ipv4",
"publicIPv6": providerPrefix + "public_ipv6",
"serverTypeCores": providerPrefix + "cores",
Expand Down Expand Up @@ -85,8 +86,21 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro
for project, client := range d.clients {

now := time.Now()

networks, err := client.Network.All(ctx)

if err != nil {
level.Warn(d.logger).Log(
"msg", "Failed to fetch networks",
"project", project,
"err", err,
)

requestFailures.WithLabelValues(project).Inc()
continue
}

servers, err := client.Server.All(ctx)
requestDuration.WithLabelValues(project).Observe(time.Since(now).Seconds())

if err != nil {
level.Warn(d.logger).Log(
Expand All @@ -99,6 +113,8 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro
continue
}

requestDuration.WithLabelValues(project).Observe(time.Since(now).Seconds())

level.Debug(d.logger).Log(
"msg", "Requested servers",
"project", project,
Expand Down Expand Up @@ -155,6 +171,15 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro
target.Labels[model.LabelName(normalizeLabel(Labels["labelPrefix"]+key))] = model.LabelValue(value)
}

for _, priv := range server.PrivateNet {
for _, network := range networks {
if network.ID == priv.Network.ID {
target.Labels[model.LabelName(normalizeNetwork(Labels["privateIPv4"]+network.Name))] = model.LabelValue(priv.IP.String())
break
}
}
}

level.Debug(d.logger).Log(
"msg", "Server added",
"project", project,
Expand Down Expand Up @@ -190,3 +215,7 @@ func (d *Discoverer) getTargets(ctx context.Context) ([]*targetgroup.Group, erro
func normalizeLabel(val string) string {
return replacer.Replace(val)
}

func normalizeNetwork(val string) string {
return replacer.Replace(val)
}

0 comments on commit 691eb1e

Please sign in to comment.