From aa8c037f74d97cfb6ca817db8f67e1986d60c8f5 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Thu, 9 Apr 2026 16:00:43 -0700 Subject: [PATCH 1/2] Simplify samples: TypeScript-only, use @microsoft/opentelemetry API - Remove JavaScript samples and v1/ folder nesting - Flatten structure to samples/src/*.ts - Update all imports from @azure/monitor-opentelemetry to @microsoft/opentelemetry - Replace useAzureMonitor/shutdownAzureMonitor with useMicrosoftOpenTelemetry/shutdownMicrosoftOpenTelemetry - Restructure options to use azureMonitor.azureMonitorExporterOptions nesting - Update README with configuration tables and clean up boilerplate --- README.md | 149 ++++++++---------- samples/README.md | 72 +++++++++ samples/package.json | 33 ++++ samples/src/basicConnection.ts | 25 +++ samples/{v1/typescript => }/src/cloudRole.ts | 18 +-- .../{v1/typescript => }/src/customMetric.ts | 21 +-- .../{v1/typescript => }/src/customTrace.ts | 21 +-- .../{v1/typescript => }/src/liveMetrics.ts | 20 +-- samples/src/offlineStorage.ts | 28 ++++ .../{v1/typescript => }/src/otlpExporter.ts | 23 +-- .../typescript => }/src/redactQueryStrings.ts | 18 +-- samples/{v1/typescript => }/src/sampling.ts | 18 +-- samples/{v1/typescript => }/tsconfig.json | 0 samples/v1/javascript/README.md | 78 --------- samples/v1/javascript/basicConnection.js | 25 --- samples/v1/javascript/cloudRole.js | 37 ----- samples/v1/javascript/customMetric.js | 33 ---- samples/v1/javascript/customTrace.js | 50 ------ samples/v1/javascript/liveMetrics.js | 27 ---- samples/v1/javascript/offlineStorage.js | 28 ---- samples/v1/javascript/otlpExporter.js | 42 ----- samples/v1/javascript/package.json | 41 ----- samples/v1/javascript/redactQueryStrings.js | 58 ------- samples/v1/javascript/sampling.js | 28 ---- samples/v1/typescript/README.md | 91 ----------- samples/v1/typescript/package.json | 48 ------ samples/v1/typescript/src/basicConnection.ts | 25 --- samples/v1/typescript/src/offlineStorage.ts | 28 ---- 28 files changed, 295 insertions(+), 790 deletions(-) create mode 100644 samples/README.md create mode 100644 samples/package.json create mode 100644 samples/src/basicConnection.ts rename samples/{v1/typescript => }/src/cloudRole.ts (73%) rename samples/{v1/typescript => }/src/customMetric.ts (60%) rename samples/{v1/typescript => }/src/customTrace.ts (71%) rename samples/{v1/typescript => }/src/liveMetrics.ts (50%) create mode 100644 samples/src/offlineStorage.ts rename samples/{v1/typescript => }/src/otlpExporter.ts (69%) rename samples/{v1/typescript => }/src/redactQueryStrings.ts (77%) rename samples/{v1/typescript => }/src/sampling.ts (58%) rename samples/{v1/typescript => }/tsconfig.json (100%) delete mode 100644 samples/v1/javascript/README.md delete mode 100644 samples/v1/javascript/basicConnection.js delete mode 100644 samples/v1/javascript/cloudRole.js delete mode 100644 samples/v1/javascript/customMetric.js delete mode 100644 samples/v1/javascript/customTrace.js delete mode 100644 samples/v1/javascript/liveMetrics.js delete mode 100644 samples/v1/javascript/offlineStorage.js delete mode 100644 samples/v1/javascript/otlpExporter.js delete mode 100644 samples/v1/javascript/package.json delete mode 100644 samples/v1/javascript/redactQueryStrings.js delete mode 100644 samples/v1/javascript/sampling.js delete mode 100644 samples/v1/typescript/README.md delete mode 100644 samples/v1/typescript/package.json delete mode 100644 samples/v1/typescript/src/basicConnection.ts delete mode 100644 samples/v1/typescript/src/offlineStorage.ts diff --git a/README.md b/README.md index 3c32c6f3..07da5591 100644 --- a/README.md +++ b/README.md @@ -1,113 +1,90 @@ -# microsoft-opentelemetry +# @microsoft/opentelemetry -TypeScript package for a Microsoft OpenTelemetry distribution that provides a single onboarding experience for observability across Azure Monitor, OTLP-compatible backends, and Microsoft-specific integrations. +Microsoft OpenTelemetry distribution for Node.js — one import, one call, full observability across Azure Monitor, OTLP-compatible backends, and A365. -This repository starts from the POC described in `azure-data/microsoft-opentelemetry-distro-poc`, but is intentionally kept minimal while the package shape and delivery plan are being defined. +## Getting Started -## Goal - -The target package should reduce fragmented setup across multiple observability stacks to one import and one configuration function. - -Intended API shape: +```bash +npm install @microsoft/opentelemetry +``` ```typescript -import { useMicrosoftOpenTelemetry } from "microsoft-opentelemetry"; +import { useMicrosoftOpenTelemetry } from "@microsoft/opentelemetry"; useMicrosoftOpenTelemetry({ azureMonitor: { azureMonitorExporterOptions: { - connectionString: "InstrumentationKey=...;IngestionEndpoint=...", + connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING, }, }, }); ``` -## Planned Scope - -- Azure Monitor exporter support -- OTLP exporter support -- Microsoft-specific agent observability extensions (A365) -- GenAI instrumentation toggles for OpenAI and LangChain -- Standard Node.js web and HTTP instrumentations -- Environment-variable driven configuration -- A stable package surface for downstream agent applications - -## OTLP Export - -OTLP HTTP export is automatically enabled when the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable is set. No code changes are needed — the distro will export traces, metrics, and logs to the specified endpoint. - -When OTLP is enabled alongside Azure Monitor, telemetry is sent to both backends simultaneously. - -### Supported environment variables - -The following standard OpenTelemetry environment variables are supported. General variables apply to all signals; signal-specific variables override the general ones. - -| Variable | Description | -|---|---| -| `OTEL_EXPORTER_OTLP_ENDPOINT` | Base endpoint for all signals (e.g. `http://localhost:4318`). Signal paths `/v1/traces`, `/v1/metrics`, `/v1/logs` are appended automatically. | -| `OTEL_EXPORTER_OTLP_HEADERS` | Comma-separated `key=value` pairs sent as HTTP headers. | -| `OTEL_EXPORTER_OTLP_COMPRESSION` | Compression algorithm: `gzip` or `none`. | -| `OTEL_EXPORTER_OTLP_TIMEOUT` | Export timeout in milliseconds (default: `10000`). | -| `OTEL_EXPORTER_OTLP_PROTOCOL` | Transport protocol: `http/protobuf` (default) or `http/json`. | -| `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT` | Full endpoint URL for traces. Overrides the base endpoint. | -| `OTEL_EXPORTER_OTLP_TRACES_HEADERS` | Additional headers for trace exports. | -| `OTEL_EXPORTER_OTLP_TRACES_COMPRESSION` | Compression for trace exports. | -| `OTEL_EXPORTER_OTLP_TRACES_TIMEOUT` | Timeout for trace exports. | -| `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT` | Full endpoint URL for metrics. | -| `OTEL_EXPORTER_OTLP_METRICS_HEADERS` | Additional headers for metric exports. | -| `OTEL_EXPORTER_OTLP_METRICS_COMPRESSION` | Compression for metric exports. | -| `OTEL_EXPORTER_OTLP_METRICS_TIMEOUT` | Timeout for metric exports. | -| `OTEL_EXPORTER_OTLP_METRICS_TEMPORALITY_PREFERENCE` | Aggregation temporality: `cumulative`, `delta`, or `lowmemory`. | -| `OTEL_EXPORTER_OTLP_METRICS_DEFAULT_HISTOGRAM_AGGREGATION` | `explicit_bucket_histogram` or `base2_exponential_bucket_histogram`. | -| `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT` | Full endpoint URL for logs. | -| `OTEL_EXPORTER_OTLP_LOGS_HEADERS` | Additional headers for log exports. | -| `OTEL_EXPORTER_OTLP_LOGS_COMPRESSION` | Compression for log exports. | -| `OTEL_EXPORTER_OTLP_LOGS_TIMEOUT` | Timeout for log exports. | - -For the full specification, see the [OpenTelemetry OTLP Exporter Configuration documentation](https://opentelemetry.io/docs/specs/otel/protocol/exporter/). - -## Current Repository Layout - -- `src/` – package source - - `index.ts` – main entry point (`useMicrosoftOpenTelemetry`) - - `types.ts` – configuration options (scoped by backend) - - `setup/` – modular setup for Azure Monitor, OTLP, A365 -- `azure-monitor-opentelemetry/` – temporary copy of Azure Monitor code (see Phase 3) -- `tests/` – test suite -- `package.json` – project metadata and dependencies -- `tsconfig.json` – TypeScript compiler configuration -- `PLANNING.md` – implementation plan and open questions - -## Development - -Install dependencies and run the build and test suite: +That's it — traces, metrics, and logs are collected automatically with built-in instrumentations for HTTP, databases, and more. -```bash -npm install -npm run build -npm run lint -npm test -``` +## Configuration -## Reference +### `MicrosoftOpenTelemetryOptions` -- POC repo: https://github.com/azure-data/microsoft-opentelemetry-distro-poc -- Planning document: [PLANNING.md](./PLANNING.md) -- Python distro: https://github.com/microsoft/opentelemetry-distro-python +| Option | Type | Default | Description | +|---|---|---|---| +| `resource` | `Resource` | auto-detected | OpenTelemetry Resource (service name, version, etc.) | +| `samplingRatio` | `number` | `1.0` | Ratio of telemetry items to transmit (0.0–1.0) | +| `tracesPerSecond` | `number` | `5` | Max traces per second. Set to `0` to use `samplingRatio` instead | +| `instrumentationOptions` | `InstrumentationOptions` | all enabled | Toggle built-in instrumentations (HTTP, MongoDB, MySQL, PostgreSQL, Redis, Azure SDK, Azure Functions, Winston, Bunyan, OpenAI Agents, LangChain) | +| `spanProcessors` | `SpanProcessor[]` | — | Additional span processors | +| `logRecordProcessors` | `LogRecordProcessor[]` | — | Additional log record processors | +| `metricReaders` | `MetricReader[]` | — | Additional metric readers | +| `views` | `ViewOptions[]` | — | Metric views | +| `azureMonitor` | `AzureMonitorOpenTelemetryOptions` | — | Azure Monitor backend config (see below) | +| `a365` | `A365Options` | — | A365 observability config | -## Contributing +### `azureMonitor` options + +| Option | Type | Default | Description | +|---|---|---|---| +| `azureMonitorExporterOptions` | `AzureMonitorExporterOptions` | — | Exporter config including `connectionString`, `storageDirectory`, `disableOfflineStorage` | +| `enableLiveMetrics` | `boolean` | `false` | Enable Live Metrics streaming | +| `enableStandardMetrics` | `boolean` | `true` | Enable standard metrics collection | +| `enableTraceBasedSamplingForLogs` | `boolean` | `true` | Enable log sampling based on trace | +| `enablePerformanceCounters` | `boolean` | — | Enable performance counter collection | +| `browserSdkLoaderOptions` | `BrowserSdkLoaderOptions` | — | Application Insights browser SDK loader config | + +### OTLP via environment variables -Read our [contributing guide](./CONTRIBUTING.md) to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to this distribution. +Set `OTEL_EXPORTER_OTLP_ENDPOINT` and OTLP export is enabled automatically — no code changes needed. Signal-specific variables (`OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`, etc.) override the base endpoint. -## Data Collection +See the [OpenTelemetry OTLP Exporter specification](https://opentelemetry.io/docs/specs/otel/protocol/exporter/) for the full list. -As this SDK is designed to enable applications to perform data collection which is sent to the Microsoft collection endpoints the following is required to identify our privacy statement. +### Example -The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft's privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices. +```typescript +import { useMicrosoftOpenTelemetry, shutdownMicrosoftOpenTelemetry } from "@microsoft/opentelemetry"; +import { resourceFromAttributes } from "@opentelemetry/resources"; -## Trademarks +useMicrosoftOpenTelemetry({ + resource: resourceFromAttributes({ "service.name": "my-app" }), + samplingRatio: 0.5, + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: process.env.APPLICATIONINSIGHTS_CONNECTION_STRING, + }, + enableLiveMetrics: true, + }, +}); + +// On shutdown +await shutdownMicrosoftOpenTelemetry(); +``` + +## Samples + +See the [samples/](./samples/) directory for working TypeScript examples covering connection setup, custom metrics, custom traces, sampling, OTLP dual-export, and more. + + +## Contributing -This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow [Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies. +See [CONTRIBUTING.md](./CONTRIBUTING.md). ## License diff --git a/samples/README.md b/samples/README.md new file mode 100644 index 00000000..4fa46b9a --- /dev/null +++ b/samples/README.md @@ -0,0 +1,72 @@ +--- +page_type: sample +languages: + - typescript +products: + - azure-monitor +urlFragment: microsoft-opentelemetry-typescript +--- + +# Microsoft OpenTelemetry distribution samples for TypeScript + +These sample programs show how to use the `@microsoft/opentelemetry` distribution in common scenarios. + +| **File Name** | **Description** | +| ------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| [basicConnection.ts][basicconnection] | Demonstrates how to configure Microsoft OpenTelemetry using a connection string. | +| [cloudRole.ts][cloudrole] | Demonstrates how to set Cloud Role Name and Cloud Role Instance using OpenTelemetry Resource attributes. | +| [customMetric.ts][custommetric] | Demonstrates how to generate custom metrics that will be sent to Azure Monitor. | +| [customTrace.ts][customtrace] | Demonstrates how to generate custom traces that will be sent to Azure Monitor. | +| [liveMetrics.ts][livemetrics] | Demonstrates how to enable or disable Live Metrics for real-time monitoring. | +| [offlineStorage.ts][offlinestorage] | Demonstrates how to configure offline storage and automatic retries for telemetry. | +| [otlpExporter.ts][otlpexporter] | Demonstrates how to enable the OTLP exporter alongside Azure Monitor to send telemetry to two locations. | +| [redactQueryStrings.ts][redactquerystrings] | Demonstrates how to redact URL query strings from telemetry to protect sensitive information. | +| [sampling.ts][sampling] | Demonstrates how to enable sampling to reduce data ingestion volume and control costs. | + +## Prerequisites + +- [Node.js LTS](https://github.com/nodejs/release#release-schedule) (>= 20.0.0) +- [TypeScript](https://www.typescriptlang.org/) (install via `npm install -g typescript`) +- An [Azure subscription](https://azure.microsoft.com/free/) with an [Application Insights](https://learn.microsoft.com/azure/azure-monitor/app/app-insights-overview) resource + +## Setup + +1. Install dependencies: + +```bash +npm install +``` + +2. Compile the samples: + +```bash +npm run build +``` + +3. Copy `sample.env` to `.env` and fill in your connection string: + +```bash +cp sample.env .env +``` + +4. Run a sample: + +```bash +node dist/basicConnection.js +``` + +Or pass the connection string directly: + +```bash +APPLICATIONINSIGHTS_CONNECTION_STRING="" node dist/basicConnection.js +``` + +[basicconnection]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/basicConnection.ts +[cloudrole]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/cloudRole.ts +[custommetric]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/customMetric.ts +[customtrace]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/customTrace.ts +[livemetrics]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/liveMetrics.ts +[offlinestorage]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/offlineStorage.ts +[otlpexporter]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/otlpExporter.ts +[redactquerystrings]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/redactQueryStrings.ts +[sampling]: https://github.com/Azure/opentelemetry-distro-javascript/blob/main/samples/src/sampling.ts diff --git a/samples/package.json b/samples/package.json new file mode 100644 index 00000000..5899caa0 --- /dev/null +++ b/samples/package.json @@ -0,0 +1,33 @@ +{ + "name": "@microsoft/opentelemetry-samples", + "private": true, + "version": "1.0.0", + "description": "Microsoft OpenTelemetry distribution samples for TypeScript", + "engines": { + "node": ">=20.0.0" + }, + "scripts": { + "build": "tsc", + "prebuild": "rimraf dist/" + }, + "repository": { + "type": "git", + "url": "https://github.com/Azure/opentelemetry-distro-javascript" + }, + "author": "Microsoft Corporation", + "license": "MIT", + "dependencies": { + "@microsoft/opentelemetry": "latest", + "dotenv": "latest", + "@opentelemetry/api": "^1.9.0", + "@opentelemetry/resources": "^2.2.0", + "@opentelemetry/semantic-conventions": "^1.38.0", + "@opentelemetry/sdk-trace-base": "^2.2.0", + "@opentelemetry/exporter-trace-otlp-http": "^0.208.0" + }, + "devDependencies": { + "@types/node": "^20.0.0", + "rimraf": "latest", + "typescript": "~5.9.3" + } +} diff --git a/samples/src/basicConnection.ts b/samples/src/basicConnection.ts new file mode 100644 index 00000000..892de232 --- /dev/null +++ b/samples/src/basicConnection.ts @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Demonstrates how to configure Microsoft OpenTelemetry using a connection string. + */ + +import { useMicrosoftOpenTelemetry } from "@microsoft/opentelemetry"; +import "dotenv/config"; + +async function main(): Promise { + useMicrosoftOpenTelemetry({ + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + }, + }, + }); + + console.log("Microsoft OpenTelemetry configured successfully!"); + console.log("Telemetry will be sent to Azure Application Insights"); +} + +main().catch(console.error); diff --git a/samples/v1/typescript/src/cloudRole.ts b/samples/src/cloudRole.ts similarity index 73% rename from samples/v1/typescript/src/cloudRole.ts rename to samples/src/cloudRole.ts index 716b99c0..e4935f0a 100644 --- a/samples/v1/typescript/src/cloudRole.ts +++ b/samples/src/cloudRole.ts @@ -5,7 +5,7 @@ * @summary Demonstrates how to set Cloud Role Name and Cloud Role Instance using OpenTelemetry Resource attributes. */ -import { useAzureMonitor } from "@azure/monitor-opentelemetry"; +import { useMicrosoftOpenTelemetry } from "@microsoft/opentelemetry"; import { resourceFromAttributes } from "@opentelemetry/resources"; import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions"; import "dotenv/config"; @@ -17,17 +17,17 @@ async function main(): Promise { "service.instance.id": process.env.OTEL_SERVICE_INSTANCE_ID || "my-instance", }); - const options = { + useMicrosoftOpenTelemetry({ resource: customResource, - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + }, }, - }; - - useAzureMonitor(options); + }); - console.log("Azure Monitor configured with custom cloud role settings:"); + console.log("Microsoft OpenTelemetry configured with custom cloud role settings:"); console.log( ` Cloud Role Name: ${process.env.OTEL_SERVICE_NAMESPACE || "my-namespace"}.${process.env.OTEL_SERVICE_NAME || "my-service"}`, ); diff --git a/samples/v1/typescript/src/customMetric.ts b/samples/src/customMetric.ts similarity index 60% rename from samples/v1/typescript/src/customMetric.ts rename to samples/src/customMetric.ts index f665d8ad..1c63fc27 100644 --- a/samples/v1/typescript/src/customMetric.ts +++ b/samples/src/customMetric.ts @@ -5,19 +5,22 @@ * @summary Demonstrates how to generate custom metrics that will be sent to Azure Monitor. */ -import { useAzureMonitor, shutdownAzureMonitor } from "@azure/monitor-opentelemetry"; +import { + useMicrosoftOpenTelemetry, + shutdownMicrosoftOpenTelemetry, +} from "@microsoft/opentelemetry"; import { metrics } from "@opentelemetry/api"; import "dotenv/config"; async function main(): Promise { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + useMicrosoftOpenTelemetry({ + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + }, }, - }; - - useAzureMonitor(options); + }); const meter = metrics.getMeter("testMeter"); const customCounter = meter.createCounter("TestCounter"); @@ -27,7 +30,7 @@ async function main(): Promise { console.log("Custom metrics sent to Azure Monitor"); - await shutdownAzureMonitor(); + await shutdownMicrosoftOpenTelemetry(); } main().catch(console.error); diff --git a/samples/v1/typescript/src/customTrace.ts b/samples/src/customTrace.ts similarity index 71% rename from samples/v1/typescript/src/customTrace.ts rename to samples/src/customTrace.ts index ca9e395c..4f608f0e 100644 --- a/samples/v1/typescript/src/customTrace.ts +++ b/samples/src/customTrace.ts @@ -5,7 +5,10 @@ * @summary Demonstrates how to generate custom traces that will be sent to Azure Monitor. */ -import { useAzureMonitor, shutdownAzureMonitor } from "@azure/monitor-opentelemetry"; +import { + useMicrosoftOpenTelemetry, + shutdownMicrosoftOpenTelemetry, +} from "@microsoft/opentelemetry"; import { context, trace, type Span } from "@opentelemetry/api"; import "dotenv/config"; @@ -22,14 +25,14 @@ function doWork(parent: Span): void { } async function main(): Promise { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + useMicrosoftOpenTelemetry({ + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + }, }, - }; - - useAzureMonitor(options); + }); const tracer = trace.getTracer("testTracer"); const parentSpan = tracer.startSpan("main"); @@ -44,7 +47,7 @@ async function main(): Promise { console.log("Custom traces sent to Azure Monitor"); - await shutdownAzureMonitor(); + await shutdownMicrosoftOpenTelemetry(); } main().catch(console.error); diff --git a/samples/v1/typescript/src/liveMetrics.ts b/samples/src/liveMetrics.ts similarity index 50% rename from samples/v1/typescript/src/liveMetrics.ts rename to samples/src/liveMetrics.ts index 91ecfda0..10c2c609 100644 --- a/samples/v1/typescript/src/liveMetrics.ts +++ b/samples/src/liveMetrics.ts @@ -5,21 +5,21 @@ * @summary Demonstrates how to enable or disable Live Metrics for real-time monitoring. */ -import { useAzureMonitor } from "@azure/monitor-opentelemetry"; +import { useMicrosoftOpenTelemetry } from "@microsoft/opentelemetry"; import "dotenv/config"; async function main(): Promise { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + useMicrosoftOpenTelemetry({ + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + }, + enableLiveMetrics: true, }, - enableLiveMetrics: true, - }; + }); - useAzureMonitor(options); - - console.log("Azure Monitor configured with Live Metrics:"); + console.log("Microsoft OpenTelemetry configured with Live Metrics:"); console.log(" Live Metrics: ENABLED"); console.log(" Check Azure Portal > Application Insights > Live Metrics Stream"); } diff --git a/samples/src/offlineStorage.ts b/samples/src/offlineStorage.ts new file mode 100644 index 00000000..26607d06 --- /dev/null +++ b/samples/src/offlineStorage.ts @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +/** + * @summary Demonstrates how to configure offline storage and automatic retries for telemetry. + */ + +import { useMicrosoftOpenTelemetry } from "@microsoft/opentelemetry"; +import "dotenv/config"; + +async function main(): Promise { + useMicrosoftOpenTelemetry({ + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + storageDirectory: "path/to/storage/directory", + disableOfflineStorage: false, + }, + }, + }); + + console.log("Microsoft OpenTelemetry configured with offline storage:"); + console.log(" Offline Storage: ENABLED"); + console.log(" Telemetry will be cached locally when disconnected"); +} + +main().catch(console.error); diff --git a/samples/v1/typescript/src/otlpExporter.ts b/samples/src/otlpExporter.ts similarity index 69% rename from samples/v1/typescript/src/otlpExporter.ts rename to samples/src/otlpExporter.ts index 535602d2..a5f2fe4d 100644 --- a/samples/v1/typescript/src/otlpExporter.ts +++ b/samples/src/otlpExporter.ts @@ -5,7 +5,10 @@ * @summary Demonstrates how to enable the OTLP exporter alongside Azure Monitor to send telemetry to two locations. */ -import { useAzureMonitor, shutdownAzureMonitor } from "@azure/monitor-opentelemetry"; +import { + useMicrosoftOpenTelemetry, + shutdownMicrosoftOpenTelemetry, +} from "@microsoft/opentelemetry"; import { trace } from "@opentelemetry/api"; import { BatchSpanProcessor } from "@opentelemetry/sdk-trace-base"; import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http"; @@ -16,15 +19,15 @@ async function main(): Promise { url: "http://localhost:4318/v1/traces", }); - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + useMicrosoftOpenTelemetry({ + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + }, }, spanProcessors: [new BatchSpanProcessor(otlpExporter)], - }; - - useAzureMonitor(options); + }); // Generate a sample span to demonstrate dual export const tracer = trace.getTracer("otlpSampleTracer"); @@ -32,11 +35,11 @@ async function main(): Promise { span.setAttribute("sample.key", "sample-value"); span.end(); - console.log("Azure Monitor configured with dual export:"); + console.log("Microsoft OpenTelemetry configured with dual export:"); console.log(" Azure Monitor: Enabled"); console.log(" OTLP Exporter: Enabled (http://localhost:4318/v1/traces)"); - await shutdownAzureMonitor(); + await shutdownMicrosoftOpenTelemetry(); } main().catch(console.error); diff --git a/samples/v1/typescript/src/redactQueryStrings.ts b/samples/src/redactQueryStrings.ts similarity index 77% rename from samples/v1/typescript/src/redactQueryStrings.ts rename to samples/src/redactQueryStrings.ts index f252889d..7c565186 100644 --- a/samples/v1/typescript/src/redactQueryStrings.ts +++ b/samples/src/redactQueryStrings.ts @@ -5,7 +5,7 @@ * @summary Demonstrates how to redact URL query strings from telemetry to protect sensitive information. */ -import { useAzureMonitor } from "@azure/monitor-opentelemetry"; +import { useMicrosoftOpenTelemetry } from "@microsoft/opentelemetry"; import "dotenv/config"; /** @@ -40,17 +40,17 @@ class RedactQueryStringProcessor { } async function main(): Promise { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + useMicrosoftOpenTelemetry({ + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + }, }, spanProcessors: [new RedactQueryStringProcessor()], - }; + }); - useAzureMonitor(options); - - console.log("Azure Monitor configured with query string redaction:"); + console.log("Microsoft OpenTelemetry configured with query string redaction:"); console.log(" Query strings will be removed from HTTP telemetry attributes"); console.log(" Protects sensitive information (SAS tokens, API keys, etc.)"); } diff --git a/samples/v1/typescript/src/sampling.ts b/samples/src/sampling.ts similarity index 58% rename from samples/v1/typescript/src/sampling.ts rename to samples/src/sampling.ts index f1bc2b90..10bd8b5c 100644 --- a/samples/v1/typescript/src/sampling.ts +++ b/samples/src/sampling.ts @@ -5,22 +5,22 @@ * @summary Demonstrates how to enable sampling to reduce data ingestion volume and control costs. */ -import { useAzureMonitor } from "@azure/monitor-opentelemetry"; +import { useMicrosoftOpenTelemetry } from "@microsoft/opentelemetry"; import "dotenv/config"; async function main(): Promise { // A sampling ratio of 0.1 means approximately 10% of traces are sent - const options = { + useMicrosoftOpenTelemetry({ samplingRatio: 0.1, - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + azureMonitor: { + azureMonitorExporterOptions: { + connectionString: + process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", + }, }, - }; + }); - useAzureMonitor(options); - - console.log("Azure Monitor configured with sampling:"); + console.log("Microsoft OpenTelemetry configured with sampling:"); console.log(" Sampling Ratio: 10% (0.1)"); console.log(" This reduces data ingestion volume and costs"); } diff --git a/samples/v1/typescript/tsconfig.json b/samples/tsconfig.json similarity index 100% rename from samples/v1/typescript/tsconfig.json rename to samples/tsconfig.json diff --git a/samples/v1/javascript/README.md b/samples/v1/javascript/README.md deleted file mode 100644 index 88732d33..00000000 --- a/samples/v1/javascript/README.md +++ /dev/null @@ -1,78 +0,0 @@ ---- -page_type: sample -languages: - - javascript -products: - - azure-monitor -urlFragment: monitor-opentelemetry-javascript ---- - -# Azure Monitor OpenTelemetry client library samples for JavaScript - -These sample programs show how to use the JavaScript client libraries for Azure Monitor OpenTelemetry in some common scenarios. - -| **File Name** | **Description** | -| ------------------------------------------- | -------------------------------------------------------------------------------------------------------- | -| [basicConnection.js][basicconnection] | Demonstrates how to configure Azure Monitor OpenTelemetry using a connection string. | -| [cloudRole.js][cloudrole] | Demonstrates how to set Cloud Role Name and Cloud Role Instance using OpenTelemetry Resource attributes. | -| [customMetric.js][custommetric] | Demonstrates how to generate custom metrics that will be sent to Azure Monitor. | -| [customTrace.js][customtrace] | Demonstrates how to generate custom traces that will be sent to Azure Monitor. | -| [liveMetrics.js][livemetrics] | Demonstrates how to enable or disable Live Metrics for real-time monitoring. | -| [offlineStorage.js][offlinestorage] | Demonstrates how to configure offline storage and automatic retries for telemetry. | -| [otlpExporter.js][otlpexporter] | Demonstrates how to enable the OTLP exporter alongside Azure Monitor to send telemetry to two locations. | -| [redactQueryStrings.js][redactquerystrings] | Demonstrates how to redact URL query strings from telemetry to protect sensitive information. | -| [sampling.js][sampling] | Demonstrates how to enable sampling to reduce data ingestion volume and control costs. | - -## Prerequisites - -The sample programs are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule). - -You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs: - -- [Azure Application Insights workspace instance][createinstance_azureapplicationinsightsworkspaceinstance] - -Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function. - -Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package]. - -## Setup - -To run the samples using the published version of the package: - -1. Install the dependencies using `npm`: - -```bash -npm install -``` - -2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically. - -3. Run whichever samples you like (note that some samples may require additional setup, see the table above): - -```bash -node basicConnection.js -``` - -Alternatively, run a single sample with the required environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): - -```bash -npx cross-env APPLICATIONINSIGHTS_CONNECTION_STRING="" node basicConnection.js -``` - -## Next Steps - -Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. - -[basicconnection]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/basicConnection.js -[cloudrole]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/cloudRole.js -[custommetric]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/customMetric.js -[customtrace]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/customTrace.js -[livemetrics]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/liveMetrics.js -[offlinestorage]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/offlineStorage.js -[otlpexporter]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/otlpExporter.js -[redactquerystrings]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/redactQueryStrings.js -[sampling]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/javascript/sampling.js -[apiref]: https://learn.microsoft.com/javascript/api/@azure/monitor-opentelemetry -[freesub]: https://azure.microsoft.com/free/ -[createinstance_azureapplicationinsightsworkspaceinstance]: https://learn.microsoft.com/azure/azure-monitor/app/app-insights-overview -[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry/README.md diff --git a/samples/v1/javascript/basicConnection.js b/samples/v1/javascript/basicConnection.js deleted file mode 100644 index 62f2a3db..00000000 --- a/samples/v1/javascript/basicConnection.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to configure Azure Monitor OpenTelemetry using a connection string. - */ - -const { useAzureMonitor } = require("@azure/monitor-opentelemetry"); -require("dotenv/config"); - -async function main() { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - }; - - useAzureMonitor(options); - - console.log("Azure Monitor configured successfully!"); - console.log("Telemetry will be sent to Azure Application Insights"); -} - -main().catch(console.error); diff --git a/samples/v1/javascript/cloudRole.js b/samples/v1/javascript/cloudRole.js deleted file mode 100644 index aff1e7b7..00000000 --- a/samples/v1/javascript/cloudRole.js +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to set Cloud Role Name and Cloud Role Instance using OpenTelemetry Resource attributes. - */ - -const { useAzureMonitor } = require("@azure/monitor-opentelemetry"); -const { resourceFromAttributes } = require("@opentelemetry/resources"); -const { ATTR_SERVICE_NAME } = require("@opentelemetry/semantic-conventions"); -require("dotenv/config"); - -async function main() { - const customResource = resourceFromAttributes({ - [ATTR_SERVICE_NAME]: process.env.OTEL_SERVICE_NAME || "my-service", - "service.namespace": process.env.OTEL_SERVICE_NAMESPACE || "my-namespace", - "service.instance.id": process.env.OTEL_SERVICE_INSTANCE_ID || "my-instance", - }); - - const options = { - resource: customResource, - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - }; - - useAzureMonitor(options); - - console.log("Azure Monitor configured with custom cloud role settings:"); - console.log( - ` Cloud Role Name: ${process.env.OTEL_SERVICE_NAMESPACE || "my-namespace"}.${process.env.OTEL_SERVICE_NAME || "my-service"}`, - ); - console.log(` Cloud Role Instance: ${process.env.OTEL_SERVICE_INSTANCE_ID || "my-instance"}`); -} - -main().catch(console.error); diff --git a/samples/v1/javascript/customMetric.js b/samples/v1/javascript/customMetric.js deleted file mode 100644 index ac544e05..00000000 --- a/samples/v1/javascript/customMetric.js +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to generate custom metrics that will be sent to Azure Monitor. - */ - -const { useAzureMonitor, shutdownAzureMonitor } = require("@azure/monitor-opentelemetry"); -const { metrics } = require("@opentelemetry/api"); -require("dotenv/config"); - -async function main() { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - }; - - useAzureMonitor(options); - - const meter = metrics.getMeter("testMeter"); - const customCounter = meter.createCounter("TestCounter"); - customCounter.add(1); - customCounter.add(2); - customCounter.add(3); - - console.log("Custom metrics sent to Azure Monitor"); - - await shutdownAzureMonitor(); -} - -main().catch(console.error); diff --git a/samples/v1/javascript/customTrace.js b/samples/v1/javascript/customTrace.js deleted file mode 100644 index 9bf474cb..00000000 --- a/samples/v1/javascript/customTrace.js +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to generate custom traces that will be sent to Azure Monitor. - */ - -const { useAzureMonitor, shutdownAzureMonitor } = require("@azure/monitor-opentelemetry"); -const { context, trace } = require("@opentelemetry/api"); -require("dotenv/config"); - -function doWork(parent) { - const ctx = trace.setSpan(context.active(), parent); - const span = trace.getTracer("testTracer").startSpan("doWork", undefined, ctx); - - try { - span.setAttribute("key", "value"); - span.addEvent("invoking doWork"); - } finally { - span.end(); - } -} - -async function main() { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - }; - - useAzureMonitor(options); - - const tracer = trace.getTracer("testTracer"); - const parentSpan = tracer.startSpan("main"); - - try { - for (let i = 0; i < 10; i += 1) { - doWork(parentSpan); - } - } finally { - parentSpan.end(); - } - - console.log("Custom traces sent to Azure Monitor"); - - await shutdownAzureMonitor(); -} - -main().catch(console.error); diff --git a/samples/v1/javascript/liveMetrics.js b/samples/v1/javascript/liveMetrics.js deleted file mode 100644 index b655e73a..00000000 --- a/samples/v1/javascript/liveMetrics.js +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to enable or disable Live Metrics for real-time monitoring. - */ - -const { useAzureMonitor } = require("@azure/monitor-opentelemetry"); -require("dotenv/config"); - -async function main() { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - enableLiveMetrics: true, - }; - - useAzureMonitor(options); - - console.log("Azure Monitor configured with Live Metrics:"); - console.log(" Live Metrics: ENABLED"); - console.log(" Check Azure Portal > Application Insights > Live Metrics Stream"); -} - -main().catch(console.error); diff --git a/samples/v1/javascript/offlineStorage.js b/samples/v1/javascript/offlineStorage.js deleted file mode 100644 index 3e788780..00000000 --- a/samples/v1/javascript/offlineStorage.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to configure offline storage and automatic retries for telemetry. - */ - -const { useAzureMonitor } = require("@azure/monitor-opentelemetry"); -require("dotenv/config"); - -async function main() { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - storageDirectory: "path/to/storage/directory", - disableOfflineStorage: false, - }, - }; - - useAzureMonitor(options); - - console.log("Azure Monitor configured with offline storage:"); - console.log(" Offline Storage: ENABLED"); - console.log(" Telemetry will be cached locally when disconnected"); -} - -main().catch(console.error); diff --git a/samples/v1/javascript/otlpExporter.js b/samples/v1/javascript/otlpExporter.js deleted file mode 100644 index d3010972..00000000 --- a/samples/v1/javascript/otlpExporter.js +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to enable the OTLP exporter alongside Azure Monitor to send telemetry to two locations. - */ - -const { useAzureMonitor, shutdownAzureMonitor } = require("@azure/monitor-opentelemetry"); -const { trace } = require("@opentelemetry/api"); -const { BatchSpanProcessor } = require("@opentelemetry/sdk-trace-base"); -const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-http"); -require("dotenv/config"); - -async function main() { - const otlpExporter = new OTLPTraceExporter({ - url: "http://localhost:4318/v1/traces", - }); - - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - spanProcessors: [new BatchSpanProcessor(otlpExporter)], - }; - - useAzureMonitor(options); - - // Generate a sample span to demonstrate dual export - const tracer = trace.getTracer("otlpSampleTracer"); - const span = tracer.startSpan("sample-operation"); - span.setAttribute("sample.key", "sample-value"); - span.end(); - - console.log("Azure Monitor configured with dual export:"); - console.log(" Azure Monitor: Enabled"); - console.log(" OTLP Exporter: Enabled (http://localhost:4318/v1/traces)"); - - await shutdownAzureMonitor(); -} - -main().catch(console.error); diff --git a/samples/v1/javascript/package.json b/samples/v1/javascript/package.json deleted file mode 100644 index 0378cf76..00000000 --- a/samples/v1/javascript/package.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "name": "@azure-samples/monitor-opentelemetry-js", - "private": true, - "version": "1.0.0", - "description": "Azure Monitor OpenTelemetry client library samples for JavaScript", - "engines": { - "node": ">=20.0.0" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/Azure/azure-sdk-for-js.git", - "directory": "sdk/monitor/monitor-opentelemetry" - }, - "keywords": [ - "node", - "azure", - "monitor", - "javascript", - "opentelemetry", - "distro", - "cloud" - ], - "author": "Microsoft Corporation", - "license": "MIT", - "bugs": { - "url": "https://github.com/Azure/azure-sdk-for-js/issues" - }, - "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry", - "dependencies": { - "@azure/monitor-opentelemetry": "latest", - "dotenv": "latest", - "@opentelemetry/resources": "^2.2.0", - "@opentelemetry/semantic-conventions": "^1.38.0", - "@opentelemetry/api": "^1.9.0", - "@opentelemetry/sdk-trace-base": "^2.2.0", - "@opentelemetry/exporter-trace-otlp-http": "^0.204.0" - }, - "devDependencies": { - "cross-env": "latest" - } -} diff --git a/samples/v1/javascript/redactQueryStrings.js b/samples/v1/javascript/redactQueryStrings.js deleted file mode 100644 index 56fd6ccc..00000000 --- a/samples/v1/javascript/redactQueryStrings.js +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to redact URL query strings from telemetry to protect sensitive information. - */ - -const { useAzureMonitor } = require("@azure/monitor-opentelemetry"); -require("dotenv/config"); - -/** - * Custom span processor that removes query strings from HTTP span attributes. - */ -class RedactQueryStringProcessor { - forceFlush() { - return Promise.resolve(); - } - - onStart() { - // No action needed on span start - } - - shutdown() { - return Promise.resolve(); - } - - onEnd(span) { - const httpAttributes = ["http.route", "http.url", "http.target"]; - - for (const attr of httpAttributes) { - const value = span.attributes[attr]; - if (typeof value === "string") { - const queryIndex = value.indexOf("?"); - if (queryIndex !== -1) { - span.attributes[attr] = value.substring(0, queryIndex); - } - } - } - } -} - -async function main() { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - spanProcessors: [new RedactQueryStringProcessor()], - }; - - useAzureMonitor(options); - - console.log("Azure Monitor configured with query string redaction:"); - console.log(" Query strings will be removed from HTTP telemetry attributes"); - console.log(" Protects sensitive information (SAS tokens, API keys, etc.)"); -} - -main().catch(console.error); diff --git a/samples/v1/javascript/sampling.js b/samples/v1/javascript/sampling.js deleted file mode 100644 index 4f84a6a9..00000000 --- a/samples/v1/javascript/sampling.js +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to enable sampling to reduce data ingestion volume and control costs. - */ - -const { useAzureMonitor } = require("@azure/monitor-opentelemetry"); -require("dotenv/config"); - -async function main() { - // A sampling ratio of 0.1 means approximately 10% of traces are sent - const options = { - samplingRatio: 0.1, - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - }; - - useAzureMonitor(options); - - console.log("Azure Monitor configured with sampling:"); - console.log(" Sampling Ratio: 10% (0.1)"); - console.log(" This reduces data ingestion volume and costs"); -} - -main().catch(console.error); diff --git a/samples/v1/typescript/README.md b/samples/v1/typescript/README.md deleted file mode 100644 index 88beeb6f..00000000 --- a/samples/v1/typescript/README.md +++ /dev/null @@ -1,91 +0,0 @@ ---- -page_type: sample -languages: - - typescript -products: - - azure-monitor -urlFragment: monitor-opentelemetry-typescript ---- - -# Azure Monitor OpenTelemetry client library samples for TypeScript - -These sample programs show how to use the TypeScript client libraries for Azure Monitor OpenTelemetry in some common scenarios. - -| **File Name** | **Description** | -| ------------------------------------------- | -------------------------------------------------------------------------------------------------------- | -| [basicConnection.ts][basicconnection] | Demonstrates how to configure Azure Monitor OpenTelemetry using a connection string. | -| [cloudRole.ts][cloudrole] | Demonstrates how to set Cloud Role Name and Cloud Role Instance using OpenTelemetry Resource attributes. | -| [customMetric.ts][custommetric] | Demonstrates how to generate custom metrics that will be sent to Azure Monitor. | -| [customTrace.ts][customtrace] | Demonstrates how to generate custom traces that will be sent to Azure Monitor. | -| [liveMetrics.ts][livemetrics] | Demonstrates how to enable or disable Live Metrics for real-time monitoring. | -| [offlineStorage.ts][offlinestorage] | Demonstrates how to configure offline storage and automatic retries for telemetry. | -| [otlpExporter.ts][otlpexporter] | Demonstrates how to enable the OTLP exporter alongside Azure Monitor to send telemetry to two locations. | -| [redactQueryStrings.ts][redactquerystrings] | Demonstrates how to redact URL query strings from telemetry to protect sensitive information. | -| [sampling.ts][sampling] | Demonstrates how to enable sampling to reduce data ingestion volume and control costs. | - -## Prerequisites - -The sample programs are compatible with [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule). - -Before running the samples in Node, they must be compiled to JavaScript using the TypeScript compiler. For more information on TypeScript, see the [TypeScript documentation][typescript]. Install the TypeScript compiler using: - -```bash -npm install -g typescript -``` - -You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs: - -- [Azure Application Insights workspace instance][createinstance_azureapplicationinsightsworkspaceinstance] - -Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function. - -Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package]. - -## Setup - -To run the samples using the published version of the package: - -1. Install the dependencies using `npm`: - -```bash -npm install -``` - -2. Compile the samples: - -```bash -npm run build -``` - -3. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically. - -4. Run whichever samples you like (note that some samples may require additional setup, see the table above): - -```bash -node dist/basicConnection.js -``` - -Alternatively, run a single sample with the required environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform): - -```bash -npx cross-env APPLICATIONINSIGHTS_CONNECTION_STRING="" node dist/basicConnection.js -``` - -## Next Steps - -Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients. - -[basicconnection]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/basicConnection.ts -[cloudrole]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/cloudRole.ts -[custommetric]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/customMetric.ts -[customtrace]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/customTrace.ts -[livemetrics]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/liveMetrics.ts -[offlinestorage]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/offlineStorage.ts -[otlpexporter]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/otlpExporter.ts -[redactquerystrings]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/redactQueryStrings.ts -[sampling]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry/samples/v1/typescript/src/sampling.ts -[apiref]: https://learn.microsoft.com/javascript/api/@azure/monitor-opentelemetry -[freesub]: https://azure.microsoft.com/free/ -[createinstance_azureapplicationinsightsworkspaceinstance]: https://learn.microsoft.com/azure/azure-monitor/app/app-insights-overview -[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry/README.md -[typescript]: https://www.typescriptlang.org/docs/home.html diff --git a/samples/v1/typescript/package.json b/samples/v1/typescript/package.json deleted file mode 100644 index 2c763686..00000000 --- a/samples/v1/typescript/package.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "name": "@azure-samples/monitor-opentelemetry-ts", - "private": true, - "version": "1.0.0", - "description": "Azure Monitor OpenTelemetry client library samples for TypeScript", - "engines": { - "node": ">=20.0.0" - }, - "scripts": { - "build": "tsc", - "prebuild": "rimraf dist/" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/Azure/azure-sdk-for-js.git", - "directory": "sdk/monitor/monitor-opentelemetry" - }, - "keywords": [ - "node", - "azure", - "monitor", - "javascript", - "opentelemetry", - "distro", - "cloud" - ], - "author": "Microsoft Corporation", - "license": "MIT", - "bugs": { - "url": "https://github.com/Azure/azure-sdk-for-js/issues" - }, - "homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry", - "dependencies": { - "@azure/monitor-opentelemetry": "latest", - "dotenv": "latest", - "@opentelemetry/resources": "^2.2.0", - "@opentelemetry/semantic-conventions": "^1.38.0", - "@opentelemetry/api": "^1.9.0", - "@opentelemetry/sdk-trace-base": "^2.2.0", - "@opentelemetry/exporter-trace-otlp-http": "^0.204.0" - }, - "devDependencies": { - "@types/node": "^20.0.0", - "cross-env": "latest", - "rimraf": "latest", - "typescript": "~5.9.3" - } -} diff --git a/samples/v1/typescript/src/basicConnection.ts b/samples/v1/typescript/src/basicConnection.ts deleted file mode 100644 index 39a0f9ef..00000000 --- a/samples/v1/typescript/src/basicConnection.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to configure Azure Monitor OpenTelemetry using a connection string. - */ - -import { useAzureMonitor } from "@azure/monitor-opentelemetry"; -import "dotenv/config"; - -async function main(): Promise { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - }, - }; - - useAzureMonitor(options); - - console.log("Azure Monitor configured successfully!"); - console.log("Telemetry will be sent to Azure Application Insights"); -} - -main().catch(console.error); diff --git a/samples/v1/typescript/src/offlineStorage.ts b/samples/v1/typescript/src/offlineStorage.ts deleted file mode 100644 index 294db43c..00000000 --- a/samples/v1/typescript/src/offlineStorage.ts +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -/** - * @summary Demonstrates how to configure offline storage and automatic retries for telemetry. - */ - -import { useAzureMonitor } from "@azure/monitor-opentelemetry"; -import "dotenv/config"; - -async function main(): Promise { - const options = { - azureMonitorExporterOptions: { - connectionString: - process.env.APPLICATIONINSIGHTS_CONNECTION_STRING || "", - storageDirectory: "path/to/storage/directory", - disableOfflineStorage: false, - }, - }; - - useAzureMonitor(options); - - console.log("Azure Monitor configured with offline storage:"); - console.log(" Offline Storage: ENABLED"); - console.log(" Telemetry will be cached locally when disconnected"); -} - -main().catch(console.error); From 2336dc1cf1b5afef361423bf6c33953e06d2fa95 Mon Sep 17 00:00:00 2001 From: Hector Hernandez <39923391+hectorhdzg@users.noreply.github.com> Date: Thu, 9 Apr 2026 16:42:34 -0700 Subject: [PATCH 2/2] Update README.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 07da5591..55f80ac1 100644 --- a/README.md +++ b/README.md @@ -44,10 +44,10 @@ That's it — traces, metrics, and logs are collected automatically with built-i | Option | Type | Default | Description | |---|---|---|---| | `azureMonitorExporterOptions` | `AzureMonitorExporterOptions` | — | Exporter config including `connectionString`, `storageDirectory`, `disableOfflineStorage` | -| `enableLiveMetrics` | `boolean` | `false` | Enable Live Metrics streaming | +| `enableLiveMetrics` | `boolean` | `true` | Enable Live Metrics streaming | | `enableStandardMetrics` | `boolean` | `true` | Enable standard metrics collection | -| `enableTraceBasedSamplingForLogs` | `boolean` | `true` | Enable log sampling based on trace | -| `enablePerformanceCounters` | `boolean` | — | Enable performance counter collection | +| `enableTraceBasedSamplingForLogs` | `boolean` | `false` | Enable log sampling based on trace | +| `enablePerformanceCounters` | `boolean` | `true` | Enable performance counter collection | | `browserSdkLoaderOptions` | `BrowserSdkLoaderOptions` | — | Application Insights browser SDK loader config | ### OTLP via environment variables