Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 9632157

Browse files
authored
Remove createScopedRequired usage (#467)
1 parent 6f5c6b3 commit 9632157

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

packages/opencensus-exporter-stackdriver/src/stackdriver-cloudtrace.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,28 +149,21 @@ export class StackdriverTraceExporter implements Exporter {
149149
* authenticates the client and calls a method to send the spans data.
150150
* @param stackdriverSpans The spans to export
151151
*/
152-
private authorize(stackdriverSpans: Span[]) {
153-
return auth.getApplicationDefault()
154-
.then((client) => {
155-
let authClient = client.credential as JWT;
156-
157-
if (authClient.createScopedRequired &&
158-
authClient.createScopedRequired()) {
159-
const scopes = ['https://www.googleapis.com/auth/cloud-platform'];
160-
authClient = authClient.createScoped(scopes);
161-
}
162-
163-
const spans: SpansWithCredentials = {
164-
name: `projects/${this.projectId}`,
165-
resource: {spans: stackdriverSpans},
166-
auth: authClient
167-
};
168-
return spans;
169-
})
170-
.catch((err) => {
171-
err.message = `authorize error: ${err.message}`;
172-
this.logger.error(err.message);
173-
throw (err);
174-
});
152+
private async authorize(stackdriverSpans: Span[]):
153+
Promise<SpansWithCredentials> {
154+
try {
155+
const client = await auth.getClient(
156+
{scopes: ['https://www.googleapis.com/auth/cloud-platform']});
157+
158+
return {
159+
name: `projects/${this.projectId}`,
160+
resource: {spans: stackdriverSpans},
161+
auth: client as JWT
162+
};
163+
} catch (err) {
164+
err.message = `authorize error: ${err.message}`;
165+
this.logger.error(err.message);
166+
throw (err);
167+
}
175168
}
176169
}

packages/opencensus-exporter-stackdriver/src/stackdriver-monitoring.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ const OC_HEADER = {
3131

3232
google.options({headers: OC_HEADER});
3333
const monitoring = google.monitoring('v3');
34-
const GOOGLEAPIS_SCOPE = 'https://www.googleapis.com/auth/cloud-platform';
3534

3635
/** Format and sends Stats to Stackdriver */
3736
export class StackdriverStatsExporter implements StatsEventListener {
@@ -187,13 +186,21 @@ export class StackdriverStatsExporter implements StatsEventListener {
187186
};
188187

189188
return new Promise((resolve, reject) => {
190-
monitoring.projects.metricDescriptors.create(
191-
request, {headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT]},
192-
(err: Error|null) => {
193-
this.logger.debug('sent metric descriptor', request.resource);
194-
err ? reject(err) : resolve();
195-
});
196-
});
189+
monitoring.projects.metricDescriptors.create(
190+
request,
191+
{headers: OC_HEADER, userAgentDirectives: [OC_USER_AGENT]},
192+
(err: Error|null) => {
193+
this.logger.debug(
194+
'sent metric descriptor', request.resource);
195+
err ? reject(err) : resolve();
196+
});
197+
})
198+
.catch((err) => {
199+
this.logger.error(
200+
`StackdriverStatsExporter: Failed to write data: ${
201+
err.message}`);
202+
this.stop();
203+
});
197204
});
198205
}
199206

@@ -209,21 +216,10 @@ export class StackdriverStatsExporter implements StatsEventListener {
209216
* Gets the Google Application Credentials from the environment variables
210217
* and authenticates the client.
211218
*/
212-
private authorize(): Promise<JWT> {
213-
return auth.getApplicationDefault()
214-
.then((client) => {
215-
let authClient = client.credential as JWT;
216-
if (authClient.createScopedRequired &&
217-
authClient.createScopedRequired()) {
218-
const scopes = [GOOGLEAPIS_SCOPE];
219-
authClient = authClient.createScoped(scopes);
220-
}
221-
return authClient;
222-
})
223-
.catch((err) => {
224-
err.message = `authorize error: ${err.message}`;
225-
throw (err);
226-
});
219+
private async authorize(): Promise<JWT> {
220+
const client = await auth.getClient(
221+
{scopes: ['https://www.googleapis.com/auth/cloud-platform']});
222+
return client as JWT;
227223
}
228224

229225
// TODO(mayurkale): Deprecate onRegisterView and onRecord apis after

0 commit comments

Comments
 (0)