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

Add MetricProducerManager to keep track of all MetricProducers #253

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import {Metric} from './types';
import {Metric, MetricProducer} from './types';

/**
* A MetricProducer producer that can be registered for exporting using
* MetricProducerManager.
*/
export abstract class MetricProducer {
export abstract class BaseMetricProducer implements MetricProducer {
/**
* Gets a collection of produced Metric`s to be exported.
* @returns {Metric[]} List of metrics
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {validateNotNull} from '../../common/validations';
import {MetricProducer, MetricProducerManager} from './types';

/**
* Keeps a set of MetricProducer that is used by exporters to determine the
* metrics that need to be exported.
*/
class BaseMetricProducerManager implements MetricProducerManager {
private metricProducers: Set<MetricProducer> = new Set<MetricProducer>();

/**
* Adds the MetricProducer to the manager if it is not already present.
*
* @param {MetricProducer} The MetricProducer to be added to the manager.
*/
add(metricProducer: MetricProducer): void {
validateNotNull(metricProducer, 'metricProducer');
if (!this.metricProducers.has(metricProducer)) {
this.metricProducers.add(metricProducer);
}
}

/**
* Removes the MetricProducer to the manager if it is present.
*
* @param {MetricProducer} The MetricProducer to be removed from the manager.
*/
remove(metricProducer: MetricProducer): void {
validateNotNull(metricProducer, 'metricProducer');
this.metricProducers.delete(metricProducer);
}

/**
* Clears all MetricProducers.
*/
removeAll(): void {
this.metricProducers.clear();
}

/**
* Returns all registered MetricProducers that should be exported.
*
* This method should be used by any metrics exporter that automatically
* exports data for MetricProducer registered with the MetricProducerManager.
*
* @return {Set<MetricProducer>} The Set of MetricProducers.
*/
getAllMetricProducer(): Set<MetricProducer> {
return this.metricProducers;
}
}

export const metricProducerManagerInstance = new BaseMetricProducerManager();
24 changes: 24 additions & 0 deletions packages/opencensus-core/src/metrics/export/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,27 @@ export interface Timestamp {
*/
nanos: number|null;
}

/**
* Keeps a set of MetricProducer that is used by exporters to determine the
* metrics that need to be exported.
*/
export interface MetricProducerManager {
/** Adds the MetricProducer to the manager */
add(metricProducer: MetricProducer): void;
/** Removes the MetricProducer to the manager */
remove(metricProducer: MetricProducer): void;
/** Clears all MetricProducers */
removeAll(): void;
/** Gets all registered MetricProducers that should be exported */
getAllMetricProducer(): Set<MetricProducer>;
}

/**
* A MetricProducer producer that can be registered for exporting using
* MetricProducerManager.
*/
export interface MetricProducer {
/** Gets a collection of produced Metric`s to be exported */
getMetrics(): Metric[];
}
42 changes: 42 additions & 0 deletions packages/opencensus-core/src/metrics/metric-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {metricProducerManagerInstance} from './export/metric-producer-manager';
import {MetricRegistry} from './metric-registry';

/**
* Class that holds the implementation instance for MetricRegistry.
*/
export class MetricsComponent {
private metricRegistry: MetricRegistry;

constructor() {
this.metricRegistry = new MetricRegistry();

// Register the MetricRegistry's MetricProducer to the global
// MetricProducerManager.
metricProducerManagerInstance.add(this.metricRegistry.getMetricProducer());
}

/**
* Returns the MetricRegistry.
*
* @return {MetricRegistry}.
*/
getMetricRegistry(): MetricRegistry {
return this.metricRegistry;
}
}
8 changes: 4 additions & 4 deletions packages/opencensus-core/src/metrics/metric-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

import {validateArrayElementsNotNull, validateNotNull} from '../common/validations';
import {MeasureUnit} from '../stats/types';
import {MeasureUnit,} from '../stats/types';

import {MetricProducer} from './export/metric-producer';
import {LabelKey, Metric, MetricDescriptorType} from './export/types';
import {BaseMetricProducer} from './export/base-metric-producer';
import {LabelKey, Metric, MetricDescriptorType, MetricProducer} from './export/types';
import {DerivedGauge} from './gauges/derived-gauge';
import {Gauge} from './gauges/gauge';
import {Meter} from './gauges/types';
Expand Down Expand Up @@ -184,7 +184,7 @@ export class MetricRegistry {
* MetricProducer that is used by exporters to determine the metrics that
* need to be exported.
*/
class MetricProducerForRegistry extends MetricProducer {
class MetricProducerForRegistry extends BaseMetricProducer {
private registeredMetrics: Map<string, Meter>;

constructor(registeredMetrics: Map<string, Meter>) {
Expand Down
23 changes: 13 additions & 10 deletions packages/opencensus-core/src/metrics/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
* limitations under the License.
*/

import {metricProducerManagerInstance} from './export/metric-producer-manager';
import {MetricProducerManager} from './export/types';
import {MetricsComponent} from './metric-component';
import {MetricRegistry} from './metric-registry';

/**
* Class for accessing the default MetricsComponent.
*/
export class Metrics {
private static readonly METRIC_REGISTRY = Metrics.newMetricRegistry();
private static readonly METRIC_COMPONENT = new MetricsComponent();

/**
* Returns the global MetricRegistry.
*
* @return {MetricRegistry}.
*/
static getMetricRegistry(): MetricRegistry {
return Metrics.METRIC_REGISTRY;
/** @return {MetricProducerManager} The global MetricProducerManager. */
static getMetricProducerManager(): MetricProducerManager {

Choose a reason for hiding this comment

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

Depending on what getMetricRegistry does, you might be able to make these properties instead of getter methods (or even potentially replacing this class for a plain module with consts - just like the previous PR feedback).

Copy link
Member Author

Choose a reason for hiding this comment

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

The only reason to use getter here is to make apis consistent across other language libraries. WDYT? I am happy to change as per your suggestion.

Choose a reason for hiding this comment

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

Personally I'm not a fan of classes used only for namespacing, since Node modules already provide a namespace and classes that are the only export in a file end up causing double-namespacing for anyone using require instead of ES6 import syntax. That being said, I can understand the consistency argument.

One thing to note, if this were a plain module end users get some extra flexibility with importing, which might be beneficial. Eg:

Importing like this which provides similar syntax to the class implementation

import * as Metrics from '....metrics';
Metrics.metricProducerManager.add(…)

Or importing only what's needed to simplify code

import {metricProducerManager} from '....metrics';
metricProducerManager.add(…)

Choose a reason for hiding this comment

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

Just noticed your question might have been specifically just about .getMetricProducerManager() vs .metricProducerManager - rather than also the replacement of the class with consts 😄

I don't have a strong opinion either way, since both are incredibly similar to the end user. All things equal I'd lean to not having to do a function call vs having to do one.

return metricProducerManagerInstance;
}

private static newMetricRegistry(): MetricRegistry {
return new MetricRegistry();
/** @return {MetricRegistry} The global MetricRegistry. */
static getMetricRegistry(): MetricRegistry {
return Metrics.METRIC_COMPONENT.getMetricRegistry();
}
}
4 changes: 2 additions & 2 deletions packages/opencensus-core/src/stats/metric-producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {MetricProducer} from '../metrics/export/metric-producer';
import {BaseMetricProducer} from '../metrics/export/base-metric-producer';
import {Metric} from '../metrics/export/types';

import {Stats} from './stats';
Expand All @@ -23,7 +23,7 @@ import {Stats} from './stats';
* A MetricProducer producer that can be registered for exporting using
* MetricProducerManager.
*/
export class MetricProducerForStats extends MetricProducer {
export class MetricProducerForStats extends BaseMetricProducer {
private statsManager: Stats;

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/opencensus-core/src/stats/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export class Stats {
*/
constructor(logger = defaultLogger) {
this.logger = logger.logger();

// TODO (mayurkale): Decide how to inject MetricProducerForStats.
// It should be something like below, but looks like not the right place.

// Create a new MetricProducerForStats and register it to
// MetricProducerManager when Stats is initialized.
// const metricProducer: MetricProducer = new MetricProducerForStats(this);
// Metrics.getMetricProducerManager().add(metricProducer);
}

/**
Expand Down
34 changes: 34 additions & 0 deletions packages/opencensus-core/test/test-metric-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import {metricProducerManagerInstance} from '../src/metrics/export/metric-producer-manager';
import {MetricsComponent} from '../src/metrics/metric-component';
import {MetricRegistry} from '../src/metrics/metric-registry';

describe('MetricsComponent()', () => {
const metricsComponent: MetricsComponent = new MetricsComponent();

it('should return a MetricRegistry instance', () => {
assert.ok(metricsComponent.getMetricRegistry() instanceof MetricRegistry);
});

it('should register metricRegistry to MetricProducerManger', () => {
assert.equal(metricProducerManagerInstance.getAllMetricProducer().size, 1);
assert.ok(metricProducerManagerInstance.getAllMetricProducer().has(
metricsComponent.getMetricRegistry().getMetricProducer()));
});
});
91 changes: 91 additions & 0 deletions packages/opencensus-core/test/test-metric-producer-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright 2019, OpenCensus Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as assert from 'assert';
import {metricProducerManagerInstance} from '../src/metrics/export/metric-producer-manager';
import {MetricRegistry} from '../src/metrics/metric-registry';

describe('MetricProducerManager()', () => {
const registry: MetricRegistry = new MetricRegistry();
const metricProducer = registry.getMetricProducer();
const registryOther: MetricRegistry = new MetricRegistry();
const metricProducerOther = registryOther.getMetricProducer();

beforeEach(() => {
metricProducerManagerInstance.removeAll();
});

describe('add()', () => {
it('should throw an error when the metricproducer is null', () => {
assert.throws(() => {
metricProducerManagerInstance.add(null);
}, /^Error: Missing mandatory metricProducer parameter$/);
});

it('add metricproducer', () => {
metricProducerManagerInstance.add(metricProducer);
const metricProducerList =
metricProducerManagerInstance.getAllMetricProducer();

assert.notDeepEqual(metricProducerList, null);
assert.equal(metricProducerList.size, 1);
});

it('should not add same metricproducer metricProducerManagerInstance',
() => {
metricProducerManagerInstance.add(metricProducer);
metricProducerManagerInstance.add(metricProducer);
metricProducerManagerInstance.add(metricProducer);
const metricProducerList =
metricProducerManagerInstance.getAllMetricProducer();

assert.equal(metricProducerList.size, 1);
assert.ok(metricProducerList.has(metricProducer));
});

it('should add different metricproducer metricProducerManagerInstance',
() => {
metricProducerManagerInstance.add(metricProducer);
metricProducerManagerInstance.add(metricProducerOther);
const metricProducerList =
metricProducerManagerInstance.getAllMetricProducer();

assert.equal(metricProducerList.size, 2);
assert.ok(metricProducerList.has(metricProducer));
assert.ok(metricProducerList.has(metricProducerOther));
});
});

describe('remove()', () => {
it('should throw an error when the metricproducer is null', () => {
assert.throws(() => {
metricProducerManagerInstance.add(null);
}, /^Error: Missing mandatory metricProducer parameter$/);
});

it('remove metricproducer', () => {
metricProducerManagerInstance.add(metricProducer);

const metricProducerList =
metricProducerManagerInstance.getAllMetricProducer();
assert.equal(metricProducerList.size, 1);
assert.ok(metricProducerList.has(metricProducer));

metricProducerManagerInstance.remove(metricProducer);
assert.equal(metricProducerList.size, 0);
});
});
});