From 9108cbae50993c0b9193f44861b5ce8b7c379e4f Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Fri, 14 Jun 2019 09:13:08 -0700 Subject: [PATCH] feat: support API endpoint override --- package.json | 2 +- ts/src/config.ts | 12 ++++++++++++ ts/src/index.ts | 7 ++++--- ts/src/profiler.ts | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 52b23df2e..f08f730c7 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "license": "Apache-2.0", "dependencies": { - "@google-cloud/common": "^1.0.0", + "@google-cloud/common": "^2.0.0", "@types/console-log-level": "^1.4.0", "@types/semver": "^6.0.0", "console-log-level": "^1.4.0", diff --git a/ts/src/config.ts b/ts/src/config.ts index e4482423c..ec6849e0a 100644 --- a/ts/src/config.ts +++ b/ts/src/config.ts @@ -20,6 +20,12 @@ const parseDuration: (str: string) => number = require('parse-duration'); // Configuration for Profiler. export interface Config extends GoogleAuthOptions { + /** + * The API endpoint of the service used to make requests. + * Defaults to `cloudprofiler.googleapis.com`. + */ + apiEndpoint?: string; + // Cloud Console projectId to associate profiles with instead of one read // from VM metadata server. projectId?: string; @@ -148,6 +154,11 @@ export interface Config extends GoogleAuthOptions { // Interface for an initialized config. export interface ProfilerConfig extends GoogleAuthOptions { + /** + * The API endpoint of the service used to make requests. + * Defaults to `cloudprofiler.googleapis.com`. + */ + apiEndpoint?: string; projectId?: string; logLevel: number; serviceContext: {service: string; version?: string}; @@ -184,6 +195,7 @@ export const defaultConfig = { initialBackoffMillis: 60 * 1000, // 1 minute 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 diff --git a/ts/src/index.ts b/ts/src/index.ts index c72a2a6dc..0acccc209 100644 --- a/ts/src/index.ts +++ b/ts/src/index.ts @@ -96,6 +96,7 @@ 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'); @@ -154,17 +155,17 @@ export function nodeVersionOkay(version: string | SemVer): boolean { * needed. Returns a profiler if creation is successful. Otherwise, returns * rejected promise. */ -export async function createProfiler(config: Config): Promise { +export async function createProfiler(config: Config = {}): Promise { if (!nodeVersionOkay(process.version)) { throw new Error( `Could not start profiler: node version ${process.version}` + ` does not satisfies "${pjson.engines.node}"` + - '\nSee https://github.com/GoogleCloudPlatform/cloud-profiler-nodejs#prerequisites' + + '\nSee https://github.com/googleapis/cloud-profiler-nodejs#prerequisites' + ' for details.' ); } - let profilerConfig: ProfilerConfig = initConfigLocal(config); + let profilerConfig = initConfigLocal(config); // Start the heap profiler if profiler config does not indicate heap profiling // is disabled. This must be done before any asynchronous calls are made so diff --git a/ts/src/profiler.ts b/ts/src/profiler.ts index eedfee50d..69e34304e 100644 --- a/ts/src/profiler.ts +++ b/ts/src/profiler.ts @@ -285,8 +285,8 @@ export class Profiler extends ServiceObject { config: ProfilerConfig; constructor(config: ProfilerConfig) { - config = config || ({} as ProfilerConfig); const serviceConfig: ServiceConfig = { + apiEndpoint: config.apiEndpoint!, baseUrl: config.baseApiUrl, scopes: [SCOPE], packageJson: pjson,