-
Notifications
You must be signed in to change notification settings - Fork 674
feat: adding Koa instrumentation #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3aaae4d
feat: add Koa instrumentation
carolinee21 9cca8d8
fix: add eslint-plugin-headers dependency
carolinee21 c5290b7
chore: incorporating PR comments
carolinee21 b4ec189
Update plugins/node/opentelemetry-koa-instrumentation/src/koa.ts
carolinee21 c3bdb37
fix: more changes based on feedback
carolinee21 c970fc4
fix: fixing linting issue
carolinee21 7e4d80a
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
carolinee21 6798351
chore: bump codecov
carolinee21 11e9e5e
chore: code style
carolinee21 1d55a79
feat: adding test cases for nested and prefixed routers
carolinee21 9ee38b6
chore: bump version to 0.9.0
carolinee21 9072652
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
carolinee21 cd12d4f
fix: bumping core dependencies to 0.10.2
carolinee21 3b9340e
chore: refactor test server logic
carolinee21 8dfb1cf
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
carolinee21 5b19cfd
Merge branch 'master' into master
obecny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,76 @@ | ||
| # Overview | ||
|
|
||
| OpenTelemetry Koa 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), to give observability to distributed systems. | ||
|
|
||
| This is a simple example that demonstrates tracing calls made in a Koa application. The example | ||
| shows key aspects of tracing such as | ||
| - Root Span (on Client) | ||
| - Child Span (on Client) | ||
| - Span Events | ||
| - Span Attributes | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| $ # from this directory | ||
| $ npm install | ||
| ``` | ||
|
|
||
| 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 | ||
|
|
||
| ### Zipkin | ||
|
|
||
| - Run the server | ||
|
|
||
| ```sh | ||
| # from this directory | ||
| $ npm run zipkin:server | ||
| ``` | ||
|
|
||
| - Run the client | ||
|
|
||
| ```sh | ||
| # from this directory | ||
| npm run zipkin:client | ||
| ``` | ||
|
|
||
| #### Zipkin UI | ||
| `zipkin:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`). | ||
| Go to Zipkin with your browser [http://localhost:9411/zipkin/traces/(your-trace-id)]() (e.g http://localhost:9411/zipkin/traces/4815c3d576d930189725f1f1d1bdfcc6) | ||
|
|
||
| <p align="center"><img src="./images/zipkin.jpg?raw=true"/></p> | ||
|
|
||
| ### Jaeger | ||
|
|
||
| - Run the server | ||
|
|
||
| ```sh | ||
| # from this directory | ||
| $ npm run jaeger:server | ||
| ``` | ||
|
|
||
| - Run the client | ||
|
|
||
| ```sh | ||
| # from this directory | ||
| npm run jaeger:client | ||
| ``` | ||
|
|
||
| #### Jaeger UI | ||
|
|
||
| `jaeger:server` script should output the `traceid` in the terminal (e.g `traceid: 4815c3d576d930189725f1f1d1bdfcc6`). | ||
| Go to Jaeger with your browser [http://localhost:16686/trace/(your-trace-id)]() (e.g http://localhost:16686/trace/4815c3d576d930189725f1f1d1bdfcc6) | ||
|
|
||
| <p align="center"><img src="images/jaeger.jpg?raw=true"/></p> | ||
|
|
||
| ## 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/master/packages/opentelemetry-node> | ||
|
|
||
| ## LICENSE | ||
|
|
||
| Apache License 2.0 |
This file contains hidden or 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,28 @@ | ||
| 'use strict'; | ||
|
|
||
| // eslint-disable-next-line import/order | ||
| const tracer = require('./tracer')('example-koa-client'); | ||
| const api = require('@opentelemetry/api'); | ||
| const axios = require('axios').default; | ||
|
|
||
| function makeRequest() { | ||
| const span = tracer.startSpan('client.makeRequest()', { | ||
| parent: tracer.getCurrentSpan(), | ||
| kind: api.SpanKind.CLIENT, | ||
| }); | ||
|
|
||
| tracer.withSpan(span, async () => { | ||
| try { | ||
| const res = await axios.get('http://localhost:8081/run_test'); | ||
| span.setStatus({ code: api.CanonicalCode.OK }); | ||
| console.log(res.statusText); | ||
| } catch (e) { | ||
| span.setStatus({ code: api.CanonicalCode.UNKNOWN, message: e.message }); | ||
| } | ||
| span.end(); | ||
| console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.'); | ||
| setTimeout(() => { console.log('Completed.'); }, 5000); | ||
| }); | ||
| } | ||
|
|
||
| makeRequest(); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 hidden or 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 @@ | ||
| { | ||
| "name": "koa-example", | ||
| "private": true, | ||
| "version": "0.8.0", | ||
| "description": "Example of Koa and @koa/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", | ||
| "lint": "eslint . --ext .js", | ||
| "lint:fix": "eslint . --ext .js --fix" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+ssh://git@github.com/open-telemetry/opentelemetry-js-contrib.git" | ||
| }, | ||
| "keywords": [ | ||
| "opentelemetry", | ||
| "koa", | ||
| "tracing", | ||
| "instrumentation" | ||
| ], | ||
| "engines": { | ||
| "node": ">=8" | ||
| }, | ||
| "author": "OpenTelemetry Authors", | ||
| "license": "Apache-2.0", | ||
| "bugs": { | ||
| "url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues" | ||
| }, | ||
| "dependencies": { | ||
| "@koa/router": "^9.3.1", | ||
| "@opentelemetry/api": "^0.8.3", | ||
|
carolinee21 marked this conversation as resolved.
Outdated
|
||
| "@opentelemetry/exporter-jaeger": "^0.8.3", | ||
| "@opentelemetry/exporter-zipkin": "^0.8.3", | ||
| "@opentelemetry/node": "^0.8.3", | ||
| "@opentelemetry/plugin-http": "^0.8.3", | ||
| "@opentelemetry/koa-instrumentation": "../../plugins/node/opentelemetry-koa-instrumentation", | ||
|
carolinee21 marked this conversation as resolved.
Outdated
|
||
| "@opentelemetry/tracing": "^0.8.3", | ||
| "axios": "^0.19.0", | ||
| "koa": "^2.13.0" | ||
| }, | ||
| "homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme", | ||
| "devDependencies": { | ||
| "cross-env": "^6.0.0", | ||
| "eslint": "^7.4.0" | ||
| } | ||
| } | ||
This file contains hidden or 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,69 @@ | ||
| 'use strict'; | ||
|
|
||
| // eslint-disable-next-line | ||
| const tracer = require('./tracer')('example-koa-server'); | ||
|
|
||
| // Adding Koa router (if desired) | ||
| const router = require('@koa/router')(); | ||
| const Koa = require('koa'); | ||
|
|
||
| // Setup koa | ||
| const app = new Koa(); | ||
| const PORT = 8081; | ||
|
|
||
| // route definitions | ||
| router.get('/run_test', runTest) | ||
| .get('/post/new', addPost) | ||
| .get('/post/:id', showNewPost); | ||
|
|
||
| async function setUp() { | ||
| app.use(no_op); | ||
| app.use(router.routes()); | ||
| } | ||
|
|
||
| /** | ||
| * Router functions: list, add, or show posts | ||
| */ | ||
| const posts = ['post 0', 'post 1', 'post 2']; | ||
|
|
||
| function addPost(ctx) { | ||
| posts.push(`post ${posts.length}`); | ||
| const currentSpan = tracer.getCurrentSpan(); | ||
| currentSpan.addEvent('Added post'); | ||
| currentSpan.setAttribute('Date', new Date()); | ||
| ctx.body = `Added post: ${posts[posts.length - 1]}`; | ||
| ctx.redirect('/post/3'); | ||
| } | ||
|
|
||
| async function showNewPost(ctx) { | ||
| const { id } = ctx.params; | ||
| console.log(`showNewPost with id: ${id}`); | ||
| const post = posts[id]; | ||
| if (!post) ctx.throw(404, 'Invalid post id'); | ||
| const syntheticDelay = 500; | ||
| await new Promise((r) => setTimeout(r, syntheticDelay)); | ||
| ctx.body = post; | ||
| } | ||
|
|
||
| function runTest(ctx) { | ||
| console.log('runTest'); | ||
| const currentSpan = tracer.getCurrentSpan(); | ||
| const { traceId } = currentSpan.context(); | ||
| console.log(`traceid: ${traceId}`); | ||
| console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`); | ||
| console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`); | ||
| ctx.body = `All posts: ${posts}`; | ||
| ctx.redirect('/post/new'); | ||
| } | ||
|
|
||
| async function no_op(ctx, next) { | ||
| console.log('Sample basic koa middleware'); | ||
| const syntheticDelay = 100; | ||
| await new Promise((r) => setTimeout(r, syntheticDelay)); | ||
| next(); | ||
| } | ||
|
|
||
| setUp().then(() => { | ||
| app.listen(PORT); | ||
| console.log(`Listening on http://localhost:${PORT}`); | ||
| }); |
This file contains hidden or 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'; | ||
|
|
||
| const opentelemetry = require('@opentelemetry/api'); | ||
| const { NodeTracerProvider } = require('@opentelemetry/node'); | ||
| const { SimpleSpanProcessor } = require('@opentelemetry/tracing'); | ||
| const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); | ||
| const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin'); | ||
|
|
||
| const EXPORTER = process.env.EXPORTER || ''; | ||
|
|
||
| module.exports = (serviceName) => { | ||
| const provider = new NodeTracerProvider({ | ||
| plugins: { | ||
| koa: { | ||
| enabled: true, | ||
| path: '@opentelemetry/koa-instrumentation', | ||
| enhancedDatabaseReporting: true, | ||
| }, | ||
| http: { | ||
| enabled: true, | ||
| path: '@opentelemetry/plugin-http', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| let exporter; | ||
| if (EXPORTER === 'jaeger') { | ||
| exporter = new JaegerExporter({ serviceName }); | ||
| } else { | ||
| exporter = new ZipkinExporter({ serviceName }); | ||
| } | ||
| provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
|
|
||
| // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings | ||
| provider.register(); | ||
|
|
||
| return opentelemetry.trace.getTracer('koa-example'); | ||
| }; |
This file contains hidden or 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 |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.