@@ -6,6 +6,7 @@ package prometheusreceiver // import "github.com/open-telemetry/opentelemetry-co
6
6
import (
7
7
"bytes"
8
8
"context"
9
+ "encoding/base64"
9
10
"errors"
10
11
"fmt"
11
12
"hash/fnv"
@@ -16,6 +17,7 @@ import (
16
17
"reflect"
17
18
"regexp"
18
19
"sort"
20
+ "strings"
19
21
"sync"
20
22
"time"
21
23
"unsafe"
@@ -174,6 +176,80 @@ func getScrapeConfigHash(jobToScrapeConfig map[string]*config.ScrapeConfig) (uin
174
176
return hash .Sum64 (), err
175
177
}
176
178
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
+
177
253
// syncTargetAllocator request jobs from targetAllocator and update underlying receiver, if the response does not match the provided compareHash.
178
254
// baseDiscoveryCfg can be used to provide additional ScrapeConfigs which will be added to the retrieved jobs.
179
255
func (r * pReceiver ) syncTargetAllocator (compareHash uint64 , allocConf * TargetAllocator , baseCfg * PromConfig ) (uint64 , error ) {
@@ -208,7 +284,13 @@ func (r *pReceiver) syncTargetAllocator(compareHash uint64, allocConf *TargetAll
208
284
}
209
285
escapedJob := url .QueryEscape (jobName )
210
286
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
+
212
294
scrapeConfig .ServiceDiscoveryConfigs = discovery.Configs {
213
295
& httpSD ,
214
296
}
0 commit comments