@@ -5,11 +5,7 @@ const MongoClientWrapper = require('./wrappers/MongoClientWrapper.js');
5
5
const MongoServerWrapper = require ( './wrappers/MongoServerWrapper.js' ) ;
6
6
const PostgresClientWrapper = require ( './wrappers/PostgresClientWrapper.js' ) ;
7
7
const PostgresServerWrapper = require ( './wrappers/PostgresServerWrapper.js' ) ;
8
- const { validateInput, addNotifications } = require ( './controllers/helpers' ) ;
9
- const { initialFetch } = require ( './controllers/kafkaHelpers' ) ;
10
-
11
- let userConfig = { } ;
12
- const chronos = { } ;
8
+ const utilities = require ( './controllers/utilities' ) ;
13
9
14
10
/**
15
11
* **********************************
@@ -31,155 +27,179 @@ const chronos = {};
31
27
* Varies per notification method
32
28
* **********************************
33
29
*/
34
- chronos . use = config => {
35
- // Validate all input fields exist and setup notifications
36
- validateInput ( config ) ;
37
- addNotifications ( config ) ;
38
30
39
- // Save config globally
40
- userConfig = config ;
41
- } ;
31
+ class Chronos {
32
+ constructor ( config ) {
33
+ if ( config === undefined ) {
34
+ throw new Error ( 'Chronos config is undefined' ) ;
35
+ }
42
36
43
- /**
44
- * Places an unique x-correlating-id into the headers of each request/response.
45
- * This is used for tracking the life cycle of the request until the response
46
- */
47
- chronos . propagate = ( ) => {
48
- hpropagate ( { propagateInResponses : true } ) ;
49
- } ;
37
+ // Validate all input fields exist and setup notifications
38
+ config = utilities . validateInput ( config ) ;
39
+ config = utilities . addNotifications ( config ) ;
40
+ this . config = config ;
41
+ }
50
42
51
- /**
52
- * **********************************************
53
- * MAIN CONTROLLER
54
- * Only supports MongoDB and PostgreSQL for now!
55
- * **********************************************
56
- */
57
- chronos . track = ( ) => {
58
- const { database, dockerized } = userConfig ;
59
-
60
- /**
61
- * If the provided database is Mongo
62
- * - Connection is made to MongoDB via the provided URI by the user.
63
- *
64
- * - 'services' collection will be created if not already and stores every microservice
65
- * that is apart of the application.
66
- *
67
- * - Information is collected if the microservice is containerized
68
- *
69
- * - 'communications' collection will be created which creates a new document for every
70
- * endpoint that the user Request travels through (tracked with hpropograte) for express routes
71
- */
72
- if ( database . type === 'MongoDB' ) {
73
- mongo . connect ( userConfig ) ;
74
- mongo . services ( userConfig ) ;
75
- mongo . docker ( userConfig ) ;
76
- mongo . health ( userConfig ) ;
77
- if ( database . connection === 'REST' ) {
78
- return mongo . communications ( userConfig ) ;
79
- }
43
+
44
+ propagate ( ) {
45
+ /**
46
+ * Places an unique x-correlating-id into the headers of each request/response.
47
+ * This is used for tracking the life cycle of the request until the response
48
+ */
49
+ hpropagate ( { propagateInResponses : true } ) ;
80
50
}
81
51
82
- /**
83
- * If the provided database is PostgreSQL
84
- * - Connection is made to the postgres client via the provided URI by the user.
85
- *
86
- * - 'services' table will be created if not already and stores every microservice
87
- * that is apart of the application.
88
- *
89
- * - Information is collected if the microservice is containerized
90
- *
91
- * - 'communications' table will be created which creates a new row entry for every
92
- * endpoint that the user Request travels through (tracked with hpropograte)
93
- */
94
- if ( database . type === 'PostgreSQL' ) {
95
- postgres . connect ( userConfig ) ;
96
- postgres . services ( userConfig ) ;
97
- postgres . docker ( userConfig ) ;
98
- postgres . health ( userConfig ) ;
99
- if ( database . connection === 'REST' ) {
100
- return postgres . communications ( userConfig ) ;
52
+
53
+ track ( ) {
54
+ /**
55
+ * **********************************************
56
+ * MAIN CONTROLLER
57
+ * Only supports MongoDB and PostgreSQL for now!
58
+ * **********************************************
59
+ */
60
+ const { database, dockerized } = this . config ;
61
+
62
+ /**
63
+ * If the provided database is Mongo
64
+ * - Connection is made to MongoDB via the provided URI by the user.
65
+ *
66
+ * - 'services' collection will be created if not already and stores every microservice
67
+ * that is apart of the application.
68
+ *
69
+ * - Information is collected if the microservice is containerized
70
+ *
71
+ * - 'communications' collection will be created which creates a new document for every
72
+ * endpoint that the user Request travels through (tracked with hpropograte) for express routes
73
+ */
74
+ if ( database . type === 'MongoDB' ) {
75
+ mongo . connect ( this . config ) ;
76
+ mongo . services ( this . config ) ;
77
+ dockerized ? mongo . docker ( this . config ) : mongo . health ( this . config ) ;
78
+ if ( database . connection === 'REST' ) {
79
+ return mongo . communications ( this . config ) ;
80
+ }
101
81
}
102
- }
103
- return null ;
104
- } ;
105
82
106
- /**
107
- * **********************************************
108
- * COLLECT KAFKA METRICS
109
- * Only supports MongoDB and PostgreSQL for now!
110
- * **********************************************
111
- */
83
+ /**
84
+ * If the provided database is PostgreSQL
85
+ * - Connection is made to the postgres client via the provided URI by the user.
86
+ *
87
+ * - 'services' table will be created if not already and stores every microservice
88
+ * that is apart of the application.
89
+ *
90
+ * - Information is collected if the microservice is containerized
91
+ *
92
+ * - 'communications' table will be created which creates a new row entry for every
93
+ * endpoint that the user Request travels through (tracked with hpropograte)
94
+ */
95
+ else if ( database . type === 'PostgreSQL' ) {
96
+ postgres . connect ( this . config ) ;
97
+ postgres . services ( this . config ) ;
98
+ dockerized ? postgres . docker ( this . config ) : postgres . health ( this . config )
99
+ if ( database . connection === 'REST' ) {
100
+ return postgres . communications ( this . config ) ;
101
+ }
102
+ }
112
103
113
- chronos . kafka = function ( ) {
114
- const { database, jmxuri } = userConfig ;
115
- if ( jmxuri === undefined ) {
116
- console . log ( 'No specified URI for a JMX Exporter' ) ;
117
- return ;
104
+ else {
105
+ throw new Error ( 'The only allowed database types are MongoDB and PostgreSQL' ) ;
106
+ }
118
107
}
119
108
120
- // Ensures that the provided URI returns correctly formatted data.
121
- initialFetch ( jmxuri ) ;
122
109
123
- if ( database . type === 'MongoDB' ) {
124
- mongo . connect ( userConfig ) ;
125
- mongo . kafka ( userConfig ) ;
126
- }
110
+ kafka ( ) {
111
+ // Test metrics server connection
112
+ utilities . testMetricsQuery ( this . config ) ;
113
+
114
+ if ( this . config . database . type === 'MongoDB' ) {
115
+ mongo . connect ( this . config ) ;
116
+ mongo . serverQuery ( this . config ) ;
117
+ }
118
+
119
+ else if ( this . config . database . type === 'PostgreSQL' ) {
120
+ postgres . connect ( this . config ) ;
121
+ postgres . serverQuery ( this . config ) ;
122
+ }
127
123
128
- if ( database . type === 'PostgreSQL' ) {
129
- postgres . connect ( userConfig ) ;
130
- postgres . kafka ( userConfig ) ;
124
+ else {
125
+ throw new Error ( 'The only allowed database types are MongoDB and PostgreSQL' ) ;
126
+ }
131
127
}
132
- } ;
133
128
134
- /**
135
- * Wraps the gRPC server object to automatically write logs to user configed DB
136
- *
137
- * If the provided database is MongoDB, connection will be made to the Mongodb Atlas
138
- *
139
- * If the provided database is PostgreSQL, connection will be made to PostgreSQL client
140
- * @param {* } server
141
- * @param {* } proto
142
- * @param {* } methods
143
- */
144
- chronos . ServerWrapper = ( server , proto , methods ) => {
145
- const { database } = userConfig ;
146
- if ( database . type === 'MongoDB' ) {
147
- return new MongoServerWrapper ( server , proto , methods , userConfig ) ;
129
+ kubernetes ( ) {
130
+ // Test metrics server connection
131
+ utilities . testMetricsQuery ( this . config ) ;
132
+
133
+ if ( this . config . database . type === 'MongoDB' ) {
134
+ mongo . connect ( this . config ) ;
135
+ mongo . serverQuery ( this . config ) ;
136
+ }
137
+
138
+ else if ( this . config . database . type === 'PostgreSQL' ) {
139
+ postgres . connect ( this . config ) ;
140
+ postgres . serverQuery ( this . config ) ;
141
+ }
142
+
143
+ else {
144
+ throw new Error ( 'The only allowed database types are MongoDB and PostgreSQL' ) ;
145
+ }
148
146
}
149
- if ( database . type === 'PostgreSQL' ) {
150
- return new PostgresServerWrapper ( server , proto , methods , userConfig ) ;
147
+
148
+
149
+ ServerWrapper ( server , proto , methods ) {
150
+ /**
151
+ * Wraps the gRPC server object to automatically write logs to user configed DB
152
+ *
153
+ * If the provided database is MongoDB, connection will be made to the Mongodb Atlas
154
+ *
155
+ * If the provided database is PostgreSQL, connection will be made to PostgreSQL client
156
+ * @param {* } server
157
+ * @param {* } proto
158
+ * @param {* } methods
159
+ */
160
+ const { database } = this . config ;
161
+ if ( database . type === 'MongoDB' ) {
162
+ return new MongoServerWrapper ( server , proto , methods , this . config ) ;
163
+ }
164
+ if ( database . type === 'PostgreSQL' ) {
165
+ return new PostgresServerWrapper ( server , proto , methods , this . config ) ;
166
+ }
167
+ return null ;
151
168
}
152
- return null ;
153
- } ;
154
- /**
155
- * Wraps the gRPC client to automatically write logs to user configed DB
156
- *
157
- * If the provided database is MongoDB, connection will be made to the Mongodb Atlas
158
- *
159
- * If the provided database is PostgreSQL, connection will be made to PostgreSQL client
160
- *
161
- * @param {* } client
162
- * @param {* } service
163
- */
164
- chronos . ClientWrapper = ( client , service ) => {
165
- const { database } = userConfig ;
166
- if ( database . type === 'MongoDB' ) {
167
- return new MongoClientWrapper ( client , service , userConfig ) ;
169
+
170
+
171
+ ClientWrapper ( client , service ) {
172
+ /**
173
+ * Wraps the gRPC client to automatically write logs to user configed DB
174
+ *
175
+ * If the provided database is MongoDB, connection will be made to the Mongodb Atlas
176
+ *
177
+ * If the provided database is PostgreSQL, connection will be made to PostgreSQL client
178
+ *
179
+ * @param {* } client
180
+ * @param {* } service
181
+ */
182
+ const { database } = this . config ;
183
+ if ( database . type === 'MongoDB' ) {
184
+ return new MongoClientWrapper ( client , service , this . config ) ;
185
+ }
186
+ if ( database . type === 'PostgreSQL' ) {
187
+ return new PostgresClientWrapper ( client , service , this . config ) ;
188
+ }
189
+ return null ;
168
190
}
169
- if ( database . type === 'PostgreSQL' ) {
170
- return new PostgresClientWrapper ( client , service , userConfig ) ;
191
+
192
+
193
+ link ( client , server ) {
194
+ /**
195
+ * Allows the passthrough of metadata from gRPC server to gRPC client
196
+ *
197
+ * @param {* } client
198
+ * @param {* } servere
199
+ */
200
+ client . metadata = server . metadataHolder ;
171
201
}
172
- return null ;
173
- } ;
174
202
175
- /**
176
- * Allows the passthrough of metadata from gRPC server to gRPC client
177
- *
178
- * @param {* } client
179
- * @param {* } servere
180
- */
181
- chronos . link = ( client , server ) => {
182
- client . metadata = server . metadataHolder ;
183
- } ;
203
+ }
184
204
185
- module . exports = chronos ;
205
+ module . exports = Chronos ;
0 commit comments