-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: eu dynamic configuration support (#439)
- Loading branch information
1 parent
87e3a64
commit 0618a90
Showing
9 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Constants from './constants'; | ||
import { getDynamicConfigApi } from './server-zone'; | ||
/** | ||
* Dynamic Configuration | ||
* Find the best server url automatically based on app users' geo location. | ||
*/ | ||
class ConfigManager { | ||
constructor() { | ||
if (!ConfigManager.instance) { | ||
this.ingestionEndpoint = Constants.EVENT_LOG_URL; | ||
ConfigManager.instance = this; | ||
} | ||
return ConfigManager.instance; | ||
} | ||
|
||
refresh(serverZone, forceHttps, callback) { | ||
let protocol = 'https'; | ||
if (!forceHttps && 'https:' !== window.location.protocol) { | ||
protocol = 'http'; | ||
} | ||
const dynamicConfigUrl = protocol + '://' + getDynamicConfigApi(serverZone); | ||
const self = this; | ||
const isIE = window.XDomainRequest ? true : false; | ||
if (isIE) { | ||
const xdr = new window.XDomainRequest(); | ||
xdr.open('GET', dynamicConfigUrl, true); | ||
xdr.onload = function () { | ||
const response = JSON.parse(xdr.responseText); | ||
self.ingestionEndpoint = response['ingestionEndpoint']; | ||
if (callback) { | ||
callback(); | ||
} | ||
}; | ||
xdr.onerror = function () {}; | ||
xdr.ontimeout = function () {}; | ||
xdr.onprogress = function () {}; | ||
xdr.send(); | ||
} else { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', dynamicConfigUrl, true); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4 && xhr.status === 200) { | ||
const response = JSON.parse(xhr.responseText); | ||
self.ingestionEndpoint = response['ingestionEndpoint']; | ||
if (callback) { | ||
callback(); | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
} | ||
} | ||
} | ||
|
||
const instance = new ConfigManager(); | ||
|
||
export default instance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import Constants from './constants'; | ||
|
||
/** | ||
* AmplitudeServerZone is for Data Residency and handling server zone related properties. | ||
* The server zones now are US and EU. | ||
* | ||
* For usage like sending data to Amplitude's EU servers, you need to configure the serverZone during nitializing. | ||
*/ | ||
const AmplitudeServerZone = { | ||
US: 'US', | ||
EU: 'EU', | ||
}; | ||
|
||
const getEventLogApi = (serverZone) => { | ||
let eventLogUrl = Constants.EVENT_LOG_URL; | ||
switch (serverZone) { | ||
case AmplitudeServerZone.EU: | ||
eventLogUrl = Constants.EVENT_LOG_EU_URL; | ||
break; | ||
case AmplitudeServerZone.US: | ||
eventLogUrl = Constants.EVENT_LOG_URL; | ||
break; | ||
default: | ||
break; | ||
} | ||
return eventLogUrl; | ||
}; | ||
|
||
const getDynamicConfigApi = (serverZone) => { | ||
let dynamicConfigUrl = Constants.DYNAMIC_CONFIG_URL; | ||
switch (serverZone) { | ||
case AmplitudeServerZone.EU: | ||
dynamicConfigUrl = Constants.DYNAMIC_CONFIG_EU_URL; | ||
break; | ||
case AmplitudeServerZone.US: | ||
dynamicConfigUrl = Constants.DYNAMIC_CONFIG_URL; | ||
break; | ||
default: | ||
break; | ||
} | ||
return dynamicConfigUrl; | ||
}; | ||
|
||
export { AmplitudeServerZone, getEventLogApi, getDynamicConfigApi }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sinon from 'sinon'; | ||
import ConfigManager from '../src/config-manager'; | ||
import { AmplitudeServerZone } from '../src/server-zone'; | ||
import Constants from '../src/constants'; | ||
|
||
describe('ConfigManager', function () { | ||
let server; | ||
beforeEach(function () { | ||
server = sinon.fakeServer.create(); | ||
}); | ||
|
||
afterEach(function () { | ||
server.restore(); | ||
}); | ||
|
||
it('ConfigManager should support EU zone', function () { | ||
ConfigManager.refresh(AmplitudeServerZone.EU, true, function () { | ||
assert.equal(Constants.EVENT_LOG_EU_URL, ConfigManager.ingestionEndpoint); | ||
}); | ||
server.respondWith('{"ingestionEndpoint": "api.eu.amplitude.com"}'); | ||
server.respond(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { AmplitudeServerZone, getEventLogApi, getDynamicConfigApi } from '../src/server-zone'; | ||
import Constants from '../src/constants'; | ||
|
||
describe('AmplitudeServerZone', function () { | ||
it('getEventLogApi should return correct event log url', function () { | ||
assert.equal(Constants.EVENT_LOG_URL, getEventLogApi(AmplitudeServerZone.US)); | ||
assert.equal(Constants.EVENT_LOG_EU_URL, getEventLogApi(AmplitudeServerZone.EU)); | ||
assert.equal(Constants.EVENT_LOG_URL, getEventLogApi('')); | ||
}); | ||
|
||
it('getDynamicConfigApi should return correct dynamic config url', function () { | ||
assert.equal(Constants.DYNAMIC_CONFIG_URL, getDynamicConfigApi(AmplitudeServerZone.US)); | ||
assert.equal(Constants.DYNAMIC_CONFIG_EU_URL, getDynamicConfigApi(AmplitudeServerZone.EU)); | ||
assert.equal(Constants.DYNAMIC_CONFIG_URL, getDynamicConfigApi('')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters