Skip to content
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: add cloudProvider as exported property for appInfo #765

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/app-info/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A utility to get application information (e.g. the system code) in a consistent
* [`appInfo.releaseVersion`](#appinforeleaseversion)
* [`appInfo.systemCode`](#appinfosystemcode)
* [`appInfo.processType`](#appinfoprocesstype)
* [`appInfo.cloudProvider](#appinfocloudprovider)
* [Contributing](#contributing)
* [License](#license)

Expand Down Expand Up @@ -77,6 +78,12 @@ For Heroku, this is derived from the first part of `process.env.DYNO`, which is

If neither `process.env.AWS_LAMBDA_FUNCTION_NAME` or `process.env.DYNO` are set, this property will be `null`

### `appInfo.cloudProvider`

Get the type of cloud provider which is set to either Heroku or AWS depending on where we think the system is hosted.

This is derived from some environment variables which we have high confidence are defined by only Heroku or AWS/Lambda

## Contributing

See the [central contributing guide for Reliability Kit](https://github.com/Financial-Times/dotcom-reliability-kit/blob/main/docs/contributing.md).
Expand Down
26 changes: 25 additions & 1 deletion packages/app-info/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,22 @@ const processType =
(process.env.DYNO && normalizeHerokuProcessType(process.env.DYNO)) ||
null;

/**
* Sets the cloud provider type.
*
* @returns {string | null}
* Returns the cloud provider type (either aws or heroku).
*/
const cloudProvider = () => {
if (process.env.AWS_LAMBDA_FUNCTION_NAME) {
return 'aws';
}
if (process.env.HEROKU_RELEASE_CREATED_AT) {
return 'heroku';
}
return null;
};

module.exports = {
/**
* The application commit hash.
Expand Down Expand Up @@ -125,7 +141,15 @@ module.exports = {
* @readonly
* @type {string | null}
*/
processType
processType,

/**
* The cloud provider type.
*
* @readonly
* @type {string | null}
*/
cloudProvider: cloudProvider()
};

// @ts-ignore
Expand Down
27 changes: 27 additions & 0 deletions packages/app-info/test/unit/lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,31 @@ describe('@dotcom-reliability-kit/app-info', () => {
});
});
});

describe('.cloudProvider', () => {
it('is set to `aws` if processType is set to `process.env.AWS_LAMBDA_FUNCTION_NAME`', () => {
if (appInfo.processType === process.env.AWS_LAMBDA_FUNCTION_NAME) {
expect(appInfo.cloudProvider).toBe('aws');
}
});

it('is set to `heroku` if `process.env.HEROKU_RELEASE_CREATED_AT` is set to true', () => {
if (process.env.HEROKU_RELEASE_CREATED_AT) {
expect(appInfo.cloudProvider).toBe('heroku');
}
});

describe('when neither `process.env.AWS_LAMBDA_FUNCTION_NAME` or `process.env.HEROKU_RELEASE_CREATED_AT` are defined', () => {
beforeEach(() => {
jest.resetModules();
delete process.env.HEROKU_RELEASE_CREATED_AT;
delete process.env.AWS_LAMBDA_FUNCTION_NAME;
appInfo = require('../../../lib');
});

it('is set to null', () => {
expect(appInfo.cloudProvider).toBe(null);
});
});
});
});