diff --git a/packages/opentelemetry-host-metrics/README.md b/packages/opentelemetry-host-metrics/README.md index 46952bd1a3..587c5e57a7 100644 --- a/packages/opentelemetry-host-metrics/README.md +++ b/packages/opentelemetry-host-metrics/README.md @@ -36,7 +36,7 @@ const meterProvider = new MeterProvider({ readers: [reader], }); -const hostMetrics = new HostMetrics({ meterProvider, name: 'example-host-metrics' }); +const hostMetrics = new HostMetrics({ meterProvider }); hostMetrics.start(); ``` diff --git a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts index a4f2232820..6d3b9ab2c5 100644 --- a/packages/opentelemetry-host-metrics/src/BaseMetrics.ts +++ b/packages/opentelemetry-host-metrics/src/BaseMetrics.ts @@ -25,12 +25,8 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from './version'; export interface MetricsCollectorConfig { // Meter Provider meterProvider?: MeterProvider; - // Character to be used to join metrics - default is "." - metricNameSeparator?: string; // Name of component - name: string; - // metric export endpoint - url?: string; + name?: string; } const DEFAULT_NAME = PACKAGE_NAME; @@ -43,12 +39,14 @@ export abstract class BaseMetrics { protected _meter: Meter; private _name: string; - constructor(config: MetricsCollectorConfig) { - this._name = config.name || DEFAULT_NAME; - const meterProvider = config.meterProvider || metrics.getMeterProvider(); - if (!config.meterProvider) { + constructor(config?: MetricsCollectorConfig) { + // Do not use `??` operator to allow falling back to default when the + // specified name is an empty string. + this._name = config?.name || DEFAULT_NAME; + if (config?.meterProvider == null) { this._logger.warn('No meter provider, using default'); } + const meterProvider = config?.meterProvider ?? metrics.getMeterProvider(); this._meter = meterProvider.getMeter(this._name, PACKAGE_VERSION); } diff --git a/packages/opentelemetry-host-metrics/test/metric.test.ts b/packages/opentelemetry-host-metrics/test/metric.test.ts index c98ad92636..ab6cb0afa4 100644 --- a/packages/opentelemetry-host-metrics/test/metric.test.ts +++ b/packages/opentelemetry-host-metrics/test/metric.test.ts @@ -103,9 +103,7 @@ describe('Host Metrics', () => { describe('constructor', () => { it('should create a new instance', () => { - const hostMetrics = new HostMetrics({ - name: 'opentelemetry-host-metrics', - }); + const hostMetrics = new HostMetrics(); assert.ok(hostMetrics instanceof HostMetrics); }); @@ -114,7 +112,6 @@ describe('Host Metrics', () => { const hostMetrics = new HostMetrics({ meterProvider, - name: 'opentelemetry-host-metrics', }); hostMetrics.start(); assert.ok(hostMetrics instanceof HostMetrics); @@ -150,7 +147,6 @@ describe('Host Metrics', () => { hostMetrics = new HostMetrics({ meterProvider, - name: '', // to get default instrumentation scope name }); await hostMetrics.start();