Comments (6)
from caddy-prometheus.
Yes, I'll make a PR today.
from caddy-prometheus.
I agree that this is worth careful consideration. Handling the status code properly has been one of the harder challenges I've encountered while writing custom Caddy plugins.
I've gleaned most of my understanding of status code handling from the official errors
, log
, and proxy
plugins.
caddy/caddyhttp/errors/errors.go
// returning 0 signals that a response has been written
...
if status >= 400 {
h.errorPage(w, r, status)
return 0, err
}
caddy/caddyhttp/log/log.go
if status >= 400 {
// There was an error up the chain, but no response has been written yet.
// The error must be handled here so the log entry will record the response size.
...
}
caddy/caddyhttp/proxy/proxy.go
// if no errors, we're done here
if backendErr == nil {
return 0, nil
}
if backendErr == httpserver.MaxBytesExceededErr {
return http.StatusRequestEntityTooLarge, backendErr
}
Keeping the above snippets in mind, here's an updated suggestion for properly capturing the status code in the Prometheus plugin. This seems to work with my barrage of tests.
// Record response to get status code and size of the reply.
rw := httpserver.NewResponseRecorder(w)
status, err := next.ServeHTTP(rw, r)
var stat int
if err != nil {
if status == 0 {
// some middlewares set the status to 0, but return an non nil error: map these to status 500
stat = 500
} else {
// if the proxy encounters an error, it returns the appropriate status code (such as 502)
stat = status
}
} else if status == 0 {
// the proxy returns a status code of 0, but the actual status is available on rw
stat = rw.Status()
}
One loose end is that I don't like how it's possible for stat
to remain unmapped at 0 (when err == nil && status != 0
), but I don't think this case comes up in practice and perhaps 0 is the best mapping if it does.
from caddy-prometheus.
/cc @achernyak
from caddy-prometheus.
@jaredririe I think in the case of err == nil && status != 0
it would be fine to map status
directly to stat
. This would allow to always cleanly pull out the stat
variable.
Also, feel free to create a PR with your updates. It looks like you've done all the leg work on this, and it checks out on my test also.
from caddy-prometheus.
Can either with of you wrap this in a PR? Cause LGTM - happy to publish a update to caddy after the merge.
from caddy-prometheus.
Related Issues (20)
- 404 and errors aren't exported anymore HOT 2
- /metrics returns 404 HOT 3
- Reuse exporter for custom metrics HOT 3
- Status code always responds 0 when using proxy. HOT 1
- publish plugin to new website HOT 1
- 429 status not reported from ratelimit middleware HOT 8
- Would it be possible to listen on the same port as Caddy itself? HOT 4
- Provide a Grafana dashboard HOT 5
- redir-ed domain don't show in stats HOT 1
- Export user agent? HOT 4
- "Family" label doesn't work HOT 2
- Record request latency
- Using this plugin with websockets causes a panic HOT 10
- Tricky to add more directives to the proxy block HOT 1
- Change Family tag to understandable values HOT 1
- Restart failed: [prometheus] failed to obtain certificate HOT 1
- Add path label in metrics HOT 7
- Caddy's import path has changed HOT 2
- Caddyext does not exist anymore :)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from caddy-prometheus.