You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
979 B
Go
37 lines
979 B
Go
2 years ago
|
package uptracehelper
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/uptrace/uptrace-go/uptrace"
|
||
|
)
|
||
|
|
||
|
type Config struct {
|
||
|
ServiceName string
|
||
|
ServiceVersion string
|
||
|
DeploymentEnvironment string
|
||
|
}
|
||
|
|
||
|
// InitTracer 初始化并等待 uptrace
|
||
|
// 配合 [git.blauwelle.com/go/crate/exegroup] 使用
|
||
|
// env
|
||
|
// - UPTRACE_DISABLED 存在就不再初始化
|
||
|
// - UPTRACE_DSN 服务端地址
|
||
|
func InitTracer(cfg Config) (func(ctx context.Context) error, func(ctx context.Context)) {
|
||
|
opts := []uptrace.Option{
|
||
|
uptrace.WithServiceName(cfg.ServiceName),
|
||
|
uptrace.WithServiceVersion(cfg.ServiceVersion),
|
||
|
}
|
||
|
if cfg.DeploymentEnvironment != "" {
|
||
|
opts = append(opts, uptrace.WithDeploymentEnvironment(cfg.DeploymentEnvironment))
|
||
|
}
|
||
|
uptrace.ConfigureOpentelemetry(opts...)
|
||
|
shutdownErr := make(chan error)
|
||
|
goFunc := func(context.Context) error {
|
||
|
return <-shutdownErr
|
||
|
}
|
||
|
stopFunc := func(ctx context.Context) {
|
||
|
shutdownErr <- uptrace.Shutdown(ctx)
|
||
|
}
|
||
|
return goFunc, stopFunc
|
||
|
}
|