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

feat!(metrics): remove batch observer #2566

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 1 addition & 55 deletions examples/metrics/metrics/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,66 +27,12 @@ meter.createObservableGauge('cpu_core_usage', {
description: 'Example of a sync observable gauge with callback',
}, async (observableResult) => { // this callback is called once per each interval
await new Promise((resolve) => {
setTimeout(()=> {resolve()}, 50);
setTimeout(() => { resolve(); }, 50);
});
observableResult.observe(getRandomValue(), { core: '1' });
observableResult.observe(getRandomValue(), { core: '2' });
});

// no callback as they will be updated in batch observer
const tempMetric = meter.createObservableGauge('cpu_temp_per_app', {
description: 'Example of sync observable gauge used with async batch observer',
});

// no callback as they will be updated in batch observer
const cpuUsageMetric = meter.createObservableGauge('cpu_usage_per_app', {
description: 'Example of sync observable gauge used with async batch observer',
});

meter.createBatchObserver((batchObserverResult) => {
Promise.all([
someAsyncMetrics(),
// simulate waiting
new Promise((resolve, reject) => {
setTimeout(resolve, 300);
}),
]).then(([apps, waiting]) => {
apps.forEach(app => {
batchObserverResult.observe({ app: app.name, core: '1' }, [
tempMetric.observation(app.core1.temp),
cpuUsageMetric.observation(app.core1.usage),
]);
batchObserverResult.observe({ app: app.name, core: '2' }, [
tempMetric.observation(app.core2.temp),
cpuUsageMetric.observation(app.core2.usage),
]);
});
});
}, {
maxTimeoutUpdateMS: 500,
},
);

function someAsyncMetrics() {
return new Promise((resolve) => {
setTimeout(() => {
const stats = [
{
name: 'app1',
core1: { usage: getRandomValue(), temp: getRandomValue() * 100 },
core2: { usage: getRandomValue(), temp: getRandomValue() * 100 },
},
{
name: 'app2',
core1: { usage: getRandomValue(), temp: getRandomValue() * 100 },
core2: { usage: getRandomValue(), temp: getRandomValue() * 100 },
},
];
resolve(stats);
}, 200);
});
}

function getRandomValue() {
return Math.random();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { BatchObserverResult } from './types/BatchObserverResult';
import { Meter } from './types/Meter';
import {
MetricOptions,
Expand Down Expand Up @@ -111,17 +110,6 @@ export class NoopMeter implements Meter {
): ObservableUpDownCounter {
return NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC;
}

/**
* Returns constant noop batch observer.
* @param name the name of the metric.
* @param callback the batch observer callback
*/
createBatchObserver(
_callback: (batchObserverResult: BatchObserverResult) => void
): NoopBatchObserver {
return NOOP_BATCH_OBSERVER;
}
}

export class NoopMetric<T> implements UnboundMetric<T> {
Expand Down Expand Up @@ -185,8 +173,6 @@ export class NoopObservableBaseMetric
}
}

export class NoopBatchObserver {}

export class NoopBoundCounter implements BoundCounter {
add(_value: number): void {
return;
Expand Down Expand Up @@ -224,5 +210,3 @@ export const NOOP_OBSERVABLE_UP_DOWN_COUNTER_METRIC = new NoopObservableBaseMetr
export const NOOP_OBSERVABLE_COUNTER_METRIC = new NoopObservableBaseMetric(
NOOP_BOUND_OBSERVABLE_BASE
);

export const NOOP_BATCH_OBSERVER = new NoopBatchObserver();
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

export * from './NoopMeter';
export * from './NoopMeterProvider';
export * from './types/BatchObserverResult';
export * from './types/BoundInstrument';
export * from './types/Meter';
export * from './types/MeterProvider';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
* limitations under the License.
*/

import { BatchObserverResult } from './BatchObserverResult';
import {
MetricOptions,
Counter,
Histogram,
ObservableGauge,
BatchObserverOptions,
UpDownCounter,
ObservableCounter,
ObservableUpDownCounter,
Expand Down Expand Up @@ -105,15 +103,4 @@ export interface Meter {
options?: MetricOptions,
callback?: (observableResult: ObservableResult) => void
): ObservableUpDownCounter;

/**
* Creates a new `BatchObserver`, can be used to update many metrics
* at the same time and when operations needs to be async
* @param callback the batch observer callback
* @param [options] the batch observer options.
*/
createBatchObserver(
callback: (batchObserverResult: BatchObserverResult) => void,
options?: BatchObserverOptions
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ export interface MetricOptions {
aggregationTemporality?: AggregationTemporality;
}

export interface BatchObserverOptions {
/**
* Indicates how long the batch metric should wait to update before cancel
*/
maxTimeoutUpdateMS?: number;
}

/** The Type of value. It describes how the data is reported. */
export enum ValueType {
INT,
Expand Down
54 changes: 0 additions & 54 deletions experimental/packages/opentelemetry-sdk-metrics-base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,60 +191,6 @@ function getRandomValue() {
}
```

### Batch Observer

Choose this kind of metric when you need to update multiple observables with the results of a single async calculation.

```js
const { MeterProvider } = require('@opentelemetry/sdk-metrics-base');
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus');

const exporter = new PrometheusExporter(
{
startServer: true,
},
() => {
console.log('prometheus scrape endpoint: http://localhost:9464/metrics');
},
);

const meter = new MeterProvider({
exporter,
interval: 3000,
}).getMeter('example-observer');

const cpuUsageMetric = meter.createObservableGauge('cpu_usage_per_app', {
description: 'CPU',
});

const MemUsageMetric = meter.createObservableGauge('mem_usage_per_app', {
description: 'Memory',
});

meter.createBatchObserver((batchObserverResult) => {
getSomeAsyncMetrics().then(metrics => {
batchObserverResult.observe({ app: 'myApp' }, [
cpuUsageMetric.observation(metrics.value1),
MemUsageMetric.observation(metrics.value2)
]);
});
});

function getSomeAsyncMetrics() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
value1: Math.random(),
value2: Math.random(),
});
}, 100)
});
}

```

See [examples/prometheus](https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/prometheus) for a short example.

### Histogram

`Histogram` is a non-additive synchronous instrument useful for recording any non-additive number, positive or negative.
Expand Down

This file was deleted.

This file was deleted.

Loading