diff --git a/packages/opencensus-exporter-stackdriver/src/stackdriver-stats-utils.ts b/packages/opencensus-exporter-stackdriver/src/stackdriver-stats-utils.ts index ed206d5bc..a23ef6cae 100644 --- a/packages/opencensus-exporter-stackdriver/src/stackdriver-stats-utils.ts +++ b/packages/opencensus-exporter-stackdriver/src/stackdriver-stats-utils.ts @@ -17,272 +17,226 @@ import {BucketOptions, DistributionBucket, DistributionValue, LabelKey, LabelValue, Metric, MetricDescriptor as OCMetricDescriptor, MetricDescriptorType, TimeSeriesPoint, Timestamp} from '@opencensus/core'; import * as os from 'os'; import * as path from 'path'; -import {Distribution, LabelDescriptor, MetricDescriptor, MetricKind, Point, TimeSeries, ValueType} from './types'; + +import {Distribution, LabelDescriptor, MetricDescriptor, MetricKind, MonitoredResource, Point, TimeSeries, ValueType} from './types'; + +const OPENCENSUS_TASK = 'opencensus_task'; +const OPENCENSUS_TASK_DESCRIPTION = 'Opencensus task identifier'; +export const OPENCENSUS_TASK_VALUE_DEFAULT = generateDefaultTaskValue(); + +/** Converts a OpenCensus MetricDescriptor to a StackDriver MetricDescriptor. */ +export function createMetricDescriptorData( + metricDescriptor: OCMetricDescriptor, metricPrefix: string, + displayNamePrefix: string): MetricDescriptor { + return { + type: getMetricType(metricDescriptor.name, metricPrefix), + description: metricDescriptor.description, + displayName: createDisplayName(metricDescriptor.name, displayNamePrefix), + metricKind: createMetricKind(metricDescriptor.type), + valueType: createValueType(metricDescriptor.type), + unit: metricDescriptor.unit, + labels: createLabelDescriptor(metricDescriptor.labelKeys) + }; +} /** - * Util methods to convert OpenCensus Metrics data models to StackDriver - * monitoring data models. + * Converts metric's timeseries to a list of TimeSeries, so that metric can be + * uploaded to StackDriver. */ -export class StackdriverStatsExporterUtils { - static readonly OPENCENSUS_TASK: string = 'opencensus_task'; - static readonly OPENCENSUS_TASK_DESCRIPTION: string = - 'Opencensus task identifier'; - static readonly OPENCENSUS_TASK_VALUE_DEFAULT = - StackdriverStatsExporterUtils.generateDefaultTaskValue(); - - /** - * Returns a task label value in the format of 'nodejs-@'. - */ - static generateDefaultTaskValue(): string { - const pid = process.pid; - const hostname = os.hostname() || 'localhost'; - return 'nodejs-' + pid + '@' + hostname; - } - - /** - * Converts a OpenCensus MetricDescriptor to a StackDriver MetricDescriptor. - * @param metricDescriptor The OpenCensus MetricDescriptor. - */ - static createMetricDescriptorData( - metricDescriptor: OCMetricDescriptor, metricPrefix: string, - displayNamePrefix: string): MetricDescriptor { - return { - type: this.getMetricType(metricDescriptor.name, metricPrefix), - description: metricDescriptor.description, - displayName: - this.createDisplayName(metricDescriptor.name, displayNamePrefix), - metricKind: this.createMetricKind(metricDescriptor.type), - valueType: this.createValueType(metricDescriptor.type), - unit: metricDescriptor.unit, - labels: this.createLabelDescriptor(metricDescriptor.labelKeys) - } as MetricDescriptor; +export function createTimeSeriesList( + metric: Metric, monitoredResource: MonitoredResource, + metricPrefix: string): TimeSeries[] { + const timeSeriesList: TimeSeries[] = []; + + // TODO(mayurkale): Use Resource API here, once available (PR#173) + const metricDescriptor = metric.descriptor; + const metricKind = createMetricKind(metricDescriptor.type); + const valueType = createValueType(metricDescriptor.type); + + for (const timeSeries of metric.timeseries) { + timeSeriesList.push({ + metric: + createMetric(metricDescriptor, timeSeries.labelValues, metricPrefix), + resource: monitoredResource, + metricKind, + valueType, + points: timeSeries.points.map(point => { + return createPoint(point, timeSeries.startTimestamp, valueType); + }) + }); } + return timeSeriesList; +} - /** - * Converts metric's timeseries to a list of TimeSeries, so that metric can be - * uploaded to StackDriver. - * @param metric The metric to get TimeSeries information from. - */ - static createTimeSeriesList( - metric: Metric, - monitoredResource: {type: string, labels: {[key: string]: string}}, - metricPrefix: string): TimeSeries[] { - const timeSeriesList: TimeSeries[] = []; - - // TODO(mayurkale): Use Resource API here, once available (PR#173) - const metricDescriptor = metric.descriptor; - const metricKind = this.createMetricKind(metricDescriptor.type); - const valueType = this.createValueType(metricDescriptor.type); +/** Creates Metric type. */ +export function getMetricType(name: string, metricPrefix: string): string { + return path.join(metricPrefix, name); +} - for (const timeSeries of metric.timeseries) { - timeSeriesList.push({ - metric: this.createMetric( - metricDescriptor, timeSeries.labelValues, metricPrefix), - resource: monitoredResource, - metricKind, - valueType, - points: timeSeries.points.map(point => { - return this.createPoint(point, timeSeries.startTimestamp, valueType); - }) - }); - } - return timeSeriesList; - } +/** Creates Metric display name. */ +export function createDisplayName( + name: string, displayNamePrefix: string): string { + return path.join(displayNamePrefix, name); +} - /** - * Gets metric type. - * @param name The metric name. - * @param metricPrefix The metric prefix. - */ - static getMetricType(name: string, metricPrefix: string): string { - return path.join(metricPrefix, name); +/** Converts a OpenCensus Type to a StackDriver MetricKind. */ +export function createMetricKind(metricDescriptorType: MetricDescriptorType): + MetricKind { + if (metricDescriptorType === MetricDescriptorType.GAUGE_INT64 || + metricDescriptorType === MetricDescriptorType.GAUGE_DOUBLE) { + return MetricKind.GAUGE; + } else if ( + metricDescriptorType === MetricDescriptorType.CUMULATIVE_INT64 || + metricDescriptorType === MetricDescriptorType.CUMULATIVE_DOUBLE || + metricDescriptorType === MetricDescriptorType.CUMULATIVE_DISTRIBUTION) { + return MetricKind.CUMULATIVE; } + return MetricKind.UNSPECIFIED; +} - /** - * Creates Metric display name. - * @param name The metric name - * @param displayNamePrefix The metric display name. - */ - static createDisplayName(name: string, displayNamePrefix: string): string { - return path.join(displayNamePrefix, name); +/** Converts a OpenCensus Type to a StackDriver ValueType. */ +export function createValueType(metricDescriptorType: MetricDescriptorType): + ValueType { + if (metricDescriptorType === MetricDescriptorType.CUMULATIVE_DOUBLE || + metricDescriptorType === MetricDescriptorType.GAUGE_DOUBLE) { + return ValueType.DOUBLE; + } else if ( + metricDescriptorType === MetricDescriptorType.GAUGE_INT64 || + metricDescriptorType === MetricDescriptorType.CUMULATIVE_INT64) { + return ValueType.INT64; + } else if ( + metricDescriptorType === MetricDescriptorType.GAUGE_DISTRIBUTION || + metricDescriptorType === MetricDescriptorType.CUMULATIVE_DISTRIBUTION) { + return ValueType.DISTRIBUTION; + } else { + return ValueType.VALUE_TYPE_UNSPECIFIED; } +} - /** - * Converts a OpenCensus Type to a StackDriver MetricKind. - * @param metricDescriptorType The kind of metric. - */ - static createMetricKind(metricDescriptorType: MetricDescriptorType): - MetricKind { - if (metricDescriptorType === MetricDescriptorType.GAUGE_INT64 || - metricDescriptorType === MetricDescriptorType.GAUGE_DOUBLE) { - return MetricKind.GAUGE; - } else if ( - metricDescriptorType === MetricDescriptorType.CUMULATIVE_INT64 || - metricDescriptorType === MetricDescriptorType.CUMULATIVE_DOUBLE || - metricDescriptorType === MetricDescriptorType.CUMULATIVE_DISTRIBUTION) { - return MetricKind.CUMULATIVE; - } - return MetricKind.UNSPECIFIED; - } +/** Constructs a LabelDescriptor from a LabelKey. */ +export function createLabelDescriptor(labelKeys: LabelKey[]): + LabelDescriptor[] { + const labelDescriptorList: LabelDescriptor[] = + labelKeys.map(labelKey => ({ + key: labelKey.key, + valueType: 'STRING', // Now we only support String type. + description: labelKey.description + })); + + // add default "opencensus_task" label. + labelDescriptorList.push({ + key: OPENCENSUS_TASK, + valueType: 'STRING', + description: OPENCENSUS_TASK_DESCRIPTION + }); + return labelDescriptorList; +} - /** - * Converts a OpenCensus Type to a StackDriver ValueType. - * @param metricDescriptorType The kind of metric. - */ - static createValueType(metricDescriptorType: MetricDescriptorType): - ValueType { - if (metricDescriptorType === MetricDescriptorType.CUMULATIVE_DOUBLE || - metricDescriptorType === MetricDescriptorType.GAUGE_DOUBLE) { - return ValueType.DOUBLE; - } else if ( - metricDescriptorType === MetricDescriptorType.GAUGE_INT64 || - metricDescriptorType === MetricDescriptorType.CUMULATIVE_INT64) { - return ValueType.INT64; - } else if ( - metricDescriptorType === MetricDescriptorType.GAUGE_DISTRIBUTION || - metricDescriptorType === MetricDescriptorType.CUMULATIVE_DISTRIBUTION) { - return ValueType.DISTRIBUTION; +/** Creates a Metric using the LabelKeys and LabelValues. */ +export function createMetric( + metricDescriptor: OCMetricDescriptor, labelValues: LabelValue[], + metricPrefix: string): {type: string; labels: {[key: string]: string};} { + const type = getMetricType(metricDescriptor.name, metricPrefix); + const labels: {[key: string]: string} = {}; + for (let i = 0; i < labelValues.length; i++) { + const value = labelValues[i].value; + if (value && metricDescriptor.labelKeys[i]) { + labels[metricDescriptor.labelKeys[i].key] = value; } else { - return ValueType.VALUE_TYPE_UNSPECIFIED; - } - } - - /** - * Constructs a LabelDescriptor from a LabelKey. - * @param tag The Tags to get TimeSeries information from. - */ - static createLabelDescriptor(labelKeys: LabelKey[]): LabelDescriptor[] { - const labelDescriptorList = labelKeys.map(labelKey => { - return { - key: labelKey.key, - valueType: 'STRING', // Now we only support String type. - description: labelKey.description - } as LabelDescriptor; - }); - - // add default "opencensus_task" label. - labelDescriptorList.push({ - key: StackdriverStatsExporterUtils.OPENCENSUS_TASK, - valueType: 'STRING', - description: StackdriverStatsExporterUtils.OPENCENSUS_TASK_DESCRIPTION - }); - return labelDescriptorList; - } - - /** - * Creates a Metric using the LabelKeys and LabelValues. - * @param metricDescriptor The OpenCensus MetricDescriptor. - * @param labelValues The OpenCensus LabelValue. - */ - static createMetric( - metricDescriptor: OCMetricDescriptor, labelValues: LabelValue[], - metricPrefix: string): {type: string; labels: {[key: string]: string};} { - const type = this.getMetricType(metricDescriptor.name, metricPrefix); - const labels: {[key: string]: string} = {}; - for (let i = 0; i < labelValues.length; i++) { - const value = labelValues[i].value; - if (value && metricDescriptor.labelKeys[i]) { - labels[metricDescriptor.labelKeys[i].key] = value; - } else { - // TODO(mayurkale) : consider to throw an error when LabelValue and - // LabelKey lengths are not same. - } + // TODO(mayurkale) : consider to throw an error when LabelValue and + // LabelKey lengths are not same. } - labels[StackdriverStatsExporterUtils.OPENCENSUS_TASK] = - StackdriverStatsExporterUtils.OPENCENSUS_TASK_VALUE_DEFAULT; - return {type, labels}; } + labels[OPENCENSUS_TASK] = OPENCENSUS_TASK_VALUE_DEFAULT; + return {type, labels}; +} - /** - * Converts timeseries's point, so that metric can be uploaded to StackDriver. - * @param point The timestamped measurement. - * @param startTimeStamp The start timestamp of timeseries. - * @param valueType The ValueType of metric. - */ - static createPoint( - point: TimeSeriesPoint, startTimeStamp: Timestamp, - valueType: ValueType): Point { - let value; - if (valueType === ValueType.INT64) { - value = {int64Value: point.value as number}; - } else if (valueType === ValueType.DOUBLE) { - value = {doubleValue: point.value as number}; - } else if (valueType === ValueType.DISTRIBUTION) { - value = { - distributionValue: - this.createDistribution(point.value as DistributionValue) - }; - } else { - // console.log(`${valueType} is not supported.`); - } - - const endTime = this.toISOString(point.timestamp); - if (startTimeStamp) { - // Must be present for cumulative metrics. - const startTime = this.toISOString(startTimeStamp); - return {interval: {startTime, endTime}, value}; - } - - return {interval: {endTime}, value}; +/** + * Converts timeseries's point, so that metric can be uploaded to StackDriver. + */ +export function createPoint( + point: TimeSeriesPoint, startTimeStamp: Timestamp, + valueType: ValueType): Point { + const value = createValue(valueType, point); + const endTime = toISOString(point.timestamp); + if (startTimeStamp) { + // Must be present for cumulative metrics. + const startTime = toISOString(startTimeStamp); + return {interval: {startTime, endTime}, value}; } + return {interval: {endTime}, value}; +} - /** - * Formats an OpenCensus Distribution to Stackdriver's format. - * @param distribution The OpenCensus Distribution Data. - */ - static createDistribution(distribution: DistributionValue): Distribution { +/** Converts a OpenCensus Point's value to a StackDriver Point value. */ +export function createValue(valueType: ValueType, point: TimeSeriesPoint) { + if (valueType === ValueType.INT64) { + return {int64Value: point.value as number}; + } else if (valueType === ValueType.DOUBLE) { + return {doubleValue: point.value as number}; + } else if (valueType === ValueType.DISTRIBUTION) { return { - count: distribution.count, - mean: distribution.count === 0 ? 0 : - distribution.sum / distribution.count, - sumOfSquaredDeviation: distribution.sumOfSquaredDeviation, - bucketOptions: { - explicitBuckets: { - bounds: this.createExplicitBucketOptions(distribution.bucketOptions) - } - }, - bucketCounts: this.createBucketCounts(distribution.buckets) + distributionValue: createDistribution(point.value as DistributionValue) }; } + throw Error(`unsupported value type: ${valueType}`); +} - /** - * Converts a OpenCensus BucketOptions to a StackDriver BucketOptions. - * @param buckets The DistributionValue BucketOptions. - */ - static createExplicitBucketOptions(bucketOptions: BucketOptions): number[] { - const explicitBucketOptions: number[] = []; - // The first bucket bound should be 0.0 because the Metrics first bucket is - // [0, first_bound) but Stackdriver monitoring bucket bounds begin with - // -infinity (first bucket is (-infinity, 0)) - explicitBucketOptions.push(0); - return explicitBucketOptions.concat(bucketOptions.explicit.bounds); - } +/** Formats an OpenCensus Distribution to Stackdriver's format. */ +export function createDistribution(distribution: DistributionValue): + Distribution { + return { + count: distribution.count, + mean: distribution.count === 0 ? 0 : distribution.sum / distribution.count, + sumOfSquaredDeviation: distribution.sumOfSquaredDeviation, + bucketOptions: { + explicitBuckets: + {bounds: createExplicitBucketOptions(distribution.bucketOptions)} + }, + bucketCounts: createBucketCounts(distribution.buckets) + }; +} - /** - * Converts a OpenCensus Buckets to a list of counts. - * @param buckets The DistributionValue buckets. - */ - static createBucketCounts(buckets: DistributionBucket[]): number[] { - const bucketCounts: number[] = []; - // The first bucket (underflow bucket) should always be 0 count because the - // Metrics first bucket is [0, first_bound) but StackDriver distribution - // consists of an underflow bucket (number 0). - bucketCounts.push(0); - buckets.map((bucket: DistributionBucket) => { - bucketCounts.push(bucket.count); - }); - return bucketCounts; - } +/** Converts a OpenCensus BucketOptions to a StackDriver BucketOptions. */ +export function createExplicitBucketOptions(bucketOptions: BucketOptions): + number[] { + const explicitBucketOptions: number[] = []; + // The first bucket bound should be 0.0 because the Metrics first bucket is + // [0, first_bound) but Stackdriver monitoring bucket bounds begin with + // -infinity (first bucket is (-infinity, 0)) + explicitBucketOptions.push(0); + return explicitBucketOptions.concat(bucketOptions.explicit.bounds); +} - private static toISOString(timestamp: Timestamp) { - const str = new Date(timestamp.seconds * 1000).toISOString(); - const nsStr = `${this.padNS(timestamp.nanos)}`.replace(/0+$/, ''); - return str.replace('000Z', `${nsStr}Z`); - } +/** Converts a OpenCensus Buckets to a list of counts. */ +export function createBucketCounts(buckets: DistributionBucket[]): number[] { + const bucketCounts: number[] = []; + // The first bucket (underflow bucket) should always be 0 count because the + // Metrics first bucket is [0, first_bound) but StackDriver distribution + // consists of an underflow bucket (number 0). + bucketCounts.push(0); + buckets.map((bucket: DistributionBucket) => { + bucketCounts.push(bucket.count); + }); + return bucketCounts; +} - private static padNS(ns: number) { - const str = `${ns}`; - const pad = '000000000'.substring(0, 9 - str.length); - return `${pad}${str}`; - } +/** Returns a task label value in the format of 'nodejs-@'. */ +function generateDefaultTaskValue(): string { + console.log('inside generateDefaultTaskValue'); + const pid = process.pid; + const hostname = os.hostname() || 'localhost'; + return 'nodejs-' + pid + '@' + hostname; +} + +function toISOString(timestamp: Timestamp) { + const str = new Date(timestamp.seconds * 1000).toISOString(); + const nsStr = `${leftZeroPad(timestamp.nanos)}`.replace(/0+$/, ''); + return str.replace('000Z', `${nsStr}Z`); +} + +/** Pad a number with 0 on the left */ +function leftZeroPad(ns: number) { + const str = `${ns}`; + const pad = '000000000'.substring(0, 9 - str.length); + return `${pad}${str}`; } diff --git a/packages/opencensus-exporter-stackdriver/src/types.ts b/packages/opencensus-exporter-stackdriver/src/types.ts index 83e035b75..bb3e37883 100644 --- a/packages/opencensus-exporter-stackdriver/src/types.ts +++ b/packages/opencensus-exporter-stackdriver/src/types.ts @@ -115,8 +115,17 @@ export interface Point { export interface TimeSeries { metric: {type: string; labels: {[key: string]: string};}; - resource: {type: string, labels: {[key: string]: string}}; + resource: MonitoredResource; metricKind: MetricKind; valueType: ValueType; points: Point[]; } + +/** Resource information. */ +export interface MonitoredResource { + /** Type identifier for the resource. */ + type: string; + + /** Set of labels that describe the resource. */ + labels: {[key: string]: string}; +} diff --git a/packages/opencensus-exporter-stackdriver/test/test-stackdriver-stats-utils.ts b/packages/opencensus-exporter-stackdriver/test/test-stackdriver-stats-utils.ts index 14e7845e2..848d4da11 100644 --- a/packages/opencensus-exporter-stackdriver/test/test-stackdriver-stats-utils.ts +++ b/packages/opencensus-exporter-stackdriver/test/test-stackdriver-stats-utils.ts @@ -16,8 +16,9 @@ import {DistributionValue, LabelKey, LabelValue, MetricDescriptor as OCMetricDescriptor, MetricDescriptorType, TimeSeriesPoint, Timestamp} from '@opencensus/core'; import * as assert from 'assert'; + import {StackdriverStatsExporter} from '../src/stackdriver-monitoring'; -import {StackdriverStatsExporterUtils} from '../src/stackdriver-stats-utils'; +import {createDisplayName, createDistribution, createLabelDescriptor, createMetric, createMetricDescriptorData, createMetricKind, createPoint, createTimeSeriesList, createValueType, getMetricType, OPENCENSUS_TASK_VALUE_DEFAULT} from '../src/stackdriver-stats-utils'; import {Distribution, MetricDescriptor, MetricKind, ValueType} from '../src/types'; const METRIC_NAME = 'metric-name'; @@ -28,20 +29,15 @@ describe('Stackdriver Stats Exporter Utils', () => { describe('createMetricKind()', () => { it('should return a Stackdriver MetricKind', () => { assert.strictEqual( - StackdriverStatsExporterUtils.createMetricKind( - MetricDescriptorType.CUMULATIVE_INT64), + createMetricKind(MetricDescriptorType.CUMULATIVE_INT64), MetricKind.CUMULATIVE); assert.strictEqual( - StackdriverStatsExporterUtils.createMetricKind( - MetricDescriptorType.GAUGE_INT64), - MetricKind.GAUGE); + createMetricKind(MetricDescriptorType.GAUGE_INT64), MetricKind.GAUGE); assert.strictEqual( - StackdriverStatsExporterUtils.createMetricKind( - MetricDescriptorType.GAUGE_DOUBLE), + createMetricKind(MetricDescriptorType.GAUGE_DOUBLE), MetricKind.GAUGE); assert.strictEqual( - StackdriverStatsExporterUtils.createMetricKind( - MetricDescriptorType.SUMMARY), + createMetricKind(MetricDescriptorType.SUMMARY), MetricKind.UNSPECIFIED); }); }); @@ -49,32 +45,23 @@ describe('Stackdriver Stats Exporter Utils', () => { describe('createValueType()', () => { it('should return a Stackdriver ValueType', () => { assert.strictEqual( - StackdriverStatsExporterUtils.createValueType( - MetricDescriptorType.GAUGE_DOUBLE), - ValueType.DOUBLE); + createValueType(MetricDescriptorType.GAUGE_DOUBLE), ValueType.DOUBLE); assert.strictEqual( - StackdriverStatsExporterUtils.createValueType( - MetricDescriptorType.CUMULATIVE_INT64), + createValueType(MetricDescriptorType.CUMULATIVE_INT64), ValueType.INT64); assert.strictEqual( - StackdriverStatsExporterUtils.createValueType( - MetricDescriptorType.GAUGE_INT64), - ValueType.INT64); + createValueType(MetricDescriptorType.GAUGE_INT64), ValueType.INT64); assert.strictEqual( - StackdriverStatsExporterUtils.createValueType( - MetricDescriptorType.CUMULATIVE_DOUBLE), + createValueType(MetricDescriptorType.CUMULATIVE_DOUBLE), ValueType.DOUBLE); assert.strictEqual( - StackdriverStatsExporterUtils.createValueType( - MetricDescriptorType.CUMULATIVE_DISTRIBUTION), + createValueType(MetricDescriptorType.CUMULATIVE_DISTRIBUTION), ValueType.DISTRIBUTION); assert.strictEqual( - StackdriverStatsExporterUtils.createValueType( - MetricDescriptorType.GAUGE_DISTRIBUTION), + createValueType(MetricDescriptorType.GAUGE_DISTRIBUTION), ValueType.DISTRIBUTION); assert.strictEqual( - StackdriverStatsExporterUtils.createValueType( - MetricDescriptorType.SUMMARY), + createValueType(MetricDescriptorType.SUMMARY), ValueType.VALUE_TYPE_UNSPECIFIED); }); }); @@ -83,23 +70,20 @@ describe('Stackdriver Stats Exporter Utils', () => { const labelKeys: LabelKey[] = [{'key': 'key', 'description': 'desc'}]; it('should return a Stackdriver LabelDescriptor', () => { - assert.deepStrictEqual( - StackdriverStatsExporterUtils.createLabelDescriptor(labelKeys), [ - {description: 'desc', key: 'key', valueType: 'STRING'}, { - description: - StackdriverStatsExporterUtils.OPENCENSUS_TASK_DESCRIPTION, - key: StackdriverStatsExporterUtils.OPENCENSUS_TASK, - valueType: 'STRING' - } - ]); + assert.deepStrictEqual(createLabelDescriptor(labelKeys), [ + {description: 'desc', key: 'key', valueType: 'STRING'}, { + description: 'Opencensus task identifier', + key: 'opencensus_task', + valueType: 'STRING' + } + ]); }); }); describe('createDisplayName()', () => { it('should return a Stackdriver DisplayName', () => { assert.strictEqual( - StackdriverStatsExporterUtils.createDisplayName( - 'demo/latency', 'custom.googleapis.com/opencensus'), + createDisplayName('demo/latency', 'custom.googleapis.com/opencensus'), 'custom.googleapis.com/opencensus/demo/latency'); }); }); @@ -107,8 +91,7 @@ describe('Stackdriver Stats Exporter Utils', () => { describe('getMetricType()', () => { it('should return a Stackdriver MetricType', () => { assert.strictEqual( - StackdriverStatsExporterUtils.getMetricType( - 'demo/latency', 'opencensus'), + getMetricType('demo/latency', 'opencensus'), 'opencensus/demo/latency'); }); }); @@ -125,39 +108,32 @@ describe('Stackdriver Stats Exporter Utils', () => { }; it('should return a Stackdriver Metric', () => { - const metric = StackdriverStatsExporterUtils.createMetric( + const metric = createMetric( metricDescriptor, labelValues, StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN); assert.strictEqual( metric.type, `custom.googleapis.com/opencensus/${METRIC_NAME}`); - assert.deepStrictEqual(metric.labels, { - 'key1': 'value1', - 'opencensus_task': - StackdriverStatsExporterUtils.generateDefaultTaskValue() - }); + assert.deepStrictEqual( + metric.labels, + {'key1': 'value1', 'opencensus_task': OPENCENSUS_TASK_VALUE_DEFAULT}); }); it('should return a Stackdriver Metric With External Metric Domain', () => { const prometheusDomain = 'external.googleapis.com/prometheus/'; - const metric = StackdriverStatsExporterUtils.createMetric( - metricDescriptor, labelValues, prometheusDomain); + const metric = + createMetric(metricDescriptor, labelValues, prometheusDomain); assert.strictEqual(metric.type, `${prometheusDomain}${METRIC_NAME}`); - assert.deepStrictEqual(metric.labels, { - 'key1': 'value1', - 'opencensus_task': - StackdriverStatsExporterUtils.generateDefaultTaskValue() - }); + assert.deepStrictEqual( + metric.labels, + {'key1': 'value1', 'opencensus_task': OPENCENSUS_TASK_VALUE_DEFAULT}); }); it('should return a Stackdriver Metric With Empty Label', () => { const prometheusDomain = 'external.googleapis.com/prometheus/'; - const metric = StackdriverStatsExporterUtils.createMetric( - metricDescriptor, [], prometheusDomain); + const metric = createMetric(metricDescriptor, [], prometheusDomain); assert.strictEqual(metric.type, `${prometheusDomain}${METRIC_NAME}`); - assert.deepStrictEqual(metric.labels, { - 'opencensus_task': - StackdriverStatsExporterUtils.generateDefaultTaskValue() - }); + assert.deepStrictEqual( + metric.labels, {'opencensus_task': OPENCENSUS_TASK_VALUE_DEFAULT}); }); }); @@ -170,8 +146,7 @@ describe('Stackdriver Stats Exporter Utils', () => { buckets: [{count: 3}, {count: 1}, {count: 2}, {count: 4}], }; it('should return a Stackdriver Distribution', () => { - const distribution: Distribution = - StackdriverStatsExporterUtils.createDistribution(distributionValue); + const distribution: Distribution = createDistribution(distributionValue); assert.strictEqual(distribution.count, 3); assert.strictEqual(distribution.mean, 0.6666666666666666); @@ -201,9 +176,8 @@ describe('Stackdriver Stats Exporter Utils', () => { }; it('should return a Stackdriver MetricDescriptor', () => { - const descriptor: MetricDescriptor = - StackdriverStatsExporterUtils.createMetricDescriptorData( - metricDescriptor, 'custom.googleapis.com/myorg/', 'myorg/'); + const descriptor: MetricDescriptor = createMetricDescriptorData( + metricDescriptor, 'custom.googleapis.com/myorg/', 'myorg/'); assert.strictEqual(descriptor.description, METRIC_DESCRIPTION); assert.strictEqual(descriptor.displayName, `myorg/${METRIC_NAME}`); @@ -214,19 +188,17 @@ describe('Stackdriver Stats Exporter Utils', () => { assert.strictEqual(descriptor.valueType, ValueType.INT64); assert.deepStrictEqual(descriptor.labels, [ {description: 'desc', key: 'key1', valueType: 'STRING'}, { - description: - StackdriverStatsExporterUtils.OPENCENSUS_TASK_DESCRIPTION, - key: StackdriverStatsExporterUtils.OPENCENSUS_TASK, + description: 'Opencensus task identifier', + key: 'opencensus_task', valueType: 'STRING' } ]); }); it('should return a Cumulative Stackdriver MetricDescriptor', () => { - const descriptor: MetricDescriptor = - StackdriverStatsExporterUtils.createMetricDescriptorData( - metricDescriptor1, - StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN, 'OpenCensus'); + const descriptor: MetricDescriptor = createMetricDescriptorData( + metricDescriptor1, StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN, + 'OpenCensus'); assert.strictEqual(descriptor.description, METRIC_DESCRIPTION); assert.strictEqual(descriptor.displayName, `OpenCensus/${METRIC_NAME}`); @@ -239,9 +211,8 @@ describe('Stackdriver Stats Exporter Utils', () => { assert.strictEqual(descriptor.valueType, ValueType.INT64); assert.deepStrictEqual(descriptor.labels, [ {description: 'desc', key: 'key1', valueType: 'STRING'}, { - description: - StackdriverStatsExporterUtils.OPENCENSUS_TASK_DESCRIPTION, - key: StackdriverStatsExporterUtils.OPENCENSUS_TASK, + description: 'Opencensus task identifier', + key: 'opencensus_task', valueType: 'STRING' } ]); @@ -267,8 +238,7 @@ describe('Stackdriver Stats Exporter Utils', () => { }; it('should return a Stackdriver Point', () => { - const pt = StackdriverStatsExporterUtils.createPoint( - doublePoint, null, ValueType.DOUBLE); + const pt = createPoint(doublePoint, null, ValueType.DOUBLE); assert.deepStrictEqual(pt, { value: {doubleValue: 12345678.2}, @@ -277,8 +247,7 @@ describe('Stackdriver Stats Exporter Utils', () => { }); it('should return a Stackdriver Cumulative Point', () => { - const pt = StackdriverStatsExporterUtils.createPoint( - intPoint, startTimestamp, ValueType.INT64); + const pt = createPoint(intPoint, startTimestamp, ValueType.INT64); assert.deepStrictEqual(pt, { value: {int64Value: 12345678}, @@ -290,7 +259,7 @@ describe('Stackdriver Stats Exporter Utils', () => { }); it('should return a Stackdriver Distribution Point', () => { - const pt = StackdriverStatsExporterUtils.createPoint( + const pt = createPoint( distributionPoint, startTimestamp, ValueType.DISTRIBUTION); assert.deepStrictEqual(pt, { @@ -358,19 +327,17 @@ describe('Stackdriver Stats Exporter Utils', () => { }; it('should return a Stackdriver TimeSeries', () => { - const timeSeriesList = StackdriverStatsExporterUtils.createTimeSeriesList( - metric, defaultResource, metricPrefix); + const timeSeriesList = + createTimeSeriesList(metric, defaultResource, metricPrefix); assert.equal(timeSeriesList.length, 1); const [timeseries] = timeSeriesList; assert.deepStrictEqual( timeseries.metric.type, 'custom.googleapis.com/opencensus/metric-name'); - assert.deepStrictEqual(timeseries.metric.labels, { - 'key1': 'value1', - 'opencensus_task': - StackdriverStatsExporterUtils.generateDefaultTaskValue() - }); + assert.deepStrictEqual( + timeseries.metric.labels, + {'key1': 'value1', 'opencensus_task': OPENCENSUS_TASK_VALUE_DEFAULT}); assert.deepStrictEqual(timeseries.metricKind, MetricKind.CUMULATIVE); assert.deepStrictEqual(timeseries.valueType, ValueType.DOUBLE); assert.deepStrictEqual(timeseries.resource, {type: 'global', labels: {}}); @@ -382,8 +349,7 @@ describe('Stackdriver Stats Exporter Utils', () => { it('should return a Stackdriver TimeSeries with custom monitored resource', () => { const timeSeriesList = - StackdriverStatsExporterUtils.createTimeSeriesList( - metric, customResource, metricPrefix); + createTimeSeriesList(metric, customResource, metricPrefix); assert.equal(timeSeriesList.length, 1); const [timeseries] = timeSeriesList; @@ -392,8 +358,7 @@ describe('Stackdriver Stats Exporter Utils', () => { 'custom.googleapis.com/opencensus/metric-name'); assert.deepStrictEqual(timeseries.metric.labels, { 'key1': 'value1', - 'opencensus_task': - StackdriverStatsExporterUtils.generateDefaultTaskValue() + 'opencensus_task': OPENCENSUS_TASK_VALUE_DEFAULT }); assert.deepStrictEqual( timeseries.resource, @@ -406,8 +371,7 @@ describe('Stackdriver Stats Exporter Utils', () => { it('should return a Stackdriver TimeSeries with Gauge and multiple timeseries', () => { const timeSeriesList = - StackdriverStatsExporterUtils.createTimeSeriesList( - gaugeMetric, defaultResource, metricPrefix); + createTimeSeriesList(gaugeMetric, defaultResource, metricPrefix); assert.equal(timeSeriesList.length, 2); const [timeseries1, timeseries2] = timeSeriesList; @@ -416,8 +380,7 @@ describe('Stackdriver Stats Exporter Utils', () => { 'custom.googleapis.com/opencensus/metric-name'); assert.deepStrictEqual(timeseries1.metric.labels, { 'key1': 'value1', - 'opencensus_task': - StackdriverStatsExporterUtils.generateDefaultTaskValue() + 'opencensus_task': OPENCENSUS_TASK_VALUE_DEFAULT }); assert.deepStrictEqual(timeseries1.metricKind, MetricKind.GAUGE); assert.deepStrictEqual(timeseries1.valueType, ValueType.DOUBLE);