@@ -5,7 +5,11 @@ import (
55 "fmt"
66 "net"
77 "net/http"
8+ "strconv"
9+ "time"
810
11+ "github.com/prometheus/client_golang/prometheus"
12+ "github.com/prometheus/client_golang/prometheus/promhttp"
913 "github.com/sirupsen/logrus"
1014
1115 "github.com/acamilleri/go-plexhooks/plex"
@@ -47,30 +51,73 @@ func (a *App) Run() error {
4751 }
4852
4953 http .HandleFunc ("/events" , a .handler ())
54+ http .Handle ("/metrics" , promhttp .Handler ())
5055
5156 a .log .Infof ("running server on %s" , a .listenAddr )
5257 return http .ListenAndServe (a .listenAddr .String (), nil )
5358}
5459
5560func (a * App ) handler () http.HandlerFunc {
5661 return func (w http.ResponseWriter , r * http.Request ) {
62+ var statusCode int = http .StatusOK
5763 defer r .Body .Close ()
58- w .WriteHeader (http .StatusOK )
5964
60- event , err := parseRequest (r )
61- if err != nil {
62- a .log .WithError (err ).Error ("failed to parse request" )
65+ trackRequestDuration := newTrackRequestDuration (r .Method , "/events" )
66+ if r .Method != http .MethodPost {
67+ statusCode = http .StatusMethodNotAllowed
68+ w .WriteHeader (statusCode )
69+ time .Sleep (time .Second * 5 )
70+ httpRequestTotal .With (prometheus.Labels {
71+ "handler" : "/events" ,
72+ "method" : r .Method ,
73+ "code" : strconv .Itoa (statusCode ),
74+ }).Inc ()
75+ trackRequestDuration .Finish ()
76+ return
6377 }
6478
65- a .log .Infof ("%s event handled" , event .Name )
66- err = a .triggerActionsOnEvent (event )
79+ err := a .handleRequest (r )
6780 if err != nil {
68- a .log .WithError (err ).Errorf ("%s event actions failed" , event .Name )
81+ statusCode = http .StatusInternalServerError
82+ w .WriteHeader (statusCode )
83+ trackRequestDuration .Finish ()
84+ httpRequestTotal .With (prometheus.Labels {
85+ "handler" : "/events" ,
86+ "method" : r .Method ,
87+ "code" : strconv .Itoa (statusCode ),
88+ }).Inc ()
89+ return
6990 }
91+
92+ w .WriteHeader (statusCode )
93+ trackRequestDuration .Finish ()
94+ httpRequestTotal .With (prometheus.Labels {
95+ "handler" : "/events" ,
96+ "method" : r .Method ,
97+ "code" : strconv .Itoa (statusCode ),
98+ }).Inc ()
7099 }
71100}
72101
102+ func (a * App ) handleRequest (r * http.Request ) error {
103+ event , err := parseRequest (r )
104+ if err != nil {
105+ a .log .WithError (err ).Error ("failed to parse request" )
106+ return err
107+ }
108+
109+ a .log .Infof ("%s event handled" , event .Name )
110+ err = a .triggerActionsOnEvent (event )
111+ if err != nil {
112+ a .log .WithError (err ).Errorf ("%s event actions failed" , event .Name )
113+ return err
114+ }
115+
116+ return nil
117+ }
118+
73119func (a * App ) triggerActionsOnEvent (event plex.Event ) error {
120+ eventsReceivedTotal .With (prometheus.Labels {"event" : event .Name .String ()}).Inc ()
74121 hookName := event .Name
75122
76123 actions := a .actions .GetByHook (hookName )
@@ -80,15 +127,26 @@ func (a *App) triggerActionsOnEvent(event plex.Event) error {
80127
81128 for _ , action := range actions {
82129 name := action .Name ()
130+ actionDuration := newTrackActionDuration (event , action )
83131
84132 a .log .Debugf ("action %s triggered" , name )
85133 err := action .Execute (event )
86134 if err != nil {
87135 a .log .WithError (err ).Errorf ("action %s failed" , name )
136+ actionsErrorTotal .With (
137+ prometheus.Labels {"event" : event .Name .String (), "action" : action .Name ()},
138+ ).Inc ()
139+ actionDuration .Finish ()
88140 continue
89141 }
142+
90143 a .log .Infof ("action %s success" , name )
144+ actionDuration .Finish ()
145+ actionsSuccessTotal .With (
146+ prometheus.Labels {"event" : event .Name .String (), "action" : action .Name ()},
147+ ).Inc ()
91148 }
149+
92150 return nil
93151}
94152
0 commit comments