Skip to content

Commit

Permalink
[Monitor OpenTelemetry] Update Metric Export Example (Azure#31520)
Browse files Browse the repository at this point in the history
### 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)
  • Loading branch information
JacksonWeber authored Oct 25, 2024
1 parent 978847c commit f6a3eda
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions sdk/monitor/monitor-opentelemetry/samples-dev/customMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,17 +21,27 @@ 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);
customCounter.add(2);
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}`);
});

0 comments on commit f6a3eda

Please sign in to comment.