-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_gorm.go
83 lines (60 loc) · 1.67 KB
/
api_gorm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package golog
import (
"context"
"errors"
"go.dtapp.net/gorequest"
"gorm.io/gorm"
)
// ApiGorm 接口日志
type ApiGorm struct {
gormClient *gorm.DB // 数据库驱动
gormConfig struct {
stats bool // 状态
tableName string // 表名
}
}
// ApiGormFun 接口日志驱动
type ApiGormFun func() *ApiGorm
// NewApiGorm 创建接口实例化
func NewApiGorm(ctx context.Context, gormClient *gorm.DB, gormTableName string) (*ApiGorm, error) {
gl := &ApiGorm{}
if gormClient == nil {
gl.gormConfig.stats = false
} else {
gl.gormClient = gormClient
if gormTableName == "" {
return nil, errors.New("没有设置表名")
} else {
gl.gormConfig.tableName = gormTableName
}
gl.gormConfig.stats = true
}
return gl, nil
}
// Middleware 中间件
func (ag *ApiGorm) Middleware(ctx context.Context, request gorequest.Response) {
// OpenTelemetry链路追踪
ctx, span := TraceStartSpan(ctx, "api")
if ag.gormConfig.stats {
ag.gormMiddleware(ctx, span, request)
}
span.End() // 结束OpenTelemetry链路追踪
}
// MiddlewareXml 中间件
func (ag *ApiGorm) MiddlewareXml(ctx context.Context, request gorequest.Response) {
// OpenTelemetry链路追踪
ctx, span := TraceStartSpan(ctx, "api.xml")
if ag.gormConfig.stats {
ag.gormMiddlewareXml(ctx, span, request)
}
span.End() // 结束OpenTelemetry链路追踪
}
// MiddlewareCustom 中间件
func (ag *ApiGorm) MiddlewareCustom(ctx context.Context, api string, request gorequest.Response) {
// OpenTelemetry链路追踪
ctx, span := TraceStartSpan(ctx, "api.custom")
if ag.gormConfig.stats {
ag.gormMiddlewareCustom(ctx, span, api, request)
}
span.End() // 结束OpenTelemetry链路追踪
}