Skip to content

Commit b8cb86a

Browse files
Han Kangbwplotka
andauthored
add process start time header to client_golang prometheus (#1278)
* add process start time header to client_golang prometheus Signed-off-by: Han Kang <[email protected]> * Apply suggestions from code review Co-authored-by: Bartlomiej Plotka <[email protected]> Signed-off-by: Han Kang <[email protected]> * fix up changes due to incorporation of feedback * fix lint issues --------- Signed-off-by: Han Kang <[email protected]> Co-authored-by: Bartlomiej Plotka <[email protected]>
1 parent 8b1a836 commit b8cb86a

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

prometheus/promhttp/http.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"fmt"
3838
"io"
3939
"net/http"
40+
"strconv"
4041
"strings"
4142
"sync"
4243
"time"
@@ -47,9 +48,10 @@ import (
4748
)
4849

4950
const (
50-
contentTypeHeader = "Content-Type"
51-
contentEncodingHeader = "Content-Encoding"
52-
acceptEncodingHeader = "Accept-Encoding"
51+
contentTypeHeader = "Content-Type"
52+
contentEncodingHeader = "Content-Encoding"
53+
acceptEncodingHeader = "Accept-Encoding"
54+
processStartTimeHeader = "Process-Start-Time-Unix"
5355
)
5456

5557
var gzipPool = sync.Pool{
@@ -121,6 +123,9 @@ func HandlerForTransactional(reg prometheus.TransactionalGatherer, opts HandlerO
121123
}
122124

123125
h := http.HandlerFunc(func(rsp http.ResponseWriter, req *http.Request) {
126+
if !opts.ProcessStartTime.IsZero() {
127+
rsp.Header().Set(processStartTimeHeader, strconv.FormatInt(opts.ProcessStartTime.Unix(), 10))
128+
}
124129
if inFlightSem != nil {
125130
select {
126131
case inFlightSem <- struct{}{}: // All good, carry on.
@@ -366,6 +371,14 @@ type HandlerOpts struct {
366371
// (which changes the identity of the resulting series on the Prometheus
367372
// server).
368373
EnableOpenMetrics bool
374+
// ProcessStartTime allows setting process start timevalue that will be exposed
375+
// with "Process-Start-Time-Unix" response header along with the metrics
376+
// payload. This allow callers to have efficient transformations to cumulative
377+
// counters (e.g. OpenTelemetry) or generally _created timestamp estimation per
378+
// scrape target.
379+
// NOTE: This feature is experimental and not covered by OpenMetrics or Prometheus
380+
// exposition format.
381+
ProcessStartTime time.Time
369382
}
370383

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

0 commit comments

Comments
 (0)