fork of whitequark.org/git-pages with mods for tangled

Add a Prometheus metric for blob/request encoding pairs.

Forcing the server to repeatedly decompress a large blob is a potential
DoS vector, so having a metric for this is essential.

Changed files
+20
src
+20
src/pages.go
··· 27 27 const notFoundPage = "404.html" 28 28 29 29 var ( 30 + serveEncodingCount = promauto.NewCounterVec(prometheus.CounterOpts{ 31 + Name: "git_pages_serve_encoding_count", 32 + Help: "Count of blob transform vs negotiated encoding", 33 + }, []string{"transform", "negotiated"}) 34 + 30 35 siteUpdatesCount = promauto.NewCounterVec(prometheus.CounterOpts{ 31 36 Name: "git_pages_site_updates", 32 37 Help: "Count of site updates in total", ··· 303 308 case Transform_Identity: 304 309 switch acceptedEncodings.Negotiate("identity") { 305 310 case "identity": 311 + serveEncodingCount. 312 + With(prometheus.Labels{"transform": "identity", "negotiated": "identity"}). 313 + Inc() 306 314 default: 307 315 negotiatedEncoding = false 316 + serveEncodingCount. 317 + With(prometheus.Labels{"transform": "identity", "negotiated": "failure"}). 318 + Inc() 308 319 } 309 320 case Transform_Zstd: 310 321 supported := []string{"zstd", "identity"} ··· 319 330 // it if Content-Encoding is unset or if it's a range request. 320 331 w.Header().Set("Content-Length", strconv.FormatInt(*entry.Size, 10)) 321 332 w.Header().Set("Content-Encoding", "zstd") 333 + serveEncodingCount. 334 + With(prometheus.Labels{"transform": "zstd", "negotiated": "zstd"}). 335 + Inc() 322 336 case "identity": 323 337 compressedData, _ := io.ReadAll(reader) 324 338 decompressedData, err := zstdDecoder.DecodeAll(compressedData, []byte{}) ··· 328 342 return err 329 343 } 330 344 reader = bytes.NewReader(decompressedData) 345 + serveEncodingCount. 346 + With(prometheus.Labels{"transform": "zstd", "negotiated": "identity"}). 347 + Inc() 331 348 default: 332 349 negotiatedEncoding = false 350 + serveEncodingCount. 351 + With(prometheus.Labels{"transform": "zstd", "negotiated": "failure"}). 352 + Inc() 333 353 } 334 354 default: 335 355 return fmt.Errorf("unexpected transform")