Skip to content

Commit

Permalink
improve telemetry by moving logic into a common source
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed May 10, 2019
1 parent f186e9b commit 7ecdf16
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 77 deletions.
27 changes: 5 additions & 22 deletions src/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/** @module auth **/

var util = require('util');

var pkg = require('../../package.json');
var utils = require('../utils');
var jsonToBase64 = utils.jsonToBase64;
var ArgumentError = require('rest-facade').ArgumentError;
Expand Down Expand Up @@ -67,8 +65,11 @@ var AuthenticationClient = function(options) {
};

if (options.telemetry !== false) {
var telemetry = jsonToBase64(options.clientInfo || this.getClientInfo());
managerOptions.headers['Auth0-Client'] = telemetry;
var clientInfo = options.clientInfo || utils.generateClientInfo();
if ('string' === typeof clientInfo.name && clientInfo.name.length > 0) {
var telemetry = jsonToBase64(clientInfo);
managerOptions.headers['Auth0-Client'] = telemetry;
}
}

/**
Expand Down Expand Up @@ -107,24 +108,6 @@ var AuthenticationClient = function(options) {
this.tokens = new TokensManager(managerOptions);
};

/**
* Return an object with information about the current client,
*
* @method getClientInfo
* @memberOf module:auth.AuthenticationClient.prototype
*
* @return {Object} Object containing client information.
*/
AuthenticationClient.prototype.getClientInfo = function() {
return {
name: 'node-auth0',
version: pkg.version,
env: {
node: process.version.replace('v', '')
}
};
};

/**
* Start passwordless flow sending an email.
*
Expand Down
28 changes: 6 additions & 22 deletions src/management/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/** @module management */

var util = require('util');

var pkg = require('../../package.json');
var utils = require('../utils');
var jsonToBase64 = utils.jsonToBase64;
var generateClientInfo = utils.generateClientInfo;
var ArgumentError = require('rest-facade').ArgumentError;
var assign = Object.assign || require('object.assign');

Expand Down Expand Up @@ -129,8 +128,11 @@ var ManagementClient = function(options) {
}

if (options.telemetry !== false) {
var telemetry = jsonToBase64(options.clientInfo || this.getClientInfo());
managerOptions.headers['Auth0-Client'] = telemetry;
var clientInfo = options.clientInfo || generateClientInfo();
if ('string' === typeof clientInfo.name && clientInfo.name.length > 0) {
var telemetry = jsonToBase64(clientInfo);
managerOptions.headers['Auth0-Client'] = telemetry;
}
}

managerOptions.retry = options.retry;
Expand Down Expand Up @@ -290,24 +292,6 @@ var ManagementClient = function(options) {
this.roles = new RolesManager(managerOptions);
};

/**
* Return an object with information about the current client,
*
* @method getClientInfo
* @memberOf module:management.ManagementClient.prototype
*
* @return {Object} Object containing client information.
*/
ManagementClient.prototype.getClientInfo = function() {
return {
name: 'node-auth0',
version: pkg.version,
env: {
node: process.version.replace('v', '')
}
};
};

/**
* Get all connections.
*
Expand Down
19 changes: 19 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Promise = require('bluebird');
var request = require('request');
var pkg = require('../package.json');

/**
* @module utils
Expand All @@ -22,6 +23,24 @@ utils.jsonToBase64 = function(json) {
.replace(/=+$/, '');
};

/**
* Return an object with information about the current client,
*
* @method generateClientInfo
* @memberOf module:utils
*
* @return {Object} Object containing client information.
*/
utils.generateClientInfo = function() {
return {
name: 'node-auth0',
version: pkg.version,
env: {
node: process.version.replace('v', '')
}
};
};

/**
* Simple wrapper that, given a class, a property name and a method name,
* creates a new method in the class that is a wrapper for the given
Expand Down
41 changes: 25 additions & 16 deletions test/auth/authentication-client.tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var expect = require('chai').expect;
var sinon = require('sinon');
var proxyquire = require('proxyquire');

var ArgumentError = require('rest-facade').ArgumentError;

Expand Down Expand Up @@ -69,22 +70,17 @@ describe('AuthenticationClient', function() {
});

describe('client info', function() {
it('should generate default telemetry value', function() {
var client = new AuthenticationClient({ token: 'token', domain: 'auth0.com' });
var pkgVersion = require('../../package.json').version;
var nodeVersion = process.version.replace('v', '');
expect(client.getClientInfo()).to.deep.equal({
name: 'node-auth0',
version: pkgVersion,
env: { node: nodeVersion }
});
});

it('should configure instances with default telemetry header', function() {
sinon.stub(AuthenticationClient.prototype, 'getClientInfo', function() {
return { name: 'test-sdk', version: 'ver-123' };
var utilsStub = {
generateClientInfo: sinon.spy(function() {
return { name: 'test-sdk', version: 'ver-123' };
})
};
var AuthenticationClientProxy = proxyquire('../../src/auth/', {
'../utils': utilsStub
});
var client = new AuthenticationClient({ token: 'token', domain: 'auth0.com' });

var client = new AuthenticationClientProxy({ token: 'token', domain: 'auth0.com' });

var requestHeaders = {
'Auth0-Client': 'eyJuYW1lIjoidGVzdC1zZGsiLCJ2ZXJzaW9uIjoidmVyLTEyMyJ9',
Expand All @@ -95,8 +91,6 @@ describe('AuthenticationClient', function() {
expect(client.passwordless.passwordless.options.headers).to.contain(requestHeaders);
expect(client.users.headers).to.contain(requestHeaders);
expect(client.tokens.headers).to.contain(requestHeaders);

AuthenticationClient.prototype.getClientInfo.restore();
});

it('should configure instances with custom telemetry header', function() {
Expand All @@ -119,6 +113,21 @@ describe('AuthenticationClient', function() {
expect(client.tokens.headers).to.contain(requestHeaders);
});

it('should configure instances without telemetry when "name" property is empty', function() {
var customTelemetry = { name: '', version: 'beta-01', env: { node: 'v10' } };
var client = new AuthenticationClient({
token: 'token',
domain: 'auth0.com',
clientInfo: customTelemetry
});

expect(client.oauth.oauth.options.headers).to.not.have.property('Auth0-Client');
expect(client.database.dbConnections.options.headers).to.not.have.property('Auth0-Client');
expect(client.passwordless.passwordless.options.headers).to.not.have.property('Auth0-Client');
expect(client.users.headers).to.not.have.property('Auth0-Client');
expect(client.tokens.headers).to.not.have.property('Auth0-Client');
});

it('should configure instances without telemetry header when disabled', function() {
var client = new AuthenticationClient({
token: 'token',
Expand Down
Loading

0 comments on commit 7ecdf16

Please sign in to comment.