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

Commit

Permalink
Nit: Export for tests and Singleton instance for BaseMetricProducerMa…
Browse files Browse the repository at this point in the history
…nager (#277)
  • Loading branch information
mayurkale22 committed Jan 12, 2019
1 parent ef3486a commit 239a9e2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ import {MetricProducer, MetricProducerManager} from './types';
* metrics that need to be exported.
*/
class BaseMetricProducerManager implements MetricProducerManager {
/** Singleton instance */
private static singletonInstance: MetricProducerManager;

private metricProducers: Set<MetricProducer> = new Set<MetricProducer>();

/** Gets the instance. */
static get instance(): MetricProducerManager {
return this.singletonInstance || (this.singletonInstance = new this());
}

/**
* Adds the MetricProducer to the manager if it is not already present.
*
Expand Down Expand Up @@ -66,4 +74,4 @@ class BaseMetricProducerManager implements MetricProducerManager {
}
}

export const metricProducerManagerInstance = new BaseMetricProducerManager();
export const metricProducerManagerInstance = BaseMetricProducerManager.instance;
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function createMetricDescriptorData(
metricDescriptor: OCMetricDescriptor, metricPrefix: string,
displayNamePrefix: string): MetricDescriptor {
return {
type: getMetricType(metricDescriptor.name, metricPrefix),
type: createMetricType(metricDescriptor.name, metricPrefix),
description: metricDescriptor.description,
displayName: createDisplayName(metricDescriptor.name, displayNamePrefix),
metricKind: createMetricKind(metricDescriptor.type),
Expand Down Expand Up @@ -69,18 +69,17 @@ export function createTimeSeriesList(
}

/** Creates Metric type. */
export function getMetricType(name: string, metricPrefix: string): string {
function createMetricType(name: string, metricPrefix: string): string {
return path.join(metricPrefix, name);
}

/** Creates Metric display name. */
export function createDisplayName(
name: string, displayNamePrefix: string): string {
function createDisplayName(name: string, displayNamePrefix: string): string {
return path.join(displayNamePrefix, name);
}

/** Converts a OpenCensus Type to a StackDriver MetricKind. */
export function createMetricKind(metricDescriptorType: MetricDescriptorType):
function createMetricKind(metricDescriptorType: MetricDescriptorType):
MetricKind {
if (metricDescriptorType === MetricDescriptorType.GAUGE_INT64 ||
metricDescriptorType === MetricDescriptorType.GAUGE_DOUBLE) {
Expand All @@ -95,7 +94,7 @@ export function createMetricKind(metricDescriptorType: MetricDescriptorType):
}

/** Converts a OpenCensus Type to a StackDriver ValueType. */
export function createValueType(metricDescriptorType: MetricDescriptorType):
function createValueType(metricDescriptorType: MetricDescriptorType):
ValueType {
if (metricDescriptorType === MetricDescriptorType.CUMULATIVE_DOUBLE ||
metricDescriptorType === MetricDescriptorType.GAUGE_DOUBLE) {
Expand All @@ -114,8 +113,7 @@ export function createValueType(metricDescriptorType: MetricDescriptorType):
}

/** Constructs a LabelDescriptor from a LabelKey. */
export function createLabelDescriptor(labelKeys: LabelKey[]):
LabelDescriptor[] {
function createLabelDescriptor(labelKeys: LabelKey[]): LabelDescriptor[] {
const labelDescriptorList: LabelDescriptor[] =
labelKeys.map(labelKey => ({
key: labelKey.key,
Expand All @@ -133,10 +131,10 @@ export function createLabelDescriptor(labelKeys: LabelKey[]):
}

/** Creates a Metric using the LabelKeys and LabelValues. */
export function createMetric(
function createMetric(
metricDescriptor: OCMetricDescriptor, labelValues: LabelValue[],
metricPrefix: string): {type: string; labels: {[key: string]: string};} {
const type = getMetricType(metricDescriptor.name, metricPrefix);
const type = createMetricType(metricDescriptor.name, metricPrefix);
const labels: {[key: string]: string} = {};
for (let i = 0; i < labelValues.length; i++) {
const value = labelValues[i].value;
Expand All @@ -154,7 +152,7 @@ export function createMetric(
/**
* Converts timeseries's point, so that metric can be uploaded to StackDriver.
*/
export function createPoint(
function createPoint(
point: TimeSeriesPoint, startTimeStamp: Timestamp,
valueType: ValueType): Point {
const value = createValue(valueType, point);
Expand All @@ -168,7 +166,7 @@ export function createPoint(
}

/** Converts a OpenCensus Point's value to a StackDriver Point value. */
export function createValue(valueType: ValueType, point: TimeSeriesPoint) {
function createValue(valueType: ValueType, point: TimeSeriesPoint) {
if (valueType === ValueType.INT64) {
return {int64Value: point.value as number};
} else if (valueType === ValueType.DOUBLE) {
Expand All @@ -182,8 +180,7 @@ export function createValue(valueType: ValueType, point: TimeSeriesPoint) {
}

/** Formats an OpenCensus Distribution to Stackdriver's format. */
export function createDistribution(distribution: DistributionValue):
Distribution {
function createDistribution(distribution: DistributionValue): Distribution {
return {
count: distribution.count,
mean: distribution.count === 0 ? 0 : distribution.sum / distribution.count,
Expand All @@ -197,8 +194,7 @@ export function createDistribution(distribution: DistributionValue):
}

/** Converts a OpenCensus BucketOptions to a StackDriver BucketOptions. */
export function createExplicitBucketOptions(bucketOptions: BucketOptions):
number[] {
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
Expand All @@ -208,7 +204,7 @@ export function createExplicitBucketOptions(bucketOptions: BucketOptions):
}

/** Converts a OpenCensus Buckets to a list of counts. */
export function createBucketCounts(buckets: DistributionBucket[]): number[] {
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
Expand Down Expand Up @@ -239,3 +235,17 @@ function leftZeroPad(ns: number) {
const pad = '000000000'.substring(0, 9 - str.length);
return `${pad}${str}`;
}

export const TEST_ONLY = {
createMetricType,
createDisplayName,
createPoint,
createMetric,
createLabelDescriptor,
createValueType,
createMetricKind,
createDistribution,
createExplicitBucketOptions,
createValue,
createBucketCounts
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {DistributionValue, LabelKey, LabelValue, MetricDescriptor as OCMetricDes
import * as assert from 'assert';

import {StackdriverStatsExporter} from '../src/stackdriver-monitoring';
import {createDisplayName, createDistribution, createLabelDescriptor, createMetric, createMetricDescriptorData, createMetricKind, createPoint, createTimeSeriesList, createValueType, getMetricType, OPENCENSUS_TASK_VALUE_DEFAULT} from '../src/stackdriver-stats-utils';
import {createMetricDescriptorData, createTimeSeriesList, OPENCENSUS_TASK_VALUE_DEFAULT, TEST_ONLY} from '../src/stackdriver-stats-utils';
import {Distribution, MetricDescriptor, MetricKind, ValueType} from '../src/types';

const METRIC_NAME = 'metric-name';
Expand All @@ -29,39 +29,43 @@ describe('Stackdriver Stats Exporter Utils', () => {
describe('createMetricKind()', () => {
it('should return a Stackdriver MetricKind', () => {
assert.strictEqual(
createMetricKind(MetricDescriptorType.CUMULATIVE_INT64),
TEST_ONLY.createMetricKind(MetricDescriptorType.CUMULATIVE_INT64),
MetricKind.CUMULATIVE);
assert.strictEqual(
createMetricKind(MetricDescriptorType.GAUGE_INT64), MetricKind.GAUGE);
TEST_ONLY.createMetricKind(MetricDescriptorType.GAUGE_INT64),
MetricKind.GAUGE);
assert.strictEqual(
createMetricKind(MetricDescriptorType.GAUGE_DOUBLE),
TEST_ONLY.createMetricKind(MetricDescriptorType.GAUGE_DOUBLE),
MetricKind.GAUGE);
assert.strictEqual(
createMetricKind(MetricDescriptorType.SUMMARY),
TEST_ONLY.createMetricKind(MetricDescriptorType.SUMMARY),
MetricKind.UNSPECIFIED);
});
});

describe('createValueType()', () => {
it('should return a Stackdriver ValueType', () => {
assert.strictEqual(
createValueType(MetricDescriptorType.GAUGE_DOUBLE), ValueType.DOUBLE);
TEST_ONLY.createValueType(MetricDescriptorType.GAUGE_DOUBLE),
ValueType.DOUBLE);
assert.strictEqual(
createValueType(MetricDescriptorType.CUMULATIVE_INT64),
TEST_ONLY.createValueType(MetricDescriptorType.CUMULATIVE_INT64),
ValueType.INT64);
assert.strictEqual(
createValueType(MetricDescriptorType.GAUGE_INT64), ValueType.INT64);
TEST_ONLY.createValueType(MetricDescriptorType.GAUGE_INT64),
ValueType.INT64);
assert.strictEqual(
createValueType(MetricDescriptorType.CUMULATIVE_DOUBLE),
TEST_ONLY.createValueType(MetricDescriptorType.CUMULATIVE_DOUBLE),
ValueType.DOUBLE);
assert.strictEqual(
createValueType(MetricDescriptorType.CUMULATIVE_DISTRIBUTION),
TEST_ONLY.createValueType(
MetricDescriptorType.CUMULATIVE_DISTRIBUTION),
ValueType.DISTRIBUTION);
assert.strictEqual(
createValueType(MetricDescriptorType.GAUGE_DISTRIBUTION),
TEST_ONLY.createValueType(MetricDescriptorType.GAUGE_DISTRIBUTION),
ValueType.DISTRIBUTION);
assert.strictEqual(
createValueType(MetricDescriptorType.SUMMARY),
TEST_ONLY.createValueType(MetricDescriptorType.SUMMARY),
ValueType.VALUE_TYPE_UNSPECIFIED);
});
});
Expand All @@ -70,7 +74,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
const labelKeys: LabelKey[] = [{'key': 'key', 'description': 'desc'}];

it('should return a Stackdriver LabelDescriptor', () => {
assert.deepStrictEqual(createLabelDescriptor(labelKeys), [
assert.deepStrictEqual(TEST_ONLY.createLabelDescriptor(labelKeys), [
{description: 'desc', key: 'key', valueType: 'STRING'}, {
description: 'Opencensus task identifier',
key: 'opencensus_task',
Expand All @@ -83,15 +87,16 @@ describe('Stackdriver Stats Exporter Utils', () => {
describe('createDisplayName()', () => {
it('should return a Stackdriver DisplayName', () => {
assert.strictEqual(
createDisplayName('demo/latency', 'custom.googleapis.com/opencensus'),
TEST_ONLY.createDisplayName(
'demo/latency', 'custom.googleapis.com/opencensus'),
'custom.googleapis.com/opencensus/demo/latency');
});
});

describe('getMetricType()', () => {
describe('createMetricType()', () => {
it('should return a Stackdriver MetricType', () => {
assert.strictEqual(
getMetricType('demo/latency', 'opencensus'),
TEST_ONLY.createMetricType('demo/latency', 'opencensus'),
'opencensus/demo/latency');
});
});
Expand All @@ -108,7 +113,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
};

it('should return a Stackdriver Metric', () => {
const metric = createMetric(
const metric = TEST_ONLY.createMetric(
metricDescriptor, labelValues,
StackdriverStatsExporter.CUSTOM_OPENCENSUS_DOMAIN);
assert.strictEqual(
Expand All @@ -120,8 +125,8 @@ describe('Stackdriver Stats Exporter Utils', () => {

it('should return a Stackdriver Metric With External Metric Domain', () => {
const prometheusDomain = 'external.googleapis.com/prometheus/';
const metric =
createMetric(metricDescriptor, labelValues, prometheusDomain);
const metric = TEST_ONLY.createMetric(
metricDescriptor, labelValues, prometheusDomain);
assert.strictEqual(metric.type, `${prometheusDomain}${METRIC_NAME}`);
assert.deepStrictEqual(
metric.labels,
Expand All @@ -130,7 +135,8 @@ describe('Stackdriver Stats Exporter Utils', () => {

it('should return a Stackdriver Metric With Empty Label', () => {
const prometheusDomain = 'external.googleapis.com/prometheus/';
const metric = createMetric(metricDescriptor, [], prometheusDomain);
const metric =
TEST_ONLY.createMetric(metricDescriptor, [], prometheusDomain);
assert.strictEqual(metric.type, `${prometheusDomain}${METRIC_NAME}`);
assert.deepStrictEqual(
metric.labels, {'opencensus_task': OPENCENSUS_TASK_VALUE_DEFAULT});
Expand All @@ -146,7 +152,8 @@ describe('Stackdriver Stats Exporter Utils', () => {
buckets: [{count: 3}, {count: 1}, {count: 2}, {count: 4}],
};
it('should return a Stackdriver Distribution', () => {
const distribution: Distribution = createDistribution(distributionValue);
const distribution: Distribution =
TEST_ONLY.createDistribution(distributionValue);

assert.strictEqual(distribution.count, 3);
assert.strictEqual(distribution.mean, 0.6666666666666666);
Expand Down Expand Up @@ -238,7 +245,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
};

it('should return a Stackdriver Point', () => {
const pt = createPoint(doublePoint, null, ValueType.DOUBLE);
const pt = TEST_ONLY.createPoint(doublePoint, null, ValueType.DOUBLE);

assert.deepStrictEqual(pt, {
value: {doubleValue: 12345678.2},
Expand All @@ -247,7 +254,8 @@ describe('Stackdriver Stats Exporter Utils', () => {
});

it('should return a Stackdriver Cumulative Point', () => {
const pt = createPoint(intPoint, startTimestamp, ValueType.INT64);
const pt =
TEST_ONLY.createPoint(intPoint, startTimestamp, ValueType.INT64);

assert.deepStrictEqual(pt, {
value: {int64Value: 12345678},
Expand All @@ -259,7 +267,7 @@ describe('Stackdriver Stats Exporter Utils', () => {
});

it('should return a Stackdriver Distribution Point', () => {
const pt = createPoint(
const pt = TEST_ONLY.createPoint(
distributionPoint, startTimestamp, ValueType.DISTRIBUTION);

assert.deepStrictEqual(pt, {
Expand Down

0 comments on commit 239a9e2

Please sign in to comment.