datadog.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package datadog
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "os"
  7. "time"
  8. log "github.com/sirupsen/logrus"
  9. "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
  10. "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
  11. "github.com/imgproxy/imgproxy/v3/config"
  12. "github.com/imgproxy/imgproxy/v3/version"
  13. )
  14. type spanCtxKey struct{}
  15. var enabled bool
  16. func Init() {
  17. if !config.DataDogEnable {
  18. return
  19. }
  20. name := os.Getenv("DD_SERVICE")
  21. if len(name) == 0 {
  22. name = "imgproxy"
  23. }
  24. tracer.Start(
  25. tracer.WithService(name),
  26. tracer.WithServiceVersion(version.Version()),
  27. tracer.WithLogger(dataDogLogger{}),
  28. )
  29. enabled = true
  30. }
  31. func Stop() {
  32. if enabled {
  33. tracer.Stop()
  34. }
  35. }
  36. func Enabled() bool {
  37. return enabled
  38. }
  39. func StartRootSpan(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
  40. if !enabled {
  41. return ctx, func() {}, rw
  42. }
  43. span := tracer.StartSpan(
  44. "request",
  45. tracer.Measured(),
  46. tracer.SpanType("web"),
  47. tracer.Tag(ext.HTTPMethod, r.Method),
  48. tracer.Tag(ext.HTTPURL, r.RequestURI),
  49. )
  50. cancel := func() { span.Finish() }
  51. newRw := dataDogResponseWriter{rw, span}
  52. return context.WithValue(ctx, spanCtxKey{}, span), cancel, newRw
  53. }
  54. func StartSpan(ctx context.Context, name string) context.CancelFunc {
  55. if !enabled {
  56. return func() {}
  57. }
  58. if rootSpan, ok := ctx.Value(spanCtxKey{}).(tracer.Span); ok {
  59. span := tracer.StartSpan(name, tracer.Measured(), tracer.ChildOf(rootSpan.Context()))
  60. return func() { span.Finish() }
  61. }
  62. return func() {}
  63. }
  64. func SendError(ctx context.Context, err error) {
  65. if !enabled {
  66. return
  67. }
  68. if rootSpan, ok := ctx.Value(spanCtxKey{}).(tracer.Span); ok {
  69. rootSpan.Finish(tracer.WithError(err))
  70. }
  71. }
  72. func SendTimeout(ctx context.Context, d time.Duration) {
  73. if !enabled {
  74. return
  75. }
  76. if rootSpan, ok := ctx.Value(spanCtxKey{}).(tracer.Span); ok {
  77. rootSpan.SetTag("timeout_duration", d)
  78. rootSpan.Finish(tracer.WithError(errors.New("Timeout")))
  79. }
  80. }
  81. type dataDogLogger struct {
  82. }
  83. func (l dataDogLogger) Log(msg string) {
  84. log.Info(msg)
  85. }
  86. type dataDogResponseWriter struct {
  87. rw http.ResponseWriter
  88. span tracer.Span
  89. }
  90. func (ddrw dataDogResponseWriter) Header() http.Header {
  91. return ddrw.rw.Header()
  92. }
  93. func (ddrw dataDogResponseWriter) Write(data []byte) (int, error) {
  94. return ddrw.rw.Write(data)
  95. }
  96. func (ddrw dataDogResponseWriter) WriteHeader(statusCode int) {
  97. ddrw.span.SetTag(ext.HTTPCode, statusCode)
  98. ddrw.rw.WriteHeader(statusCode)
  99. }