Skip to content

Commit 6542ee5

Browse files
authored
[receiver/prometheusreceiver] Configuring httpSD http client from TAs clienthttp (#34035)
**Description:** Translating relevant fields from TargetAllocator's confighttps to be used in the service discovery http client config This assures that the relevant config defiend for the TA is used by the service discovery jobs in addition to the scrape configs (as solved by #31449) **Link to tracking Issue:** Resolves #33370 **Testing:** Added unit tests. **Documentation:**
1 parent ca8dd14 commit 6542ee5

File tree

4 files changed

+165
-3
lines changed

4 files changed

+165
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: prometheusreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Ensure Target Allocator's confighttp is used in the receiver's service discovery
11+
12+
13+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
14+
issues: [33370]
15+
16+
# (Optional) One or more lines of additional information to render under the primary note.
17+
# These lines will be padded with 2 spaces and then inserted directly into the document.
18+
# Use pipe (|) for multiline entries.
19+
subtext:
20+
21+
# If your change doesn't affect end users or the exported elements of any package,
22+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
23+
# Optional: The change log or logs in which this entry should be included.
24+
# e.g. '[user]' or '[user, api]'
25+
# Include 'user' if the change is relevant to end users.
26+
# Include 'api' if there is a change to a library API.
27+
# Default: '[user]'
28+
change_logs: []

receiver/prometheusreceiver/go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ require (
1717
go.opentelemetry.io/collector/component v0.107.1-0.20240816132030-9fd84668bb02
1818
go.opentelemetry.io/collector/component/componentstatus v0.107.1-0.20240816132030-9fd84668bb02
1919
go.opentelemetry.io/collector/config/confighttp v0.107.1-0.20240816132030-9fd84668bb02
20+
go.opentelemetry.io/collector/config/configopaque v1.13.1-0.20240816132030-9fd84668bb02
21+
go.opentelemetry.io/collector/config/configtls v1.13.1-0.20240816132030-9fd84668bb02
2022
go.opentelemetry.io/collector/confmap v0.107.1-0.20240816132030-9fd84668bb02
2123
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.107.1-0.20240816132030-9fd84668bb02
2224
go.opentelemetry.io/collector/consumer v0.107.1-0.20240816132030-9fd84668bb02
@@ -168,10 +170,8 @@ require (
168170
go.opentelemetry.io/collector/component/componentprofiles v0.107.1-0.20240816132030-9fd84668bb02 // indirect
169171
go.opentelemetry.io/collector/config/configauth v0.107.1-0.20240816132030-9fd84668bb02 // indirect
170172
go.opentelemetry.io/collector/config/configcompression v1.13.1-0.20240816132030-9fd84668bb02 // indirect
171-
go.opentelemetry.io/collector/config/configopaque v1.13.1-0.20240816132030-9fd84668bb02 // indirect
172173
go.opentelemetry.io/collector/config/configretry v1.13.1-0.20240816132030-9fd84668bb02 // indirect
173174
go.opentelemetry.io/collector/config/configtelemetry v0.107.1-0.20240816132030-9fd84668bb02 // indirect
174-
go.opentelemetry.io/collector/config/configtls v1.13.1-0.20240816132030-9fd84668bb02 // indirect
175175
go.opentelemetry.io/collector/config/internal v0.107.1-0.20240816132030-9fd84668bb02 // indirect
176176
go.opentelemetry.io/collector/connector v0.107.1-0.20240816132030-9fd84668bb02 // indirect
177177
go.opentelemetry.io/collector/consumer/consumerprofiles v0.107.1-0.20240816132030-9fd84668bb02 // indirect

receiver/prometheusreceiver/metrics_receiver.go

+83-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package prometheusreceiver // import "github.com/open-telemetry/opentelemetry-co
66
import (
77
"bytes"
88
"context"
9+
"encoding/base64"
910
"errors"
1011
"fmt"
1112
"hash/fnv"
@@ -16,6 +17,7 @@ import (
1617
"reflect"
1718
"regexp"
1819
"sort"
20+
"strings"
1921
"sync"
2022
"time"
2123
"unsafe"
@@ -174,6 +176,80 @@ func getScrapeConfigHash(jobToScrapeConfig map[string]*config.ScrapeConfig) (uin
174176
return hash.Sum64(), err
175177
}
176178

179+
// ConvertTLSVersion converts a string TLS version to the corresponding config.TLSVersion value in prometheus common.
180+
func convertTLSVersion(version string) (commonconfig.TLSVersion, error) {
181+
normalizedVersion := "TLS" + strings.ReplaceAll(version, ".", "")
182+
183+
if tlsVersion, exists := commonconfig.TLSVersions[normalizedVersion]; exists {
184+
return tlsVersion, nil
185+
}
186+
return 0, fmt.Errorf("unsupported TLS version: %s", version)
187+
}
188+
189+
// configureSDHTTPClientConfig configures the http client for the service discovery manager
190+
// based on the provided TargetAllocator configuration.
191+
func configureSDHTTPClientConfigFromTA(httpSD *promHTTP.SDConfig, allocConf *TargetAllocator) error {
192+
httpSD.HTTPClientConfig.FollowRedirects = false
193+
194+
httpSD.HTTPClientConfig.TLSConfig = commonconfig.TLSConfig{
195+
InsecureSkipVerify: allocConf.TLSSetting.InsecureSkipVerify,
196+
ServerName: allocConf.TLSSetting.ServerName,
197+
CAFile: allocConf.TLSSetting.CAFile,
198+
CertFile: allocConf.TLSSetting.CertFile,
199+
KeyFile: allocConf.TLSSetting.KeyFile,
200+
}
201+
202+
if allocConf.TLSSetting.CAPem != "" {
203+
decodedCA, err := base64.StdEncoding.DecodeString(string(allocConf.TLSSetting.CAPem))
204+
if err != nil {
205+
return fmt.Errorf("failed to decode CA: %w", err)
206+
}
207+
httpSD.HTTPClientConfig.TLSConfig.CA = string(decodedCA)
208+
}
209+
210+
if allocConf.TLSSetting.CertPem != "" {
211+
decodedCert, err := base64.StdEncoding.DecodeString(string(allocConf.TLSSetting.CertPem))
212+
if err != nil {
213+
return fmt.Errorf("failed to decode Cert: %w", err)
214+
}
215+
httpSD.HTTPClientConfig.TLSConfig.Cert = string(decodedCert)
216+
}
217+
218+
if allocConf.TLSSetting.KeyPem != "" {
219+
decodedKey, err := base64.StdEncoding.DecodeString(string(allocConf.TLSSetting.KeyPem))
220+
if err != nil {
221+
return fmt.Errorf("failed to decode Key: %w", err)
222+
}
223+
httpSD.HTTPClientConfig.TLSConfig.Key = commonconfig.Secret(decodedKey)
224+
}
225+
226+
if allocConf.TLSSetting.MinVersion != "" {
227+
minVersion, err := convertTLSVersion(allocConf.TLSSetting.MinVersion)
228+
if err != nil {
229+
return err
230+
}
231+
httpSD.HTTPClientConfig.TLSConfig.MinVersion = minVersion
232+
}
233+
234+
if allocConf.TLSSetting.MaxVersion != "" {
235+
maxVersion, err := convertTLSVersion(allocConf.TLSSetting.MaxVersion)
236+
if err != nil {
237+
return err
238+
}
239+
httpSD.HTTPClientConfig.TLSConfig.MaxVersion = maxVersion
240+
}
241+
242+
if allocConf.ProxyURL != "" {
243+
proxyURL, err := url.Parse(allocConf.ProxyURL)
244+
if err != nil {
245+
return err
246+
}
247+
httpSD.HTTPClientConfig.ProxyURL = commonconfig.URL{URL: proxyURL}
248+
}
249+
250+
return nil
251+
}
252+
177253
// syncTargetAllocator request jobs from targetAllocator and update underlying receiver, if the response does not match the provided compareHash.
178254
// baseDiscoveryCfg can be used to provide additional ScrapeConfigs which will be added to the retrieved jobs.
179255
func (r *pReceiver) syncTargetAllocator(compareHash uint64, allocConf *TargetAllocator, baseCfg *PromConfig) (uint64, error) {
@@ -208,7 +284,13 @@ func (r *pReceiver) syncTargetAllocator(compareHash uint64, allocConf *TargetAll
208284
}
209285
escapedJob := url.QueryEscape(jobName)
210286
httpSD.URL = fmt.Sprintf("%s/jobs/%s/targets?collector_id=%s", allocConf.Endpoint, escapedJob, allocConf.CollectorID)
211-
httpSD.HTTPClientConfig.FollowRedirects = false
287+
288+
err = configureSDHTTPClientConfigFromTA(&httpSD, allocConf)
289+
if err != nil {
290+
r.settings.Logger.Error("Failed to configure http client config", zap.Error(err))
291+
return 0, err
292+
}
293+
212294
scrapeConfig.ServiceDiscoveryConfigs = discovery.Configs{
213295
&httpSD,
214296
}

receiver/prometheusreceiver/metrics_receiver_target_allocator_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ package prometheusreceiver
77

88
import (
99
"context"
10+
"encoding/base64"
1011
"encoding/json"
1112
"net/http"
1213
"net/http/httptest"
14+
"net/url"
1315
"strings"
1416
"sync"
1517
"sync/atomic"
@@ -24,6 +26,8 @@ import (
2426
"github.com/stretchr/testify/assert"
2527
"github.com/stretchr/testify/require"
2628
"go.opentelemetry.io/collector/component/componenttest"
29+
"go.opentelemetry.io/collector/config/configopaque"
30+
"go.opentelemetry.io/collector/config/configtls"
2731
"go.opentelemetry.io/collector/consumer/consumertest"
2832
"go.opentelemetry.io/collector/receiver/receivertest"
2933
)
@@ -776,3 +780,51 @@ func TestTargetAllocatorJobRetrieval(t *testing.T) {
776780
})
777781
}
778782
}
783+
784+
func TestConfigureSDHTTPClientConfigFromTA(t *testing.T) {
785+
ta := &TargetAllocator{}
786+
ta.TLSSetting = configtls.ClientConfig{
787+
InsecureSkipVerify: true,
788+
ServerName: "test.server",
789+
Config: configtls.Config{
790+
CAFile: "/path/to/ca",
791+
CertFile: "/path/to/cert",
792+
KeyFile: "/path/to/key",
793+
CAPem: configopaque.String(base64.StdEncoding.EncodeToString([]byte("test-ca"))),
794+
CertPem: configopaque.String(base64.StdEncoding.EncodeToString([]byte("test-cert"))),
795+
KeyPem: configopaque.String(base64.StdEncoding.EncodeToString([]byte("test-key"))),
796+
MinVersion: "1.2",
797+
MaxVersion: "1.3",
798+
},
799+
}
800+
ta.ProxyURL = "http://proxy.test"
801+
802+
httpSD := &promHTTP.SDConfig{RefreshInterval: model.Duration(30 * time.Second)}
803+
804+
err := configureSDHTTPClientConfigFromTA(httpSD, ta)
805+
806+
assert.NoError(t, err)
807+
808+
assert.Equal(t, false, httpSD.HTTPClientConfig.FollowRedirects)
809+
assert.Equal(t, true, httpSD.HTTPClientConfig.TLSConfig.InsecureSkipVerify)
810+
assert.Equal(t, "test.server", httpSD.HTTPClientConfig.TLSConfig.ServerName)
811+
assert.Equal(t, "/path/to/ca", httpSD.HTTPClientConfig.TLSConfig.CAFile)
812+
assert.Equal(t, "/path/to/cert", httpSD.HTTPClientConfig.TLSConfig.CertFile)
813+
assert.Equal(t, "/path/to/key", httpSD.HTTPClientConfig.TLSConfig.KeyFile)
814+
assert.Equal(t, "test-ca", httpSD.HTTPClientConfig.TLSConfig.CA)
815+
assert.Equal(t, "test-cert", httpSD.HTTPClientConfig.TLSConfig.Cert)
816+
assert.Equal(t, commonconfig.Secret("test-key"), httpSD.HTTPClientConfig.TLSConfig.Key)
817+
assert.Equal(t, commonconfig.TLSVersions["TLS12"], httpSD.HTTPClientConfig.TLSConfig.MinVersion)
818+
assert.Equal(t, commonconfig.TLSVersions["TLS13"], httpSD.HTTPClientConfig.TLSConfig.MaxVersion)
819+
820+
parsedProxyURL, _ := url.Parse("http://proxy.test")
821+
assert.Equal(t, commonconfig.URL{URL: parsedProxyURL}, httpSD.HTTPClientConfig.ProxyURL)
822+
823+
// Test case with empty TargetAllocator
824+
emptyTA := &TargetAllocator{}
825+
emptyHTTPSD := &promHTTP.SDConfig{RefreshInterval: model.Duration(30 * time.Second)}
826+
827+
err = configureSDHTTPClientConfigFromTA(emptyHTTPSD, emptyTA)
828+
829+
assert.NoError(t, err)
830+
}

0 commit comments

Comments
 (0)