-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: metric observer * chore: update after review * chore: reviews Co-authored-by: Mayur Kale <[email protected]>
- Loading branch information
1 parent
151106a
commit d16c691
Showing
24 changed files
with
489 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Overview | ||
|
||
OpenTelemetry metrics allow a user to collect data and export it to a metrics backend like Prometheus. | ||
|
||
This is a simple example that demonstrates basic metrics collection and exports those metrics to a Prometheus compatible endpoint. | ||
|
||
## Installation | ||
|
||
```sh | ||
$ # from this directory | ||
$ npm install | ||
``` | ||
|
||
How to setup [Prometheus](https://prometheus.io/docs/prometheus/latest/getting_started/) please check | ||
[Setup Prometheus](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-exporter-prometheus) | ||
|
||
## Run the Application | ||
- Run the example | ||
|
||
### Observer | ||
```sh | ||
$ npm run start:observer | ||
``` | ||
|
||
### Prometheus | ||
1. In prometheus search for "metric_observer" | ||
|
||
### Links | ||
1. Prometheus Scrape Endpoint http://localhost:9464/metrics | ||
2. Prometheus graph http://localhost:9090/graph | ||
|
||
### Example | ||
<p align="center"><img src="metrics/observer.png"/></p> | ||
|
||
## Useful links | ||
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
- For more information on OpenTelemetry metrics, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-metrics> | ||
|
||
## LICENSE | ||
|
||
Apache License 2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
const { MeterProvider } = require('@opentelemetry/metrics'); | ||
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: 1000, | ||
}).getMeter('example-observer'); | ||
|
||
const otelCpuUsage = meter.createObserver('metric_observer', { | ||
monotonic: false, | ||
labelKeys: ['pid', 'core'], | ||
description: 'Example of a observer', | ||
}); | ||
|
||
function getCpuUsage() { | ||
return Math.random(); | ||
} | ||
|
||
otelCpuUsage.setCallback((observerResult) => { | ||
observerResult.observe(getCpuUsage, meter.labels({ pid: process.pid, core: '1' })); | ||
observerResult.observe(getCpuUsage, meter.labels({ pid: process.pid, core: '2' })); | ||
observerResult.observe(getCpuUsage, meter.labels({ pid: process.pid, core: '3' })); | ||
observerResult.observe(getCpuUsage, meter.labels({ pid: process.pid, core: '4' })); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "example-metrics", | ||
"private": true, | ||
"version": "0.4.0", | ||
"description": "Example of using @opentelemetry/metrics", | ||
"main": "index.js", | ||
"scripts": { | ||
"start:observer": "node metrics/observer.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/open-telemetry/opentelemetry-js.git" | ||
}, | ||
"keywords": [ | ||
"opentelemetry", | ||
"http", | ||
"tracing", | ||
"metrics" | ||
], | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"author": "OpenTelemetry Authors", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/open-telemetry/opentelemetry-js/issues" | ||
}, | ||
"dependencies": { | ||
"@opentelemetry/exporter-prometheus": "^0.4.0", | ||
"@opentelemetry/metrics": "^0.4.0" | ||
}, | ||
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/*! | ||
* Copyright 2020, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { LabelSet } from './Metric'; | ||
|
||
/** | ||
* Interface that is being used in function setCallback for Observer Metric | ||
*/ | ||
export interface ObserverResult { | ||
observers: Map<LabelSet, Function>; | ||
observe(callback: Function, labelSet: LabelSet): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.