-
Notifications
You must be signed in to change notification settings - Fork 789
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add basic tracer example (#277)
- Loading branch information
1 parent
7fff538
commit 45be4b5
Showing
1 changed file
with
31 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const opentelemetry = require('@opentelemetry/core'); | ||
const { BasicTracer } = require('@opentelemetry/basic-tracer'); | ||
|
||
// Initialize the OpenTelemetry APIs to use the BasicTracer bindings | ||
opentelemetry.initGlobalTracer(new BasicTracer()); | ||
|
||
// Create a span. A span must be closed. | ||
const span = opentelemetry.getTracer().startSpan('main'); | ||
for (let i = 0; i < 10; i++) { | ||
doWork(span); | ||
} | ||
// Be sure to end the span. | ||
span.end(); | ||
|
||
function doWork (parent) { | ||
// Start another span. In this example, the main method already started a | ||
// span, so that'll be the parent span, and this will be a child span. | ||
const span = opentelemetry.getTracer().startSpan('doWork', { | ||
parent: parent | ||
}); | ||
|
||
for (let i = 0; i <= 40000000; i++) {} // short delay | ||
|
||
// Set attributes to the span. | ||
span.setAttribute('key', 'value'); | ||
|
||
// Annotate our span to capture metadata about our operation | ||
span.addEvent('invoking doWork').end(); | ||
} |