Skip to content

Commit 6202c80

Browse files
[monitor-opentelemetry-exporter] update old dependencies (Azure#22127)
1 parent 1d1b9c4 commit 6202c80

File tree

14 files changed

+716
-21
lines changed

14 files changed

+716
-21
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 29 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/monitor/monitor-opentelemetry-exporter/package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"build:browser": "echo skipped",
1313
"build:test": "echo skipped",
1414
"build:node": "tsc -p . && dev-tool run bundle --browser-test=false",
15+
"build:samples": "dev-tool samples publish --force",
1516
"build": "npm run build:node && npm run build:browser && api-extractor run --local",
1617
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
1718
"extract-api": "tsc -p . && api-extractor run --local",
@@ -34,9 +35,7 @@
3435
"report": "nyc report --reporter=json",
3536
"test-opentelemetry-versions": "node test-opentelemetry-versions.js 2>&1",
3637
"prepare": "npm run build",
37-
"pack": "npm pack 2>&1",
38-
"build:samples": "echo Obsolete.",
39-
"execute:samples": "dev-tool samples run samples-dev"
38+
"pack": "npm pack 2>&1"
4039
},
4140
"engines": {
4241
"node": ">=12.0.0"
@@ -86,15 +85,15 @@
8685
"@azure/dev-tool": "^1.0.0",
8786
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
8887
"@microsoft/api-extractor": "7.18.11",
89-
"@opentelemetry/instrumentation": "^0.28.0",
90-
"@opentelemetry/instrumentation-http": "^0.28.0",
88+
"@opentelemetry/instrumentation": "^0.29.2",
89+
"@opentelemetry/instrumentation-http": "^0.29.2",
9190
"@opentelemetry/sdk-trace-node": "^1.2.0",
9291
"@types/mocha": "^7.0.2",
9392
"@types/node": "^12.0.0",
9493
"dotenv": "^8.2.0",
9594
"eslint": "^8.0.0",
9695
"eslint-plugin-node": "^11.1.0",
97-
"execa": "^5.0.0",
96+
"execa": "^6.0.0",
9897
"mocha": "^7.1.1",
9998
"nock": "^12.0.3",
10099
"nyc": "^15.0.0",
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- javascript
5+
products:
6+
- azure-monitor
7+
urlFragment: monitor-opentelemetry-exporter-javascript-beta
8+
---
9+
10+
# Azure Monitor Trace Exporter client library samples for JavaScript (Beta)
11+
12+
These sample programs show how to use the JavaScript client libraries for Azure Monitor Trace Exporter in some common scenarios.
13+
14+
| **File Name** | **Description** |
15+
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
16+
| [basicTracerNode.js][basictracernode] | use opentelemetry tracing to instrument a Node.js application. Basic use of Tracing in Node.js application. |
17+
| [httpSample.js][httpsample] | demonstrates OpenTelemetry http Instrumentation. It is about how OpenTelemetry will instrument the Node.js native http module. |
18+
19+
## Prerequisites
20+
21+
The sample programs are compatible with [LTS versions of Node.js](https://nodejs.org/about/releases/).
22+
23+
You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs:
24+
25+
- [Azure Application Insights workspace instance][createinstance_azureapplicationinsightsworkspaceinstance]
26+
27+
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.
28+
29+
Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package].
30+
31+
## Setup
32+
33+
To run the samples using the published version of the package:
34+
35+
1. Install the dependencies using `npm`:
36+
37+
```bash
38+
npm install
39+
```
40+
41+
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.
42+
43+
3. Run whichever samples you like (note that some samples may require additional setup, see the table above):
44+
45+
```bash
46+
node basicTracerNode.js
47+
```
48+
49+
Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform):
50+
51+
```bash
52+
npx cross-env APPLICATIONINSIGHTS_CONNECTION_STRING="<applicationinsights connection string>" node basicTracerNode.js
53+
```
54+
55+
## Next Steps
56+
57+
Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.
58+
59+
[basictracernode]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry-exporter/samples/v1-beta/javascript/basicTracerNode.js
60+
[httpsample]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/monitor/monitor-opentelemetry-exporter/samples/v1-beta/javascript/httpSample.js
61+
[apiref]: https://docs.microsoft.com/javascript/api/@azure/monitor-opentelemetry-exporter
62+
[freesub]: https://azure.microsoft.com/free/
63+
[createinstance_azureapplicationinsightsworkspaceinstance]: https://docs.microsoft.com/azure/azure-monitor/app/app-insights-overview
64+
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/monitor/monitor-opentelemetry-exporter/README.md
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This example shows how to use
6+
* [@opentelemetry/sdk-trace-base](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-tracing)
7+
* to instrument a simple Node.js application - e.g. a batch job.
8+
*
9+
* @summary use opentelemetry tracing to instrument a Node.js application. Basic use of Tracing in Node.js application.
10+
*/
11+
12+
const opentelemetry = require("@opentelemetry/api");
13+
const { Resource } = require("@opentelemetry/resources");
14+
const { SemanticResourceAttributes } = require("@opentelemetry/semantic-conventions");
15+
const { BasicTracerProvider, SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");
16+
const { AzureMonitorTraceExporter } = require("@azure/monitor-opentelemetry-exporter");
17+
18+
// Load the .env file if it exists
19+
require("dotenv").config();
20+
21+
const provider = new BasicTracerProvider({
22+
resource: new Resource({
23+
[SemanticResourceAttributes.SERVICE_NAME]: "basic-service",
24+
}),
25+
});
26+
27+
// Configure span processor to send spans to the exporter
28+
const exporter = new AzureMonitorTraceExporter({
29+
connectionString:
30+
process.env["APPLICATIONINSIGHTS_CONNECTION_STRING"] || "<your connection string>",
31+
});
32+
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
33+
34+
/**
35+
* Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings.
36+
*
37+
* This registers the tracer provider with the OpenTelemetry API as the global
38+
* tracer provider. This means when you call API methods like
39+
* `opentelemetry.trace.getTracer`, they will use this tracer provider. If you
40+
* do not register a global tracer provider, instrumentation which calls these
41+
* methods will receive no-op implementations.
42+
*/
43+
provider.register();
44+
const tracer = opentelemetry.trace.getTracer("example-basic-tracer-node");
45+
46+
async function main() {
47+
// Create a span. A span must be closed.
48+
const parentSpan = tracer.startSpan("main");
49+
for (let i = 0; i < 10; i += 1) {
50+
doWork(parentSpan);
51+
}
52+
// Be sure to end the span.
53+
parentSpan.end();
54+
55+
// flush and close the connection.
56+
exporter.shutdown();
57+
}
58+
59+
function doWork(parent) {
60+
// Start another span. In this example, the main method already started a
61+
// span, so that'll be the parent span, and this will be a child span.
62+
const ctx = opentelemetry.trace.setSpan(opentelemetry.context.active(), parent);
63+
const span = tracer.startSpan("doWork", undefined, ctx);
64+
65+
// simulate some random work.
66+
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
67+
// empty
68+
}
69+
70+
// Set attributes to the span.
71+
span.setAttribute("key", "value");
72+
73+
// Annotate our span to capture metadata about our operation
74+
span.addEvent("invoking doWork");
75+
76+
span.end();
77+
}
78+
79+
main().catch((error) => {
80+
console.error("An error occurred:", error);
81+
process.exit(1);
82+
});
83+
84+
module.exports = { main };

0 commit comments

Comments
 (0)