monitoring.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package monitoring
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. "github.com/imgproxy/imgproxy/v3/monitoring/cloudwatch"
  7. "github.com/imgproxy/imgproxy/v3/monitoring/datadog"
  8. "github.com/imgproxy/imgproxy/v3/monitoring/newrelic"
  9. "github.com/imgproxy/imgproxy/v3/monitoring/otel"
  10. "github.com/imgproxy/imgproxy/v3/monitoring/prometheus"
  11. "github.com/imgproxy/imgproxy/v3/monitoring/stats"
  12. )
  13. // Monitoring holds all monitoring service instances
  14. type Monitoring struct {
  15. config *Config
  16. stats *stats.Stats
  17. prometheus *prometheus.Prometheus
  18. newrelic *newrelic.NewRelic
  19. datadog *datadog.DataDog
  20. otel *otel.Otel
  21. cloudwatch *cloudwatch.CloudWatch
  22. }
  23. // New creates a new Monitoring instance
  24. func New(ctx context.Context, config *Config, workersNumber int) (*Monitoring, error) {
  25. if err := config.Validate(); err != nil {
  26. return nil, err
  27. }
  28. m := &Monitoring{
  29. config: config,
  30. stats: stats.New(workersNumber),
  31. }
  32. var prErr, nlErr, ddErr, otelErr, cwErr error
  33. m.prometheus, prErr = prometheus.New(&config.Prometheus, m.stats)
  34. m.newrelic, nlErr = newrelic.New(&config.NewRelic, m.stats)
  35. m.datadog, ddErr = datadog.New(&config.DataDog, m.stats)
  36. m.otel, otelErr = otel.New(&config.OpenTelemetry, m.stats)
  37. m.cloudwatch, cwErr = cloudwatch.New(ctx, &config.CloudWatch, m.stats)
  38. err := errors.Join(prErr, nlErr, ddErr, otelErr, cwErr)
  39. return m, err
  40. }
  41. // Enabled returns true if at least one monitoring service is enabled
  42. func (m *Monitoring) Enabled() bool {
  43. return m.prometheus.Enabled() ||
  44. m.newrelic.Enabled() ||
  45. m.datadog.Enabled() ||
  46. m.otel.Enabled() ||
  47. m.cloudwatch.Enabled()
  48. }
  49. // Stats returns the stats instance
  50. func (m *Monitoring) Stats() *stats.Stats {
  51. return m.stats
  52. }
  53. // Stop stops all monitoring services
  54. func (m *Monitoring) Stop(ctx context.Context) {
  55. m.newrelic.Stop(ctx)
  56. m.datadog.Stop()
  57. m.otel.Stop(ctx)
  58. m.cloudwatch.Stop()
  59. }
  60. // StartPrometheus starts the Prometheus metrics server
  61. func (m *Monitoring) StartPrometheus(cancel context.CancelFunc) error {
  62. return m.prometheus.StartServer(cancel)
  63. }
  64. func (m *Monitoring) StartRequest(ctx context.Context, rw http.ResponseWriter, r *http.Request) (context.Context, context.CancelFunc, http.ResponseWriter) {
  65. promCancel, rw := m.prometheus.StartRequest(rw)
  66. ctx, nrCancel, rw := m.newrelic.StartTransaction(ctx, rw, r)
  67. ctx, ddCancel, rw := m.datadog.StartRootSpan(ctx, rw, r)
  68. ctx, otelCancel, rw := m.otel.StartRootSpan(ctx, rw, r)
  69. cancel := func() {
  70. promCancel()
  71. nrCancel()
  72. ddCancel()
  73. otelCancel()
  74. }
  75. return ctx, cancel, rw
  76. }
  77. func (m *Monitoring) SetMetadata(ctx context.Context, meta Meta) {
  78. for key, value := range meta {
  79. m.newrelic.SetMetadata(ctx, key, value)
  80. m.datadog.SetMetadata(ctx, key, value)
  81. m.otel.SetMetadata(ctx, key, value)
  82. }
  83. }
  84. func (m *Monitoring) StartQueueSegment(ctx context.Context) context.CancelFunc {
  85. promCancel := m.prometheus.StartQueueSegment()
  86. nrCancel := m.newrelic.StartSegment(ctx, "Queue", nil)
  87. ddCancel := m.datadog.StartSpan(ctx, "queue", nil)
  88. otelCancel := m.otel.StartSpan(ctx, "queue", nil)
  89. cancel := func() {
  90. promCancel()
  91. nrCancel()
  92. ddCancel()
  93. otelCancel()
  94. }
  95. return cancel
  96. }
  97. func (m *Monitoring) StartDownloadingSegment(ctx context.Context, meta Meta) context.CancelFunc {
  98. promCancel := m.prometheus.StartDownloadingSegment()
  99. nrCancel := m.newrelic.StartSegment(ctx, "Downloading image", meta)
  100. ddCancel := m.datadog.StartSpan(ctx, "downloading_image", meta)
  101. otelCancel := m.otel.StartSpan(ctx, "downloading_image", meta)
  102. cancel := func() {
  103. promCancel()
  104. nrCancel()
  105. ddCancel()
  106. otelCancel()
  107. }
  108. return cancel
  109. }
  110. func (m *Monitoring) StartProcessingSegment(ctx context.Context, meta Meta) context.CancelFunc {
  111. promCancel := m.prometheus.StartProcessingSegment()
  112. nrCancel := m.newrelic.StartSegment(ctx, "Processing image", meta)
  113. ddCancel := m.datadog.StartSpan(ctx, "processing_image", meta)
  114. otelCancel := m.otel.StartSpan(ctx, "processing_image", meta)
  115. cancel := func() {
  116. promCancel()
  117. nrCancel()
  118. ddCancel()
  119. otelCancel()
  120. }
  121. return cancel
  122. }
  123. func (m *Monitoring) StartStreamingSegment(ctx context.Context) context.CancelFunc {
  124. promCancel := m.prometheus.StartStreamingSegment()
  125. nrCancel := m.newrelic.StartSegment(ctx, "Streaming image", nil)
  126. ddCancel := m.datadog.StartSpan(ctx, "streaming_image", nil)
  127. otelCancel := m.otel.StartSpan(ctx, "streaming_image", nil)
  128. cancel := func() {
  129. promCancel()
  130. nrCancel()
  131. ddCancel()
  132. otelCancel()
  133. }
  134. return cancel
  135. }
  136. func (m *Monitoring) SendError(ctx context.Context, errType string, err error) {
  137. m.prometheus.IncrementErrorsTotal(errType)
  138. m.newrelic.SendError(ctx, errType, err)
  139. m.datadog.SendError(ctx, errType, err)
  140. m.otel.SendError(ctx, errType, err)
  141. }