Skip to content

Commit

Permalink
Configuring httpSD http client from TAs clienthttp
Browse files Browse the repository at this point in the history
  • Loading branch information
itielo committed Jul 11, 2024
1 parent db016c5 commit dde0fb7
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion receiver/prometheusreceiver/metrics_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package prometheusreceiver // import "github.com/open-telemetry/opentelemetry-co
import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"io"
Expand All @@ -14,6 +15,7 @@ import (
"os"
"reflect"
"regexp"
"strings"
"sync"
"time"
"unsafe"
Expand Down Expand Up @@ -145,6 +147,80 @@ func (r *pReceiver) startTargetAllocator(allocConf *TargetAllocator, baseCfg *Pr
return nil
}

// ConvertTLSVersion converts a string TLS version to the corresponding config.TLSVersion value in prometheus common.
func convertTLSVersion(version string) (commonconfig.TLSVersion, error) {
normalizedVersion := "TLS" + strings.TrimPrefix(version, ".")

if tlsVersion, exists := commonconfig.TLSVersions[normalizedVersion]; exists {
return tlsVersion, nil
}
return 0, fmt.Errorf("unsupported TLS version: %s", version)
}

// configureSDHTTPClientConfig configures the http client for the service discovery manager
// based on the provided TargetAllocator configuration.
func configureSDHTTPClientConfigFromTA(httpSD *promHTTP.SDConfig, allocConf *TargetAllocator) error {
httpSD.HTTPClientConfig.FollowRedirects = false

httpSD.HTTPClientConfig.TLSConfig = commonconfig.TLSConfig{
InsecureSkipVerify: allocConf.TLSSetting.InsecureSkipVerify,
ServerName: allocConf.TLSSetting.ServerName,
CAFile: allocConf.TLSSetting.CAFile,
CertFile: allocConf.TLSSetting.CertFile,
KeyFile: allocConf.TLSSetting.KeyFile,
}

if allocConf.TLSSetting.CAPem != "" {
decodedCA, err := base64.StdEncoding.DecodeString(string(allocConf.TLSSetting.CAPem))
if err != nil {
return fmt.Errorf("failed to decode CA: %w", err)
}
httpSD.HTTPClientConfig.TLSConfig.CA = string(decodedCA)
}

if allocConf.TLSSetting.CertPem != "" {
decodedCert, err := base64.StdEncoding.DecodeString(string(allocConf.TLSSetting.CertPem))
if err != nil {
return fmt.Errorf("failed to decode Cert: %w", err)
}
httpSD.HTTPClientConfig.TLSConfig.Cert = string(decodedCert)
}

if allocConf.TLSSetting.KeyPem != "" {
decodedKey, err := base64.StdEncoding.DecodeString(string(allocConf.TLSSetting.KeyPem))
if err != nil {
return fmt.Errorf("failed to decode Key: %w", err)
}
httpSD.HTTPClientConfig.TLSConfig.Key = commonconfig.Secret(decodedKey)
}

if allocConf.TLSSetting.MinVersion != "" {
minVersion, err := convertTLSVersion(allocConf.TLSSetting.MinVersion)
if err != nil {
return err
}
httpSD.HTTPClientConfig.TLSConfig.MinVersion = minVersion
}

if allocConf.TLSSetting.MaxVersion != "" {
maxVersion, err := convertTLSVersion(allocConf.TLSSetting.MaxVersion)
if err != nil {
return err
}
httpSD.HTTPClientConfig.TLSConfig.MaxVersion = maxVersion
}

if allocConf.ProxyURL != "" {
proxyURL, err := url.Parse(allocConf.ProxyURL)
if err != nil {
return err
}
httpSD.HTTPClientConfig.ProxyURL = commonconfig.URL{URL: proxyURL}
}

return nil
}

// syncTargetAllocator request jobs from targetAllocator and update underlying receiver, if the response does not match the provided compareHash.
// baseDiscoveryCfg can be used to provide additional ScrapeConfigs which will be added to the retrieved jobs.
func (r *pReceiver) syncTargetAllocator(compareHash uint64, allocConf *TargetAllocator, baseCfg *PromConfig) (uint64, error) {
Expand Down Expand Up @@ -179,7 +255,13 @@ func (r *pReceiver) syncTargetAllocator(compareHash uint64, allocConf *TargetAll
}
escapedJob := url.QueryEscape(jobName)
httpSD.URL = fmt.Sprintf("%s/jobs/%s/targets?collector_id=%s", allocConf.Endpoint, escapedJob, allocConf.CollectorID)
httpSD.HTTPClientConfig.FollowRedirects = false

err = configureSDHTTPClientConfigFromTA(&httpSD, allocConf)
if err != nil {
r.settings.Logger.Error("Failed to configure http client config", zap.Error(err))
return 0, err
}

scrapeConfig.ServiceDiscoveryConfigs = discovery.Configs{
&httpSD,
}
Expand Down

0 comments on commit dde0fb7

Please sign in to comment.