Skip to content

Commit

Permalink
feat: try not break websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdan-shulha committed Nov 3, 2024
1 parent e803daa commit a2956be
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Dockerfile
/caddy
/CHANGELOG.md
/README.md
/Caddyfile.dev
Binary file removed caddy
Binary file not shown.
42 changes: 23 additions & 19 deletions observability/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func (m *Observer) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyh
m.metrics.requestsInFlight.WithLabelValues(m.ServiceID, m.ProcessID, serverName, m.RuleID).Add(1)
defer m.metrics.requestsInFlight.WithLabelValues(m.ServiceID, m.ProcessID, serverName, m.RuleID).Add(-1)

recorder := newResponseRecorder(w)
recorder := caddyhttp.NewResponseRecorder(w, nil, func(statusCode int, header http.Header) bool {
m.metrics.requestsTtfb.WithLabelValues(m.ServiceID, m.ProcessID, serverName, m.RuleID, strconv.Itoa(statusCode)).Observe(time.Since(startTime).Seconds())

return false
})

err := next.ServeHTTP(recorder, r)
status := strconv.Itoa(recorder.Status())
Expand All @@ -29,31 +33,31 @@ func (m *Observer) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyh
m.metrics.requestsCount.WithLabelValues(m.ServiceID, m.ProcessID, serverName, m.RuleID, "500").Add(1)
}

if !recorder.firstByte.IsZero() {
m.metrics.requestsTtfb.WithLabelValues(m.ServiceID, m.ProcessID, serverName, m.RuleID, status).Observe(time.Since(recorder.firstByte).Seconds())
}
// if !recorder.firstByte.IsZero() {
// m.metrics.requestsTtfb.WithLabelValues(m.ServiceID, m.ProcessID, serverName, m.RuleID, status).Observe(time.Since(recorder.firstByte).Seconds())
// }

m.metrics.requestsDuration.WithLabelValues(m.ServiceID, m.ProcessID, serverName, m.RuleID, status).Observe(time.Since(startTime).Seconds())

return err
}

type ResponseRecorder struct {
caddyhttp.ResponseRecorder
// type ResponseRecorder struct {
// caddyhttp.ResponseRecorder

firstByte time.Time
}
// firstByte time.Time
// }

func newResponseRecorder(w http.ResponseWriter) *ResponseRecorder {
return &ResponseRecorder{
ResponseRecorder: caddyhttp.NewResponseRecorder(w, nil, nil),
}
}
// func newResponseRecorder(w http.ResponseWriter) *ResponseRecorder {
// return &ResponseRecorder{
// ResponseRecorder: caddyhttp.NewResponseRecorder(w, nil, nil),
// }
// }

func (r *ResponseRecorder) WriteHeader(statusCode int) {
if r.firstByte.IsZero() {
r.firstByte = time.Now()
}
// func (r *ResponseRecorder) WriteHeader(statusCode int) {
// if r.firstByte.IsZero() {
// r.firstByte = time.Now()
// }

r.ResponseRecorder.WriteHeader(statusCode)
}
// r.ResponseRecorder.WriteHeader(statusCode)
// }

0 comments on commit a2956be

Please sign in to comment.