Skip to content

Commit

Permalink
Add application handler result to metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Jul 7, 2023
1 parent de92118 commit ff1aa68
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 19 deletions.
30 changes: 25 additions & 5 deletions service/adapters/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const (
labelHandlerName = "handlerName"
labelRelayDownloaderState = "state"
labelTopic = "topic"

labelResult = "result"
labelResultSuccess = "success"
labelResultError = "error"
)

type Prometheus struct {
Expand All @@ -31,14 +35,14 @@ func NewPrometheus(logger logging.Logger) *Prometheus {
Name: "application_handler_calls_total",
Help: "Total number of calls to application handlers.",
},
[]string{labelHandlerName},
[]string{labelHandlerName, labelResult},
),
applicationHandlerCallDurationHistogram: promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "application_handler_calls_duration",
Help: "Duration of calls to application handlers in seconds.",
},
[]string{labelHandlerName},
[]string{labelHandlerName, labelResult},
),
relayDownloaderStateGauge: promauto.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down Expand Up @@ -87,14 +91,30 @@ func NewApplicationCall(p *Prometheus, handlerName string, logger logging.Logger
}
}

func (a *ApplicationCall) End() {
func (a *ApplicationCall) End(err error) {
duration := time.Since(a.start)

a.logger.Debug().
WithField("handlerName", a.handlerName).
WithField("duration", duration).
WithError(err).
Message("application call")

a.p.applicationHandlerCallsCounter.With(prometheus.Labels{labelHandlerName: a.handlerName}).Inc()
a.p.applicationHandlerCallDurationHistogram.With(prometheus.Labels{labelHandlerName: a.handlerName}).Observe(duration.Seconds())
labels := a.getLabels(err)
a.p.applicationHandlerCallsCounter.With(labels).Inc()
a.p.applicationHandlerCallDurationHistogram.With(labels).Observe(duration.Seconds())
}

func (a *ApplicationCall) getLabels(err error) prometheus.Labels {
labels := prometheus.Labels{
labelHandlerName: a.handlerName,
}

if err == nil {
labels[labelResult] = labelResultSuccess
} else {
labels[labelResult] = labelResultError
}

return labels
}
2 changes: 1 addition & 1 deletion service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,5 @@ type Metrics interface {
}

type ApplicationCall interface {
End()
End(err error)
}
2 changes: 1 addition & 1 deletion service/app/handler_get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewGetEventsHandler(
}

func (h *GetEventsHandler) Handle(ctx context.Context, filters domain.Filters) <-chan EventOrEOSEOrError {
defer h.metrics.TrackApplicationCall("getEvents").End()
defer h.metrics.TrackApplicationCall("getEvents").End(nil)

ch := make(chan EventOrEOSEOrError)
go h.send(ctx, filters, ch)
Expand Down
4 changes: 2 additions & 2 deletions service/app/handler_get_public_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func NewGetPublicKeysHandler(
}
}

func (h *GetPublicKeysHandler) Handle(ctx context.Context, relay domain.RelayAddress) ([]domain.PublicKey, error) {
defer h.metrics.TrackApplicationCall("getPublicKeys").End()
func (h *GetPublicKeysHandler) Handle(ctx context.Context, relay domain.RelayAddress) (keys []domain.PublicKey, err error) {
defer func() { h.metrics.TrackApplicationCall("getPublicKeys").End(err) }()

var result []domain.PublicKey
if err := h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error {
Expand Down
4 changes: 2 additions & 2 deletions service/app/handler_get_relays.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func NewGetRelaysHandler(
}
}

func (h *GetRelaysHandler) Handle(ctx context.Context) ([]domain.RelayAddress, error) {
defer h.metrics.TrackApplicationCall("getRelays").End()
func (h *GetRelaysHandler) Handle(ctx context.Context) (addresses []domain.RelayAddress, err error) {
defer func() { h.metrics.TrackApplicationCall("getRelays").End(err) }()

var result []domain.RelayAddress
if err := h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error {
Expand Down
4 changes: 2 additions & 2 deletions service/app/handler_get_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func NewGetTokensHandler(
}
}

func (h *GetTokensHandler) Handle(ctx context.Context, publicKey domain.PublicKey) ([]domain.APNSToken, error) {
defer h.metrics.TrackApplicationCall("getTokens").End()
func (h *GetTokensHandler) Handle(ctx context.Context, publicKey domain.PublicKey) (tokens []domain.APNSToken, err error) {
defer func() { h.metrics.TrackApplicationCall("getTokens").End(err) }()

var result []domain.APNSToken
if err := h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error {
Expand Down
4 changes: 2 additions & 2 deletions service/app/handler_process_saved_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func NewProcessSavedEventHandler(
}
}

func (h *ProcessSavedEventHandler) Handle(ctx context.Context, cmd ProcessSavedEvent) error {
defer h.metrics.TrackApplicationCall("processSavedEvent").End()
func (h *ProcessSavedEventHandler) Handle(ctx context.Context, cmd ProcessSavedEvent) (err error) {
defer func() { h.metrics.TrackApplicationCall("processSavedEvent").End(err) }()

logger := h.logger.WithField("event.id", cmd.eventId.Hex())

Expand Down
4 changes: 2 additions & 2 deletions service/app/handler_save_received_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func NewSaveReceivedEventHandler(
}
}

func (h *SaveReceivedEventHandler) Handle(ctx context.Context, cmd SaveReceivedEvent) error {
defer h.metrics.TrackApplicationCall("saveReceivedEvent").End()
func (h *SaveReceivedEventHandler) Handle(ctx context.Context, cmd SaveReceivedEvent) (err error) {
defer func() { h.metrics.TrackApplicationCall("saveReceivedEvent").End(err) }()

if !domain.ShouldDownloadEventKind(cmd.event.Kind()) {
return fmt.Errorf("event '%s' shouldn't have been downloaded", cmd.event.String())
Expand Down
4 changes: 2 additions & 2 deletions service/app/handler_save_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewSaveRegistrationHandler(
}
}

func (h *SaveRegistrationHandler) Handle(ctx context.Context, cmd SaveRegistration) error {
defer h.metrics.TrackApplicationCall("saveRegistration").End()
func (h *SaveRegistrationHandler) Handle(ctx context.Context, cmd SaveRegistration) (err error) {
defer func() { h.metrics.TrackApplicationCall("saveRegistration").End(err) }()

h.logger.Debug().
WithField("apnsToken", cmd.registration.APNSToken().Hex()).
Expand Down

0 comments on commit ff1aa68

Please sign in to comment.