Skip to content

Commit

Permalink
pyroscope: add go tools
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Login <batazor@evrone.com>
  • Loading branch information
batazor committed Feb 20, 2023
1 parent b9c265c commit 9417722
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ require (
github.com/ory/dockertest/v3 v3.9.1
github.com/prometheus/client_golang v1.14.0
github.com/pterm/pterm v0.12.54
github.com/pyroscope-io/client v0.6.0
github.com/pyroscope-io/otel-profiling-go v0.4.0
github.com/rabbitmq/amqp091-go v1.7.0
github.com/redis/go-redis/extra/redisotel/v9 v9.0.2
github.com/redis/go-redis/v9 v9.0.2
Expand Down Expand Up @@ -199,6 +201,7 @@ require (
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/pyroscope-io/godeltaprof v0.1.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,12 @@ github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5b
github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s=
github.com/pterm/pterm v0.12.54 h1:7DX218ZhG2v3NsvmvsHeTJHC92zUyK9Bgeqbu0x4mG0=
github.com/pterm/pterm v0.12.54/go.mod h1:x6HvVq6rUC/Ik2u3MxMgS6kIx2Mlj1qLq5xquul2TWs=
github.com/pyroscope-io/client v0.6.0 h1:rcUFgcnfmuyVYDYT+4d0zfqc8YedOyruHSsUb9ImaBw=
github.com/pyroscope-io/client v0.6.0/go.mod h1:4h21iOU4pUOq0prKyDlvYRL+SCKsBc5wKiEtV+rJGqU=
github.com/pyroscope-io/godeltaprof v0.1.0 h1:UBqtjt0yZi4jTxqZmLAs34XG6ycS3vUTlhEUSq4NHLE=
github.com/pyroscope-io/godeltaprof v0.1.0/go.mod h1:psMITXp90+8pFenXkKIpNhrfmI9saQnPbba27VIaiQE=
github.com/pyroscope-io/otel-profiling-go v0.4.0 h1:Hk/rbUqOWoByoWy1tt4r5BX5xoKAvs5drr0511Ki8ic=
github.com/pyroscope-io/otel-profiling-go v0.4.0/go.mod h1:MXaofiWU7PgLP7eISUZJYVO4Z8WYMqpkYgeP4XrPLyg=
github.com/rabbitmq/amqp091-go v1.7.0 h1:V5CF5qPem5OGSnEo8BoSbsDGwejg6VUJsKEdneaoTUo=
github.com/rabbitmq/amqp091-go v1.7.0/go.mod h1:wfClAtY0C7bOHxd3GjmF26jEHn+rR/0B3+YV+Vn9/NI=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
Expand Down Expand Up @@ -1637,6 +1643,7 @@ go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.3
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.20.0/go.mod h1:2AboqHi0CiIZU0qwhtUfCYD1GeUzvvIXWNkhDt7ZMG4=
go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo=
go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs=
go.opentelemetry.io/otel v1.4.1/go.mod h1:StM6F/0fSwpd8dKWDCdRr7uRvEPYdW0hBSlbdTiUde4=
go.opentelemetry.io/otel v1.6.3/go.mod h1:7BgNga5fNlF/iZjG06hM3yofffp0ofKCDwSXx1GC4dI=
go.opentelemetry.io/otel v1.12.0/go.mod h1:geaoz0L0r1BEOR81k7/n9W4TCXYCJ7bPO7K374jQHG0=
go.opentelemetry.io/otel v1.13.0 h1:1ZAKnNQKwBBxFtww/GwxNUyTf0AxkZzrukO8MeXqe4Y=
Expand All @@ -1662,6 +1669,7 @@ go.opentelemetry.io/otel/sdk/export/metric v0.20.0/go.mod h1:h7RBNMsDJ5pmI1zExLi
go.opentelemetry.io/otel/sdk/metric v0.20.0/go.mod h1:knxiS8Xd4E/N+ZqKmUPf3gTTZ4/0TjTXukfxjzSTpHE=
go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw=
go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk=
go.opentelemetry.io/otel/trace v1.4.1/go.mod h1:iYEVbroFCNut9QkwEczV9vMRPHNKSSwYZjulEtsmhFc=
go.opentelemetry.io/otel/trace v1.6.3/go.mod h1:GNJQusJlUgZl9/TQBPKU/Y/ty+0iVB5fjhKeJGZPGFs=
go.opentelemetry.io/otel/trace v1.12.0/go.mod h1:pHlgBynn6s25qJ2szD+Bv+iwKJttjHSI3lUAyf0GNuQ=
go.opentelemetry.io/otel/trace v1.13.0 h1:CBgRZ6ntv+Amuj1jDsMhZtlAPT6gbyIRdaIzFhfBSdY=
Expand Down
30 changes: 30 additions & 0 deletions internal/di/pkg/profiling/profiling.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package profiling
import (
"net/http"
"net/http/pprof"
"runtime"

"github.com/pyroscope-io/client/pyroscope"
"github.com/spf13/viper"

"github.com/shortlink-org/shortlink/internal/pkg/logger"
"github.com/shortlink-org/shortlink/internal/pkg/logger/field"
Expand Down Expand Up @@ -31,5 +35,31 @@ func New(log logger.Logger) PprofEndpoint {
"addr": "0.0.0.0:7071",
})

// These 2 lines are only required if you're using mutex or block profiling
// Read the explanation below for how to set these rates:
runtime.SetMutexProfileFraction(5)
runtime.SetBlockProfileRate(5)

pyroscope.Start(pyroscope.Config{
ApplicationName: viper.GetString("SERVICE_NAME"),
ServerAddress: "http://pyroscope.pyroscope:4040",
Logger: nil,
ProfileTypes: []pyroscope.ProfileType{
// these profile types are enabled by default:
pyroscope.ProfileCPU,
pyroscope.ProfileAllocObjects,
pyroscope.ProfileAllocSpace,
pyroscope.ProfileInuseObjects,
pyroscope.ProfileInuseSpace,

// these profile types are optional:
pyroscope.ProfileGoroutines,
pyroscope.ProfileMutexCount,
pyroscope.ProfileMutexDuration,
pyroscope.ProfileBlockCount,
pyroscope.ProfileBlockDuration,
},
})

return pprofMux
}
8 changes: 5 additions & 3 deletions internal/di/pkg/traicing/traicing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
)

func New(ctx context.Context, log logger.Logger) (*trace.TracerProvider, func(), error) {
viper.SetDefault("TRACER_URI", "localhost:14268/api/traces") // Tracing addr:host
viper.SetDefault("TRACER_URI", "localhost:14268/api/traces") // Tracing addr:host
viper.SetDefault("PYROSCOPE_URI", "http://pyroscope.pyroscope:4040") // Pyroscope addr:host

config := traicing.Config{
ServiceName: viper.GetString("SERVICE_NAME"),
URI: viper.GetString("TRACER_URI"),
ServiceName: viper.GetString("SERVICE_NAME"),
URI: viper.GetString("TRACER_URI"),
PyroscopeURI: viper.GetString("PYROSCOPE_URI"),
}

tracer, tracerClose, err := traicing.Init(ctx, config, log)
Expand Down
21 changes: 13 additions & 8 deletions internal/pkg/traicing/traicing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package traicing
import (
"context"

otelpyroscope "github.com/pyroscope-io/otel-profiling-go"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/jaeger"
"go.opentelemetry.io/otel/propagation"
Expand All @@ -20,14 +21,14 @@ import (

// Init returns an instance of Jaeger Tracer that samples 100% of traces and logs all spans to stdout.
func Init(ctx context.Context, cnf Config, log logger.Logger) (trace.TracerProvider, func(), error) {
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(cnf.URI)))
exporter, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(cnf.URI)))
if err != nil {
return nil, nil, err
}

tp := tracesdk.NewTracerProvider(
// Always be sure to batch in production.
tracesdk.WithBatcher(exp),
tracesdk.WithBatcher(exporter),
// Record information about this application in a Resource.
tracesdk.WithResource(resource.NewWithAttributes(
semconv.SchemaURL,
Expand All @@ -36,12 +37,20 @@ func Init(ctx context.Context, cnf Config, log logger.Logger) (trace.TracerProvi
)

cleanup := func() {
_ = exp.Shutdown(ctx)
_ = exporter.Shutdown(ctx)
_ = tp.Shutdown(ctx)
}

// Register the global Tracer provider
otel.SetTracerProvider(tp)
otel.SetTracerProvider(otelpyroscope.NewTracerProvider(
tp,
otelpyroscope.WithAppName(cnf.ServiceName),
otelpyroscope.WithPyroscopeURL(cnf.URI),
otelpyroscope.WithRootSpanOnly(true),
otelpyroscope.WithAddSpanName(true),
otelpyroscope.WithProfileURL(true),
otelpyroscope.WithProfileBaselineURL(true),
))

// Register the W3C trace context and baggage propagators so data is propagated across services/processes
otel.SetTextMapPropagator(
Expand All @@ -51,10 +60,6 @@ func Init(ctx context.Context, cnf Config, log logger.Logger) (trace.TracerProvi
),
)

// Register our TracerProvider as the global so any imported
// instrumentation in the future will default to using it.
otel.SetTracerProvider(tp)

log.Info(`Tracing enable`, field.Fields{
"uri": cnf.URI,
})
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/traicing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package traicing

// Config ...
type Config struct {
ServiceName string
URI string
ServiceName string
URI string
PyroscopeURI string
}

0 comments on commit 9417722

Please sign in to comment.