diff --git a/src/telemetry/setup.go b/src/telemetry/setup.go index 5e86ba0..c83663a 100644 --- a/src/telemetry/setup.go +++ b/src/telemetry/setup.go @@ -2,14 +2,18 @@ package telemetry import ( "context" + "fmt" "log/slog" "os" + "runtime" "git.kmsign.ru/royalcat/tstor/pkg/rlog" "github.com/agoda-com/opentelemetry-go/otelslog" "github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs" "github.com/agoda-com/opentelemetry-logs-go/exporters/otlp/otlplogs/otlplogshttp" logsdk "github.com/agoda-com/opentelemetry-logs-go/sdk/logs" + otelpyroscope "github.com/grafana/otel-profiling-go" + "github.com/grafana/pyroscope-go" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" "go.opentelemetry.io/otel/exporters/prometheus" @@ -99,7 +103,8 @@ func Setup(ctx context.Context, endpoint string) (*Client, error) { trace.WithBatcher(traceExporter), trace.WithResource(r), ) - otel.SetTracerProvider(client.tracerProvider) + // otel.SetTracerProvider(client.tracerProvider) + otel.SetTracerProvider(otelpyroscope.NewTracerProvider(client.tracerProvider)) log.Info("otel tracing provider initialized") logExporter, err := otlplogs.NewExporter(ctx, @@ -122,5 +127,55 @@ func Setup(ctx context.Context, endpoint string) (*Client, error) { ) client.log = slog.Default() + runtime.SetMutexProfileFraction(5) + runtime.SetBlockProfileRate(5) + _, err = pyroscope.Start(pyroscope.Config{ + ApplicationName: appName, + // replace this with the address of pyroscope server + ServerAddress: "https://pyroscope.kmsign.ru", + // you can disable logging by setting this to nil + Logger: &pyroscopeLogger{ + log: rlog.ComponentLog("metrics.pyroscope"), + }, + 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, + }, + }) + if err != nil { + return client, nil + } + return client, nil } + +type pyroscopeLogger struct { + log *slog.Logger +} + +var _ pyroscope.Logger = (*pyroscopeLogger)(nil) + +// Debugf implements pyroscope.Logger. +func (p *pyroscopeLogger) Debugf(msg string, args ...any) { + p.log.Debug(fmt.Sprintf(msg, args...)) +} + +// Errorf implements pyroscope.Logger. +func (p *pyroscopeLogger) Errorf(msg string, args ...any) { + p.log.Error(fmt.Sprintf(msg, args...)) +} + +// Infof implements pyroscope.Logger. +func (p *pyroscopeLogger) Infof(msg string, args ...any) { + p.log.Info(fmt.Sprintf(msg, args...)) +}