-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Bartlomiej Obecny <[email protected]>
- Loading branch information
Showing
23 changed files
with
1,514 additions
and
8 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,45 @@ | ||
# Overview | ||
|
||
OpenTelemetry Router Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (we can use Zipkin or Jaeger for this example). This example demonstrates tracing calls made to Router API. All generated spans include following attributes: | ||
|
||
- `http.route`: resolved route; | ||
- `router.name`: the name of the handler or middleware; | ||
- `router.type`: either `middleware` or `request_handler`; | ||
- `router.version`: `router` version running. | ||
|
||
## Setup | ||
|
||
Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html) | ||
or | ||
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one) | ||
|
||
## Run the Application | ||
|
||
First install the dependencies: | ||
|
||
```sh | ||
npm install | ||
``` | ||
|
||
### Zipkin | ||
|
||
```sh | ||
npm run zipkin:server # Run the server | ||
npm run zipkin:client # Run the client in a separate terminal | ||
``` | ||
|
||
### Jaeger | ||
|
||
```sh | ||
npm run jaeger:server # Run the server | ||
npm run jaeger:client # Run the client in a separate terminal | ||
``` | ||
|
||
## Useful links | ||
|
||
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-node> | ||
|
||
## LICENSE | ||
|
||
Apache License 2.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
// required to initialize the service name for the auto-instrumentation | ||
require('./tracer')('example-client'); | ||
// eslint-disable-next-line import/order | ||
const http = require('http'); | ||
|
||
/** A function which makes requests and handles response. */ | ||
function makeRequest(path) { | ||
// span corresponds to outgoing requests. Here, we have manually created | ||
// the span, which is created to track work that happens outside of the | ||
// request lifecycle entirely. | ||
http.get({ | ||
host: 'localhost', | ||
headers: { | ||
accept: 'text/plain', | ||
}, | ||
port: 8080, | ||
path, | ||
}, (response) => { | ||
response.on('data', (chunk) => console.log(path, '::', chunk.toString('utf8'))); | ||
response.on('end', () => { | ||
console.log(path, 'status', response.statusCode); | ||
}); | ||
}); | ||
|
||
// The process must live for at least the interval past any traces that | ||
// must be exported, or some risk being lost if they are recorded after the | ||
// last export. | ||
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.'); | ||
setTimeout(() => { console.log('Completed.'); }, 5000); | ||
} | ||
|
||
makeRequest('/hello/world'); | ||
// 404 | ||
makeRequest('/bye/world'); | ||
// error | ||
makeRequest('/err'); |
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,45 @@ | ||
{ | ||
"name": "router-example", | ||
"private": true, | ||
"version": "0.16.0", | ||
"description": "Example of router integration with OpenTelemetry", | ||
"main": "index.js", | ||
"scripts": { | ||
"zipkin:server": "cross-env EXPORTER=zipkin node ./server.js", | ||
"zipkin:client": "cross-env EXPORTER=zipkin node ./client.js", | ||
"jaeger:server": "cross-env EXPORTER=jaeger node ./server.js", | ||
"jaeger:client": "cross-env EXPORTER=jaeger node ./client.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/api": "^0.18.0", | ||
"@opentelemetry/exporter-jaeger": "^0.18.0", | ||
"@opentelemetry/exporter-zipkin": "^0.18.0", | ||
"@opentelemetry/instrumentation": "^0.18.0", | ||
"@opentelemetry/instrumentation-http": "^0.18.0", | ||
"@opentelemetry/instrumentation-router": "^0.16.0", | ||
"@opentelemetry/node": "^0.18.0", | ||
"@opentelemetry/tracing": "^0.18.0", | ||
"router": "^1.3.5" | ||
}, | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
require('./tracer')('example-router-server'); | ||
|
||
// `setDefaultName` shows up in spans as the name | ||
const setDefaultName = (req, res, next) => { | ||
req.defaultName = 'Stranger'; | ||
next(); | ||
}; | ||
|
||
const http = require('http'); | ||
const Router = require('router'); | ||
|
||
const router = Router(); | ||
|
||
router.use(setDefaultName); | ||
|
||
router.param('name', (req, res, next, name) => { | ||
req.params.name = typeof name === 'string' ? name.toUpperCase() : req.defaultName; | ||
next(); | ||
}); | ||
|
||
// eslint-disable-next-line prefer-arrow-callback | ||
router.get('/hello/:name', function greetingHandler(req, res) { | ||
res.setHeader('Content-Type', 'text/plain; charset=utf-8'); | ||
res.end(`Hello, ${req.params.name}!`); | ||
}); | ||
|
||
// eslint-disable-next-line prefer-arrow-callback | ||
router.get('/err', function erroringRoute(req, res, next) { | ||
next(new Error('Broken!')); | ||
}); | ||
|
||
// eslint-disable-next-line prefer-arrow-callback, func-names | ||
const server = http.createServer(function (req, res) { | ||
router(req, res, (error) => { | ||
if (error) { | ||
res.statusCode = 500; | ||
} else { | ||
res.statusCode = 404; | ||
} | ||
res.end(); | ||
}); | ||
}); | ||
|
||
server.listen(8080); |
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,50 @@ | ||
'use strict'; | ||
|
||
const opentelemetry = require('@opentelemetry/api'); | ||
|
||
const { diag, DiagConsoleLogger, DiagLogLevel } = opentelemetry; | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.VERBOSE); | ||
|
||
const { registerInstrumentations } = require('@opentelemetry/instrumentation'); | ||
const { NodeTracerProvider } = require('@opentelemetry/node'); | ||
const { SimpleSpanProcessor, ConsoleSpanExporter } = require('@opentelemetry/tracing'); | ||
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); | ||
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin'); | ||
|
||
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); | ||
const { RouterInstrumentation } = require('@opentelemetry/instrumentation-router'); | ||
|
||
const Exporter = ((exporterParam) => { | ||
if (typeof exporterParam === 'string') { | ||
const exporterString = exporterParam.toLowerCase(); | ||
if (exporterString.startsWith('z')) { | ||
return ZipkinExporter; | ||
} | ||
if (exporterString.startsWith('j')) { | ||
return JaegerExporter; | ||
} | ||
} | ||
return ConsoleSpanExporter; | ||
})(process.env.EXPORTER); | ||
|
||
module.exports = (serviceName) => { | ||
const provider = new NodeTracerProvider(); | ||
registerInstrumentations({ | ||
tracerProvider: provider, | ||
instrumentations: [ | ||
HttpInstrumentation, | ||
RouterInstrumentation, | ||
], | ||
}); | ||
|
||
const exporter = new Exporter({ | ||
serviceName, | ||
}); | ||
|
||
provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
|
||
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings | ||
provider.register(); | ||
|
||
return opentelemetry.trace.getTracer('router-example'); | ||
}; |
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
1 change: 1 addition & 0 deletions
1
plugins/node/opentelemetry-instrumentation-router/.eslintignore
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 @@ | ||
build |
7 changes: 7 additions & 0 deletions
7
plugins/node/opentelemetry-instrumentation-router/.eslintrc.js
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,7 @@ | ||
module.exports = { | ||
"env": { | ||
"mocha": true, | ||
"node": true | ||
}, | ||
...require('../../../eslint.config.js') | ||
} |
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.