Skip to content

Commit

Permalink
update common
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanmar511 committed Jun 26, 2019
1 parent 619b857 commit 75ecc4f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
8 changes: 1 addition & 7 deletions ts/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ export interface Config extends GoogleAuthOptions {
// https://nodejs.org/dist/latest-v9.x/docs/api/timers.html#timers_settimeout_callback_delay_args.
serverBackoffCapMillis?: number;

// Allows user to specify API URL other than
// https://cloudprofiler.googleapis.com/v2.
baseApiUrl?: string;

// Time between profile collection.
// For testing with startLocal() only.
localProfilingPeriodMillis?: number;
Expand Down Expand Up @@ -158,7 +154,7 @@ export interface ProfilerConfig extends GoogleAuthOptions {
* The API endpoint of the service used to make requests.
* Defaults to `cloudprofiler.googleapis.com`.
*/
apiEndpoint?: string;
apiEndpoint: string;
projectId?: string;
logLevel: number;
serviceContext: {service: string; version?: string};
Expand All @@ -174,7 +170,6 @@ export interface ProfilerConfig extends GoogleAuthOptions {
backoffCapMillis: number;
backoffMultiplier: number;
serverBackoffCapMillis: number;
baseApiUrl: string;
localProfilingPeriodMillis: number;
localLogPeriodMillis: number;
localTimeDurationMillis: number;
Expand All @@ -196,7 +191,6 @@ export const defaultConfig = {
backoffCapMillis: parseDuration('1h'),
backoffMultiplier: 1.3,
apiEndpoint: 'cloudprofiler.googleapis.com',
baseApiUrl: 'https://cloudprofiler.googleapis.com/v2',

// This is the largest duration for setTimeout which does not cause it to
// run immediately.
Expand Down
2 changes: 1 addition & 1 deletion ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ function initConfigLocal(config: Config): ProfilerConfig {
}

const mergedConfig = extend(true, {}, defaultConfig, mergedUserConfigs);
mergedConfig.baseApiUrl = `https://${mergedConfig.apiEndpoint}/v2`;

if (!hasService(mergedConfig)) {
throw new Error('Service must be specified in the configuration');
Expand Down Expand Up @@ -195,6 +194,7 @@ export async function createProfiler(config: Config = {}): Promise<Profiler> {
*
*/
export async function start(config: Config = {}): Promise<void> {
console.log('STARTING PROFILER')
let profiler: Profiler;
try {
profiler = await createProfiler(config);
Expand Down
10 changes: 7 additions & 3 deletions ts/src/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,17 @@ export class Profiler extends ServiceObject {
private profileTypes: string[];
private retryer: Retryer;
private sourceMapper: SourceMapper | undefined;
private baseApiUrl: string;

// Public for testing.
config: ProfilerConfig;

constructor(config: ProfilerConfig) {
config = config || ({} as ProfilerConfig);
const baseApiUrl = `https://${config.apiEndpoint}/v2`;
const serviceConfig: ServiceConfig = {
apiEndpoint: config.apiEndpoint!,
baseUrl: config.baseApiUrl,
apiEndpoint: config.apiEndpoint,
baseUrl: baseApiUrl,
scopes: [SCOPE],
packageJson: pjson,
};
Expand All @@ -296,6 +299,7 @@ export class Profiler extends ServiceObject {
baseUrl: '/',
});
this.config = config;
this.baseApiUrl = baseApiUrl;

this.logger = createLogger(this.config.logLevel);

Expand Down Expand Up @@ -471,7 +475,7 @@ export class Profiler extends ServiceObject {
}
const options = {
method: 'PATCH',
uri: this.config.baseApiUrl + '/' + prof.name,
uri: this.baseApiUrl + '/' + prof.name,
body: prof,
json: true,
};
Expand Down
10 changes: 5 additions & 5 deletions ts/test/test-init-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ describe('createProfiler', () => {
localProfilingPeriodMillis: 1000,
localTimeDurationMillis: 1000,
localLogPeriodMillis: 10000,
baseApiUrl: 'https://cloudprofiler.googleapis.com/v2',
apiEndpoint: 'cloudprofiler.googleapis.com',
};
const sourceMapSearchPath: string[] = [];

const disableSourceMapParams = {
sourceMapSearchPath: ['path'],
disableSourceMaps: true,
Expand Down Expand Up @@ -342,14 +342,14 @@ describe('createProfiler', () => {
}
});

it('should set baseApiUrl to non-default value', async () => {
it('should set apiEndpoint to non-default value', async () => {
metadataStub = sinon.stub(gcpMetadata, 'instance');
metadataStub.throwsException('cannot access metadata');

const config = Object.assign(
{
apiEndpoint: 'test-cloudprofiler.sandbox.googleapis.com',
serviceContext: {version: '', service: 'fake-service'},
baseApiUrl: 'https://test-cloudprofiler.sandbox.googleapis.com/v2',
},
disableSourceMapParams
);
Expand All @@ -358,7 +358,7 @@ describe('createProfiler', () => {
disableHeap: false,
disableTime: false,
logLevel: 2,
baseApiUrl: 'https://test-cloudprofiler.sandbox.googleapis.com/v2',
apiEndpoint: 'test-cloudprofiler.sandbox.googleapis.com',
};
const expConfig = Object.assign(
{},
Expand Down
27 changes: 15 additions & 12 deletions ts/test/test-profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ const parseDuration: (str: string) => number = require('parse-duration');

const fakeCredentials = require('../../ts/test/fixtures/gcloud-credentials.json');

const API = 'https://cloudprofiler.googleapis.com/v2';
const TEST_API = 'https://test-cloudprofiler.sandbox.googleapis.com/v2';
const API = 'cloudprofiler.googleapis.com';
const TEST_API = 'test-cloudprofiler.sandbox.googleapis.com';

const FULL_API = `https://${API}/v2`;
const FULL_TEST_API = `https://${TEST_API}/v2`;

const testConfig: ProfilerConfig = {
projectId: 'test-projectId',
Expand All @@ -62,12 +65,12 @@ const testConfig: ProfilerConfig = {
backoffCapMillis: parseDuration('1h'),
backoffMultiplier: 1.3,
serverBackoffCapMillis: parseDuration('7d'),
baseApiUrl: API,
localProfilingPeriodMillis: 1000,
localTimeDurationMillis: 1000,
localLogPeriodMillis: 1000,
sourceMapSearchPath: [],
disableSourceMaps: true,
apiEndpoint: API,
};

nock.disableNetConnect();
Expand Down Expand Up @@ -391,7 +394,7 @@ describe('Profiler', () => {
labels: {instance: 'test-instance'},
};
nockOauth2();
const apiMock = nock(API)
const apiMock = nock(FULL_API)
.patch('/' + requestProf.name)
.once()
.reply(200);
Expand All @@ -407,12 +410,12 @@ describe('Profiler', () => {
labels: {instance: 'test-instance'},
};
nockOauth2();
const apiMock = nock(TEST_API)
const apiMock = nock(FULL_TEST_API)
.patch('/' + requestProf.name)
.once()
.reply(200);
const config = extend(true, {}, testConfig);
config.baseApiUrl = TEST_API;
config.apiEndpoint = TEST_API;
const profiler = new Profiler(config);
await profiler.profileAndUpload(requestProf);
assert.strictEqual(apiMock.isDone(), true, 'completed call to test API');
Expand Down Expand Up @@ -442,7 +445,7 @@ describe('Profiler', () => {
labels: {version: config.serviceContext.version},
};
nockOauth2();
const requestProfileMock = nock(API)
const requestProfileMock = nock(FULL_API)
.post('/projects/' + testConfig.projectId + '/profiles')
.once()
.reply(200, response);
Expand All @@ -454,7 +457,7 @@ describe('Profiler', () => {
it('should successfully create profile using non-default api', async () => {
const config = extend(true, {}, testConfig);
config.disableHeap = true;
config.baseApiUrl = TEST_API;
config.apiEndpoint = TEST_API;
const response = {
name: 'projects/12345678901/test-projectId',
profileType: 'WALL',
Expand All @@ -467,7 +470,7 @@ describe('Profiler', () => {
labels: {version: config.serviceContext.version},
};
nockOauth2();
const requestProfileMock = nock(TEST_API)
const requestProfileMock = nock(FULL_TEST_API)
.post('/projects/' + config.projectId + '/profiles')
.once()
.reply(200, response);
Expand All @@ -490,7 +493,7 @@ describe('Profiler', () => {
labels: {version: config.serviceContext.version},
};
nockOauth2();
const requestProfileMock = nock(API)
const requestProfileMock = nock(FULL_API)
.post('/projects/' + testConfig.projectId + '/profiles')
.once()
.reply(200, response);
Expand All @@ -504,7 +507,7 @@ describe('Profiler', () => {
config.disableHeap = true;
const response = {name: 'projects/12345678901/test-projectId'};
nockOauth2();
const requestProfileMock = nock(API)
const requestProfileMock = nock(FULL_API)
.post('/projects/' + testConfig.projectId + '/profiles')
.once()
.reply(200, response);
Expand Down Expand Up @@ -597,7 +600,7 @@ describe('Profiler', () => {
additionalField: 'additionalField',
};
nockOauth2();
const requestProfileMock = nock(API)
const requestProfileMock = nock(FULL_API)
.post('/projects/' + testConfig.projectId + '/profiles')
.once()
.reply(200, response);
Expand Down

0 comments on commit 75ecc4f

Please sign in to comment.