集成 otel 记录接口性能数据
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
func Init(ctx context.Context) error {
|
||||
errs := make([]error, 0)
|
||||
|
||||
errs = append(errs, initBaiyin())
|
||||
errs = append(errs, initAlipay())
|
||||
errs = append(errs, initWechatPay())
|
||||
@@ -18,22 +17,14 @@ func Init(ctx context.Context) error {
|
||||
errs = append(errs, initAsynq(Redis))
|
||||
errs = append(errs, initProxy())
|
||||
errs = append(errs, initSft())
|
||||
|
||||
errs = append(errs, initOtel(ctx))
|
||||
return u.CombineErrors(errs)
|
||||
}
|
||||
|
||||
func Stop() error {
|
||||
func Close() error {
|
||||
var errs = make([]error, 0)
|
||||
|
||||
err := stopRedis()
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
err = stopOrm()
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
errs = append(errs, closeRedis())
|
||||
errs = append(errs, closeOrm())
|
||||
errs = append(errs, closeOtel())
|
||||
return u.CombineErrors(errs)
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ func initOrm() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func stopOrm() error {
|
||||
func closeOrm() error {
|
||||
if DB != nil {
|
||||
conn, err := DB.DB()
|
||||
if err != nil {
|
||||
|
||||
50
web/globals/otel.go
Normal file
50
web/globals/otel.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package globals
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
|
||||
)
|
||||
|
||||
var tp *trace.TracerProvider
|
||||
|
||||
func initOtel(ctx context.Context) error {
|
||||
exporter, err := otlptracegrpc.New(ctx,
|
||||
otlptracegrpc.WithEndpoint("localhost:4317"),
|
||||
otlptracegrpc.WithInsecure(),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("初始化 trace exporter 失败: %w", err)
|
||||
}
|
||||
|
||||
tp = trace.NewTracerProvider(
|
||||
trace.WithBatcher(exporter),
|
||||
trace.WithSampler(trace.AlwaysSample()),
|
||||
trace.WithResource(
|
||||
resource.NewWithAttributes(
|
||||
semconv.SchemaURL,
|
||||
semconv.ServiceNameKey.String("lanhu-platform"),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
otel.SetTracerProvider(tp)
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func closeOtel() error {
|
||||
err := tp.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
return fmt.Errorf("关闭 trace provider 失败: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func initRedis() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func stopRedis() error {
|
||||
func closeRedis() error {
|
||||
if Redis != nil {
|
||||
return Redis.Close()
|
||||
}
|
||||
|
||||
@@ -23,6 +23,12 @@ func RunApp(pCtx context.Context) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("初始化依赖失败: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
err := base.Close()
|
||||
if err != nil {
|
||||
slog.Error("关闭依赖失败", "error", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// 运行服务
|
||||
g.Go(func() error {
|
||||
|
||||
Reference in New Issue
Block a user