uptracehelper/change GoStop to Bootstrap;

develop uptracehelper/v0.3.0
Ge Song 10 months ago
parent a844e9e34c
commit 5cf4430bc8

@ -2,6 +2,7 @@ package uptracehelper
import ( import (
"context" "context"
"time"
"github.com/uptrace/uptrace-go/uptrace" "github.com/uptrace/uptrace-go/uptrace"
) )
@ -10,6 +11,7 @@ type Config struct {
ServiceName string ServiceName string
ServiceVersion string ServiceVersion string
DeploymentEnvironment string DeploymentEnvironment string
ShutdownTimeout time.Duration
} }
func Setup(cfg Config) { func Setup(cfg Config) {
@ -23,19 +25,21 @@ func Setup(cfg Config) {
uptrace.ConfigureOpentelemetry(opts...) uptrace.ConfigureOpentelemetry(opts...)
} }
// GoStop 初始化并等待 uptrace // Bootstrap 初始化并等待 uptrace
// 配合 [git.blauwelle.com/go/crate/exegroup] 使用 // 配合 [git.blauwelle.com/go/crate/exegroup] 使用
// env // env
// - UPTRACE_DISABLED 存在就不再初始化 // - UPTRACE_DISABLED 存在就不再初始化
// - UPTRACE_DSN 服务端地址 // - UPTRACE_DSN 服务端地址
func GoStop(cfg Config) (func(ctx context.Context) error, func(ctx context.Context)) { func Bootstrap(cfg Config) func(ctx context.Context) error {
Setup(cfg) Setup(cfg)
shutdownErr := make(chan error, 1) return func(ctx context.Context) error {
goFunc := func(context.Context) error { <-ctx.Done()
return <-shutdownErr shutdownCtx := context.Background()
if cfg.ShutdownTimeout >= 0 {
var cancel func()
shutdownCtx, cancel = context.WithTimeout(shutdownCtx, cfg.ShutdownTimeout)
defer cancel()
}
return uptrace.Shutdown(shutdownCtx)
} }
stopFunc := func(ctx context.Context) {
shutdownErr <- uptrace.Shutdown(ctx)
}
return goFunc, stopFunc
} }

Loading…
Cancel
Save