From 16bff40d1bf5d3729db235c90f560b4e3387c13c Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Thu, 27 Jun 2024 07:13:09 +0100 Subject: [PATCH] fix(host-metrics): make host metrics constructor options optional (#2300) Co-authored-by: Marc Pichler --- packages/opentelemetry-host-metrics/README.md | 2 +- .../src/BaseMetrics.ts | 16 +++++++--------- .../test/metric.test.ts | 5 +---- 3 files changed, 9 insertions(+), 14 deletions(-) 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..f3844e57e9 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);