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

Plug-in Derived Cumulative into the Metric-Registry #513

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

- Add Cumulative (`DoubleCumulative`, `Int64Cumulative`) APIs.
- Add Cumulative (`DoubleCumulative`, `LongCumulative`, , `DerivedDoubleCumulative`, `DerivedLongCumulative`) APIs.

**This release has a breaking change. Please test your code accordingly after upgrading.**

Expand Down
4 changes: 2 additions & 2 deletions packages/opencensus-core/src/common/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export function validateArrayElementsNotNull<T>(
}

/** Throws an error if any of the map elements is null. */
export function validateMapElementNotNull<T>(
map: Map<T, T>, errorMessage: string) {
export function validateMapElementNotNull<K, V>(
map: Map<K, V>, errorMessage: string) {
for (const [key, value] of map.entries()) {
if (key == null || value == null) {
throw new Error(`${errorMessage} elements should not be a NULL`);
Expand Down
96 changes: 77 additions & 19 deletions packages/opencensus-core/src/metrics/metric-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/

import {getTimestampWithProcessHRTime} from '../common/time-util';
import {validateArrayElementsNotNull, validateDuplicateKeys, validateMapElementNotNull, validateNotNull} from '../common/validations';
import {MeasureUnit} from '../stats/types';
import {Cumulative} from './cumulative/cumulative';
import {DerivedCumulative} from './cumulative/derived-cumulative';
import {BaseMetricProducer} from './export/base-metric-producer';
import {Metric, MetricDescriptorType, MetricProducer} from './export/types';
import {LabelKey, LabelValue, Metric, MetricDescriptorType, MetricProducer, Timestamp} from './export/types';
import {DerivedGauge} from './gauges/derived-gauge';
import {Gauge} from './gauges/gauge';
import {Meter, MetricOptions} from './types';
Expand Down Expand Up @@ -61,9 +63,7 @@ export class MetricRegistry {
MetricRegistry.DEFAULT_CONSTANT_LABEL;
// TODO(mayurkale): Add support for resource

validateArrayElementsNotNull(labelKeys, MetricRegistry.LABEL_KEY);
validateMapElementNotNull(constantLabels, MetricRegistry.CONSTANT_LABELS);
validateDuplicateKeys(labelKeys, constantLabels);
this.validateLables(labelKeys, constantLabels);

const labelKeysCopy = Object.assign([], labelKeys);
const int64Gauge = new Gauge(
Expand Down Expand Up @@ -92,9 +92,7 @@ export class MetricRegistry {
MetricRegistry.DEFAULT_CONSTANT_LABEL;
// TODO(mayurkale): Add support for resource

validateArrayElementsNotNull(labelKeys, MetricRegistry.LABEL_KEY);
validateMapElementNotNull(constantLabels, MetricRegistry.CONSTANT_LABELS);
validateDuplicateKeys(labelKeys, constantLabels);
this.validateLables(labelKeys, constantLabels);

const labelKeysCopy = Object.assign([], labelKeys);
const doubleGauge = new Gauge(
Expand Down Expand Up @@ -123,9 +121,7 @@ export class MetricRegistry {
MetricRegistry.DEFAULT_CONSTANT_LABEL;
// TODO(mayurkale): Add support for resource

validateArrayElementsNotNull(labelKeys, MetricRegistry.LABEL_KEY);
validateMapElementNotNull(constantLabels, MetricRegistry.CONSTANT_LABELS);
validateDuplicateKeys(labelKeys, constantLabels);
this.validateLables(labelKeys, constantLabels);

const labelKeysCopy = Object.assign([], labelKeys);
const derivedInt64Gauge = new DerivedGauge(
Expand Down Expand Up @@ -154,9 +150,7 @@ export class MetricRegistry {
MetricRegistry.DEFAULT_CONSTANT_LABEL;
// TODO(mayurkale): Add support for resource

validateArrayElementsNotNull(labelKeys, MetricRegistry.LABEL_KEY);
validateMapElementNotNull(constantLabels, MetricRegistry.CONSTANT_LABELS);
validateDuplicateKeys(labelKeys, constantLabels);
this.validateLables(labelKeys, constantLabels);

const labelKeysCopy = Object.assign([], labelKeys);
const derivedDoubleGauge = new DerivedGauge(
Expand Down Expand Up @@ -185,9 +179,7 @@ export class MetricRegistry {
MetricRegistry.DEFAULT_CONSTANT_LABEL;
// TODO(mayurkale): Add support for resource

validateArrayElementsNotNull(labelKeys, MetricRegistry.LABEL_KEY);
validateMapElementNotNull(constantLabels, MetricRegistry.CONSTANT_LABELS);
validateDuplicateKeys(labelKeys, constantLabels);
this.validateLables(labelKeys, constantLabels);

const labelKeysCopy = Object.assign([], labelKeys);
const int64Cumulative = new Cumulative(
Expand Down Expand Up @@ -216,9 +208,7 @@ export class MetricRegistry {
MetricRegistry.DEFAULT_CONSTANT_LABEL;
// TODO(mayurkale): Add support for resource

validateArrayElementsNotNull(labelKeys, MetricRegistry.LABEL_KEY);
validateMapElementNotNull(constantLabels, MetricRegistry.CONSTANT_LABELS);
validateDuplicateKeys(labelKeys, constantLabels);
this.validateLables(labelKeys, constantLabels);

const labelKeysCopy = Object.assign([], labelKeys);
const doubleCumulative = new Cumulative(
Expand All @@ -228,6 +218,66 @@ export class MetricRegistry {
return doubleCumulative;
}

/**
* Builds a new derived Int64 Cumulative to be added to the registry.
*
* @param name The name of the metric.
* @param options The options for the metric.
* @returns A Int64 DerivedCumulative metric.
*/
addDerivedInt64Cumulative(name: string, options?: MetricOptions):
DerivedCumulative {
const description =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like some of this code is duplicated with other metric adding functions. That isn't necessarily bad but makes me wonder if there would be a more templatized approach to the different metric types. (On the other hand, Duplication is better than the wrong abstraction so I think this is fine)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks.

(options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
const labelKeys =
(options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
const constantLabels = (options && options.constantLabels) ||
MetricRegistry.DEFAULT_CONSTANT_LABEL;
// TODO(mayurkale): Add support for resource

this.validateLables(labelKeys, constantLabels);

const labelKeysCopy = Object.assign([], labelKeys);
const startTime: Timestamp = getTimestampWithProcessHRTime();
const derivedInt64Cumulative = new DerivedCumulative(
validateNotNull(name, MetricRegistry.NAME), description, unit,
MetricDescriptorType.CUMULATIVE_INT64, labelKeysCopy, constantLabels,
startTime);
this.registerMetric(name, derivedInt64Cumulative);
return derivedInt64Cumulative;
}

/**
* Builds a new derived Double Cumulative to be added to the registry.
*
* @param name The name of the metric.
* @param options The options for the metric.
* @returns A Double DerivedCumulative metric.
*/
addDerivedDoubleCumulative(name: string, options?: MetricOptions):
DerivedCumulative {
const description =
(options && options.description) || MetricRegistry.DEFAULT_DESCRIPTION;
const unit = (options && options.unit) || MetricRegistry.DEFAULT_UNIT;
const labelKeys =
(options && options.labelKeys) || MetricRegistry.DEFAULT_LABEL_KEYS;
const constantLabels = (options && options.constantLabels) ||
MetricRegistry.DEFAULT_CONSTANT_LABEL;
// TODO(mayurkale): Add support for resource

this.validateLables(labelKeys, constantLabels);

const labelKeysCopy = Object.assign([], labelKeys);
const startTime: Timestamp = getTimestampWithProcessHRTime();
const derivedDoubleCumulative = new DerivedCumulative(
validateNotNull(name, MetricRegistry.NAME), description, unit,
MetricDescriptorType.CUMULATIVE_DOUBLE, labelKeysCopy, constantLabels,
startTime);
this.registerMetric(name, derivedDoubleCumulative);
return derivedDoubleCumulative;
}

/**
* Registers metric to register.
*
Expand All @@ -250,6 +300,14 @@ export class MetricRegistry {
getMetricProducer(): MetricProducer {
return this.metricProducer;
}

/** Validates labelKeys and constantLabels. */
private validateLables(
labelKeys: LabelKey[], constantLabels: Map<LabelKey, LabelValue>) {
validateArrayElementsNotNull(labelKeys, MetricRegistry.LABEL_KEY);
validateMapElementNotNull(constantLabels, MetricRegistry.CONSTANT_LABELS);
validateDuplicateKeys(labelKeys, constantLabels);
}
}

/**
Expand Down
Loading