抽取 Gin、配置、日志、数据库、Redis 与出站 HTTP 基础能力。 新增统一接入指南,将能力清单和相对链接检查纳入 make verify。 对齐远端 module 路径,修正 Redis 读写超时特殊值映射。 验证:make verify、make race-check 均通过。
This commit is contained in:
commit
b4c3ac622c
55 files changed
+5862
No files matched your search
@@ -0,0 +1,36 @@
|
||||
// Package diagnostic 保存可信入口生成的标量诊断信息,不依赖 HTTP 或业务身份。
|
||||
package diagnostic
|
||||
|
||||
import "context"
|
||||
|
||||
type contextKey struct{}
|
||||
|
||||
// Snapshot 是单次调用的诊断快照,不持有请求、数据库会话和可变对象。
|
||||
type Snapshot struct {
|
||||
RequestID string `json:"request_id" dc:"入口生成的请求标识"`
|
||||
TraceID string `json:"trace_id" dc:"入口生成的链路标识"`
|
||||
URI string `json:"uri" dc:"不含查询参数的请求路径"`
|
||||
Method string `json:"method" dc:"HTTP 方法,后台调用为空"`
|
||||
Service string `json:"service" dc:"入口声明的服务名称"`
|
||||
}
|
||||
|
||||
// With 将快照存入标准上下文。
|
||||
// 入参:ctx 为父上下文,snapshot 为可信入口生成的标量快照。
|
||||
// 返回:保留取消和截止时间的派生上下文。
|
||||
func With(ctx context.Context, snapshot Snapshot) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
return context.WithValue(ctx, contextKey{}, snapshot)
|
||||
}
|
||||
|
||||
// From 读取诊断快照。
|
||||
// 入参:ctx 为标准上下文,允许 nil。
|
||||
// 返回:快照副本,缺省时返回零值,不伪造链路标识。
|
||||
func From(ctx context.Context) Snapshot {
|
||||
if ctx == nil {
|
||||
return Snapshot{}
|
||||
}
|
||||
snapshot, _ := ctx.Value(contextKey{}).(Snapshot)
|
||||
return snapshot
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package diagnostic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSnapshotPreservesCancellationAndCopiesValues(t *testing.T) {
|
||||
parent, cancel := context.WithCancel(context.Background())
|
||||
original := Snapshot{TraceID: "trusted"}
|
||||
ctx := With(parent, original)
|
||||
original.TraceID = "changed"
|
||||
snapshot := From(ctx)
|
||||
snapshot.TraceID = "local"
|
||||
if From(ctx).TraceID != "trusted" {
|
||||
t.Fatal("snapshot was modified")
|
||||
}
|
||||
cancel()
|
||||
if ctx.Err() != context.Canceled {
|
||||
t.Fatal("cancellation lost")
|
||||
}
|
||||
if From(context.Background()) != (Snapshot{}) {
|
||||
t.Fatal("created fake diagnostics")
|
||||
}
|
||||
}
|
||||