-
Notifications
You must be signed in to change notification settings - Fork 32
/
pyroscope.go
87 lines (72 loc) · 2.61 KB
/
pyroscope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package metrics
import (
otelpyroscope "github.com/grafana/otel-profiling-go"
"github.com/grafana/pyroscope-go"
"github.com/synapsecns/sanguine/core"
"github.com/synapsecns/sanguine/core/config"
"github.com/synapsecns/sanguine/core/metrics/internal"
"go.opentelemetry.io/otel/trace"
"os"
)
// StartPyroscope starts the pyroscope profiler.
// this will not run if the pyroscope endpoint is not set.
func StartPyroscope(info config.BuildInfo) *pyroscope.Profiler {
if !core.HasEnv(internal.PyroscopeEndpoint) {
return nil
}
// note: because pyroscope is a profiler, we cannot use buildinfo for the application name.
pf, err := pyroscope.Start(pyroscope.Config{
ApplicationName: info.Name(),
// replace this with the address of pyroscope server
ServerAddress: os.Getenv(internal.PyroscopeEndpoint),
// you can disable logging by setting this to nil
Logger: pyroscopeLogger{},
// Uncomment this line if you're having issues:
//Logger: pyroscope.StandardLogger,
// you can provide static tags via a map:
Tags: map[string]string{
"hostname": os.Getenv("HOSTNAME"),
"version": info.Version(),
"commit": info.Commit(),
},
ProfileTypes: []pyroscope.ProfileType{
// these profile types are enabled by default:
pyroscope.ProfileCPU,
pyroscope.ProfileAllocObjects,
pyroscope.ProfileAllocSpace,
pyroscope.ProfileInuseObjects,
pyroscope.ProfileInuseSpace,
},
})
if err != nil {
logger.Warn(err)
}
return pf
}
type pyroscopeLogger struct{}
func (p pyroscopeLogger) Infof(_ string, _ ...interface{}) {
// do nothing
}
func (p pyroscopeLogger) Debugf(_ string, _ ...interface{}) {
// do nothing
}
func (p pyroscopeLogger) Errorf(str string, args ...interface{}) {
logger.Warnf(str, args...)
}
var _ pyroscope.Logger = &pyroscopeLogger{}
// PyroscopeWrapTracerProvider wraps the tracer provider with pyroscope.
// The traceProvider is not affected if the pyroscope endpoint is not set.
func PyroscopeWrapTracerProvider(provider trace.TracerProvider, buildInfo config.BuildInfo, extraOpts ...otelpyroscope.Option) trace.TracerProvider {
if !core.HasEnv(pyroscopeEndpoint) {
return provider
}
opts := append([]otelpyroscope.Option{
otelpyroscope.WithAppName(buildInfo.Name()),
otelpyroscope.WithPyroscopeURL(os.Getenv(pyroscopeEndpoint)),
otelpyroscope.WithRootSpanOnly(true),
// we only need this sort of linkinf for the jaeger ui.
otelpyroscope.WithProfileURL(core.HasEnv(internal.PyroscopeJaegerUIEnabled)),
otelpyroscope.WithProfileBaselineURL(core.HasEnv(internal.PyroscopeJaegerUIEnabled)),
}, extraOpts...)
return otelpyroscope.NewTracerProvider(provider, opts...)
}