Skip to content

Commit 1f7b5aa

Browse files
committed
chore: updates based pr feedback #269
1 parent bcd326d commit 1f7b5aa

8 files changed

+55
-43
lines changed

anclafx/provide.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
package anclafx
44

55
import (
6-
"github.com/prometheus/client_golang/prometheus"
76
"github.com/xmidt-org/ancla"
87
"github.com/xmidt-org/ancla/chrysom"
9-
"github.com/xmidt-org/touchstone"
108
"go.uber.org/fx"
119
)
1210

@@ -23,17 +21,6 @@ func Provide() fx.Option {
2321
chrysom.ProvideDefaultListenerReader,
2422
chrysom.ProvideListenerClient,
2523
),
26-
touchstone.Gauge(
27-
prometheus.GaugeOpts{
28-
Name: chrysom.WebhookListSizeGaugeName,
29-
Help: chrysom.WebhookListSizeGaugeHelp,
30-
}),
31-
touchstone.CounterVec(
32-
prometheus.CounterOpts{
33-
Name: chrysom.PollsTotalCounterName,
34-
Help: chrysom.PollsTotalCounterHelp,
35-
},
36-
chrysom.OutcomeLabel,
37-
),
24+
chrysom.ProvideMetrics(),
3825
)
3926
}

chrysom/basicClient.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type BasicClientConfig struct {
6666

6767
// BasicClient is the client used to make requests to Argus.
6868
type BasicClient struct {
69-
client HTTPClient
69+
client *http.Client
7070
auth Acquirer
7171
storeBaseURL string
7272
bucket string
@@ -88,16 +88,12 @@ const (
8888
// Items is a slice of model.Item(s) .
8989
type Items []model.Item
9090

91-
type HTTPClient interface {
92-
Do(*http.Request) (*http.Response, error)
93-
}
94-
9591
type BasicClientIn struct {
9692
fx.In
9793

9894
BasicClientConfig BasicClientConfig
9995
// (Optional) Defaults to http.DefaultClient.
100-
HTTPClient HTTPClient
96+
HTTPClient *http.Client `name:"chrysom_http_client"`
10197
}
10298

10399
// ProvideBasicClient provides a new BasicClient.
@@ -112,7 +108,7 @@ func ProvideBasicClient(in BasicClientIn) (*BasicClient, error) {
112108

113109
// NewBasicClient creates a new BasicClient that can be used to
114110
// make requests to Argus.
115-
func NewBasicClient(config BasicClientConfig, client HTTPClient) (*BasicClient, error) {
111+
func NewBasicClient(config BasicClientConfig, client *http.Client) (*BasicClient, error) {
116112
err := validateBasicConfig(&config)
117113
if err != nil {
118114
return nil, err

chrysom/basicClient_test.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ func TestValidateBasicConfig(t *testing.T) {
3232
type testCase struct {
3333
Description string
3434
Input *BasicClientConfig
35-
Client HTTPClient
35+
Client *http.Client
3636
ExpectedErr error
3737
ExpectedConfig *BasicClientConfig
3838
}
3939

4040
allDefaultsCaseConfig := &BasicClientConfig{
41-
Address: "http://awesome-argus-hostname.io",
41+
Address: "example.com",
4242
Bucket: "bucket-name",
4343
}
4444
allDefinedCaseConfig := &BasicClientConfig{
45-
Address: "http://legit-argus-hostname.io",
45+
Address: "example.com",
4646
Bucket: "amazing-bucket",
4747
}
4848

@@ -57,22 +57,22 @@ func TestValidateBasicConfig(t *testing.T) {
5757
{
5858
Description: "No bucket",
5959
Input: &BasicClientConfig{
60-
Address: "http://awesome-argus-hostname.io",
60+
Address: "example.com",
6161
},
6262
ExpectedErr: ErrBucketEmpty,
6363
},
6464
{
6565
Description: "All default values",
6666
Input: &BasicClientConfig{
67-
Address: "http://awesome-argus-hostname.io",
67+
Address: "example.com",
6868
Bucket: "bucket-name",
6969
},
7070
ExpectedConfig: allDefaultsCaseConfig,
7171
},
7272
{
7373
Description: "All defined",
7474
Input: &BasicClientConfig{
75-
Address: "http://legit-argus-hostname.io",
75+
Address: "example.com",
7676
Bucket: "amazing-bucket",
7777
},
7878
ExpectedConfig: allDefinedCaseConfig,
@@ -110,15 +110,15 @@ func TestSendRequest(t *testing.T) {
110110
{
111111
Description: "New Request fails",
112112
Method: "what method?",
113-
URL: "http://argus-hostname.io",
113+
URL: "example.com",
114114
ExpectedErr: errNewRequestFailure,
115115
MockAuth: "",
116116
MockError: nil,
117117
},
118118
{
119119
Description: "Auth acquirer fails",
120120
Method: http.MethodGet,
121-
URL: "http://argus-hostname.io",
121+
URL: "example.com",
122122
MockError: errFails,
123123
MockAuth: "",
124124
ExpectedErr: ErrAuthAcquirerFailure,
@@ -134,7 +134,7 @@ func TestSendRequest(t *testing.T) {
134134
{
135135
Description: "Happy path",
136136
Method: http.MethodPut,
137-
URL: "http://argus-hostname.io",
137+
URL: "example.com",
138138
Body: []byte("testing"),
139139
Owner: "HappyCaseOwner",
140140
ExpectedResponse: response{
@@ -147,7 +147,7 @@ func TestSendRequest(t *testing.T) {
147147
{
148148
Description: "Happy path with default http client",
149149
Method: http.MethodPut,
150-
URL: "http://argus-hostname.io",
150+
URL: "example.com",
151151
Body: []byte("testing"),
152152
Owner: "HappyCaseOwner",
153153
ExpectedResponse: response{
@@ -176,13 +176,13 @@ func TestSendRequest(t *testing.T) {
176176
server := httptest.NewServer(echoHandler)
177177
defer server.Close()
178178

179-
var httpClient HTTPClient = server.Client()
179+
httpClient := server.Client()
180180
if tc.NilHTTPClient {
181181
httpClient = nil
182182
}
183183

184184
client, err := NewBasicClient(BasicClientConfig{
185-
Address: "http://argus-hostname.io",
185+
Address: "example.com",
186186
Bucket: "bucket-name",
187187
}, httpClient)
188188

chrysom/listenerClient.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ type ListenerClientIn struct {
4646
// Listener fetches a copy of all items within a bucket on
4747
// an interval based on `BasicClientConfig.PullInterval`.
4848
// (Optional). If not provided, listening won't be enabled for this client.
49-
Listener Listener
50-
Config BasicClientConfig
49+
Listener Listener
50+
// Config configures the ancla client and its listeners.
51+
Config BasicClientConfig
52+
// PollsTotalCounter measures the number of polls (and their success/failure outcomes) to fetch new items.
5153
PollsTotalCounter *prometheus.CounterVec `name:"chrysom_polls_total"`
52-
Reader Reader
54+
// Reader is the DB interface used to fetch new items using `GeItems`.
55+
Reader Reader
5356
}
5457

5558
// ListenerClient is the client used to poll Argus for updates.

chrysom/metrics.go

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
package chrysom
55

6+
import (
7+
"github.com/prometheus/client_golang/prometheus"
8+
"github.com/xmidt-org/touchstone"
9+
"go.uber.org/fx"
10+
)
11+
612
// Names
713
const (
814
WebhookListSizeGaugeName = "webhook_list_size"
@@ -21,3 +27,20 @@ const (
2127
SuccessOutcome = "success"
2228
FailureOutcome = "failure"
2329
)
30+
31+
func ProvideMetrics() fx.Option {
32+
return fx.Options(
33+
touchstone.Gauge(
34+
prometheus.GaugeOpts{
35+
Name: WebhookListSizeGaugeName,
36+
Help: WebhookListSizeGaugeHelp,
37+
}),
38+
touchstone.CounterVec(
39+
prometheus.CounterOpts{
40+
Name: PollsTotalCounterName,
41+
Help: PollsTotalCounterHelp,
42+
},
43+
OutcomeLabel,
44+
),
45+
)
46+
}

service.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,16 @@ func ProvideService(in ClientServiceIn) *ClientService {
114114
}
115115

116116
// TODO: Refactor and move Watch and Listener related code to chrysom.
117+
type DefaultListenersIn struct {
118+
fx.In
117119

118-
type DefaultListenerOut struct {
119-
Watchers []Watch `group:"watchers,flatten"`
120+
WebhookListSizeGauge prometheus.Gauge `name:"webhook_list_size"`
120121
}
121122

122-
type DefaultListenersIn struct {
123-
WebhookListSizeGauge prometheus.Gauge `name:"webhook_list_size"`
123+
type DefaultListenerOut struct {
124+
fx.Out
125+
126+
Watchers []Watch `group:"watchers,flatten"`
124127
}
125128

126129
func ProvideDefaultListeners(in DefaultListenersIn) DefaultListenerOut {

webhookValidationConfig.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type OptionsConfig struct {
5151
}
5252

5353
// BuildURLChecker translates the configuration into url Checker to be run on the webhook.
54-
func BuildURLChecker(config ValidatorConfig) (*urlegit.Checker, error) {
54+
func (config *ValidatorConfig) BuildURLChecker() (*urlegit.Checker, error) {
5555
var o []urlegit.Option
5656
if len(config.URL.Schemes) > 0 {
5757
o = append(o, urlegit.OnlyAllowSchemes(config.URL.Schemes...))

webhookValidationConfig_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func TestBuildValidURLFuncs(t *testing.T) {
102102
for _, tc := range tcs {
103103
t.Run(tc.desc, func(t *testing.T) {
104104
assert := assert.New(t)
105-
vals, err := BuildURLChecker(tc.config)
105+
vals, err := tc.config.BuildURLChecker()
106106
if tc.expectedErr != nil {
107107
assert.True(errors.Is(err, tc.expectedErr),
108108
fmt.Errorf("error [%v] doesn't contain error [%v] in its err chain",
@@ -116,7 +116,7 @@ func TestBuildValidURLFuncs(t *testing.T) {
116116
}
117117

118118
func TestBuildOptions(t *testing.T) {
119-
checker, err := BuildURLChecker(buildAllConfig)
119+
checker, err := buildAllConfig.BuildURLChecker()
120120
assert.NoError(t, err)
121121
opts := buildAllConfig.BuildOptions(checker)
122122
assert.NotNil(t, opts)

0 commit comments

Comments
 (0)