mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
discovery/triton: pass context to the client (#5235)
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
This commit is contained in:
parent
9de0ab3c8a
commit
fe7a1bcfc6
|
@ -159,7 +159,7 @@ func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
// Get an initial set right away.
|
// Get an initial set right away.
|
||||||
tg, err := d.refresh()
|
tg, err := d.refresh(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
level.Error(d.logger).Log("msg", "Refreshing targets failed", "err", err)
|
level.Error(d.logger).Log("msg", "Refreshing targets failed", "err", err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -169,7 +169,7 @@ func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
tg, err := d.refresh()
|
tg, err := d.refresh(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
level.Error(d.logger).Log("msg", "Refreshing targets failed", "err", err)
|
level.Error(d.logger).Log("msg", "Refreshing targets failed", "err", err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -181,7 +181,7 @@ func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Discovery) refresh() (tg *targetgroup.Group, err error) {
|
func (d *Discovery) refresh(ctx context.Context) (tg *targetgroup.Group, err error) {
|
||||||
t0 := time.Now()
|
t0 := time.Now()
|
||||||
defer func() {
|
defer func() {
|
||||||
refreshDuration.Observe(time.Since(t0).Seconds())
|
refreshDuration.Observe(time.Since(t0).Seconds())
|
||||||
|
@ -200,22 +200,27 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) {
|
||||||
Source: endpoint,
|
Source: endpoint,
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := d.client.Get(endpoint)
|
req, err := http.NewRequest("GET", endpoint, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tg, fmt.Errorf("an error occurred when requesting targets from the discovery endpoint. %s", err)
|
return nil, err
|
||||||
|
}
|
||||||
|
req = req.WithContext(ctx)
|
||||||
|
resp, err := d.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("an error occurred when requesting targets from the discovery endpoint: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tg, fmt.Errorf("an error occurred when reading the response body. %s", err)
|
return nil, fmt.Errorf("an error occurred when reading the response body: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dr := DiscoveryResponse{}
|
dr := DiscoveryResponse{}
|
||||||
err = json.Unmarshal(data, &dr)
|
err = json.Unmarshal(data, &dr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tg, fmt.Errorf("an error occurred unmarshaling the discovery response json. %s", err)
|
return nil, fmt.Errorf("an error occurred unmarshaling the discovery response json: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, container := range dr.Containers {
|
for _, container := range dr.Containers {
|
||||||
|
|
|
@ -67,8 +67,12 @@ var (
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func newTritonDiscovery(c SDConfig) (*Discovery, error) {
|
||||||
|
return New(nil, &c)
|
||||||
|
}
|
||||||
|
|
||||||
func TestTritonSDNew(t *testing.T) {
|
func TestTritonSDNew(t *testing.T) {
|
||||||
td, err := New(nil, &conf)
|
td, err := newTritonDiscovery(conf)
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Assert(t, td != nil, "")
|
testutil.Assert(t, td != nil, "")
|
||||||
testutil.Assert(t, td.client != nil, "")
|
testutil.Assert(t, td.client != nil, "")
|
||||||
|
@ -81,13 +85,13 @@ func TestTritonSDNew(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTritonSDNewBadConfig(t *testing.T) {
|
func TestTritonSDNewBadConfig(t *testing.T) {
|
||||||
td, err := New(nil, &badconf)
|
td, err := newTritonDiscovery(badconf)
|
||||||
testutil.NotOk(t, err, "")
|
testutil.NotOk(t, err, "")
|
||||||
testutil.Assert(t, td == nil, "")
|
testutil.Assert(t, td == nil, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTritonSDNewGroupsConfig(t *testing.T) {
|
func TestTritonSDNewGroupsConfig(t *testing.T) {
|
||||||
td, err := New(nil, &groupsconf)
|
td, err := newTritonDiscovery(groupsconf)
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Assert(t, td != nil, "")
|
testutil.Assert(t, td != nil, "")
|
||||||
testutil.Assert(t, td.client != nil, "")
|
testutil.Assert(t, td.client != nil, "")
|
||||||
|
@ -102,14 +106,11 @@ func TestTritonSDNewGroupsConfig(t *testing.T) {
|
||||||
|
|
||||||
func TestTritonSDRun(t *testing.T) {
|
func TestTritonSDRun(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
td, err = New(nil, &conf)
|
td, _ = newTritonDiscovery(conf)
|
||||||
ch = make(chan []*targetgroup.Group)
|
ch = make(chan []*targetgroup.Group)
|
||||||
ctx, cancel = context.WithCancel(context.Background())
|
ctx, cancel = context.WithCancel(context.Background())
|
||||||
)
|
)
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
|
||||||
testutil.Assert(t, td != nil, "")
|
|
||||||
|
|
||||||
wait := make(chan struct{})
|
wait := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
td.Run(ctx, ch)
|
td.Run(ctx, ch)
|
||||||
|
@ -160,21 +161,29 @@ func TestTritonSDRefreshMultipleTargets(t *testing.T) {
|
||||||
|
|
||||||
func TestTritonSDRefreshNoServer(t *testing.T) {
|
func TestTritonSDRefreshNoServer(t *testing.T) {
|
||||||
var (
|
var (
|
||||||
td, err = New(nil, &conf)
|
td, _ = newTritonDiscovery(conf)
|
||||||
)
|
)
|
||||||
testutil.Ok(t, err)
|
|
||||||
testutil.Assert(t, td != nil, "")
|
|
||||||
|
|
||||||
tg, rerr := td.refresh()
|
_, err := td.refresh(context.Background())
|
||||||
testutil.NotOk(t, rerr, "")
|
testutil.NotOk(t, err, "")
|
||||||
testutil.Equals(t, strings.Contains(rerr.Error(), "an error occurred when requesting targets from the discovery endpoint."), true)
|
testutil.Equals(t, strings.Contains(err.Error(), "an error occurred when requesting targets from the discovery endpoint"), true)
|
||||||
testutil.Assert(t, tg != nil, "")
|
}
|
||||||
testutil.Assert(t, tg.Targets == nil, "")
|
|
||||||
|
func TestTritonSDRefreshCancelled(t *testing.T) {
|
||||||
|
var (
|
||||||
|
td, _ = newTritonDiscovery(conf)
|
||||||
|
)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
_, err := td.refresh(ctx)
|
||||||
|
testutil.NotOk(t, err, "")
|
||||||
|
testutil.Equals(t, strings.Contains(err.Error(), context.Canceled.Error()), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTritonSDRefresh(t *testing.T, dstr string) []model.LabelSet {
|
func testTritonSDRefresh(t *testing.T, dstr string) []model.LabelSet {
|
||||||
var (
|
var (
|
||||||
td, err = New(nil, &conf)
|
td, _ = newTritonDiscovery(conf)
|
||||||
s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, dstr)
|
fmt.Fprintln(w, dstr)
|
||||||
}))
|
}))
|
||||||
|
@ -182,25 +191,22 @@ func testTritonSDRefresh(t *testing.T, dstr string) []model.LabelSet {
|
||||||
|
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
|
|
||||||
u, uperr := url.Parse(s.URL)
|
u, err := url.Parse(s.URL)
|
||||||
testutil.Ok(t, uperr)
|
testutil.Ok(t, err)
|
||||||
testutil.Assert(t, u != nil, "")
|
testutil.Assert(t, u != nil, "")
|
||||||
|
|
||||||
host, strport, sherr := net.SplitHostPort(u.Host)
|
host, strport, err := net.SplitHostPort(u.Host)
|
||||||
testutil.Ok(t, sherr)
|
testutil.Ok(t, err)
|
||||||
testutil.Assert(t, host != "", "")
|
testutil.Assert(t, host != "", "")
|
||||||
testutil.Assert(t, strport != "", "")
|
testutil.Assert(t, strport != "", "")
|
||||||
|
|
||||||
port, atoierr := strconv.Atoi(strport)
|
port, err := strconv.Atoi(strport)
|
||||||
testutil.Ok(t, atoierr)
|
testutil.Ok(t, err)
|
||||||
testutil.Assert(t, port != 0, "")
|
testutil.Assert(t, port != 0, "")
|
||||||
|
|
||||||
td.sdConfig.Port = port
|
td.sdConfig.Port = port
|
||||||
|
|
||||||
testutil.Ok(t, err)
|
tg, err := td.refresh(context.Background())
|
||||||
testutil.Assert(t, td != nil, "")
|
|
||||||
|
|
||||||
tg, err := td.refresh()
|
|
||||||
testutil.Ok(t, err)
|
testutil.Ok(t, err)
|
||||||
testutil.Assert(t, tg != nil, "")
|
testutil.Assert(t, tg != nil, "")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue