Skip to content

Commit

Permalink
feat: deep merge user config into default meter configs (#1491)
Browse files Browse the repository at this point in the history
Signed-off-by: Naseem <[email protected]>
  • Loading branch information
Naseem authored Jan 15, 2021
1 parent f6702bd commit fdb5b53
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
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.20",
"@types/sinon": "9.0.10",
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

0 comments on commit fdb5b53

Please sign in to comment.