Skip to content

Commit bed5a77

Browse files
committed
fix: 修复所有 lint 错误
- 替换已弃用的 Jaeger exporter 为 OTLP HTTP exporter - 修复 cache.CloseRedis() 返回值未检查问题 - 为所有 Terminate 调用添加 //nolint:errcheck 注释 - 更新日志信息:Jaeger -> OTLP - 添加 OTLP HTTP exporter 依赖到 go.mod
1 parent 601e299 commit bed5a77

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
go.opentelemetry.io/otel v1.24.0
2020
go.opentelemetry.io/otel/trace v1.24.0
2121
go.opentelemetry.io/otel/sdk v1.24.0
22+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.24.0
2223
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.49.0
2324
github.com/testcontainers/testcontainers-go v0.28.0
2425
github.com/testcontainers/testcontainers-go/modules/postgres v0.28.0

internal/app/app.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ func Run() error {
3636
if err := cache.InitRedis(); err != nil {
3737
utils.LogWarn("Redis初始化失败,缓存功能将不可用: %v", err)
3838
}
39-
defer cache.CloseRedis()
39+
defer func() {
40+
if err := cache.CloseRedis(); err != nil {
41+
utils.LogWarn("关闭Redis连接失败: %v", err)
42+
}
43+
}()
4044

4145
// 初始化限流器(滑动窗口 + 令牌桶)
4246
middleware.InitRateLimiters()
@@ -48,7 +52,7 @@ func Run() error {
4852
} else {
4953
defer cleanupTracing()
5054
if cfg.JaegerEndpoint != "" {
51-
utils.LogInfo("Tracing 已启用(Jaeger: %s)", cfg.JaegerEndpoint)
55+
utils.LogInfo("Tracing 已启用(OTLP: %s)", cfg.JaegerEndpoint)
5256
}
5357
}
5458

internal/integration/integration_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ func setupTestDB(ctx context.Context) (*db.Pool, func(), error) {
4242
// 获取容器的主机和端口
4343
host, err := pgContainer.Host(ctx)
4444
if err != nil {
45-
_ = pgContainer.Terminate(ctx)
45+
_ = pgContainer.Terminate(ctx) //nolint:errcheck
4646
return nil, nil, err
4747
}
4848
port, err := pgContainer.MappedPort(ctx, "5432")
4949
if err != nil {
50-
_ = pgContainer.Terminate(ctx)
50+
_ = pgContainer.Terminate(ctx) //nolint:errcheck
5151
return nil, nil, err
5252
}
5353

@@ -63,20 +63,20 @@ func setupTestDB(ctx context.Context) (*db.Pool, func(), error) {
6363
}
6464
pool, err := db.New(ctx, cfg)
6565
if err != nil {
66-
_ = pgContainer.Terminate(ctx)
66+
_ = pgContainer.Terminate(ctx) //nolint:errcheck
6767
return nil, nil, err
6868
}
6969

7070
// 执行迁移
7171
if err := db.Migrate(ctx, pool); err != nil {
7272
pool.Close()
73-
_ = pgContainer.Terminate(ctx)
73+
_ = pgContainer.Terminate(ctx) //nolint:errcheck
7474
return nil, nil, err
7575
}
7676

7777
cleanup := func() {
7878
pool.Close()
79-
_ = pgContainer.Terminate(ctx)
79+
_ = pgContainer.Terminate(ctx) //nolint:errcheck
8080
}
8181

8282
return pool, cleanup, nil
@@ -96,12 +96,12 @@ func setupTestRedis(ctx context.Context) (string, func(), error) {
9696

9797
endpoint, err := redisContainer.Endpoint(ctx, "")
9898
if err != nil {
99-
_ = redisContainer.Terminate(ctx)
99+
_ = redisContainer.Terminate(ctx) //nolint:errcheck
100100
return "", nil, err
101101
}
102102

103103
cleanup := func() {
104-
_ = redisContainer.Terminate(ctx)
104+
_ = redisContainer.Terminate(ctx) //nolint:errcheck
105105
}
106106

107107
return endpoint, cleanup, nil

internal/tracing/tracing.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* OpenTelemetry Tracing
33
* 实现 redo.md 3.1:分布式追踪
4+
* 使用 OTLP HTTP exporter(替代已弃用的 Jaeger exporter)
45
*/
56
package tracing
67

@@ -10,7 +11,7 @@ import (
1011
icfg "short-link/internal/config"
1112

1213
"go.opentelemetry.io/otel"
13-
"go.opentelemetry.io/otel/exporters/jaeger"
14+
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
1415
"go.opentelemetry.io/otel/propagation"
1516
"go.opentelemetry.io/otel/sdk/resource"
1617
tracesdk "go.opentelemetry.io/otel/sdk/trace"
@@ -24,15 +25,20 @@ var (
2425

2526
// InitTracing 初始化 OpenTelemetry Tracing
2627
func InitTracing(cfg *icfg.Config) (func(), error) {
27-
// 如果未配置 Jaeger,则不启用追踪
28+
// 如果未配置 Jaeger endpoint,则不启用追踪
2829
if cfg.JaegerEndpoint == "" {
2930
return func() {}, nil
3031
}
3132

32-
// 创建 Jaeger exporter
33-
exp, err := jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(cfg.JaegerEndpoint)))
33+
// 创建 OTLP HTTP exporter(替代已弃用的 Jaeger exporter)
34+
// Jaeger 现在支持 OTLP,所以我们可以使用 OTLP exporter
35+
ctx := context.Background()
36+
exp, err := otlptracehttp.New(ctx,
37+
otlptracehttp.WithEndpoint(cfg.JaegerEndpoint),
38+
otlptracehttp.WithInsecure(), // 如果使用 HTTPS,请移除此选项并配置 TLS
39+
)
3440
if err != nil {
35-
return nil, fmt.Errorf("创建 Jaeger exporter 失败: %w", err)
41+
return nil, fmt.Errorf("创建 OTLP exporter 失败: %w", err)
3642
}
3743

3844
// 创建 resource

0 commit comments

Comments
 (0)