@@ -14,6 +14,9 @@ import (
14
14
15
15
"github.com/rudderlabs/rudder-go-kit/config"
16
16
kithttputil "github.com/rudderlabs/rudder-go-kit/httputil"
17
+ "github.com/rudderlabs/rudder-go-kit/logger"
18
+ "github.com/rudderlabs/rudder-go-kit/stats"
19
+ obskit "github.com/rudderlabs/rudder-observability-kit/go/labels"
17
20
"github.com/rudderlabs/rudder-server/services/controlplane/identity"
18
21
"github.com/rudderlabs/rudder-server/utils/types"
19
22
)
@@ -27,9 +30,18 @@ type singleWorkspaceConfig struct {
27
30
28
31
workspaceIDOnce sync.Once
29
32
workspaceID string
33
+
34
+ logger logger.Logger
35
+ httpCallsStat stats.Counter
30
36
}
31
37
32
38
func (wc * singleWorkspaceConfig ) SetUp () error {
39
+ wc .httpCallsStat = stats .Default .NewStat ("backend_config_http_calls" , stats .CountType )
40
+
41
+ if wc .logger == nil {
42
+ wc .logger = logger .NewLogger ().Child ("backend-config" ).Withn (obskit .WorkspaceID (wc .workspaceID ))
43
+ }
44
+
33
45
if configFromFile {
34
46
if wc .configJSONPath == "" {
35
47
return fmt .Errorf ("valid configJSONPath is required when configFromFile is set to true" )
@@ -42,6 +54,9 @@ func (wc *singleWorkspaceConfig) SetUp() error {
42
54
if wc .token == "" {
43
55
return fmt .Errorf ("single workspace: empty workspace config token" )
44
56
}
57
+
58
+ wc .logger .Infon ("Setup backend config complete" )
59
+
45
60
return nil
46
61
}
47
62
@@ -60,9 +75,9 @@ func (wc *singleWorkspaceConfig) Get(ctx context.Context) (map[string]ConfigT, e
60
75
61
76
// getFromApi gets the workspace config from api
62
77
func (wc * singleWorkspaceConfig ) getFromAPI (ctx context.Context ) (map [string ]ConfigT , error ) {
63
- config := make (map [string ]ConfigT )
78
+ conf := make (map [string ]ConfigT )
64
79
if wc .configBackendURL == nil {
65
- return config , fmt .Errorf ("single workspace: config backend url is nil" )
80
+ return conf , fmt .Errorf ("single workspace: config backend url is nil" )
66
81
}
67
82
68
83
var (
@@ -78,13 +93,15 @@ func (wc *singleWorkspaceConfig) getFromAPI(ctx context.Context) (map[string]Con
78
93
79
94
backoffWithMaxRetry := backoff .WithContext (backoff .WithMaxRetries (backoff .NewExponentialBackOff (), 3 ), ctx )
80
95
err := backoff .RetryNotify (operation , backoffWithMaxRetry , func (err error , t time.Duration ) {
81
- pkgLogger .Warnf ("Failed to fetch config from API with error: %v, retrying after %v" , err , t )
96
+ wc .logger .Warnn ("Failed to fetch backend config from API" ,
97
+ obskit .Error (err ), logger .NewDurationField ("retryAfter" , t ),
98
+ )
82
99
})
83
100
if err != nil {
84
101
if ctx .Err () == nil {
85
- pkgLogger . Errorf ("Error sending request to the server: %v " , err )
102
+ wc . logger . Errorn ("Error sending request to the server" , obskit . Error ( err ) )
86
103
}
87
- return config , err
104
+ return conf , err
88
105
}
89
106
90
107
configEnvHandler := wc .configEnvHandler
@@ -95,41 +112,46 @@ func (wc *singleWorkspaceConfig) getFromAPI(ctx context.Context) (map[string]Con
95
112
var sourcesJSON ConfigT
96
113
err = json .Unmarshal (respBody , & sourcesJSON )
97
114
if err != nil {
98
- pkgLogger . Errorf ("Error while parsing request: %v " , err )
99
- return config , err
115
+ wc . logger . Errorn ("Error while parsing request" , obskit . Error ( err ) )
116
+ return conf , err
100
117
}
101
118
sourcesJSON .ApplyReplaySources ()
102
119
workspaceID := sourcesJSON .WorkspaceID
103
120
104
121
wc .workspaceIDOnce .Do (func () {
105
122
wc .workspaceID = workspaceID
106
123
})
107
- config [workspaceID ] = sourcesJSON
124
+ conf [workspaceID ] = sourcesJSON
108
125
109
- return config , nil
126
+ return conf , nil
110
127
}
111
128
112
129
// getFromFile reads the workspace config from JSON file
113
130
func (wc * singleWorkspaceConfig ) getFromFile () (map [string ]ConfigT , error ) {
114
- pkgLogger .Debug ("Reading workspace config from JSON file" )
115
- config := make (map [string ]ConfigT )
131
+ wc .logger .Debugn ("Reading workspace config from JSON file" )
132
+
133
+ conf := make (map [string ]ConfigT )
116
134
data , err := IoUtil .ReadFile (wc .configJSONPath )
117
135
if err != nil {
118
- pkgLogger .Errorf ("Unable to read backend config from file: %s with error : %s" , wc .configJSONPath , err .Error ())
119
- return config , err
136
+ wc .logger .Errorn ("Unable to read backend config from file" ,
137
+ logger .NewStringField ("path" , wc .configJSONPath ), obskit .Error (err ),
138
+ )
139
+ return conf , err
120
140
}
121
141
var configJSON ConfigT
122
142
if err = json .Unmarshal (data , & configJSON ); err != nil {
123
- pkgLogger .Errorf ("Unable to parse backend config from file: %s" , wc .configJSONPath )
124
- return config , err
143
+ wc .logger .Errorn ("Unable to parse backend config from file" ,
144
+ logger .NewStringField ("path" , wc .configJSONPath ), obskit .Error (err ),
145
+ )
146
+ return conf , err
125
147
}
126
148
workspaceID := configJSON .WorkspaceID
127
149
wc .workspaceIDOnce .Do (func () {
128
- pkgLogger . Info ("Read workspace config from JSON file" )
150
+ wc . logger . Infon ("Read workspace config from JSON file" )
129
151
wc .workspaceID = workspaceID
130
152
})
131
- config [workspaceID ] = configJSON
132
- return config , nil
153
+ conf [workspaceID ] = configJSON
154
+ return conf , nil
133
155
}
134
156
135
157
func (wc * singleWorkspaceConfig ) makeHTTPRequest (ctx context.Context , url string ) ([]byte , error ) {
@@ -146,6 +168,8 @@ func (wc *singleWorkspaceConfig) makeHTTPRequest(ctx context.Context, url string
146
168
req .URL .RawQuery = q .Encode ()
147
169
}
148
170
171
+ defer wc .httpCallsStat .Increment ()
172
+
149
173
client := & http.Client {Timeout : config .GetDuration ("HttpClient.backendConfig.timeout" , 30 , time .Second )}
150
174
resp , err := client .Do (req )
151
175
if err != nil {
0 commit comments