handler/meter: add HTTPHandlerFunc method
Some checks failed
coverage / build (push) Successful in 2m37s
test / test (push) Failing after 16m22s
sync / sync (push) Successful in 9s

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 573086694b

View File

@@ -5,6 +5,8 @@ import (
"compress/gzip" "compress/gzip"
"context" "context"
"fmt" "fmt"
"io"
http "net/http"
"strings" "strings"
"sync" "sync"
@@ -85,6 +87,33 @@ func NewHandler(opts ...Option) *Handler {
return &Handler{Options: options} 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
} else {
wr = w
}
if err := h.Options.Meter.Write(wr, 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 { func (h *Handler) Metrics(ctx context.Context, req *codecpb.Frame, rsp *codecpb.Frame) error {
log, ok := logger.FromContext(ctx) log, ok := logger.FromContext(ctx)
if !ok { if !ok {