[Monitor OpenTelemetry] Update Metric Export Example (#31520)

### Packages impacted by this PR
@azure/monitor-opentelemetry

Updated the metric export example to a sample app that properly exports
metrics to Azure Monitor.

### Checklists
- [x] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [x] Added a changelog (if necessary)
This commit is contained in:
Jackson Weber 2024-10-25 11:45:07 -07:00 коммит произвёл GitHub
Родитель 978847c29d
Коммит f6a3eda8cf
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 17 добавлений и 11 удалений

Просмотреть файл

@ -5,11 +5,7 @@
* @summary Demonstrates how to run generate custom metrics that will be sent to Azure Monitor
*/
import {
useAzureMonitor,
AzureMonitorOpenTelemetryOptions,
shutdownAzureMonitor,
} from "@azure/monitor-opentelemetry";
import { useAzureMonitor, AzureMonitorOpenTelemetryOptions } from "@azure/monitor-opentelemetry";
import { metrics } from "@opentelemetry/api";
// Load the .env file if it exists
@ -25,8 +21,11 @@ const options: AzureMonitorOpenTelemetryOptions = {
useAzureMonitor(options);
export async function main() {
// Get Meter and create custom metric
const express = require("express");
const app = express();
const PORT = 8080;
async function metricExport(): Promise<void> {
const meter = metrics.getMeter("testMeter");
const customCounter = meter.createCounter("TestCounter");
customCounter.add(1);
@ -34,8 +33,15 @@ export async function main() {
customCounter.add(3);
}
main().catch(async (error) => {
console.error("An error occurred:", error);
await shutdownAzureMonitor();
process.exit(1);
async function setupRoutes(): Promise<void> {
app.get("/", async (_req: any, res: any) => {
await metricExport().then(() => {
res.send("Metrics sent to Azure Monitor");
});
});
}
setupRoutes().then(() => {
app.listen(PORT);
console.log(`Listening on http://localhost:${PORT}`);
});