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

Commit f68aa53

Browse files
yamadayukimayurkale22
authored andcommitted
Enable to receive credentials object in creating Stackdriver exporter (#541)
* Enable to receive credentials object in creating Stackdriver exporter * Add test suite with credentials option * Fix credentials field comment * Update CHANGELOG.md
1 parent f9b097d commit f68aa53

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
## Unreleased
66

77
- Exporter/Stats/Stackdriver: Add support for exemplar
8+
- exporter-stackdriver: Add support the credentials option used for authentication instead of your application default credentials
89

910
## 0.0.13 - 2019-05-20
1011
- Exporter/Stats/Prometheus: Fix missing tags for HTTP metrics

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616

1717
import {Exporter, ExporterBuffer, Span as OCSpan, SpanContext} from '@opencensus/core';
1818
import {logger, Logger} from '@opencensus/core';
19-
import {auth, JWT} from 'google-auth-library';
19+
import {auth as globalAuth, GoogleAuth, JWT} from 'google-auth-library';
2020
import {google} from 'googleapis';
2121
import {getDefaultResource} from './common-utils';
2222
import {createAttributes, createLinks, createTimeEvents, getResourceLabels, stringToTruncatableString} from './stackdriver-cloudtrace-utils';
2323
import {AttributeValue, Span, SpansWithCredentials, StackdriverExporterOptions} from './types';
2424

2525
google.options({headers: {'x-opencensus-outgoing-request': 0x1}});
2626
const cloudTrace = google.cloudtrace('v2');
27+
let auth = globalAuth;
2728

2829
/** Format and sends span information to Stackdriver */
2930
export class StackdriverTraceExporter implements Exporter {
@@ -39,6 +40,9 @@ export class StackdriverTraceExporter implements Exporter {
3940
this.exporterBuffer = new ExporterBuffer(this, options);
4041
this.RESOURCE_LABELS =
4142
getResourceLabels(getDefaultResource(this.projectId));
43+
if (options.credentials) {
44+
auth = new GoogleAuth({credentials: options.credentials});
45+
}
4246
}
4347

4448
/**

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import {logger, Logger, Measurement, Metric, MetricDescriptor as OCMetricDescriptor, MetricProducerManager, Metrics, StatsEventListener, TagKey, TagValue, version, View} from '@opencensus/core';
18-
import {auth, JWT} from 'google-auth-library';
18+
import {auth as globalAuth, GoogleAuth, JWT} from 'google-auth-library';
1919
import {google} from 'googleapis';
2020
import {getDefaultResource} from './common-utils';
2121
import {createMetricDescriptorData, createTimeSeriesList} from './stackdriver-monitoring-utils';
@@ -31,6 +31,7 @@ const OC_HEADER = {
3131

3232
google.options({headers: OC_HEADER});
3333
const monitoring = google.monitoring('v3');
34+
let auth = globalAuth;
3435

3536
/** Format and sends Stats to Stackdriver */
3637
export class StackdriverStatsExporter implements StatsEventListener {
@@ -63,6 +64,9 @@ export class StackdriverStatsExporter implements StatsEventListener {
6364
this.onMetricUploadError = options.onMetricUploadError;
6465
}
6566
this.DEFAULT_RESOURCE = getDefaultResource(this.projectId);
67+
if (options.credentials) {
68+
auth = new GoogleAuth({credentials: options.credentials});
69+
}
6670
}
6771

6872
/**

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import {Bucket, ExporterConfig} from '@opencensus/core';
18-
import {JWT} from 'google-auth-library';
18+
import {JWT, JWTInput} from 'google-auth-library';
1919

2020
export type Span = {
2121
name?: string,
@@ -151,6 +151,11 @@ export interface StackdriverExporterOptions extends ExporterConfig {
151151
* of a stackdriver metric. Optional
152152
*/
153153
prefix?: string;
154+
/**
155+
* If this field is set, its contents will be used for authentication
156+
* instead of your application default credentials. Optional
157+
*/
158+
credentials?: JWTInput;
154159

155160
/**
156161
* Is called whenever the exporter fails to upload metrics to stackdriver.

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import {CoreTracer, logger, Span as OCSpan, version} from '@opencensus/core';
1818
import * as assert from 'assert';
19+
import * as fs from 'fs';
1920
import * as nock from 'nock';
2021

2122
import {Span, StackdriverExporterOptions, StackdriverTraceExporter} from '../src/';
@@ -225,5 +226,68 @@ describe('Stackdriver Trace Exporter', function() {
225226
});
226227
});
227228
});
229+
230+
describe('with credentials option', () => {
231+
const FAKE_CREDENTIALS = JSON.parse(
232+
fs.readFileSync(__dirname + '/fixtures/fakecredentials.json')
233+
.toString());
234+
235+
before(() => {
236+
exporterOptions = {
237+
projectId: PROJECT_ID,
238+
bufferTimeout: 200,
239+
logger: testLogger,
240+
credentials: FAKE_CREDENTIALS,
241+
};
242+
});
243+
244+
it('should export traces to stackdriver', () => {
245+
return tracer.startRootSpan(
246+
{name: 'sdExportTestRootSpan'}, async (rootSpan: OCSpan) => {
247+
const span =
248+
tracer.startChildSpan({name: 'sdExportTestChildSpan'});
249+
250+
nocks.oauth2(body => true);
251+
nocks.batchWrite(PROJECT_ID, (body: {spans: Span[]}): boolean => {
252+
assert.strictEqual(body.spans.length, 2);
253+
const spans = body.spans;
254+
assert.strictEqual(spans[0].spanId, rootSpan.id);
255+
assert.strictEqual(spans[1].spanId, span.id);
256+
return true;
257+
});
258+
span.end();
259+
rootSpan.end();
260+
261+
return exporter.publish([rootSpan]).then(result => {
262+
assert.ok(result.indexOf('batchWriteSpans sucessfully') >= 0);
263+
});
264+
});
265+
});
266+
267+
it('should fail exporting by network error', async () => {
268+
nock('https://cloudtrace.googleapis.com')
269+
.intercept(
270+
'/v2/projects/' + exporterOptions.projectId +
271+
'/traces:batchWrite',
272+
'post')
273+
.reply(443, 'Simulated Network Error');
274+
275+
nocks.oauth2(body => true);
276+
277+
return tracer.startRootSpan(
278+
{name: 'sdErrorExportTestRootSpan'}, (rootSpan: OCSpan) => {
279+
const span =
280+
tracer.startChildSpan({name: 'sdErrorExportTestChildSpan'});
281+
span.end();
282+
rootSpan.end();
283+
284+
return exporter.publish([rootSpan]).then(result => {
285+
assert.ok(
286+
result.message.indexOf(
287+
'batchWriteSpans error: Simulated Network Error') >= 0);
288+
});
289+
});
290+
});
291+
});
228292
});
229293
});

0 commit comments

Comments
 (0)