Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add process start time header to client_golang prometheus #1278

Merged
merged 4 commits into from May 26, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 24 additions & 5 deletions prometheus/promhttp/http.go
Expand Up @@ -37,21 +37,28 @@
"fmt"
"io"
"net/http"
"strconv"
"strings"
"sync"
"time"

"github.com/prometheus/common/expfmt"

"github.com/prometheus/client_golang/prometheus"

Check failure on line 45 in prometheus/promhttp/http.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/prometheus/client_golang (goimports)
"github.com/prometheus/common/expfmt"
logicalhan marked this conversation as resolved.
Show resolved Hide resolved
)

const (
contentTypeHeader = "Content-Type"
contentEncodingHeader = "Content-Encoding"
acceptEncodingHeader = "Accept-Encoding"
contentTypeHeader = "Content-Type"
contentEncodingHeader = "Content-Encoding"
acceptEncodingHeader = "Accept-Encoding"
processStartTimeHeader = "Process-Start-Time"
logicalhan marked this conversation as resolved.
Show resolved Hide resolved
)

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 +128,9 @@
}

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 +376,15 @@
// (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.
logicalhan marked this conversation as resolved.
Show resolved Hide resolved
EnableProcessStartTimeHeader bool
logicalhan marked this conversation as resolved.
Show resolved Hide resolved
}

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