Skip to content

Commit

Permalink
feat: support API endpoint override
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Jun 14, 2019
1 parent 094986e commit 9108cba
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions ts/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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<Profiler> {
export async function createProfiler(config: Config = {}): Promise<Profiler> {
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
Expand Down
2 changes: 1 addition & 1 deletion ts/src/profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9108cba

Please sign in to comment.