# MetricsManager

**Kind**: interface | **Module**: [Insights](https://docs.interop.io/browser/reference/javascript/insights/index.md) | **Access**: `io.insights.metrics`

**Source**: https://docs.interop.io/browser/reference/javascript/insights/metricsmanager/index.html

Metrics API.

## Properties

- **`settings`** (`MetricsSettings`, required)
  Metrics settings.
- **`started`** (`boolean`, required)
  Whether or not this instance is started.

## Methods

### get

Creates or retrieves a metric by name.

If a metric is already created with this name, it will be returned.

Otherwise, if settings exist for a metric with this name, either provided
by the platform or by configuration, a metric will be created.

Otherwise, returns undefined.

Subsequent calls to this method with the same name return the same metric.

```ts
(name: string) => Metric | undefined
```

**Parameters**

- **`name`** (`string`, required)
  the name of the metric, e.g. platform_startup

**Returns**: `Metric | undefined`

### getFromSettings

Creates or retrieves a metric based on the provided settings.

If a metric is already created with this name, it will be returned.

Otherwise, if settings exist for a metric with this name, either provided
by the platform or by configuration, they will be merged with the
settings provided to this method before creating it.

Otherwise, the settings provided will be used as is.

Subsequent calls to this method with the same name return the same metric.

```ts
(settings: MetricSettings) => Metric | undefined
```

**Parameters**

- **`settings`** (`MetricSettings`, required)
  settings to use when creating the metric

**Returns**: `Metric | undefined`

### getObservable

Creates or retrieves an observable metric by name.

If a metric is already created with this name, it will be returned.

Otherwise, if settings exist for a metric with this name, either provided
by the platform or by configuration, a metric will be created.

Otherwise, returns undefined.

Subsequent calls to this method with the same name return the same metric.

```ts
<TData extends Attributes>(name: string, observeCallback: (result: ObservableResult<TData>) => Promise<void>, subscribeCallback?: () => Promise<void>, unsubscribeCallback?: () => Promise<void>) => Metric | undefined
```

**Parameters**

- **`name`** (`string`, required)
  the name of the metric, e.g. platform_startup
- **`observeCallback`** (`(result: ObservableResult<TData>) => Promise<void>`, required)
  callback that provides the observable metric's value, invoked once per publishInterval
- **`subscribeCallback`** (`() => Promise<void>`, optional)
  optional callback invoked when the metric is started, can be used to 
  set up subscriptions if the metric is measuring event data
- **`unsubscribeCallback`** (`() => Promise<void>`, optional)
  optional callback invoked when the metric is stopped, can be used to
  destroy subscriptions if the metric is measuring event data

**Returns**: `Metric | undefined`

### getObservableFromSettings

Creates or retrieves an observable metric based on the provided settings.

If a metric is already created with this name, it will be returned.

Otherwise, if settings exist for a metric with this name, either provided
by the platform or by configuration, they will be merged with the
settings provided to this method before creating it.

Otherwise, the settings provided will be used as is.

Subsequent calls to this method with the same name return the same metric.

```ts
<TData extends Attributes>(settings: IOInsights.MetricSettings, observeCallback: (result: ObservableResult<TData>) => Promise<void>, subscribeCallback?: () => Promise<void>, unsubscribeCallback?: () => Promise<void>) => Metric | undefined
```

**Parameters**

- **`settings`** (`IOInsights.MetricSettings`, required)
  settings to use when creating the metric
- **`observeCallback`** (`(result: ObservableResult<TData>) => Promise<void>`, required)
  callback that provides the observable metric's value, invoked once per publishInterval
- **`subscribeCallback`** (`() => Promise<void>`, optional)
  optional callback invoked when the metric is started, can be used to 
  set up subscriptions if the metric is measuring event data
- **`unsubscribeCallback`** (`() => Promise<void>`, optional)
  optional callback invoked when the metric is stopped, can be used to
  destroy subscriptions if the metric is measuring event data

**Returns**: `Metric | undefined`

### start

Starts publishing data.

```ts
() => Promise<void>
```

**Returns**: `Promise<void>`

### stop

Stops publishing data.

```ts
() => Promise<void>
```

**Returns**: `Promise<void>`

### waitForFinalExport

Waits for any pending data to be published, with the option to specify a timeout.

```ts
(timeoutMs?: number) => Promise<void>
```

**Parameters**

- **`timeoutMs`** (`number`, optional)
  maximum time to wait for export

**Returns**: `Promise<void>`

## Related types

- [Metric](https://docs.interop.io/browser/reference/javascript/insights/metric/index.md)
- [MetricSettings](https://docs.interop.io/browser/reference/javascript/insights/metricsettings/index.md)
- [MetricsSettings](https://docs.interop.io/browser/reference/javascript/insights/metricssettings/index.md)
