| 12345678910111213141516171819202122232425262728293031323334 |
- package audit
- import (
- "context"
- "strings"
- "github.com/google/uuid"
- "github.com/zeromicro/go-zero/core/trace"
- )
- // EnsureTraceAndSpanID resolves the trace/span ids from go-zero's OTel
- // context, generating UUID fallbacks (stored in ctx) when empty. It also
- // returns the caller identity from the RequestMeta, mirroring sdf-serve.
- func EnsureTraceAndSpanID(ctx context.Context) (context.Context, string, string, string, string, string) {
- traceID := trace.TraceIDFromContext(ctx)
- spanID := trace.SpanIDFromContext(ctx)
- if traceID == "" {
- traceID = strings.ReplaceAll(uuid.NewString(), "-", "")
- ctx = context.WithValue(ctx, TraceKey, traceID)
- }
- if spanID == "" {
- spanID = strings.ReplaceAll(uuid.NewString(), "-", "")
- ctx = context.WithValue(ctx, SpanKey, spanID)
- }
- requestMeta := GetRequestMeta(ctx)
- return ctx, traceID, spanID, requestMeta.UserId, requestMeta.UserName, requestMeta.ApiKey
- }
- // NewUUID returns a uuid string without dashes.
- func NewUUID() string {
- return strings.ReplaceAll(uuid.NewString(), "-", "")
- }
|