Skip to content
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
6 changes: 6 additions & 0 deletions cmd/machine-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import (
"github.com/gardener/machine-controller-manager/pkg/util/provider/app/options"
_ "github.com/gardener/machine-controller-manager/pkg/util/reflector/prometheus" // for reflector metric registration
_ "github.com/gardener/machine-controller-manager/pkg/util/workqueue/prometheus" // for workqueue metric registration
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/pflag"
"github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/metrics"
cp "github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/provider"
"github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/spi"
"k8s.io/component-base/cli/flag"
"k8s.io/component-base/logs"
"k8s.io/klog/v2"
)

func init() {
prometheus.MustRegister(metrics.NewExporter())
}

func main() {
s := options.NewMCServer()
s.AddFlags(pflag.CommandLine)
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ go 1.26.2

require (
github.com/gardener/machine-controller-manager v0.62.1
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.32.1
github.com/onsi/gomega v1.42.1
github.com/prometheus/client_golang v1.23.2
github.com/prometheus/client_model v0.6.2
github.com/spf13/pflag v1.0.10
github.com/stackitcloud/stackit-sdk-go/core v0.26.0
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.14.1
Expand Down Expand Up @@ -33,17 +36,15 @@ require (
github.com/google/gnostic-models v0.7.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.23.2 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.67.5 // indirect
github.com/prometheus/procfs v0.19.2 // indirect
github.com/spf13/cobra v1.10.2 // indirect
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

"github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/metrics"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api"
Expand Down Expand Up @@ -74,6 +75,8 @@ func createIAASClient(serviceAccountKey string) (*iaas.APIClient, error) {
opts = append(opts, config.WithTokenEndpoint(tokenEndpoint))
}

opts = append(opts, config.WithHTTPClient(metrics.NewHTTPClient("machine-controller-manager-provider-stackit")))

iaasClient, err := iaas.NewAPIClient(opts...)
if err != nil {
return nil, fmt.Errorf("failed to create STACKIT SDK API client: %w", err)
Expand Down
128 changes: 128 additions & 0 deletions pkg/metrics/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package metrics

import (
"net/http"
"runtime"
"strconv"
"strings"
"time"
"unicode"
"unicode/utf8"

"github.com/prometheus/client_golang/prometheus"
)

const UnknownOperation = "UnknownOperation"

func NewHTTPClient(componentName string) *http.Client {
return WrapHTTPClient(http.DefaultClient, componentName)
}

func WrapHTTPClient(client *http.Client, componentName string) *http.Client {
if client == nil {
return nil
}
wrappedClient := *client

baseTransport := client.Transport
if baseTransport == nil {
baseTransport = http.DefaultTransport
}

// Chain your instrumented round tripper
wrappedClient.Transport = &InstrumentedRoundTripper{
base: baseTransport,
componentName: componentName,
}

return &wrappedClient
}

type InstrumentedRoundTripper struct {
base http.RoundTripper
componentName string
}

func (rt *InstrumentedRoundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
startTime := time.Now()
response, err := rt.base.RoundTrip(request)
duration := time.Since(startTime)

statusCode := "network_error"
if response != nil {
statusCode = strconv.Itoa(response.StatusCode)
}

// request.Host is optional so we can fallback to request.URL.Host (if available)
host := request.Host
if host == "" && request.URL != nil {
host = request.URL.Host
}

labels := prometheus.Labels{
componentLabel: rt.componentName,
hostLabel: host,
methodLabel: request.Method,
operationLabel: getSDKOperationName(),
codeLabel: statusCode,
}

HTTPRequestDurationHistogram.With(labels).Observe(duration.Seconds())
HTTPRequestCount.With(labels).Inc()

isHTTPError := response != nil && response.StatusCode >= 400
isNetworkError := err != nil

if isHTTPError || isNetworkError {
HTTPErrorCount.With(labels).Inc()
}

return response, err
}

// getSDKOperationName returns the name of the STACKIT SDK function. To do this the function gets the last 10 callers and checks
// for functions from the stackitcloud/stackit-sdk-go. It fall back to UnknownOperation if no function was found.
func getSDKOperationName() string {
pc := make([]uintptr, 10)

// Skip 3 because the first 3 are always Callers, getSDKOperationName, RoundTrip.
n := runtime.Callers(3, pc)
if n == 0 {
return UnknownOperation
}

frames := runtime.CallersFrames(pc[:n])
moreFrames := true
for moreFrames {
var frame runtime.Frame
frame, moreFrames = frames.Next()

if !strings.Contains(frame.Function, "stackitcloud/stackit-sdk-go") {
continue
}

parts := strings.Split(frame.Function, ".")
if len(parts) > 0 {
funcName := parts[len(parts)-1]

// Skip function names with 0 len
// Skip Execute, because there is a function with more detailed name
// Skip RoundTrip, because this only the RoundTrip for the AuthFlow
if funcName == "" ||
funcName == "Execute" ||
funcName == "RoundTrip" {
continue
}

// Skip Private functions
r, _ := utf8.DecodeRuneInString(funcName)
if !unicode.IsUpper(r) {
continue
}

return strings.TrimSuffix(funcName, "Execute")
}
}

return UnknownOperation
}
Loading