gorm.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2020 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. /**
  4. * @Author: mac
  5. * @Description: gorm opentracing plugins
  6. * @Date: 2020/3/25 10:36
  7. */
  8. package trace
  9. import (
  10. "context"
  11. //"fmt"
  12. //"strings"
  13. //"github.com/opentracing/opentracing-go/log"
  14. opentracing "github.com/opentracing/opentracing-go"
  15. "gorm.io/gorm"
  16. //"github.com/opentracing/opentracing-go/ext"
  17. )
  18. const (
  19. parentSpanGormKey = "opentracingParentSpan"
  20. spanGormKey = "opentracingSpan"
  21. )
  22. // SetSpanToGorm sets span to gorm settings, returns cloned DB
  23. func SetSpanToGorm(ctx context.Context, db *gorm.DB) *gorm.DB {
  24. if ctx == nil {
  25. return db
  26. }
  27. parentSpan := opentracing.SpanFromContext(ctx)
  28. if parentSpan == nil {
  29. return db
  30. }
  31. return db.Set(parentSpanGormKey, parentSpan)
  32. }
  33. // AddGormCallbacks adds callbacks for tracing, you should call SetSpanToGorm to make them work
  34. func AddGormCallbacks(db *gorm.DB) {
  35. //callbacks := newCallbacks()
  36. //registerCallbacks(db, "create", callbacks)
  37. //registerCallbacks(db, "query", callbacks)
  38. //registerCallbacks(db, "update", callbacks)
  39. //registerCallbacks(db, "delete", callbacks)
  40. //registerCallbacks(db, "row_query", callbacks)
  41. }
  42. type callbacks struct{}
  43. func newCallbacks() *callbacks {
  44. return &callbacks{}
  45. }
  46. //
  47. //func (c *callbacks) beforeCreate(scope *gorm.Scope) { c.before(scope, "INSERT") }
  48. //func (c *callbacks) afterCreate(scope *gorm.Scope) { c.after(scope, "INSERT") }
  49. //func (c *callbacks) beforeQuery(scope *gorm.Scope) { c.before(scope, "SELECT") }
  50. //func (c *callbacks) afterQuery(scope *gorm.Scope) { c.after(scope, "SELECT") }
  51. //func (c *callbacks) beforeUpdate(scope *gorm.Scope) { c.before(scope, "UPDATE") }
  52. //func (c *callbacks) afterUpdate(scope *gorm.Scope) { c.after(scope, "UPDATE") }
  53. //func (c *callbacks) beforeDelete(scope *gorm.Scope) { c.before(scope, "DELETE") }
  54. //func (c *callbacks) afterDelete(scope *gorm.Scope) { c.after(scope, "DELETE") }
  55. //func (c *callbacks) beforeRowQuery(scope *gorm.Scope) { c.before(scope, "") }
  56. //func (c *callbacks) afterRowQuery(scope *gorm.Scope) { c.after(scope, "") }
  57. //
  58. //func (c *callbacks) before(scope *gorm.Scope, operation string) {
  59. // val, ok := scope.Get(parentSpanGormKey)
  60. // if !ok {
  61. // return
  62. // }
  63. // parentSpan := val.(opentracing.Span)
  64. // tr := parentSpan.Tracer()
  65. // // 如果是其他sql类型,将下面两个mysql改为对应的类型即可
  66. // if operation == "" {
  67. // operation = strings.ToUpper(strings.Split(scope.SQL, " ")[0])
  68. // }
  69. // sp := tr.StartSpan("MYSQL:"+operation, opentracing.ChildOf(parentSpan.Context()))
  70. // ext.DBType.Set(sp, "mysql")
  71. // scope.Set(spanGormKey, sp)
  72. //}
  73. //
  74. //func (c *callbacks) after(scope *gorm.DB, operation string) {
  75. // val, ok := scope.Get(spanGormKey)
  76. // if !ok {
  77. // return
  78. // }
  79. // sp := val.(opentracing.Span)
  80. // if operation == "" {
  81. // operation = strings.ToUpper(strings.Split(scope.SQL, " ")[0])
  82. // }
  83. // if scope.HasError() {
  84. // ext.Error.Set(sp, scope.HasError())
  85. // //ext.DBStatement.Set(sp, scope.SQL)
  86. // sp.LogFields(
  87. // log.String("db.table", scope.TableName()),
  88. // log.String("db.method", operation),
  89. // log.String("db.sql", scope.SQL),
  90. // log.Object("db.err", scope.DB().Error),
  91. // log.Int64("db.count", scope.DB().RowsAffected),
  92. // )
  93. // } else {
  94. // sp.LogFields(log.String("db.sql", scope.SQL),
  95. // log.Int64("db.count", scope.DB().RowsAffected))
  96. // }
  97. //
  98. // sp.Finish()
  99. //}
  100. //
  101. //func registerCallbacks(db *gorm.DB, name string, c *callbacks) {
  102. // beforeName := fmt.Sprintf("tracing:%v_before", name)
  103. // afterName := fmt.Sprintf("tracing:%v_after", name)
  104. // gormCallbackName := fmt.Sprintf("gorm:%v", name)
  105. // // gorm does some magic, if you pass CallbackProcessor here - nothing works
  106. // switch name {
  107. // case "create":
  108. // db.Callback().Create().Before(gormCallbackName).Register(beforeName, c.beforeCreate)
  109. // db.Callback().Create().After(gormCallbackName).Register(afterName, c.afterCreate)
  110. // case "query":
  111. // db.Callback().Query().Before(gormCallbackName).Register(beforeName, c.beforeQuery)
  112. // db.Callback().Query().After(gormCallbackName).Register(afterName, c.afterQuery)
  113. // case "update":
  114. // db.Callback().Update().Before(gormCallbackName).Register(beforeName, c.beforeUpdate)
  115. // db.Callback().Update().After(gormCallbackName).Register(afterName, c.afterUpdate)
  116. // case "delete":
  117. // db.Callback().Delete().Before(gormCallbackName).Register(beforeName, c.beforeDelete)
  118. // db.Callback().Delete().After(gormCallbackName).Register(afterName, c.afterDelete)
  119. // case "row_query":
  120. // db.Callback().Raw().Before(gormCallbackName).Register(beforeName, c.beforeRowQuery)
  121. // db.Callback().Raw().After(gormCallbackName).Register(afterName, c.afterRowQuery)
  122. // }
  123. //}