### Run Docker Compose Example Source: https://github.com/grafana/pyroscope-go/blob/main/example/http/README.md Instructions to run the provided Docker Compose setup for the Pyroscope HTTP handler example. This allows testing the integration in a containerized environment. ```bash docker-compose up --build ``` -------------------------------- ### Install Pyroscope Go Module Source: https://github.com/grafana/pyroscope-go/blob/main/README.md Use 'go get' to add the pyroscope-go module to your project dependencies. ```bash go get github.com/grafana/pyroscope-go ``` -------------------------------- ### Start Profiling a Go Application Source: https://github.com/grafana/pyroscope-go/blob/main/README.md Configure and start the Pyroscope profiler within your Go application's main function. Ensure to replace placeholder values for server address and authentication if necessary. ```go package main import "github.com/grafana/pyroscope-go" func main() { pyroscope.Start(pyroscope.Config{ ApplicationName: "simple.golang.app", // replace this with the address of pyroscope server ServerAddress: "http://pyroscope-server:4040", // you can disable logging by setting this to nil Logger: pyroscope.StandardLogger, // Optional HTTP Basic authentication (Grafana Cloud) BasicAuthUser: "", BasicAuthPassword: "", // Optional Pyroscope tenant ID (only needed if using multi-tenancy). Not needed for Grafana Cloud. // TenantID: "", // by default all profilers are enabled, // but you can select the ones you want to use: ProfileTypes: []pyroscope.ProfileType{ pyroscope.ProfileCPU, pyroscope.ProfileAllocObjects, pyroscope.ProfileAllocSpace, pyroscope.ProfileInuseObjects, pyroscope.ProfileInuseSpace, }, }) // your code goes here } ``` -------------------------------- ### Using Go Tool Pprof for Delta Profiles Source: https://github.com/grafana/pyroscope-go/blob/main/godeltaprof/README.md This command demonstrates how to use the `go tool pprof` command to generate a delta profile for the heap. It specifies a `seconds` parameter to capture a profile after a delay, allowing for comparison between two points in time. ```bash go tool pprof http://localhost:6060/debug/pprof/heap?seconds=30 ``` -------------------------------- ### Go HTTP Server with Pyroscope and Standard Pprof Source: https://github.com/grafana/pyroscope-go/blob/main/example/http/README.md Sets up an HTTP server that integrates Pyroscope's continuous profiler with the standard Go pprof routes. This allows collecting CPU profiles via HTTP while sending them to Pyroscope. ```go package main import ( "net/http" "net/http/pprof" "time" "github.com/grafana/pyroscope-go" pyroscope_pprof "github.com/grafana/pyroscope-go/http/pprof" ) func main() { // Starting pyroscope profiler pyroscope.Start(pyroscope.Config{ ApplicationName: "example-app", ServerAddress: "http://pyroscope:4040", }) // Setting up HTTP server mux := http.NewServeMux() server := &http.Server{ Addr: ":8080", Handler: mux, } // Standard pprof routes (copied from /net/http/pprof) mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) mux.HandleFunc("/debug/pprof/trace", pprof.Trace) // This route is special: note that we're using Pyroscope handler here mux.HandleFunc("/debug/pprof/profile", pyroscope_pprof.Profile) go doMeaninglessWork() server.ListenAndServe() } // doMeaninglessWork does meaningless work to show CPU usage func doMeaninglessWork() { for { for t := time.Now(); time.Now().Sub(t).Seconds() < 1; { } } } ``` -------------------------------- ### Benchmark Results Comparison Source: https://github.com/grafana/pyroscope-go/blob/main/godeltaprof/README.md These benchmark results compare the performance and profile sizes of three methods for generating memory profiles: standard runtime/pprof (BenchmarkOG), DataDog's fastdelta (BenchmarkFastDelta), and godeltaprof (BenchmarkGodeltaprof). godeltaprof shows significantly reduced profile sizes. ```text BenchmarkOG 63 181862189 ns/op profile sizes: [209117 209107 209077 209089 209095 209076 209088 209082 209090 209092] BenchmarkFastDelta 43 273936764 ns/op profile sizes: [169300 10815 8969 9511 9752 9376 9545 8959 10357 9536] BenchmarkGodeltaprof 366 31148264 ns/op profile sizes: [208898 11485 9347 9967 10291 9848 10085 9285 11033 9986] ``` -------------------------------- ### Collect and Analyze Profile Data Source: https://github.com/grafana/pyroscope-go/blob/main/example/http/README.md Commands to collect a CPU profile from the running service and analyze it using the pprof tool. This demonstrates how to access and inspect the generated profile data. ```bash curl http://localhost:8080/debug/pprof/profile > profile.pprof pprof -http=:8081 profile.pprof ``` -------------------------------- ### Enable Pull Mode Profiling Source: https://github.com/grafana/pyroscope-go/blob/main/README.md To enable pull mode, ensure profiling routes are exposed by your HTTP server. This typically involves importing the net/http/pprof package. ```go import _ "net/http/pprof" ``` -------------------------------- ### Add Tags to Profiling Data Source: https://github.com/grafana/pyroscope-go/blob/main/README.md Apply tags to profiling data using either Pyroscope's TagWrapper or pprof.Do for filtering in the UI. Ensure context and labels are correctly provided. ```go // these two ways of adding tags are equivalent: pyroscope.TagWrapper(context.Background(), pyroscope.Labels("controller", "slow_controller"), func(c context.Context) { slowCode() }) pprof.Do(context.Background(), pprof.Labels("controller", "slow_controller"), func(c context.Context) { slowCode() }) ``` -------------------------------- ### Register Pyroscope CPU Profiler HTTP Handler Source: https://github.com/grafana/pyroscope-go/blob/main/http/pprof/README.md Register the Pyroscope CPU profiler handler to a custom path. Avoids conflicts with the default Go pprof handler. ```go package main import ( "net/http" "github.com/grafana/pyroscope-go/http/pprof" ) func main() { http.HandleFunc("/debug/pprof/cpu", pprof.Profile) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.