trace.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package memcache
  2. import (
  3. "context"
  4. "strconv"
  5. "strings"
  6. "go-common/library/net/trace"
  7. )
  8. const (
  9. _traceFamily = "memcache"
  10. _traceSpanKind = "client"
  11. _traceComponentName = "library/cache/memcache"
  12. _tracePeerService = "memcache"
  13. )
  14. type traceConn struct {
  15. Conn
  16. ctx context.Context
  17. address string
  18. }
  19. func (t *traceConn) setTrace(action, statement string) func(error) error {
  20. parent, ok := trace.FromContext(t.ctx)
  21. if !ok {
  22. return func(err error) error { return err }
  23. }
  24. span := parent.Fork(_traceFamily, "Memcache:"+action)
  25. span.SetTag(
  26. trace.String(trace.TagSpanKind, _traceSpanKind),
  27. trace.String(trace.TagComponent, _traceComponentName),
  28. trace.String(trace.TagPeerService, _tracePeerService),
  29. trace.String(trace.TagPeerAddress, t.address),
  30. trace.String(trace.TagDBStatement, action+" "+statement),
  31. )
  32. return func(err error) error {
  33. span.Finish(&err)
  34. return err
  35. }
  36. }
  37. func (t *traceConn) WithContext(ctx context.Context) Conn {
  38. t.ctx = ctx
  39. t.Conn = t.Conn.WithContext(ctx)
  40. return t
  41. }
  42. func (t *traceConn) Add(item *Item) error {
  43. finishFn := t.setTrace("Add", item.Key)
  44. return finishFn(t.Conn.Add(item))
  45. }
  46. func (t *traceConn) Set(item *Item) error {
  47. finishFn := t.setTrace("Set", item.Key)
  48. return finishFn(t.Conn.Set(item))
  49. }
  50. func (t *traceConn) Replace(item *Item) error {
  51. finishFn := t.setTrace("Replace", item.Key)
  52. return finishFn(t.Conn.Replace(item))
  53. }
  54. func (t *traceConn) Get(key string) (*Item, error) {
  55. finishFn := t.setTrace("Get", key)
  56. item, err := t.Conn.Get(key)
  57. return item, finishFn(err)
  58. }
  59. func (t *traceConn) GetMulti(keys []string) (map[string]*Item, error) {
  60. finishFn := t.setTrace("GetMulti", strings.Join(keys, " "))
  61. items, err := t.Conn.GetMulti(keys)
  62. return items, finishFn(err)
  63. }
  64. func (t *traceConn) Delete(key string) error {
  65. finishFn := t.setTrace("Delete", key)
  66. return finishFn(t.Conn.Delete(key))
  67. }
  68. func (t *traceConn) Increment(key string, delta uint64) (newValue uint64, err error) {
  69. finishFn := t.setTrace("Increment", key+" "+strconv.FormatUint(delta, 10))
  70. newValue, err = t.Conn.Increment(key, delta)
  71. return newValue, finishFn(err)
  72. }
  73. func (t *traceConn) Decrement(key string, delta uint64) (newValue uint64, err error) {
  74. finishFn := t.setTrace("Decrement", key+" "+strconv.FormatUint(delta, 10))
  75. newValue, err = t.Conn.Decrement(key, delta)
  76. return newValue, finishFn(err)
  77. }
  78. func (t *traceConn) CompareAndSwap(item *Item) error {
  79. finishFn := t.setTrace("CompareAndSwap", item.Key)
  80. return finishFn(t.Conn.CompareAndSwap(item))
  81. }
  82. func (t *traceConn) Touch(key string, seconds int32) (err error) {
  83. finishFn := t.setTrace("Touch", key+" "+strconv.Itoa(int(seconds)))
  84. return finishFn(t.Conn.Touch(key, seconds))
  85. }