Indicate cache hits in HTTP trace log (#29533)
Some checks failed
Release / call-build-workflow (push) Has been cancelled
Release / Publish Docker :release (push) Has been cancelled
Release / Github & APT (push) Has been cancelled
Release / Demo (push) Has been cancelled
Release / Hassio Addon (push) Has been cancelled

This commit is contained in:
andig 2026-05-01 13:56:58 +02:00 • committed by GitHub
parent 9b8f29a6bd
commit 7753d88809
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 10 deletions

View file

@ -101,37 +101,39 @@ func NewHTTP(log *util.Logger, method, uri string, insecure bool, cache time.Dur
method: method,
}
// override the transport to accept self-signed certificates
// build the cache stack without logging so the logging tripper
// can sit outside the cache and see cached responses too
var base http.RoundTripper = transport.Default()
if insecure {
p.Client.Transport = request.NewTripper(log, transport.Insecure())
base = transport.Insecure()
}
if cache > 0 {
// remove no-cache response headers
p.Client.Transport = &transport.Modifier{
base = &transport.Modifier{
Modifier: func(resp *http.Response) error {
dropNoCache(resp, "Cache-Control")
dropNoCache(resp, "Pragma")
return nil
},
Base: p.Client.Transport,
Base: base,
}
}
// http cache
p.Client.Transport = &httpcache.Transport{
base = &httpcache.Transport{
Cache: mc,
MarkCachedResponses: true,
Transport: p.Client.Transport,
Transport: base,
}
if cache > 0 {
cacheHeader := fmt.Sprintf("max-age=%d, must-revalidate", int(cache.Seconds()))
p.Client.Transport = &transport.Decorator{
base = &transport.Decorator{
Decorator: transport.DecorateHeaders(map[string]string{
"Cache-Control": cacheHeader,
}),
Base: p.Client.Transport,
Base: base,
}
// for cached requests enforce single inflight GET
@ -140,6 +142,9 @@ func NewHTTP(log *util.Logger, method, uri string, insecure bool, cache time.Dur
}
}
// logging is outermost so cache hits are visible in the trace log
p.Client.Transport = request.NewTripper(log, base)
return p
}

View file

@ -11,6 +11,7 @@ import (
"github.com/evcc-io/evcc/util"
"github.com/prometheus/client_golang/prometheus"
"github.com/sandrolain/httpcache"
)
type roundTripper struct {
@ -129,8 +130,6 @@ func dump(r io.ReadCloser, w *strings.Builder) error {
}
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
r.log.TRACE.Printf("%s %s", req.Method, req.URL.String())
// add evcc user agent
if req.Header.Get("User-Agent") == "" {
req = req.Clone(req.Context())
@ -165,6 +164,12 @@ func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
reqMetric.WithLabelValues(req.URL.Hostname()).Observe(time.Since(startTime).Seconds())
var cached string
if err == nil && resp.Header.Get(httpcache.XFromCache) != "" {
cached = " CACHED"
}
r.log.TRACE.Printf("%s%s %s", req.Method, cached, req.URL.String())
if err == nil {
resMetric.WithLabelValues(req.URL.Hostname(), strconv.Itoa(resp.StatusCode)).Add(1)