-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: eu dynamic configuration support #439
Merged
+203
−2
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated to
false
, to not breaking current customers with custom proxy server.