Skip to content

Commit

Permalink
add process start time header to client_golang prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
logicalhan committed May 25, 2023
1 parent 8b1a836 commit f2722c8
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions prometheus/promhttp/http.go
Expand Up @@ -37,21 +37,30 @@ import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/prometheus/common/expfmt"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)

const (
contentTypeHeader = "Content-Type"
contentEncodingHeader = "Content-Encoding"
acceptEncodingHeader = "Accept-Encoding"
contentTypeHeader = "Content-Type"
contentEncodingHeader = "Content-Encoding"
acceptEncodingHeader = "Accept-Encoding"
processStartTimeHeader = "Process-Start-Time"
)

var (
processStartTime time.Time
)

func init() {
processStartTime = time.Now()
}

var gzipPool = sync.Pool{
New: func() interface{} {
return gzip.NewWriter(nil)
Expand Down Expand Up @@ -121,6 +130,9 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
}

h := http.HandlerFunc(func(rsp http.ResponseWriter, req *http.Request) {
if opts.EnableProcessStartTimeHeader {
rsp.Header().Set(processStartTimeHeader, strconv.FormatInt(processStartTime.Unix(), 10))
}
if inFlightSem != nil {
select {
case inFlightSem <- struct{}{}: // All good, carry on.
Expand Down Expand Up @@ -366,6 +378,15 @@ type HandlerOpts struct {
// (which changes the identity of the resulting series on the Prometheus
// server).
EnableOpenMetrics bool
// If true, a process start time header is added to the response along
// with the metrics payload. This is useful because you receive the headers
// prior to the response body, and for large responses, this allows the
// scraping agent to stream metrics using the process start time to
// correctly offset counter metrics. The alternative is to use the metric
// process_start_time_seconds, which unfortunately tends to fall at the end
// of the response body, requiring the scraping agent to buffer the entire
// body in memory until process_start_time_seconds is reached.
EnableProcessStartTimeHeader bool
}

// gzipAccepted returns whether the client will accept gzip-encoded content.
Expand Down

0 comments on commit f2722c8

Please sign in to comment.