context.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package utils
  2. import (
  3. "context"
  4. "gd_management_gateway/common.in/span"
  5. beegoctx "github.com/astaxie/beego/context"
  6. "github.com/smallnest/rpcx/share"
  7. )
  8. func NewContextFromBeegoCtx(ctx *beegoctx.Context) context.Context {
  9. // request metadata
  10. metadata := make(map[string]string)
  11. if spanID, err := span.ParseSpanID(ctx.Input.Header("span")); err == nil {
  12. metadata["span"] = span.NewSpanID(*spanID).String()
  13. }
  14. return context.WithValue(context.Background(), share.ReqMetaDataKey, metadata)
  15. }
  16. func NewContextFromRpcCtx(ctx context.Context) context.Context {
  17. // request metadata
  18. metadata := make(map[string]string)
  19. if preMetadata := ctx.Value(share.ReqMetaDataKey).(map[string]string); preMetadata != nil {
  20. if v, ok := preMetadata["span"]; ok {
  21. if spanID, err := span.ParseSpanID(v); err == nil {
  22. metadata["span"] = span.NewSpanID(*spanID).String()
  23. }
  24. }
  25. }
  26. return context.WithValue(context.Background(), share.ReqMetaDataKey, metadata)
  27. }
  28. func GetSpanFromRpcCtx(ctx context.Context) (spanID *span.SpanID, err error) {
  29. if preMetadata := ctx.Value(share.ReqMetaDataKey).(map[string]string); preMetadata != nil {
  30. if v, ok := preMetadata["span"]; ok {
  31. spanID, err = span.ParseSpanID(v)
  32. }
  33. }
  34. return
  35. }
  36. func GetMethodFromRpcCtx(ctx context.Context) (method string) {
  37. if preMetadata := ctx.Value(share.ReqMetaDataKey).(map[string]string); preMetadata != nil {
  38. if v, ok := preMetadata["method"]; ok {
  39. method = v
  40. }
  41. }
  42. return
  43. }