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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"env": {
"node": true
},
"extends": "airbnb-base",
"parserOptions": {
"sourceType": "script"
},
"rules": {
"strict": ["error", "global"],
"no-use-before-define": ["error", "nofunc"],
"no-console": "off",
"import/no-unresolved": "off",
"no-unused-vars": ["error", { "argsIgnorePattern": "^_" }]
},
"ignorePatterns": "**/*_pb.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# OpenTelemetry JavaScript Examples

This directory contains examples of how to run real applications with OpenTelemetry JavaScript.

## Work in Progress Examples

These examples are using work in progress metrics packages.

|Name | Description | Complexity Level |
------------- | ------------- | ------------ |
|[metrics](metrics/) | Basic metrics collection and exports those metrics to a Prometheus compatible endpoint | Beginner |

## Contributing

Please see [CONTRIBUTING.md](https://github.com/open-telemetry/opentelemetry-js/blob/main/CONTRIBUTING.md) for instructions on how to contribute.

## LICENSE

Apache License 2.0
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);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "example-metrics",
"private": true,
"version": "0.25.0",
"version": "0.27.0",
"description": "Example of using @opentelemetry/sdk-metrics-base",
"main": "index.js",
"scripts": {
Expand All @@ -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"
}