-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instance id to socks metrics (#2137)
(cherry picked from commit 76bd1c8)
- Loading branch information
1 parent
53db8fa
commit 80f72bd
Showing
13 changed files
with
248 additions
and
16 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,87 @@ | ||
import { setInstanceId, getInstanceId, resetForTests } from '../src/instance-id'; | ||
import { axiosRequest } from '../src/axios'; | ||
import { asMock } from '@dydxprotocol-indexer/dev'; | ||
import logger from '../src/logger'; | ||
import config from '../src/config'; | ||
|
||
jest.mock('../src/axios', () => ({ | ||
...(jest.requireActual('../src/axios') as object), | ||
axiosRequest: jest.fn(), | ||
})); | ||
|
||
describe('instance-id', () => { | ||
describe('setInstanceId', () => { | ||
const defaultTaskArn = 'defaultTaskArn'; | ||
const defaultResponse = { | ||
TaskARN: defaultTaskArn, | ||
}; | ||
const ecsUrl = config.ECS_CONTAINER_METADATA_URI_V4; | ||
|
||
beforeEach(() => { | ||
config.ECS_CONTAINER_METADATA_URI_V4 = ecsUrl; | ||
resetForTests(); | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
asMock(axiosRequest).mockResolvedValue(defaultResponse); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should set instance id to task ARN in staging', async () => { | ||
jest.spyOn(config, 'isStaging').mockReturnValueOnce(true); | ||
config.ECS_CONTAINER_METADATA_URI_V4 = 'url'; | ||
await setInstanceId(); | ||
|
||
expect(getInstanceId()).toEqual(defaultTaskArn); | ||
}); | ||
|
||
it('should set instance id to task ARN in production', async () => { | ||
jest.spyOn(config, 'isProduction').mockReturnValueOnce(true); | ||
config.ECS_CONTAINER_METADATA_URI_V4 = 'url'; | ||
await setInstanceId(); | ||
|
||
expect(getInstanceId()).toEqual(defaultTaskArn); | ||
}); | ||
|
||
it('should not call metadata endpoint if not production or staging', async () => { | ||
config.ECS_CONTAINER_METADATA_URI_V4 = 'url'; | ||
await setInstanceId(); | ||
|
||
expect(getInstanceId()).not.toEqual(defaultTaskArn); | ||
expect(asMock(axiosRequest)).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not set instance id if already set', async () => { | ||
jest.spyOn(config, 'isStaging').mockReturnValue(true); | ||
config.ECS_CONTAINER_METADATA_URI_V4 = 'url'; | ||
await setInstanceId(); | ||
const instanceId = getInstanceId(); | ||
await setInstanceId(); | ||
|
||
expect(getInstanceId()).toEqual(instanceId); | ||
expect(axiosRequest).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should log error and set instance id to uuid if request errors', async () => { | ||
jest.spyOn(config, 'isStaging').mockReturnValue(true); | ||
config.ECS_CONTAINER_METADATA_URI_V4 = 'url'; | ||
const loggerErrorSpy = jest.spyOn(logger, 'error'); | ||
const emptyInstanceId = getInstanceId(); | ||
asMock(axiosRequest).mockRejectedValueOnce(new Error()); | ||
await setInstanceId(); | ||
|
||
expect(loggerErrorSpy).toHaveBeenCalledTimes(1); | ||
expect(getInstanceId()).not.toEqual(emptyInstanceId); | ||
}); | ||
|
||
it('should not call metadata endpoint if url is empty', async () => { | ||
jest.spyOn(config, 'isStaging').mockReturnValue(true); | ||
await setInstanceId(); | ||
|
||
expect(axiosRequest).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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
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,51 @@ | ||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
import { axiosRequest } from './axios'; | ||
import config from './config'; | ||
import logger from './logger'; | ||
|
||
let INSTANCE_ID: string = ''; | ||
|
||
export function getInstanceId(): string { | ||
return INSTANCE_ID; | ||
} | ||
|
||
export async function setInstanceId(): Promise<void> { | ||
if (INSTANCE_ID !== '') { | ||
return; | ||
} | ||
if (config.ECS_CONTAINER_METADATA_URI_V4 !== '' && | ||
( | ||
config.isProduction() || config.isStaging() | ||
) | ||
) { | ||
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint-v4.html | ||
const taskUrl = `${config.ECS_CONTAINER_METADATA_URI_V4}/task`; | ||
try { | ||
const response = await axiosRequest({ | ||
method: 'GET', | ||
url: taskUrl, | ||
}) as { TaskARN: string }; | ||
INSTANCE_ID = response.TaskARN; | ||
} catch (error) { | ||
logger.error({ | ||
at: 'instance-id#setInstanceId', | ||
message: 'Failed to retrieve task arn from metadata endpoint. Falling back to uuid.', | ||
error, | ||
taskUrl, | ||
}); | ||
INSTANCE_ID = uuidv4(); | ||
} | ||
} else { | ||
INSTANCE_ID = uuidv4(); | ||
|
||
} | ||
} | ||
|
||
// Exported for tests | ||
export function resetForTests(): void { | ||
if (!config.isTest()) { | ||
throw new Error(`resetForTests() cannot be called for env: ${config.NODE_ENV}`); | ||
} | ||
INSTANCE_ID = ''; | ||
} |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"outDir": "build" | ||
}, | ||
"include": [ | ||
"src" | ||
"src", | ||
"__tests__" | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.