Skip to content

Commit 651386b

Browse files
authored
PRW2: use client-golang codes (#7027)
Signed-off-by: SungJin1212 <[email protected]>
1 parent d3c6964 commit 651386b

File tree

8 files changed

+1106
-32
lines changed

8 files changed

+1106
-32
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ require (
8888
github.com/oklog/ulid/v2 v2.1.1
8989
github.com/parquet-go/parquet-go v0.25.1
9090
github.com/prometheus-community/parquet-common v0.0.0-20250827225610-65f0b68d35e6
91+
github.com/prometheus/client_golang/exp v0.0.0-20250914183048-a974e0d45e0a
9192
github.com/prometheus/procfs v0.16.1
9293
github.com/sercand/kuberesolver/v5 v5.1.1
9394
github.com/tjhop/slog-gokit v0.1.4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,8 @@ github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP
16311631
github.com/prometheus/client_golang v1.11.1/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0=
16321632
github.com/prometheus/client_golang v1.23.0-rc.1 h1:Is/nGODd8OsJlNQSybeYBwY/B6aHrN7+QwVUYutHSgw=
16331633
github.com/prometheus/client_golang v1.23.0-rc.1/go.mod h1:i/o0R9ByOnHX0McrTMTyhYvKE4haaf2mW08I+jGAjEE=
1634+
github.com/prometheus/client_golang/exp v0.0.0-20250914183048-a974e0d45e0a h1:RF1vfKM34/3DbGNis22BGd6sDDY3XBi0eM7pYqmOEO0=
1635+
github.com/prometheus/client_golang/exp v0.0.0-20250914183048-a974e0d45e0a/go.mod h1:FGJuwvfcPY0V5enm+w8zF1RNS062yugQtPPQp1c4Io4=
16341636
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
16351637
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
16361638
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=

pkg/util/push/push.go

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import (
55
"fmt"
66
"net/http"
77
"strconv"
8-
"strings"
98

109
"github.com/go-kit/log/level"
11-
"github.com/prometheus/prometheus/config"
10+
"github.com/prometheus/client_golang/exp/api/remote"
1211
"github.com/prometheus/prometheus/model/labels"
1312
writev2 "github.com/prometheus/prometheus/prompb/io/prometheus/write/v2"
1413
"github.com/prometheus/prometheus/util/compression"
@@ -125,14 +124,14 @@ func Handler(remoteWrite2Enabled bool, maxRecvMsgSize int, sourceIPs *middleware
125124
contentType = appProtoContentType
126125
}
127126

128-
msgType, err := parseProtoMsg(contentType)
127+
msgType, err := remote.ParseProtoMsg(contentType)
129128
if err != nil {
130129
level.Error(logger).Log("Error decoding remote write request", "err", err)
131130
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
132131
return
133132
}
134133

135-
if msgType != config.RemoteWriteProtoMsgV1 && msgType != config.RemoteWriteProtoMsgV2 {
134+
if msgType != remote.WriteV1MessageType && msgType != remote.WriteV2MessageType {
136135
level.Error(logger).Log("Not accepted msg type", "msgType", msgType, "err", err)
137136
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
138137
return
@@ -148,9 +147,9 @@ func Handler(remoteWrite2Enabled bool, maxRecvMsgSize int, sourceIPs *middleware
148147
}
149148

150149
switch msgType {
151-
case config.RemoteWriteProtoMsgV1:
150+
case remote.WriteV1MessageType:
152151
handlePRW1()
153-
case config.RemoteWriteProtoMsgV2:
152+
case remote.WriteV2MessageType:
154153
handlePRW2()
155154
}
156155
} else {
@@ -165,32 +164,6 @@ func setPRW2RespHeader(w http.ResponseWriter, samples, histograms, exemplars int
165164
w.Header().Set(rw20WrittenExemplarsHeader, strconv.FormatInt(exemplars, 10))
166165
}
167166

168-
// Refer to parseProtoMsg in https://github.com/prometheus/prometheus/blob/main/storage/remote/write_handler.go
169-
func parseProtoMsg(contentType string) (config.RemoteWriteProtoMsg, error) {
170-
contentType = strings.TrimSpace(contentType)
171-
172-
parts := strings.Split(contentType, ";")
173-
if parts[0] != appProtoContentType {
174-
return "", fmt.Errorf("expected %v as the first (media) part, got %v content-type", appProtoContentType, contentType)
175-
}
176-
// Parse potential https://www.rfc-editor.org/rfc/rfc9110#parameter
177-
for _, p := range parts[1:] {
178-
pair := strings.Split(p, "=")
179-
if len(pair) != 2 {
180-
return "", fmt.Errorf("as per https://www.rfc-editor.org/rfc/rfc9110#parameter expected parameters to be key-values, got %v in %v content-type", p, contentType)
181-
}
182-
if pair[0] == "proto" {
183-
ret := config.RemoteWriteProtoMsg(pair[1])
184-
if err := ret.Validate(); err != nil {
185-
return "", fmt.Errorf("got %v content type; %w", contentType, err)
186-
}
187-
return ret, nil
188-
}
189-
}
190-
// No "proto=" parameter, assuming v1.
191-
return config.RemoteWriteProtoMsgV1, nil
192-
}
193-
194167
func convertV2RequestToV1(req *writev2.Request) (cortexpb.PreallocWriteRequest, error) {
195168
var v1Req cortexpb.PreallocWriteRequest
196169
v1Timeseries := make([]cortexpb.PreallocTimeseries, 0, len(req.Timeseries))

vendor/github.com/prometheus/client_golang/exp/LICENSE

Lines changed: 201 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)