handler/meter: add HTTPHandlerFunc method
Some checks failed
coverage / build (push) Has been cancelled
test / test (push) Has been cancelled

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2025-08-04 21:43:15 +03:00
parent 3992a2bfb2
commit 4ece8b1d18

View File

@@ -5,6 +5,8 @@ import (
"compress/gzip"
"context"
"fmt"
"io"
http "net/http"
"strings"
"sync"
@@ -85,6 +87,31 @@ func NewHandler(opts ...Option) *Handler {
return &Handler{Options: options}
}
func (h *Handler) HTTPHandlerFunc(w http.ResponseWriter, r *http.Request) {
var wr io.Writer
if v := r.Header.Get(acceptEncodingHeader); strings.Contains(v, "gzip") && !h.Options.DisableCompress {
w.Header().Add(contentEncodingHeader, "gzip")
gz := gzipPool.Get().(*gzip.Writer)
defer gzipPool.Put(gz)
gz.Reset(w)
defer gz.Close()
wr = gz
}
if err := h.Options.Meter.Write(w, h.Options.MeterOptions...); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// gz.Flush() must be called after writing metrics to ensure buffered data is written to the underlying writer.
if gz, ok := wr.(*gzip.Writer); ok {
gz.Flush()
}
}
func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error {
log, ok := logger.FromContext(ctx)
if !ok {