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
8 changes: 8 additions & 0 deletions cmd/application-load-balancer-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"os"

"github.com/stackitcloud/application-load-balancer-controller/pkg/controller/ingress"
"github.com/stackitcloud/application-load-balancer-controller/pkg/metrics"
albclient "github.com/stackitcloud/application-load-balancer-controller/pkg/stackit"
stackitconfig "github.com/stackitcloud/application-load-balancer-controller/pkg/stackit/config"
sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
albsdk "github.com/stackitcloud/stackit-sdk-go/services/alb/v2api"
certsdk "github.com/stackitcloud/stackit-sdk-go/services/certificates/v2api"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -32,6 +34,10 @@ type options struct {
cloudConfig string
}

func init() {
ctrlmetrics.Registry.MustRegister(metrics.NewExporter())
}

// nolint:funlen // This function isn't awfully complex.
func main() {
var opts options
Expand Down Expand Up @@ -75,13 +81,15 @@ func main() {
}
albOpts := []sdkconfig.ConfigurationOption{
sdkconfig.WithUserAgent("application-load-balancer-controller"),
sdkconfig.WithHTTPClient(metrics.NewHTTPClient("application-load-balancer-controller")),
}
if config.Global.APIEndpoints.ApplicationLoadBalancerAPI != "" {
albOpts = append(albOpts, sdkconfig.WithEndpoint(config.Global.APIEndpoints.ApplicationLoadBalancerAPI))
}

certOpts := []sdkconfig.ConfigurationOption{
sdkconfig.WithUserAgent("application-load-balancer-controller"),
sdkconfig.WithHTTPClient(metrics.NewHTTPClient("application-load-balancer-controller")),
}
if config.Global.APIEndpoints.ApplicationLoadBalancerCertificateAPI != "" {
certOpts = append(certOpts, sdkconfig.WithEndpoint(config.Global.APIEndpoints.ApplicationLoadBalancerCertificateAPI))
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require (
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/stackitcloud/stackit-sdk-go/core v0.26.0
github.com/stackitcloud/stackit-sdk-go/services/alb v0.17.1
github.com/stackitcloud/stackit-sdk-go/services/certificates v1.9.1
Expand Down Expand Up @@ -38,13 +40,12 @@ require (
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 // 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/pflag v1.0.9 // indirect
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