Skip to content

Commit

Permalink
fix(host-metrics): make host metrics constructor options optional (#2300
Browse files Browse the repository at this point in the history
)

Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com>
  • Loading branch information
legendecas and pichlermarc authored Jun 27, 2024
1 parent 0ed4038 commit 16bff40
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-host-metrics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

Expand Down
16 changes: 7 additions & 9 deletions packages/opentelemetry-host-metrics/src/BaseMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

Expand Down
5 changes: 1 addition & 4 deletions packages/opentelemetry-host-metrics/test/metric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -114,7 +112,6 @@ describe('Host Metrics', () => {

const hostMetrics = new HostMetrics({
meterProvider,
name: 'opentelemetry-host-metrics',
});
hostMetrics.start();
assert.ok(hostMetrics instanceof HostMetrics);
Expand Down

0 comments on commit 16bff40

Please sign in to comment.