Skip to content

Commit 9795200

Browse files
authored
Add new config option to specify which cronitor region should be used for telemetry (#43)
1 parent a58d2ff commit 9795200

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

README.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -227,17 +227,18 @@ monitor.delete() // destroy the monitor
227227

228228
## Package Configuration
229229

230-
The package needs to be configured with your account's `API key`, which is available on the [account settings](https://cronitor.io/settings) page. You can also optionally specify an `api_version`, an `environment`, and a request `timeout`.
230+
The package needs to be configured with your account's `API key`, which is available on the [account settings](https://cronitor.io/settings) page. You can also optionally specify an `api_version`, an `environment`, a request `timeout`, and a cronitor `region`.
231231

232-
These can also be supplied using the environment variables `CRONITOR_API_KEY`, `CRONITOR_API_VERSION`, `CRONITOR_ENVIRONMENT`, `CRONITOR_TIMEOUT`.
232+
These can also be supplied using the environment variables `CRONITOR_API_KEY`, `CRONITOR_API_VERSION`, `CRONITOR_ENVIRONMENT`, `CRONITOR_TIMEOUT`, `CRONITOR_REGION`.
233233

234234
```javascript
235235
const cronitor = require('cronitor')(
236236
'cronitor_api_key',
237237
{
238238
apiVersion: '2020-10-01',
239239
environment: 'staging',
240-
timeout: 5000
240+
timeout: 5000,
241+
region: 'eu'
241242
});
242243
```
243244

lib/cronitor.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function Cronitor(apiKey, config = {}) {
1818
const version = config.apiVersion || process.env.CRONITOR_API_VERSION || null;
1919
const timeout = config.timeout || process.env.CRONITOR_TIMEOUT || 10000;
2020
const env = config.environment || config.env || process.env.CRONITOR_ENVIRONMENT || process.env.CRONITOR_ENV || null;
21+
const region = config.region || process.env.CRONITOR_REGION || null;
2122
const headers = {
2223
'User-Agent': 'cronitor-js',
2324
'Authorization': 'Basic ' + new Buffer.from(apiKey + ':').toString('base64'),
@@ -26,11 +27,14 @@ function Cronitor(apiKey, config = {}) {
2627
if (path) this.path = path;
2728
if (version) headers['Cronitor-Version'] = version;
2829

30+
let subdomain = ""
31+
if (region === "eu") subdomain = "eu."
32+
2933
this._api = {
3034
key: apiKey,
3135
version,
3236
env: env,
33-
pingUrl: (key) => `https://cronitor.link/ping/${apiKey}/${key}`,
37+
pingUrl: (key) => `https://${subdomain}cronitor.link/ping/${apiKey}/${key}`,
3438
monitorUrl: (key) => `https://cronitor.io/api/monitors${key ? '/' + key : ''}`,
3539
axios: axios.create({
3640
baseURL: '',

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cronitor",
3-
"version": "2.4.1",
3+
"version": "2.5.0",
44
"description": "A simple library for reliably monitoring cron jobs, control-loops, or other important system events with Cronitor.",
55
"main": "lib/cronitor.js",
66
"repository": {

test/test.js

+9
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ describe('Monitor', () => {
192192
expect(stub).to.be.calledWith(`${monitor._api.pingUrl(monitor.key)}`);
193193
done();
194194
});
195+
196+
it('should use eu subdomain when region is set to eu', function(done) {
197+
const euCronitor = require('../lib/cronitor')('apiKey123', { region: 'eu' });
198+
const monitor = new euCronitor.Monitor('a-key');
199+
const stub = sinon.stub(monitor._api.axios, 'get');
200+
monitor.ok();
201+
expect(stub).to.be.calledWith('https://eu.cronitor.link/ping/apiKey123/a-key');
202+
done();
203+
});
195204
});
196205

197206
describe('Event', () => {

0 commit comments

Comments
 (0)