mirror of
				https://github.com/binwiederhier/ntfy.git
				synced 2025-10-30 12:32:12 +01:00 
			
		
		
		
	More metrics
This commit is contained in:
		
							parent
							
								
									754b456320
								
							
						
					
					
						commit
						fe731d43cd
					
				
					 8 changed files with 75 additions and 28 deletions
				
			
		|  | @ -67,6 +67,7 @@ func (c *fileCache) Write(id string, in io.Reader, limiters ...util.Limiter) (in | |||
| 	} | ||||
| 	c.mu.Lock() | ||||
| 	c.totalSizeCurrent += size | ||||
| 	metrics.attachmentsTotalSize.Set(float64(c.totalSizeCurrent)) | ||||
| 	c.mu.Unlock() | ||||
| 	return size, nil | ||||
| } | ||||
|  | @ -89,6 +90,7 @@ func (c *fileCache) Remove(ids ...string) error { | |||
| 	c.mu.Lock() | ||||
| 	c.totalSizeCurrent = size | ||||
| 	c.mu.Unlock() | ||||
| 	metrics.attachmentsTotalSize.Set(float64(size)) | ||||
| 	return nil | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -596,8 +596,14 @@ func (s *Server) handleMatrixDiscovery(w http.ResponseWriter) error { | |||
| } | ||||
| 
 | ||||
| func (s *Server) handlePublishInternal(r *http.Request, v *visitor) (*message, error) { | ||||
| 	t := fromContext[*topic](r, contextTopic) | ||||
| 	vrate := fromContext[*visitor](r, contextRateVisitor) | ||||
| 	t, err := fromContext[*topic](r, contextTopic) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	vrate, err := fromContext[*visitor](r, contextRateVisitor) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	body, err := util.Peek(r.Body, s.config.MessageLimit) | ||||
| 	if err != nil { | ||||
| 		return nil, err | ||||
|  | @ -676,6 +682,9 @@ func (s *Server) handlePublishInternal(r *http.Request, v *visitor) (*message, e | |||
| 	s.mu.Lock() | ||||
| 	s.messages++ | ||||
| 	s.mu.Unlock() | ||||
| 	if unifiedpush { | ||||
| 		metrics.unifiedPushPublishedSuccess.Inc() | ||||
| 	} | ||||
| 	return m, nil | ||||
| } | ||||
| 
 | ||||
|  | @ -693,9 +702,16 @@ func (s *Server) handlePublishMatrix(w http.ResponseWriter, r *http.Request, v * | |||
| 	_, err := s.handlePublishInternal(r, v) | ||||
| 	if err != nil { | ||||
| 		metrics.messagesPublishedFailure.Inc() | ||||
| 		metrics.matrixPublishedFailure.Inc() | ||||
| 		if e, ok := err.(*errHTTP); ok && e.HTTPCode == errHTTPInsufficientStorageUnifiedPush.HTTPCode { | ||||
| 			topic := fromContext[*topic](r, contextTopic) | ||||
| 			pushKey := fromContext[string](r, contextMatrixPushKey) | ||||
| 			topic, err := fromContext[*topic](r, contextTopic) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			pushKey, err := fromContext[string](r, contextMatrixPushKey) | ||||
| 			if err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			if time.Since(topic.LastAccess()) > matrixRejectPushKeyForUnifiedPushTopicWithoutRateVisitorAfter { | ||||
| 				return writeMatrixResponse(w, pushKey) | ||||
| 			} | ||||
|  | @ -703,6 +719,7 @@ func (s *Server) handlePublishMatrix(w http.ResponseWriter, r *http.Request, v * | |||
| 		return err | ||||
| 	} | ||||
| 	metrics.messagesPublishedSuccess.Inc() | ||||
| 	metrics.matrixPublishedSuccess.Inc() | ||||
| 	return writeMatrixSuccess(w) | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -9,17 +9,23 @@ var ( | |||
| ) | ||||
| 
 | ||||
| type serverMetrics struct { | ||||
| 	messagesPublishedSuccess prometheus.Counter | ||||
| 	messagesPublishedFailure prometheus.Counter | ||||
| 	messagesCached           prometheus.Gauge | ||||
| 	firebasePublishedSuccess prometheus.Counter | ||||
| 	firebasePublishedFailure prometheus.Counter | ||||
| 	emailsPublishedSuccess   prometheus.Counter | ||||
| 	emailsPublishedFailure   prometheus.Counter | ||||
| 	visitors                 prometheus.Gauge | ||||
| 	subscribers              prometheus.Gauge | ||||
| 	topics                   prometheus.Gauge | ||||
| 	httpRequests             *prometheus.CounterVec | ||||
| 	messagesPublishedSuccess    prometheus.Counter | ||||
| 	messagesPublishedFailure    prometheus.Counter | ||||
| 	messagesCached              prometheus.Gauge | ||||
| 	firebasePublishedSuccess    prometheus.Counter | ||||
| 	firebasePublishedFailure    prometheus.Counter | ||||
| 	emailsPublishedSuccess      prometheus.Counter | ||||
| 	emailsPublishedFailure      prometheus.Counter | ||||
| 	emailsReceivedSuccess       prometheus.Counter | ||||
| 	emailsReceivedFailure       prometheus.Counter | ||||
| 	unifiedPushPublishedSuccess prometheus.Counter | ||||
| 	matrixPublishedSuccess      prometheus.Counter | ||||
| 	matrixPublishedFailure      prometheus.Counter | ||||
| 	attachmentsTotalSize        prometheus.Gauge | ||||
| 	visitors                    prometheus.Gauge | ||||
| 	subscribers                 prometheus.Gauge | ||||
| 	topics                      prometheus.Gauge | ||||
| 	httpRequests                *prometheus.CounterVec | ||||
| } | ||||
| 
 | ||||
| func newMetrics() *serverMetrics { | ||||
|  | @ -45,6 +51,24 @@ func newMetrics() *serverMetrics { | |||
| 		emailsPublishedFailure: prometheus.NewCounter(prometheus.CounterOpts{ | ||||
| 			Name: "ntfy_emails_sent_failure", | ||||
| 		}), | ||||
| 		emailsReceivedSuccess: prometheus.NewCounter(prometheus.CounterOpts{ | ||||
| 			Name: "ntfy_emails_received_success", | ||||
| 		}), | ||||
| 		emailsReceivedFailure: prometheus.NewCounter(prometheus.CounterOpts{ | ||||
| 			Name: "ntfy_emails_received_failure", | ||||
| 		}), | ||||
| 		unifiedPushPublishedSuccess: prometheus.NewCounter(prometheus.CounterOpts{ | ||||
| 			Name: "ntfy_unifiedpush_published_success", | ||||
| 		}), | ||||
| 		matrixPublishedSuccess: prometheus.NewCounter(prometheus.CounterOpts{ | ||||
| 			Name: "ntfy_matrix_published_success", | ||||
| 		}), | ||||
| 		matrixPublishedFailure: prometheus.NewCounter(prometheus.CounterOpts{ | ||||
| 			Name: "ntfy_matrix_published_failure", | ||||
| 		}), | ||||
| 		attachmentsTotalSize: prometheus.NewGauge(prometheus.GaugeOpts{ | ||||
| 			Name: "ntfy_attachments_total_size", | ||||
| 		}), | ||||
| 		visitors: prometheus.NewGauge(prometheus.GaugeOpts{ | ||||
| 			Name: "ntfy_visitors_total", | ||||
| 		}), | ||||
|  | @ -66,6 +90,12 @@ func newMetrics() *serverMetrics { | |||
| 		m.firebasePublishedFailure, | ||||
| 		m.emailsPublishedSuccess, | ||||
| 		m.emailsPublishedFailure, | ||||
| 		m.emailsReceivedSuccess, | ||||
| 		m.emailsReceivedFailure, | ||||
| 		m.unifiedPushPublishedSuccess, | ||||
| 		m.matrixPublishedSuccess, | ||||
| 		m.matrixPublishedFailure, | ||||
| 		m.attachmentsTotalSize, | ||||
| 		m.visitors, | ||||
| 		m.subscribers, | ||||
| 		m.topics, | ||||
|  |  | |||
|  | @ -12,6 +12,7 @@ const ( | |||
| 	contextRateVisitor contextKey = iota + 2586 | ||||
| 	contextTopic | ||||
| 	contextMatrixPushKey | ||||
| 	contextUnifiedPush | ||||
| ) | ||||
| 
 | ||||
| func (s *Server) limitRequests(next handleFunc) handleFunc { | ||||
|  |  | |||
|  | @ -165,6 +165,7 @@ func (s *smtpSession) Data(r io.Reader) error { | |||
| 		s.backend.mu.Lock() | ||||
| 		s.backend.success++ | ||||
| 		s.backend.mu.Unlock() | ||||
| 		metrics.emailsReceivedSuccess.Inc() | ||||
| 		return nil | ||||
| 	}) | ||||
| } | ||||
|  | @ -217,6 +218,7 @@ func (s *smtpSession) withFailCount(fn func() error) error { | |||
| 		// We do not want to spam the log with WARN messages. | ||||
| 		logem(s.conn).Err(err).Debug("Incoming mail error") | ||||
| 		s.backend.failure++ | ||||
| 		metrics.emailsReceivedFailure.Inc() | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|  |  | |||
|  | @ -107,10 +107,10 @@ func withContext(r *http.Request, ctx map[contextKey]any) *http.Request { | |||
| 	return r.WithContext(c) | ||||
| } | ||||
| 
 | ||||
| func fromContext[T any](r *http.Request, key contextKey) T { | ||||
| func fromContext[T any](r *http.Request, key contextKey) (T, error) { | ||||
| 	t, ok := r.Context().Value(key).(T) | ||||
| 	if !ok { | ||||
| 		panic(fmt.Sprintf("cannot find key %v in request context", key)) | ||||
| 		return t, fmt.Errorf("cannot find key %v in request context", key) | ||||
| 	} | ||||
| 	return t | ||||
| 	return t, nil | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 binwiederhier
						binwiederhier