-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into warn-on-already-required
- Loading branch information
Showing
26 changed files
with
1,526 additions
and
9 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
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
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
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 @@ | ||
service_account_key.json |
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,37 @@ | ||
# Overview | ||
|
||
This example shows how to use [@opentelemetry/tracing](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-tracing) to instrument a simple Node.js application - e.g. a batch job - and export spans either to [Stackdriver Trace](https://cloud.google.com/trace/). | ||
|
||
## Installation | ||
|
||
```sh | ||
$ # from this directory | ||
$ npm install | ||
``` | ||
|
||
## Authenticate | ||
|
||
If you are running in a GCP environment, the exporter will automatically authenticate as the service account of your environment. Please make sure that it has permission to access stackdriver trace. | ||
|
||
If you are not running in a GCP environment you will need to create a service account and save the service account key json in the root of this example named `service_account_key.json`. For more information, visit <https://cloud.google.com/docs/authentication/>. | ||
|
||
## Run the Application | ||
|
||
```sh | ||
$ # from this directory | ||
$ npm start | ||
``` | ||
|
||
## View traces | ||
|
||
https://console.cloud.google.com/traces/list?project=your-project-id | ||
|
||
<p align="center"><img src="images/trace.png?raw=true"/></p> | ||
|
||
## Useful links | ||
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
- For more information on tracing, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-tracing> | ||
|
||
## LICENSE | ||
|
||
Apache License 2.0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,64 @@ | ||
'use strict'; | ||
|
||
const opentelemetry = require('@opentelemetry/core'); | ||
const { BasicTracer, SimpleSpanProcessor } = require('@opentelemetry/tracing'); | ||
const { CanonicalCode } = require('@opentelemetry/types'); | ||
const { StackdriverTraceExporter } = require('@opentelemetry/exporter-stackdriver-trace'); | ||
|
||
// Initialize an exporter | ||
const exporter = new StackdriverTraceExporter({ | ||
serviceName: 'basic-service', | ||
logger: new opentelemetry.ConsoleLogger() | ||
}); | ||
|
||
const tracer = new BasicTracer(); | ||
|
||
// Configure span processor to send spans to the provided exporter | ||
tracer.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
|
||
// Initialize the OpenTelemetry APIs to use the BasicTracer bindings | ||
opentelemetry.initGlobalTracer(tracer); | ||
|
||
// Create a span. A span must be closed. | ||
const root = opentelemetry.getTracer().startSpan('main'); | ||
const related = opentelemetry.getTracer().startSpan('related', { | ||
links: [{ spanContext: root.context() }] | ||
}); | ||
|
||
for (let i = 0; i < 10; i++) { | ||
doWork(root); | ||
doWork(related); | ||
} | ||
// Be sure to end the span. | ||
root.setStatus({ | ||
code: CanonicalCode.UNKNOWN | ||
}) | ||
root.end(); | ||
related.end(); | ||
|
||
// flush and close the connection. | ||
exporter.shutdown(); | ||
|
||
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 | ||
}); | ||
|
||
// simulate some random work. | ||
const work = Math.floor(Math.random() * 40000000); | ||
for (let i = 0; i <= work; i++) { } | ||
|
||
if (work % 2 === 1) { | ||
span.setStatus({ | ||
code: CanonicalCode.UNKNOWN | ||
}) | ||
} | ||
|
||
// Set attributes to the span. | ||
span.setAttribute('key', 'value'); | ||
|
||
// Annotate our span to capture metadata about our operation | ||
span.addEvent('invoking doWork').end(); | ||
} |
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,37 @@ | ||
{ | ||
"name": "example-stackdriver-trace", | ||
"private": true, | ||
"version": "0.3.1", | ||
"description": "Example of using @opentelemetry/exporter-stackdriver-trace in Node.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"start": "cross-env GOOGLE_APPLICATION_CREDENTIALS=service_account_key.json node ./index.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/open-telemetry/opentelemetry-js.git" | ||
}, | ||
"keywords": [ | ||
"opentelemetry", | ||
"http", | ||
"tracing" | ||
], | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"author": "OpenTelemetry Authors", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/open-telemetry/opentelemetry-js/issues" | ||
}, | ||
"dependencies": { | ||
"@opentelemetry/core": "^0.3.1", | ||
"@opentelemetry/exporter-stackdriver-trace": "^0.3.1", | ||
"@opentelemetry/tracing": "^0.3.1", | ||
"@opentelemetry/types": "^0.3.1" | ||
}, | ||
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", | ||
"devDependencies": { | ||
"cross-env": "^6.0.0" | ||
} | ||
} |
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
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,4 @@ | ||
/bin | ||
/coverage | ||
/doc | ||
/test |
Oops, something went wrong.