otgorm.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package tracer
  2. const (
  3. parentSpanGormKey = "opentracingParentSpan"
  4. spanGormKey = "opentracingSpan"
  5. )
  6. //
  7. //// SetSpanToGorm sets span to gorm settings, returns cloned DB
  8. //func SetSpanToGorm(ctx context.Context, db *gorm.DB) *gorm.DB {
  9. // if ctx == nil {
  10. // return db
  11. // }
  12. // parentSpan := opentracing.SpanFromContext(ctx)
  13. // if parentSpan == nil {
  14. // return db
  15. // }
  16. // return db.Set(parentSpanGormKey, parentSpan)
  17. //}
  18. //
  19. //// AddGormCallbacks adds callbacks for tracing, you should call SetSpanToGorm to make them work
  20. //func AddGormCallbacks(db *gorm.DB) {
  21. // callbacks := newCallbacks()
  22. // registerCallbacks(db, "create", callbacks)
  23. // registerCallbacks(db, "query", callbacks)
  24. // registerCallbacks(db, "update", callbacks)
  25. // registerCallbacks(db, "delete", callbacks)
  26. // registerCallbacks(db, "row_query", callbacks)
  27. //}
  28. //
  29. //type callbacks struct{}
  30. //
  31. //func newCallbacks() *callbacks {
  32. // return &callbacks{}
  33. //}
  34. //
  35. //func (c *callbacks) beforeCreate(scope *gorm.Scope) { c.before(scope) }
  36. //func (c *callbacks) afterCreate(scope *gorm.Scope) { c.after(scope, "INSERT") }
  37. //func (c *callbacks) beforeQuery(scope *gorm.Scope) { c.before(scope) }
  38. //func (c *callbacks) afterQuery(scope *gorm.Scope) { c.after(scope, "SELECT") }
  39. //func (c *callbacks) beforeUpdate(scope *gorm.Scope) { c.before(scope) }
  40. //func (c *callbacks) afterUpdate(scope *gorm.Scope) { c.after(scope, "UPDATE") }
  41. //func (c *callbacks) beforeDelete(scope *gorm.Scope) { c.before(scope) }
  42. //func (c *callbacks) afterDelete(scope *gorm.Scope) { c.after(scope, "DELETE") }
  43. //func (c *callbacks) beforeRowQuery(scope *gorm.Scope) { c.before(scope) }
  44. //func (c *callbacks) afterRowQuery(scope *gorm.Scope) { c.after(scope, "") }
  45. //
  46. //func (c *callbacks) before(scope *gorm.Scope) {
  47. // val, ok := scope.Get(parentSpanGormKey)
  48. // if !ok {
  49. // return
  50. // }
  51. // parentSpan := val.(opentracing.Span)
  52. // tr := parentSpan.Tracer()
  53. // sp := tr.StartSpan("sql", opentracing.ChildOf(parentSpan.Context()))
  54. // ext.DBType.Set(sp, "sql")
  55. // scope.Set(spanGormKey, sp)
  56. //}
  57. //
  58. //func (c *callbacks) after(scope *gorm.Scope, operation string) {
  59. // val, ok := scope.Get(spanGormKey)
  60. // if !ok {
  61. // return
  62. // }
  63. // sp := val.(opentracing.Span)
  64. // if operation == "" {
  65. // operation = strings.ToUpper(strings.Split(scope.SQL, " ")[0])
  66. // }
  67. // ext.Error.Set(sp, scope.HasError())
  68. // ext.DBStatement.Set(sp, scope.SQL)
  69. // sp.SetTag("db.sqlvars", scope.SQLVars)
  70. // sp.SetTag("db.table", scope.TableName())
  71. // sp.SetTag("db.method", operation)
  72. // sp.SetTag("db.err", scope.HasError())
  73. // sp.SetTag("db.count", scope.DB().RowsAffected)
  74. // sp.Finish()
  75. //}
  76. //
  77. //func registerCallbacks(db *gorm.DB, name string, c *callbacks) {
  78. // beforeName := fmt.Sprintf("tracing:%v_before", name)
  79. // afterName := fmt.Sprintf("tracing:%v_after", name)
  80. // gormCallbackName := fmt.Sprintf("gorm:%v", name)
  81. // // gorm does some magic, if you pass CallbackProcessor here - nothing works
  82. // switch name {
  83. // case "create":
  84. // db.Callback().Create().Before(gormCallbackName).Register(beforeName, c.beforeCreate)
  85. // db.Callback().Create().After(gormCallbackName).Register(afterName, c.afterCreate)
  86. // case "query":
  87. // db.Callback().Query().Before(gormCallbackName).Register(beforeName, c.beforeQuery)
  88. // db.Callback().Query().After(gormCallbackName).Register(afterName, c.afterQuery)
  89. // case "update":
  90. // db.Callback().Update().Before(gormCallbackName).Register(beforeName, c.beforeUpdate)
  91. // db.Callback().Update().After(gormCallbackName).Register(afterName, c.afterUpdate)
  92. // case "delete":
  93. // db.Callback().Delete().Before(gormCallbackName).Register(beforeName, c.beforeDelete)
  94. // db.Callback().Delete().After(gormCallbackName).Register(afterName, c.afterDelete)
  95. // case "row_query":
  96. // db.Callback().RowQuery().Before(gormCallbackName).Register(beforeName, c.beforeRowQuery)
  97. // db.Callback().RowQuery().After(gormCallbackName).Register(afterName, c.afterRowQuery)
  98. // }
  99. //}