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

fix(host-metrics): make host metrics constructor options optional #2300

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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