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

Commit 869ce34

Browse files
authored
Adds metric name prefix override for [exporter-stackdriver] (#151)
1 parent 0a49765 commit 869ce34

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import {AggregationType, Bucket, DistributionData, logger, Logger, Measurement, MeasureType, StatsEventListener, View} from '@opencensus/core';
1818
import {auth, JWT} from 'google-auth-library';
1919
import {google} from 'googleapis';
20+
import * as path from 'path';
2021

2122
import {Distribution, LabelDescriptor, MetricDescriptor, MetricKind, Point, StackdriverExporterOptions, TimeSeries, ValueType} from './types';
2223

@@ -27,12 +28,18 @@ const monitoring = google.monitoring('v3');
2728
export class StackdriverStatsExporter implements StatsEventListener {
2829
private delay: number;
2930
private projectId: string;
31+
private metricPrefix: string;
32+
static readonly CUSTOM_OPENCENSUS_DOMAIN: string =
33+
'custom.googleapis.com/opencensus';
3034
static readonly DELAY: number = 60000;
3135
logger: Logger;
3236

3337
constructor(options: StackdriverExporterOptions) {
34-
this.delay = options.delay || StackdriverStatsExporter.DELAY;
38+
this.delay =
39+
options.delay != null ? options.delay : StackdriverStatsExporter.DELAY;
3540
this.projectId = options.projectId;
41+
this.metricPrefix = options.metricPrefix ||
42+
StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN;
3643
this.logger = options.logger || logger.logger();
3744
}
3845

@@ -139,10 +146,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
139146
}
140147

141148
return {
142-
metric: {
143-
type: `custom.googleapis.com/${view.name}`,
144-
labels: measurement.tags
145-
},
149+
metric: {type: this.getMetricType(view.name), labels: measurement.tags},
146150
resource: {type: 'global', labels: resourceLabels},
147151
metricKind: this.createMetricKind(view.aggregation),
148152
valueType: this.createValueType(view),
@@ -183,6 +187,14 @@ export class StackdriverStatsExporter implements StatsEventListener {
183187
return buckets.map(bucket => bucket.count);
184188
}
185189

190+
/**
191+
* Gets metric type
192+
* @param name The view name
193+
*/
194+
private getMetricType(name: string): string {
195+
return path.join(this.metricPrefix, name);
196+
}
197+
186198
/**
187199
* Creates a Stackdriver LabelDescriptor from given Tags.
188200
* @param tag The Tags to get TimeSeries information from.
@@ -200,7 +212,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
200212
*/
201213
private createMetricDescriptorData(view: View): MetricDescriptor {
202214
return {
203-
type: `custom.googleapis.com/${view.name}`,
215+
type: this.getMetricType(view.name),
204216
description: view.description || view.measure.description,
205217
displayName: view.measure.name,
206218
metricKind: this.createMetricKind(view.aggregation),

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export interface StackdriverExporterOptions extends ExporterConfig {
4242
* projectId project id defined to stackdriver
4343
*/
4444
projectId: string;
45+
/**
46+
* Prefix for metric overrides the OpenCensus prefix
47+
* of a stackdriver metric. Optional
48+
*/
49+
metricPrefix?: string;
4550
}
4651

4752
export interface TracesWithCredentials {

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as assert from 'assert';
1919
import * as fs from 'fs';
2020
import * as mocha from 'mocha';
2121
import * as nock from 'nock';
22+
import * as path from 'path';
2223

2324
import {StackdriverStatsExporter} from '../src/stackdriver-monitoring';
2425
import {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
*/
5961
function 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
*/
9395
function 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

Comments
 (0)