2021-03-05 18:47:23 +00:00
|
|
|
package fetcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
2024-01-17 16:49:34 +00:00
|
|
|
"github.com/hetznercloud/hcloud-go/v2/hcloud"
|
2021-03-05 18:47:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ Fetcher = &serverTraffic{}
|
|
|
|
|
2021-03-05 18:53:37 +00:00
|
|
|
// NewServerTraffic creates a new fetcher that will collect pricing information on server traffic.
|
2023-01-25 19:24:33 +00:00
|
|
|
func NewServerTraffic(pricing *PriceProvider, additionalLabels ...string) Fetcher {
|
2023-01-25 20:23:54 +00:00
|
|
|
return &serverTraffic{newBase(pricing, "server_traffic", []string{"location", "type"}, additionalLabels...)}
|
2021-03-05 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type serverTraffic struct {
|
|
|
|
*baseFetcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (serverTraffic serverTraffic) Run(client *hcloud.Client) error {
|
2024-01-17 19:19:21 +00:00
|
|
|
servers, err := client.Server.All(ctx)
|
2021-03-05 18:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range servers {
|
|
|
|
location := s.Datacenter.Location
|
|
|
|
|
2023-01-25 19:24:33 +00:00
|
|
|
labels := append([]string{
|
|
|
|
s.Name,
|
|
|
|
location.Name,
|
|
|
|
s.ServerType.Name,
|
|
|
|
},
|
|
|
|
parseAdditionalLabels(serverTraffic.additionalLabels, s.Labels)...,
|
|
|
|
)
|
|
|
|
|
2021-03-05 19:28:59 +00:00
|
|
|
additionalTraffic := int(s.OutgoingTraffic) - int(s.IncludedTraffic)
|
2021-03-05 18:47:23 +00:00
|
|
|
if additionalTraffic < 0 {
|
2023-01-25 19:24:33 +00:00
|
|
|
serverTraffic.hourly.WithLabelValues(labels...).Set(0)
|
|
|
|
serverTraffic.monthly.WithLabelValues(labels...).Set(0)
|
2021-03-05 18:47:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-07-25 18:59:46 +00:00
|
|
|
serverTrafficPrice, err := serverTraffic.pricing.ServerTraffic(s.ServerType, location.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
monthlyPrice := math.Ceil(float64(additionalTraffic)/sizeTB) * serverTrafficPrice
|
2021-03-05 18:47:23 +00:00
|
|
|
hourlyPrice := pricingPerHour(monthlyPrice)
|
|
|
|
|
2023-01-25 19:24:33 +00:00
|
|
|
serverTraffic.hourly.WithLabelValues(labels...).Set(hourlyPrice)
|
|
|
|
serverTraffic.monthly.WithLabelValues(labels...).Set(monthlyPrice)
|
2021-03-05 18:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|