Skip to content

Commit

Permalink
Merge pull request #924 from apricote/hcloud-go-metrics
Browse files Browse the repository at this point in the history
✨ Add metrics from hcloud-go
  • Loading branch information
janiskemper authored Sep 22, 2023
2 parents a4d68a4 + ec559ee commit 39b5036
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/services/hcloud/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"strings"

"github.com/hetznercloud/hcloud-go/v2/hcloud"
"github.com/prometheus/client_golang/prometheus"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

const errStringUnauthorized = "(unauthorized)"
Expand Down Expand Up @@ -76,7 +78,17 @@ type Factory interface {

// NewClient creates new HCloud clients.
func (f *factory) NewClient(hcloudToken string) Client {
return &realClient{client: hcloud.NewClient(hcloud.WithToken(hcloudToken))}
opts := []hcloud.ClientOption{
hcloud.WithToken(hcloudToken),
}

// controller-runtime hides their default prometheus registry it uses (and exposes via HTTP) behind a custom
// interface. hcloud-go wants the actual registry struct, so we need to get it from the controller-runtime package.
if registry, ok := metrics.Registry.(*prometheus.Registry); ok {
opts = append(opts, hcloud.WithInstrumentation(registry))
}

return &realClient{client: hcloud.NewClient(opts...)}
}

type factory struct{}
Expand Down

0 comments on commit 39b5036

Please sign in to comment.