@@ -19,6 +19,7 @@ import * as assert from 'assert';
1919import * as fs from 'fs' ;
2020import * as mocha from 'mocha' ;
2121import * as nock from 'nock' ;
22+ import * as path from 'path' ;
2223
2324import { StackdriverStatsExporter } from '../src/stackdriver-monitoring' ;
2425import { LabelDescriptor , MetricDescriptor , MetricKind , StackdriverExporterOptions , TimeSeries , ValueType } from '../src/types' ;
@@ -55,9 +56,11 @@ class ExporterTestLogger implements Logger {
5556 * Asserts MetricDescriptors' values given its originating view.
5657 * @param metricDescriptor The MetricDescriptor to be asserted.
5758 * @param view The originating view.
59+ * @param prefix Optional metric prefix.
5860 */
5961function assertMetricDescriptor (
60- metricDescriptor : MetricDescriptor , view : View ) {
62+ metricDescriptor : MetricDescriptor , view : View ,
63+ prefix : string = StackdriverStatsExporter . CUSTOM_OPENCENSUS_DOMAIN ) {
6164 let metricKind : MetricKind ;
6265 if ( view . aggregation === AggregationType . SUM ) {
6366 metricKind = MetricKind . CUMULATIVE ;
@@ -74,8 +77,7 @@ function assertMetricDescriptor(
7477 valueType = ValueType . INT64 ;
7578 }
7679
77- assert . strictEqual (
78- metricDescriptor . type , `custom.googleapis.com/${ view . name } ` ) ;
80+ assert . strictEqual ( metricDescriptor . type , `${ prefix } /${ view . name } ` ) ;
7981 assert . strictEqual ( metricDescriptor . description , view . description ) ;
8082 assert . strictEqual ( metricDescriptor . displayName , view . measure . name ) ;
8183 assert . strictEqual ( metricDescriptor . metricKind , metricKind ) ;
@@ -92,7 +94,8 @@ function assertMetricDescriptor(
9294 */
9395function assertTimeSeries (
9496 timeSeries : TimeSeries , view : View , measurement : Measurement ,
95- projectId : string ) {
97+ projectId : string ,
98+ prefix : string = StackdriverStatsExporter . CUSTOM_OPENCENSUS_DOMAIN ) {
9699 const resourceLabels : { [ key : string ] : string } = { project_id : projectId } ;
97100
98101 let metricKind : MetricKind ;
@@ -111,8 +114,7 @@ function assertTimeSeries(
111114 valueType = ValueType . INT64 ;
112115 }
113116
114- assert . strictEqual (
115- timeSeries . metric . type , `custom.googleapis.com/${ view . name } ` ) ;
117+ assert . strictEqual ( timeSeries . metric . type , `${ prefix } /${ view . name } ` ) ;
116118 assert . deepEqual ( timeSeries . metric . labels , measurement . tags ) ;
117119 assert . strictEqual ( timeSeries . resource . type , 'global' ) ;
118120 assert . ok ( timeSeries . resource . labels . project_id ) ;
@@ -306,6 +308,35 @@ describe('Stackdriver Stats Exporter', function() {
306308 } ) ;
307309 } ) ;
308310
311+ describe ( 'With metricPrefix option' , ( ) => {
312+ exporterOptions = Object . assign ( { metricPrefix : 'test' } , exporterOptions ) ;
313+ exporter = new StackdriverStatsExporter ( exporterOptions ) ;
314+ stats . registerExporter ( exporter ) ;
315+
316+ it ( `should be reflected when onRegisterView is called` , async ( ) => {
317+ if ( dryrun ) {
318+ nocks . metricDescriptors ( PROJECT_ID , null , null , false ) ;
319+ }
320+ await exporter . onRegisterView ( viewMetricDescriptor ) . then ( ( ) => {
321+ return assertMetricDescriptor (
322+ exporterTestLogger . debugBuffer [ 0 ] , viewMetricDescriptor ,
323+ exporterOptions . metricPrefix ) ;
324+ } ) ;
325+ } ) ;
326+
327+ it ( `should be reflected when onRecord is called` , async ( ) => {
328+ if ( dryrun ) {
329+ nocks . timeSeries ( PROJECT_ID , null , null , false ) ;
330+ }
331+ viewTimeSeries . recordMeasurement ( measurement ) ;
332+ await exporter . onRecord ( [ viewTimeSeries ] , measurement ) . then ( ( ) => {
333+ return assertTimeSeries (
334+ exporterTestLogger . debugBuffer [ 0 ] [ 0 ] , viewTimeSeries , measurement ,
335+ PROJECT_ID , exporterOptions . metricPrefix ) ;
336+ } ) ;
337+ } ) ;
338+ } ) ;
339+
309340 describe ( 'With no network connection' , ( ) => {
310341 it ( '.onRegisterView() Should fail by network error' , async ( ) => {
311342 nock ( 'https://monitoring.googleapis.com' )
0 commit comments