id.go 680 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package id
  4. import (
  5. "context"
  6. "fmt"
  7. "strconv"
  8. )
  9. // GetUniqueID 获取唯一id (分布式)
  10. func GetUniqueID() (uint64, error) {
  11. if sf == nil {
  12. return 0, fmt.Errorf("sf is nil.")
  13. }
  14. return sf.NextID()
  15. }
  16. // GetRequestId 生成request id
  17. // 如果开启了jaeger trace,则使用其生成的trace id 作为request id
  18. func GetRequestId(ctx context.Context) string {
  19. if rid, ok := ctx.Value("RequestId").(string); ok && rid != "" {
  20. return rid
  21. }
  22. id, _ := GetUniqueID()
  23. newId := strconv.Itoa(int(id))
  24. ctx = context.WithValue(ctx, "RequestId", newId)
  25. return newId
  26. }