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
74 changes: 74 additions & 0 deletions cmd/activator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ import (
"log"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"

"github.com/grafana/pyroscope-go"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"

Expand Down Expand Up @@ -92,6 +95,14 @@ type config struct {
// TODO: run loadtests using these flags to determine optimal default values.
MaxIdleProxyConns int `split_words:"true" default:"1000"`
MaxIdleProxyConnsPerHost int `split_words:"true" default:"100"`

// Pyroscope continuous profiling configuration (optional)
PyroscopeServerAddress string `split_words:"true"`
PyroscopeBasicAuthUser string `split_words:"true"`
PyroscopeBasicAuthPass string `split_words:"true"`
PyroscopeTags string `split_words:"true"` // Format: "key1=value1;key2=value2"
MutexProfileRate int `split_words:"true" default:"100"`
BlockProfileRate int `split_words:"true" default:"10000"`
}

func main() {
Expand Down Expand Up @@ -158,6 +169,46 @@ func main() {
ctx = pkglogging.WithLogger(ctx, logger)
defer flush(logger)

// Initialize Pyroscope continuous profiling if server address is configured
if env.PyroscopeServerAddress != "" {
runtime.SetMutexProfileFraction(env.MutexProfileRate)
runtime.SetBlockProfileRate(env.BlockProfileRate)

// Parse custom tags and add default hostname tag
tags := parsePyroscopeTags(env.PyroscopeTags)
tags["hostname"] = env.PodName

pyroscopeConfig := pyroscope.Config{
ApplicationName: "knative.activator",
ServerAddress: env.PyroscopeServerAddress,
Logger: logger,
Tags: tags,
ProfileTypes: []pyroscope.ProfileType{
pyroscope.ProfileCPU,
pyroscope.ProfileAllocObjects,
pyroscope.ProfileAllocSpace,
pyroscope.ProfileInuseObjects,
pyroscope.ProfileInuseSpace,
pyroscope.ProfileGoroutines,
pyroscope.ProfileMutexCount,
pyroscope.ProfileMutexDuration,
pyroscope.ProfileBlockCount,
pyroscope.ProfileBlockDuration,
},
}

// Add basic auth if credentials are provided
if env.PyroscopeBasicAuthUser != "" && env.PyroscopeBasicAuthPass != "" {
pyroscopeConfig.BasicAuthUser = env.PyroscopeBasicAuthUser
pyroscopeConfig.BasicAuthPassword = env.PyroscopeBasicAuthPass
}

if _, err := pyroscope.Start(pyroscopeConfig); err != nil {
log.Printf("Failed to start Pyroscope profiler: %v", err)
} else {
log.Printf("Pyroscope profiler started, sending to %s", env.PyroscopeServerAddress)
}
}
// Run informers instead of starting them from the factory to prevent the sync hanging because of empty handler.
if err := controller.StartInformers(ctx.Done(), informers...); err != nil {
logger.Fatalw("Failed to start informers", zap.Error(err))
Expand Down Expand Up @@ -439,3 +490,26 @@ func getEnvInt(key string, defaultVal int) int {
}
return defaultVal
}

// parsePyroscopeTags parses a semicolon-separated string of key=value pairs into a map.
// Example: "env=prod;cluster=canary" -> map[string]string{"env": "prod", "cluster": "canary"}
func parsePyroscopeTags(tagsStr string) map[string]string {
tags := make(map[string]string)
if tagsStr == "" {
return tags
}
for _, pair := range strings.Split(tagsStr, ";") {
pair = strings.TrimSpace(pair)
if pair == "" {
continue
}
if key, value, ok := strings.Cut(pair, "="); ok {
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)
if key != "" {
tags[key] = value
}
}
}
return tags
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/google/go-containerregistry/pkg/authn/k8schain v0.0.0-20250115185438-c4dd792fa06c
github.com/google/gofuzz v1.2.0
github.com/gorilla/websocket v1.5.3
github.com/grafana/pyroscope-go v1.2.7
github.com/hashicorp/golang-lru v1.0.2
github.com/influxdata/influxdb-client-go/v2 v2.9.0
github.com/kelseyhightower/envconfig v1.4.0
Expand Down Expand Up @@ -109,6 +110,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/grafana/pyroscope-go/godeltaprof v0.1.9 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/influxdata/tdigest v0.0.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grafana/pyroscope-go v1.2.7 h1:VWBBlqxjyR0Cwk2W6UrE8CdcdD80GOFNutj0Kb1T8ac=
github.com/grafana/pyroscope-go v1.2.7/go.mod h1:o/bpSLiJYYP6HQtvcoVKiE9s5RiNgjYTj1DhiddP2Pc=
github.com/grafana/pyroscope-go/godeltaprof v0.1.9 h1:c1Us8i6eSmkW+Ez05d3co8kasnuOY813tbMN8i/a3Og=
github.com/grafana/pyroscope-go/godeltaprof v0.1.9/go.mod h1:2+l7K7twW49Ct4wFluZD3tZ6e0SjanjcUUBPVD/UuGU=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc h1:GN2Lv3MGO7AS6PrRoT6yV5+wkrOpcszoIsO4+4ds248=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc/go.mod h1:+JKpmjMGhpgPL+rXZ5nsZieVzvarn86asRlBg4uNGnk=
github.com/grpc-ecosystem/grpc-gateway v1.14.6/go.mod h1:zdiPV4Yse/1gnckTHtghG4GkDEdKCRJduHpTxT3/jcw=
Expand Down Expand Up @@ -491,6 +495,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
6 changes: 6 additions & 0 deletions vendor/github.com/grafana/pyroscope-go/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions vendor/github.com/grafana/pyroscope-go/.golangci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/grafana/pyroscope-go/CODEOWNERS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading