1
0
mirror of https://github.com/nginxinc/nginx-prometheus-exporter.git synced 2025-08-08 05:02:04 +03:00

Don't try to connect to nginx on client creation (#504)

The client now is created without making a call to nginx.
When Collect() is called we try to get the metrics from nginx and if
it's not available it will set 'up' to 0 and not fail anymore.
This commit is contained in:
Luca Comellini
2023-10-09 18:09:38 +02:00
committed by GitHub
parent 06af414484
commit 2ab961100c
3 changed files with 8 additions and 117 deletions

View File

@@ -1,91 +1,11 @@
package main
import (
"errors"
"reflect"
"testing"
"time"
"github.com/go-kit/log"
)
func TestCreateClientWithRetries(t *testing.T) {
t.Parallel()
type args struct {
client interface{}
err error
retries uint
retryInterval time.Duration
}
tests := []struct {
name string
args args
expectedRetries int
want interface{}
wantErr bool
}{
{
name: "getClient returns a valid client",
args: args{
client: "client",
err: nil,
},
expectedRetries: 0,
want: "client",
wantErr: false,
},
{
name: "getClient returns an error after no retries",
args: args{
client: nil,
err: errors.New("error"),
},
expectedRetries: 0,
want: nil,
wantErr: true,
},
{
name: "getClient returns an error after retries",
args: args{
client: nil,
err: errors.New("error"),
retries: 3,
retryInterval: time.Millisecond * 1,
},
expectedRetries: 3,
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
invocations := 0
getClient := func() (interface{}, error) {
invocations++
return tt.args.client, tt.args.err
}
got, err := createClientWithRetries(getClient, tt.args.retries, tt.args.retryInterval, log.NewNopLogger())
actualRetries := invocations - 1
if actualRetries != tt.expectedRetries {
t.Errorf("createClientWithRetries() got %v retries, expected %v", actualRetries, tt.expectedRetries)
return
} else if (err != nil) != tt.wantErr {
t.Errorf("createClientWithRetries() error = %v, wantErr %v", err, tt.wantErr)
return
} else if err != nil && tt.wantErr {
return
} else if !reflect.DeepEqual(got, tt.want) {
t.Errorf("createClientWithRetries() = %v, want %v", got, tt.want)
}
})
}
}
func TestParsePositiveDuration(t *testing.T) {
t.Parallel()