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

Commit

Permalink
Adds metric name prefix override for [exporter-stackdriver] (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaikevych authored Oct 18, 2018
1 parent 0a49765 commit 869ce34
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import {AggregationType, Bucket, DistributionData, logger, Logger, Measurement, MeasureType, StatsEventListener, View} from '@opencensus/core';
import {auth, JWT} from 'google-auth-library';
import {google} from 'googleapis';
import * as path from 'path';

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

Expand All @@ -27,12 +28,18 @@ const monitoring = google.monitoring('v3');
export class StackdriverStatsExporter implements StatsEventListener {
private delay: number;
private projectId: string;
private metricPrefix: string;
static readonly CUSTOM_OPENCENSUS_DOMAIN: string =
'custom.googleapis.com/opencensus';
static readonly DELAY: number = 60000;
logger: Logger;

constructor(options: StackdriverExporterOptions) {
this.delay = options.delay || StackdriverStatsExporter.DELAY;
this.delay =
options.delay != null ? options.delay : StackdriverStatsExporter.DELAY;
this.projectId = options.projectId;
this.metricPrefix = options.metricPrefix ||
StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN;
this.logger = options.logger || logger.logger();
}

Expand Down Expand Up @@ -139,10 +146,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
}

return {
metric: {
type: `custom.googleapis.com/${view.name}`,
labels: measurement.tags
},
metric: {type: this.getMetricType(view.name), labels: measurement.tags},
resource: {type: 'global', labels: resourceLabels},
metricKind: this.createMetricKind(view.aggregation),
valueType: this.createValueType(view),
Expand Down Expand Up @@ -183,6 +187,14 @@ export class StackdriverStatsExporter implements StatsEventListener {
return buckets.map(bucket => bucket.count);
}

/**
* Gets metric type
* @param name The view name
*/
private getMetricType(name: string): string {
return path.join(this.metricPrefix, name);
}

/**
* Creates a Stackdriver LabelDescriptor from given Tags.
* @param tag The Tags to get TimeSeries information from.
Expand All @@ -200,7 +212,7 @@ export class StackdriverStatsExporter implements StatsEventListener {
*/
private createMetricDescriptorData(view: View): MetricDescriptor {
return {
type: `custom.googleapis.com/${view.name}`,
type: this.getMetricType(view.name),
description: view.description || view.measure.description,
displayName: view.measure.name,
metricKind: this.createMetricKind(view.aggregation),
Expand Down
5 changes: 5 additions & 0 deletions packages/opencensus-exporter-stackdriver/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export interface StackdriverExporterOptions extends ExporterConfig {
* projectId project id defined to stackdriver
*/
projectId: string;
/**
* Prefix for metric overrides the OpenCensus prefix
* of a stackdriver metric. Optional
*/
metricPrefix?: string;
}

export interface TracesWithCredentials {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as assert from 'assert';
import * as fs from 'fs';
import * as mocha from 'mocha';
import * as nock from 'nock';
import * as path from 'path';

import {StackdriverStatsExporter} from '../src/stackdriver-monitoring';
import {LabelDescriptor, MetricDescriptor, MetricKind, StackdriverExporterOptions, TimeSeries, ValueType} from '../src/types';
Expand Down Expand Up @@ -55,9 +56,11 @@ class ExporterTestLogger implements Logger {
* Asserts MetricDescriptors' values given its originating view.
* @param metricDescriptor The MetricDescriptor to be asserted.
* @param view The originating view.
* @param prefix Optional metric prefix.
*/
function assertMetricDescriptor(
metricDescriptor: MetricDescriptor, view: View) {
metricDescriptor: MetricDescriptor, view: View,
prefix: string = StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN) {
let metricKind: MetricKind;
if (view.aggregation === AggregationType.SUM) {
metricKind = MetricKind.CUMULATIVE;
Expand All @@ -74,8 +77,7 @@ function assertMetricDescriptor(
valueType = ValueType.INT64;
}

assert.strictEqual(
metricDescriptor.type, `custom.googleapis.com/${view.name}`);
assert.strictEqual(metricDescriptor.type, `${prefix}/${view.name}`);
assert.strictEqual(metricDescriptor.description, view.description);
assert.strictEqual(metricDescriptor.displayName, view.measure.name);
assert.strictEqual(metricDescriptor.metricKind, metricKind);
Expand All @@ -92,7 +94,8 @@ function assertMetricDescriptor(
*/
function assertTimeSeries(
timeSeries: TimeSeries, view: View, measurement: Measurement,
projectId: string) {
projectId: string,
prefix: string = StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN) {
const resourceLabels: {[key: string]: string} = {project_id: projectId};

let metricKind: MetricKind;
Expand All @@ -111,8 +114,7 @@ function assertTimeSeries(
valueType = ValueType.INT64;
}

assert.strictEqual(
timeSeries.metric.type, `custom.googleapis.com/${view.name}`);
assert.strictEqual(timeSeries.metric.type, `${prefix}/${view.name}`);
assert.deepEqual(timeSeries.metric.labels, measurement.tags);
assert.strictEqual(timeSeries.resource.type, 'global');
assert.ok(timeSeries.resource.labels.project_id);
Expand Down Expand Up @@ -306,6 +308,35 @@ describe('Stackdriver Stats Exporter', function() {
});
});

describe('With metricPrefix option', () => {
exporterOptions = Object.assign({metricPrefix: 'test'}, exporterOptions);
exporter = new StackdriverStatsExporter(exporterOptions);
stats.registerExporter(exporter);

it(`should be reflected when onRegisterView is called`, async () => {
if (dryrun) {
nocks.metricDescriptors(PROJECT_ID, null, null, false);
}
await exporter.onRegisterView(viewMetricDescriptor).then(() => {
return assertMetricDescriptor(
exporterTestLogger.debugBuffer[0], viewMetricDescriptor,
exporterOptions.metricPrefix);
});
});

it(`should be reflected when onRecord is called`, async () => {
if (dryrun) {
nocks.timeSeries(PROJECT_ID, null, null, false);
}
viewTimeSeries.recordMeasurement(measurement);
await exporter.onRecord([viewTimeSeries], measurement).then(() => {
return assertTimeSeries(
exporterTestLogger.debugBuffer[0][0], viewTimeSeries, measurement,
PROJECT_ID, exporterOptions.metricPrefix);
});
});
});

describe('With no network connection', () => {
it('.onRegisterView() Should fail by network error', async () => {
nock('https://monitoring.googleapis.com')
Expand Down

0 comments on commit 869ce34

Please sign in to comment.