Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: user config + default config merge for MeterProvider, Meter #1491

Merged
merged 3 commits into from
Jan 15, 2021
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
4 changes: 3 additions & 1 deletion packages/opentelemetry-metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"access": "public"
},
"devDependencies": {
"@types/lodash.merge": "^4.6.6",
"@types/mocha": "8.2.0",
"@types/node": "14.14.12",
"@types/sinon": "9.0.9",
Expand All @@ -56,6 +57,7 @@
"dependencies": {
"@opentelemetry/api": "^0.14.0",
"@opentelemetry/core": "^0.14.0",
"@opentelemetry/resources": "^0.14.0"
"@opentelemetry/resources": "^0.14.0",
"lodash.merge": "^4.6.2"
}
}
32 changes: 17 additions & 15 deletions packages/opentelemetry-metrics/src/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import { ConsoleLogger, InstrumentationLibrary } from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import { BatchObserver } from './BatchObserver';
import { BaseBoundInstrument } from './BoundInstrument';
import { Processor } from './export/Processor';
import { UpDownCounterMetric } from './UpDownCounterMetric';
import { CounterMetric } from './CounterMetric';
import { UpDownSumObserverMetric } from './UpDownSumObserverMetric';
import { ValueRecorderMetric } from './ValueRecorderMetric';
import { Metric } from './Metric';
import { ValueObserverMetric } from './ValueObserverMetric';
import { SumObserverMetric } from './SumObserverMetric';
import { DEFAULT_METRIC_OPTIONS, DEFAULT_CONFIG, MeterConfig } from './types';
import { UngroupedProcessor } from './export/Processor';
import { PushController } from './export/Controller';
import { NoopExporter } from './export/NoopExporter';
import { Processor, UngroupedProcessor } from './export/Processor';
import { Metric } from './Metric';
import { SumObserverMetric } from './SumObserverMetric';
import { DEFAULT_CONFIG, DEFAULT_METRIC_OPTIONS, MeterConfig } from './types';
import { UpDownCounterMetric } from './UpDownCounterMetric';
import { UpDownSumObserverMetric } from './UpDownSumObserverMetric';
import { ValueObserverMetric } from './ValueObserverMetric';
import { ValueRecorderMetric } from './ValueRecorderMetric';
import merge = require('lodash.merge');

/**
* Meter is an implementation of the {@link Meter} interface.
Expand All @@ -51,15 +51,17 @@ export class Meter implements api.Meter {
*/
constructor(
instrumentationLibrary: InstrumentationLibrary,
config: MeterConfig = DEFAULT_CONFIG
config: MeterConfig = {}
) {
this._logger = config.logger || new ConsoleLogger(config.logLevel);
this._processor = config.processor ?? new UngroupedProcessor();
this._resource = config.resource || Resource.createTelemetrySDKResource();
const mergedConfig = merge({}, DEFAULT_CONFIG, config);
this._logger = mergedConfig.logger || new ConsoleLogger(config.logLevel);
this._processor = mergedConfig.processor ?? new UngroupedProcessor();
this._resource =
mergedConfig.resource || Resource.createTelemetrySDKResource();
this._instrumentationLibrary = instrumentationLibrary;
// start the push controller
const exporter = config.exporter || new NoopExporter();
const interval = config.interval;
const exporter = mergedConfig.exporter || new NoopExporter();
const interval = mergedConfig.interval;
this._controller = new PushController(this, exporter, interval);
}

Expand Down
14 changes: 9 additions & 5 deletions packages/opentelemetry-metrics/src/MeterProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* limitations under the License.
*/

import { ConsoleLogger } from '@opentelemetry/core';
import * as api from '@opentelemetry/api';
import { ConsoleLogger } from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import { Meter } from '.';
import { DEFAULT_CONFIG, MeterConfig } from './types';
import merge = require('lodash.merge');

/**
* This class represents a meter provider which platform libraries can extend
Expand All @@ -31,10 +32,13 @@ export class MeterProvider implements api.MeterProvider {
readonly resource: Resource;
readonly logger: api.Logger;

constructor(config: MeterConfig = DEFAULT_CONFIG) {
this.logger = config.logger ?? new ConsoleLogger(config.logLevel);
this.resource = config.resource ?? Resource.createTelemetrySDKResource();
this._config = Object.assign({}, config, {
constructor(config: MeterConfig = {}) {
const mergedConfig = merge({}, DEFAULT_CONFIG, config);
this.logger =
mergedConfig.logger ?? new ConsoleLogger(mergedConfig.logLevel);
this.resource =
mergedConfig.resource ?? Resource.createTelemetrySDKResource();
this._config = Object.assign({}, mergedConfig, {
logger: this.logger,
resource: this.resource,
});
Expand Down