Skip to content

Commit

Permalink
feat(core): include jsii runtime version in analytics (#1288)
Browse files Browse the repository at this point in the history
Add the jsii runtime version to the library version reporting
under "jsii-runtime". jsii runtime clients populate the JSII_AGENT
environment variable with this information (see aws/jsii#325).

If JSII_AGENT is not defined, we assume this is a node.js runtime
and include the node.js version.

Fixes #1258
Fixes awslabs/cdk-ops#127
  • Loading branch information
Elad Ben-Israel authored Dec 5, 2018
1 parent 5478913 commit f06de18
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/@aws-cdk/cdk/lib/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ export class App extends Root {
}
}

// add jsii runtime version
libraries['jsii-runtime'] = getJsiiAgentVersion();

return { libraries };
}

Expand Down Expand Up @@ -205,3 +208,15 @@ function findNpmPackage(fileName: string): { name: string, version: string, priv
return s;
}
}

function getJsiiAgentVersion() {
let jsiiAgent = process.env.JSII_AGENT;

// if JSII_AGENT is not specified, we will assume this is a node.js runtime
// and plug in our node.js version
if (!jsiiAgent) {
jsiiAgent = `node.js/${process.version}`;
}

return jsiiAgent;
}
31 changes: 31 additions & 0 deletions packages/@aws-cdk/cdk/test/test.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function withApp(context: { [key: string]: any } | undefined, block: (app: App)
}

const app = new App();

block(app);

app.run();
Expand Down Expand Up @@ -266,6 +267,36 @@ export = {

test.done();
},

'runtime library versions'(test: Test) {
const response = withApp({}, app => {
const stack = new Stack(app, 'stack1');
new Resource(stack, 'MyResource', { type: 'Resource::Type' });
});

const libs = response.runtime.libraries;

const version = require('../package.json').version;
test.deepEqual(libs['@aws-cdk/cdk'], version);
test.deepEqual(libs['@aws-cdk/cx-api'], version);
test.deepEqual(libs['jsii-runtime'], `node.js/${process.version}`);
test.done();
},

'jsii-runtime version loaded from JSII_AGENT'(test: Test) {
process.env.JSII_AGENT = 'Java/1.2.3.4';

const response = withApp({}, app => {
const stack = new Stack(app, 'stack1');
new Resource(stack, 'MyResource', { type: 'Resource::Type' });
});

const libs = response.runtime.libraries;
test.deepEqual(libs['jsii-runtime'], `Java/1.2.3.4`);

delete process.env.JSII_AGENT;
test.done();
}
};

class MyConstruct extends Construct {
Expand Down

0 comments on commit f06de18

Please sign in to comment.