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

Update metrics example #2658

Merged
merged 8 commits into from
Dec 18, 2021
27 changes: 17 additions & 10 deletions examples/metrics/metrics/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ const meter = new MeterProvider({
interval: 2000,
}).getMeter('example-meter');

meter.createObservableGauge('cpu_core_usage', async (observableResult) => { // this callback is called once per each interval
await new Promise((resolve) => {
setTimeout(() => { resolve(); }, 50);
});
observableResult.observe(getRandomValue(), { core: '1' });
observableResult.observe(getRandomValue(), { core: '2' });
}, {
description: 'Example of a sync observable gauge with callback',
// async callback - for operation that needs to wait for value
meter.createObservableGauge('cpu_core_usage', {
description: 'Example of an async observable gauge with callback',
}, async (observableResult) => {
const value = await getAsyncValue();
observableResult.observe(value, { core: '1' });
observableResult.observe(value, { core: '2' });
});

function getRandomValue() {
return Math.random();
function getAsyncValue() {
return new Promise((resolve) => {
setTimeout(()=> {
resolve(Math.random());
}, 100);
});
}

setInterval(function(){
console.log("simulating an app being kept open")
}, 5000);
6 changes: 3 additions & 3 deletions examples/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
},
"dependencies": {
"@opentelemetry/api": "^1.0.2",
"@opentelemetry/core": "0.25.0",
"@opentelemetry/exporter-prometheus": "0.25.0",
"@opentelemetry/sdk-metrics-base": "0.25.0"
"@opentelemetry/core": "^1.0.1",
"@opentelemetry/exporter-prometheus": "^0.27.0",
"@opentelemetry/sdk-metrics-base": "^0.27.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme"
}