|
|
|
package uptracehelper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/uptrace/uptrace-go/uptrace"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ServiceName string
|
|
|
|
ServiceVersion string
|
|
|
|
DeploymentEnvironment string
|
|
|
|
ShutdownTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func Setup(cfg Config) {
|
|
|
|
opts := []uptrace.Option{
|
|
|
|
uptrace.WithServiceName(cfg.ServiceName),
|
|
|
|
uptrace.WithServiceVersion(cfg.ServiceVersion),
|
|
|
|
}
|
|
|
|
if cfg.DeploymentEnvironment != "" {
|
|
|
|
opts = append(opts, uptrace.WithDeploymentEnvironment(cfg.DeploymentEnvironment))
|
|
|
|
}
|
|
|
|
uptrace.ConfigureOpentelemetry(opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bootstrap 初始化并等待 uptrace
|
|
|
|
// 配合 [git.blauwelle.com/go/crate/exegroup] 使用
|
|
|
|
// env
|
|
|
|
// - UPTRACE_DISABLED 存在就不再初始化
|
|
|
|
// - UPTRACE_DSN 服务端地址
|
|
|
|
func Bootstrap(cfg Config) func(ctx context.Context) error {
|
|
|
|
Setup(cfg)
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
<-ctx.Done()
|
|
|
|
shutdownCtx := context.Background()
|
|
|
|
if cfg.ShutdownTimeout >= 0 {
|
|
|
|
var cancel func()
|
|
|
|
shutdownCtx, cancel = context.WithTimeout(shutdownCtx, cfg.ShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
return uptrace.Shutdown(shutdownCtx)
|
|
|
|
}
|
|
|
|
}
|