Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add private ips to labels #261

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -7,6 +7,7 @@
* `__meta_hcloud_disk`
* `__meta_hcloud_image_name`
* `__meta_hcloud_image_type`
* `__meta_hcloud_ipv4_<name>`
* `__meta_hcloud_label_<name>`
* `__meta_hcloud_location`
* `__meta_hcloud_memory`
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)
}