-
Notifications
You must be signed in to change notification settings - Fork 832
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opentelemetry-web-example): add example for metrics protobuf exp…
…orter
- Loading branch information
1 parent
62aa91c
commit e9f90ca
Showing
8 changed files
with
2,948 additions
and
1,887 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
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
24 changes: 24 additions & 0 deletions
24
examples/opentelemetry-web/examples/metrics-proto/index.html
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,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="utf-8"> | ||
<title>Metrics Example</title> | ||
<base href="/"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
|
||
<body> | ||
Example of using metrics with Collector Exporter | ||
<script type="text/javascript" src="metrics-proto.js"></script> | ||
<br/> | ||
<button id="startBtn">Start metrics</button> | ||
<button id="stopBtn">Stop metrics</button> | ||
<br/> | ||
|
||
If you run the collector from this example, you should see metrics at: <br/> | ||
<a href="http://localhost:9090/graph?g0.range_input=1m&g0.expr=requests&g0.tab=0/" target="_blank">http://localhost:9090/</a> | ||
|
||
</body> | ||
|
||
</html> |
56 changes: 56 additions & 0 deletions
56
examples/opentelemetry-web/examples/metrics-proto/index.js
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,56 @@ | ||
const { DiagConsoleLogger, DiagLogLevel, diag, metrics } = require('@opentelemetry/api'); | ||
const { createMetricsExporter } = require('@opentelemetry/exporter-metrics-otlp-proto'); | ||
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics'); | ||
|
||
// Optional and only needed to see the internal diagnostic logging (during development) | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG); | ||
|
||
let interval; | ||
let meter; | ||
|
||
function stopMetrics() { | ||
console.log('STOPPING METRICS proto'); | ||
clearInterval(interval); | ||
metrics.getMeterProvider().shutdown() | ||
.then(() => metrics.disable()); | ||
} | ||
|
||
function startMetrics() { | ||
console.log('STARTING METRICS'); | ||
|
||
const meterProvider = new MeterProvider(); | ||
|
||
meterProvider.addMetricReader(new PeriodicExportingMetricReader({ | ||
exporter: createMetricsExporter({}), | ||
exportIntervalMillis: 1000 | ||
})); | ||
|
||
metrics.setGlobalMeterProvider(meterProvider); | ||
|
||
meter = metrics.getMeter('example-exporter-collector') | ||
|
||
const requestCounter = meter.createCounter('requests', { | ||
description: 'Example of a Counter', | ||
}); | ||
|
||
const upDownCounter = meter.createUpDownCounter('test_up_down_counter', { | ||
description: 'Example of a UpDownCounter', | ||
}); | ||
|
||
const attributes = { environment: 'staging' }; | ||
|
||
interval = setInterval(() => { | ||
requestCounter.add(1, attributes); | ||
upDownCounter.add(Math.random() > 0.5 ? 1 : -1, attributes); | ||
}, 1000); | ||
} | ||
|
||
const addClickEvents = () => { | ||
const startBtn = document.getElementById('startBtn'); | ||
|
||
const stopBtn = document.getElementById('stopBtn'); | ||
startBtn.addEventListener('click', startMetrics); | ||
stopBtn.addEventListener('click', stopMetrics); | ||
}; | ||
|
||
window.addEventListener('load', addClickEvents); |
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
Oops, something went wrong.