Skip to content

Commit 89f4253

Browse files
committed
Merge remote-tracking branch 'prometheus/main' into add-unit
Signed-off-by: Arianna Vespri <[email protected]>
2 parents 1755cb6 + 52e512c commit 89f4253

File tree

18 files changed

+356
-211
lines changed

18 files changed

+356
-211
lines changed

.circleci/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ workflows:
9595
matrix:
9696
parameters:
9797
go_version:
98-
- "1.20"
9998
- "1.21"
99+
- "1.22"
100100
- test-assets:
101101
name: assets-go-<< matrix.go_version >>
102102
matrix:
103103
parameters:
104104
go_version:
105-
- "1.21"
105+
- "1.22"
106106
- style:
107107
name: style
108-
go_version: "1.21"
108+
go_version: "1.22"

.github/workflows/golangci-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: install Go
2929
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
3030
with:
31-
go-version: 1.21.x
31+
go-version: 1.22.x
3232
- name: Install snmp_exporter/generator dependencies
3333
run: sudo apt-get update && sudo apt-get -y install libsnmp-dev
3434
if: github.repository == 'prometheus/snmp_exporter'

Makefile.common

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ SKIP_GOLANGCI_LINT :=
6262
GOLANGCI_LINT :=
6363
GOLANGCI_LINT_OPTS ?=
6464
GOLANGCI_LINT_VERSION ?= v1.55.2
65-
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64.
65+
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64/arm64.
6666
# windows isn't included here because of the path separator being different.
6767
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
68-
ifeq ($(GOHOSTARCH),$(filter $(GOHOSTARCH),amd64 i386))
68+
ifeq ($(GOHOSTARCH),$(filter $(GOHOSTARCH),amd64 i386 arm64))
6969
# If we're in CI and there is an Actions file, that means the linter
7070
# is being run in Actions, so we don't need to run it here.
7171
ifneq (,$(SKIP_GOLANGCI_LINT))

assets/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/prometheus/common/assets
22

3-
go 1.20
3+
go 1.21

config/http_config.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ type HTTPClientConfig struct {
309309
// The omitempty flag is not set, because it would be hidden from the
310310
// marshalled configuration when set to false.
311311
EnableHTTP2 bool `yaml:"enable_http2" json:"enable_http2"`
312+
// Host optionally overrides the Host header to send.
313+
// If empty, the host from the URL will be used.
314+
Host string `yaml:"host,omitempty" json:"host,omitempty"`
312315
// Proxy configuration.
313316
ProxyConfig `yaml:",inline"`
314317
}
@@ -427,6 +430,7 @@ type httpClientOptions struct {
427430
http2Enabled bool
428431
idleConnTimeout time.Duration
429432
userAgent string
433+
host string
430434
}
431435

432436
// HTTPClientOption defines an option that can be applied to the HTTP client.
@@ -467,6 +471,13 @@ func WithUserAgent(ua string) HTTPClientOption {
467471
}
468472
}
469473

474+
// WithHost allows setting the host header.
475+
func WithHost(host string) HTTPClientOption {
476+
return func(opts *httpClientOptions) {
477+
opts.host = host
478+
}
479+
}
480+
470481
// NewClient returns a http.Client using the specified http.RoundTripper.
471482
func newClient(rt http.RoundTripper) *http.Client {
472483
return &http.Client{Transport: rt}
@@ -568,6 +579,10 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
568579
rt = NewUserAgentRoundTripper(opts.userAgent, rt)
569580
}
570581

582+
if opts.host != "" {
583+
rt = NewHostRoundTripper(opts.host, rt)
584+
}
585+
571586
// Return a new configured RoundTripper.
572587
return rt, nil
573588
}
@@ -1164,11 +1179,21 @@ type userAgentRoundTripper struct {
11641179
rt http.RoundTripper
11651180
}
11661181

1182+
type hostRoundTripper struct {
1183+
host string
1184+
rt http.RoundTripper
1185+
}
1186+
11671187
// NewUserAgentRoundTripper adds the user agent every request header.
11681188
func NewUserAgentRoundTripper(userAgent string, rt http.RoundTripper) http.RoundTripper {
11691189
return &userAgentRoundTripper{userAgent, rt}
11701190
}
11711191

1192+
// NewHostRoundTripper sets the [http.Request.Host] of every request.
1193+
func NewHostRoundTripper(host string, rt http.RoundTripper) http.RoundTripper {
1194+
return &hostRoundTripper{host, rt}
1195+
}
1196+
11721197
func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
11731198
req = cloneRequest(req)
11741199
req.Header.Set("User-Agent", rt.userAgent)
@@ -1181,6 +1206,19 @@ func (rt *userAgentRoundTripper) CloseIdleConnections() {
11811206
}
11821207
}
11831208

1209+
func (rt *hostRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
1210+
req = cloneRequest(req)
1211+
req.Host = rt.host
1212+
req.Header.Set("Host", rt.host)
1213+
return rt.rt.RoundTrip(req)
1214+
}
1215+
1216+
func (rt *hostRoundTripper) CloseIdleConnections() {
1217+
if ci, ok := rt.rt.(closeIdler); ok {
1218+
ci.CloseIdleConnections()
1219+
}
1220+
}
1221+
11841222
func (c HTTPClientConfig) String() string {
11851223
b, err := yaml.Marshal(c)
11861224
if err != nil {

config/http_config_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
"testing"
3636
"time"
3737

38-
yaml "gopkg.in/yaml.v2"
38+
"gopkg.in/yaml.v2"
3939
)
4040

4141
const (
@@ -1671,6 +1671,32 @@ func TestOAuth2UserAgent(t *testing.T) {
16711671
}
16721672
}
16731673

1674+
func TestHost(t *testing.T) {
1675+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1676+
if r.Host != "localhost.localdomain" {
1677+
t.Fatalf("Expected Host header in request to be 'localhost.localdomain', got '%s'", r.Host)
1678+
}
1679+
1680+
w.Header().Add("Content-Type", "application/json")
1681+
}))
1682+
defer ts.Close()
1683+
1684+
config := DefaultHTTPClientConfig
1685+
1686+
rt, err := NewRoundTripperFromConfig(config, "test_host", WithHost("localhost.localdomain"))
1687+
if err != nil {
1688+
t.Fatal(err)
1689+
}
1690+
1691+
client := http.Client{
1692+
Transport: rt,
1693+
}
1694+
_, err = client.Get(ts.URL)
1695+
if err != nil {
1696+
t.Fatal(err)
1697+
}
1698+
}
1699+
16741700
func TestOAuth2WithFile(t *testing.T) {
16751701
var expectedAuth string
16761702
ts := newTestOAuthServer(t, &expectedAuth)

expfmt/decode.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,29 @@ func ResponseFormat(h http.Header) Format {
4545

4646
mediatype, params, err := mime.ParseMediaType(ct)
4747
if err != nil {
48-
return FmtUnknown
48+
return fmtUnknown
4949
}
5050

5151
const textType = "text/plain"
5252

5353
switch mediatype {
5454
case ProtoType:
5555
if p, ok := params["proto"]; ok && p != ProtoProtocol {
56-
return FmtUnknown
56+
return fmtUnknown
5757
}
5858
if e, ok := params["encoding"]; ok && e != "delimited" {
59-
return FmtUnknown
59+
return fmtUnknown
6060
}
61-
return FmtProtoDelim
61+
return fmtProtoDelim
6262

6363
case textType:
6464
if v, ok := params["version"]; ok && v != TextVersion {
65-
return FmtUnknown
65+
return fmtUnknown
6666
}
67-
return FmtText
67+
return fmtText
6868
}
6969

70-
return FmtUnknown
70+
return fmtUnknown
7171
}
7272

7373
// NewDecoder returns a new decoder based on the given input format.

expfmt/decode_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -421,27 +421,27 @@ func testDiscriminatorHTTPHeader(t testing.TB) {
421421
}{
422422
{
423423
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="io.prometheus.client.MetricFamily"; encoding="delimited"`},
424-
output: FmtProtoDelim,
424+
output: fmtProtoDelim,
425425
},
426426
{
427427
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="illegal"; encoding="delimited"`},
428-
output: FmtUnknown,
428+
output: fmtUnknown,
429429
},
430430
{
431431
input: map[string]string{"Content-Type": `application/vnd.google.protobuf; proto="io.prometheus.client.MetricFamily"; encoding="illegal"`},
432-
output: FmtUnknown,
432+
output: fmtUnknown,
433433
},
434434
{
435435
input: map[string]string{"Content-Type": `text/plain; version=0.0.4`},
436-
output: FmtText,
436+
output: fmtText,
437437
},
438438
{
439439
input: map[string]string{"Content-Type": `text/plain`},
440-
output: FmtText,
440+
output: fmtText,
441441
},
442442
{
443443
input: map[string]string{"Content-Type": `text/plain; version=0.0.3`},
444-
output: FmtUnknown,
444+
output: fmtUnknown,
445445
},
446446
}
447447

@@ -547,7 +547,7 @@ func TestTextDecoderWithBufioReader(t *testing.T) {
547547

548548
var decoded bool
549549
r := bufio.NewReader(strings.NewReader(example))
550-
dec := NewDecoder(r, FmtText)
550+
dec := NewDecoder(r, fmtText)
551551
for {
552552
var mf dto.MetricFamily
553553
if err := dec.Decode(&mf); err != nil {

expfmt/encode.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ func Negotiate(h http.Header) Format {
7676
if ac.Type+"/"+ac.SubType == ProtoType && ac.Params["proto"] == ProtoProtocol {
7777
switch ac.Params["encoding"] {
7878
case "delimited":
79-
return FmtProtoDelim + escapingScheme
79+
return fmtProtoDelim + escapingScheme
8080
case "text":
81-
return FmtProtoText + escapingScheme
81+
return fmtProtoText + escapingScheme
8282
case "compact-text":
83-
return FmtProtoCompact + escapingScheme
83+
return fmtProtoCompact + escapingScheme
8484
}
8585
}
8686
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
87-
return FmtText + escapingScheme
87+
return fmtText + escapingScheme
8888
}
8989
}
90-
return FmtText + escapingScheme
90+
return fmtText + escapingScheme
9191
}
9292

9393
// NegotiateIncludingOpenMetrics works like Negotiate but includes
@@ -109,26 +109,26 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
109109
if ac.Type+"/"+ac.SubType == ProtoType && ac.Params["proto"] == ProtoProtocol {
110110
switch ac.Params["encoding"] {
111111
case "delimited":
112-
return FmtProtoDelim + escapingScheme
112+
return fmtProtoDelim + escapingScheme
113113
case "text":
114-
return FmtProtoText + escapingScheme
114+
return fmtProtoText + escapingScheme
115115
case "compact-text":
116-
return FmtProtoCompact + escapingScheme
116+
return fmtProtoCompact + escapingScheme
117117
}
118118
}
119119
if ac.Type == "text" && ac.SubType == "plain" && (ver == TextVersion || ver == "") {
120-
return FmtText + escapingScheme
120+
return fmtText + escapingScheme
121121
}
122122
if ac.Type+"/"+ac.SubType == OpenMetricsType && (ver == OpenMetricsVersion_0_0_1 || ver == OpenMetricsVersion_1_0_0 || ver == "") {
123123
switch ver {
124124
case OpenMetricsVersion_1_0_0:
125-
return FmtOpenMetrics_1_0_0 + escapingScheme
125+
return fmtOpenMetrics_1_0_0 + escapingScheme
126126
default:
127-
return FmtOpenMetrics_0_0_1 + escapingScheme
127+
return fmtOpenMetrics_0_0_1 + escapingScheme
128128
}
129129
}
130130
}
131-
return FmtText + escapingScheme
131+
return fmtText + escapingScheme
132132
}
133133

134134
// NewEncoder returns a new encoder based on content type negotiation. All
@@ -139,7 +139,13 @@ func NegotiateIncludingOpenMetrics(h http.Header) Format {
139139
// interface is kept for backwards compatibility.
140140
// In cases where the Format does not allow for UTF-8 names, the global
141141
// NameEscapingScheme will be applied.
142-
func NewEncoder(w io.Writer, format Format, options ...ToOpenMetricsOption) Encoder {
142+
//
143+
// NewEncoder can be called with additional options to customize the OpenMetrics text output.
144+
// For example:
145+
// NewEncoder(w, FmtOpenMetrics_1_0_0, WithCreatedLines())
146+
//
147+
// Extra options are ignored for all other formats.
148+
func NewEncoder(w io.Writer, format Format, options ...EncoderOption) Encoder {
143149
escapingScheme := format.ToEscapingScheme()
144150

145151
switch format.FormatType() {
@@ -178,7 +184,7 @@ func NewEncoder(w io.Writer, format Format, options ...ToOpenMetricsOption) Enco
178184
case TypeOpenMetrics:
179185
return encoderCloser{
180186
encode: func(v *dto.MetricFamily) error {
181-
_, err := MetricFamilyToOpenMetrics(w, model.EscapeMetricFamily(v, escapingScheme), options...)
187+
_, err := MetricFamilyToOpenMetrics(w, model.EscapeMetricFamily(v, escapingScheme)) // , options...?
182188
return err
183189
},
184190
close: func() error {

0 commit comments

Comments
 (0)