-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: creating meta package for default auto instrumentations for no…
…de (#379) * chore: creating metapackage for default auto instrumentations * chore: adding unit tests, fixing exports in plugins * chore: fixing failing tests * chore: lint * chore: exporting mongodb config and aligning name of config * chore: fixing configs, enabling mongodb * chore: simplified metapackage types (#2) * chore: use one map to manage instrumentation types * chore: lint * chore: use default config * chore: merge configs * style: no any * chore: infer config type * style: lint * style: no any * chore: export config map type * chore: bring back deleted debug log * chore: adding redis, fixing links Co-authored-by: Daniel Dyla <[email protected]>
- Loading branch information
Showing
22 changed files
with
756 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
// eslint-disable-next-line import/order | ||
const tracer = require('./tracer')('example-express-client'); | ||
const api = require('@opentelemetry/api'); | ||
const axios = require('axios').default; | ||
|
||
function makeRequest() { | ||
const span = tracer.startSpan('client.makeRequest()', { | ||
kind: api.SpanKind.CLIENT, | ||
}); | ||
|
||
api.context.with(api.setSpan(api.ROOT_CONTEXT, span), async () => { | ||
try { | ||
const res = await axios.get('http://localhost:8080/run_test'); | ||
span.setStatus({ code: api.SpanStatusCode.OK }); | ||
console.log(res.statusText); | ||
} catch (e) { | ||
span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message }); | ||
} | ||
span.end(); | ||
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.'); | ||
setTimeout(() => { console.log('Completed.'); }, 5000); | ||
}); | ||
} | ||
|
||
makeRequest(); |
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,47 @@ | ||
{ | ||
"name": "instrumentations-node-example", | ||
"private": true, | ||
"version": "0.14.0", | ||
"description": "Example of using meta package for default auto instrumentations in node", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint . --ext .js", | ||
"lint:fix": "eslint . --ext .js --fix", | ||
"zipkin:server": "cross-env EXPORTER=zipkin node ./server.js", | ||
"zipkin:client": "cross-env EXPORTER=zipkin node ./client.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/open-telemetry/opentelemetry-js-contrib.git" | ||
}, | ||
"keywords": [ | ||
"opentelemetry", | ||
"instrumentations", | ||
"plugins", | ||
"tracing", | ||
"instrumentation" | ||
], | ||
"engines": { | ||
"node": ">=8.5.0" | ||
}, | ||
"author": "OpenTelemetry Authors", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/open-telemetry/opentelemetry-js-contrib/issues" | ||
}, | ||
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme", | ||
"devDependencies": { | ||
"cross-env": "^6.0.0", | ||
"eslint": "^7.4.0" | ||
}, | ||
"dependencies": { | ||
"@opentelemetry/api": "^0.18.0", | ||
"@opentelemetry/auto-instrumentations-node": "^0.14.0", | ||
"@opentelemetry/exporter-collector": "^0.18.0", | ||
"@opentelemetry/instrumentation": "^0.18.0", | ||
"@opentelemetry/node": "^0.18.0", | ||
"@opentelemetry/tracing": "^0.18.0", | ||
"axios": "^0.21.1", | ||
"express": "^4.17.1" | ||
} | ||
} |
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,57 @@ | ||
'use strict'; | ||
|
||
// eslint-disable-next-line | ||
require('./tracer')('example-meta-node'); | ||
|
||
// Require in rest of modules | ||
const express = require('express'); | ||
const axios = require('axios').default; | ||
|
||
// Setup express | ||
const app = express(); | ||
const PORT = 8080; | ||
|
||
const getCrudController = () => { | ||
const router = express.Router(); | ||
const resources = []; | ||
router.get('/', (req, res) => res.send(resources)); | ||
router.post('/', (req, res) => { | ||
resources.push(req.body); | ||
return res.status(201).send(req.body); | ||
}); | ||
return router; | ||
}; | ||
|
||
const authMiddleware = (req, res, next) => { | ||
const { authorization } = req.headers; | ||
if (authorization && authorization.includes('secret_token')) { | ||
next(); | ||
} else { | ||
res.sendStatus(401); | ||
} | ||
}; | ||
|
||
async function setupRoutes() { | ||
app.use(express.json()); | ||
|
||
app.get('/run_test', async (req, res) => { | ||
const createdCat = await axios.post(`http://localhost:${PORT}/cats`, { | ||
name: 'Tom', | ||
friends: [ | ||
'Jerry', | ||
], | ||
}, { | ||
headers: { | ||
Authorization: 'secret_token', | ||
}, | ||
}); | ||
|
||
return res.status(201).send(createdCat.data); | ||
}); | ||
app.use('/cats', authMiddleware, getCrudController()); | ||
} | ||
|
||
setupRoutes().then(() => { | ||
app.listen(PORT); | ||
console.log(`Listening on http://localhost:${PORT}`); | ||
}); |
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,58 @@ | ||
'use strict'; | ||
|
||
const { | ||
diag, trace, DiagConsoleLogger, DiagLogLevel, | ||
} = require('@opentelemetry/api'); | ||
const { NodeTracerProvider } = require('@opentelemetry/node'); | ||
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node'); | ||
const { CollectorTraceExporter } = require('@opentelemetry/exporter-collector'); | ||
const { SimpleSpanProcessor } = require('@opentelemetry/tracing'); | ||
const { registerInstrumentations } = require('@opentelemetry/instrumentation'); | ||
|
||
module.exports = () => { | ||
// enable diag to see all messages | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL); | ||
|
||
const exporter = new CollectorTraceExporter({ | ||
serviceName: 'basic-service', | ||
}); | ||
|
||
const provider = new NodeTracerProvider(); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
provider.register(); | ||
|
||
registerInstrumentations({ | ||
instrumentations: [ | ||
getNodeAutoInstrumentations({ | ||
'@opentelemetry/instrumentation-http': { | ||
applyCustomAttributesOnSpan: (span) => { | ||
span.setAttribute('foo2', 'bar2'); | ||
}, | ||
}, | ||
}), | ||
// disable old plugins - this can be removed once plugins are deprecated | ||
// and removed from registerInstrumentations | ||
{ | ||
plugins: { | ||
mongodb: { enabled: false, path: '@opentelemetry/plugin-mongodb' }, | ||
grpc: { enabled: false, path: '@opentelemetry/plugin-grpc' }, | ||
'@grpc/grpc-js': { enabled: false, path: '@opentelemetry/plugin-grpc-js' }, | ||
http: { enabled: false, path: '@opentelemetry/plugin-http' }, | ||
https: { enabled: false, path: '@opentelemetry/plugin-httsps' }, | ||
mysql: { enabled: false, path: '@opentelemetry/plugin-mysql' }, | ||
pg: { enabled: false, path: '@opentelemetry/plugin-pg' }, | ||
redis: { enabled: false, path: '@opentelemetry/plugin-redis' }, | ||
ioredis: { enabled: false, path: '@opentelemetry/plugin-ioredis' }, | ||
'pg-pool': { enabled: false, path: '@opentelemetry/plugin-pg-pool' }, | ||
express: { enabled: false, path: '@opentelemetry/plugin-express' }, | ||
'@hapi/hapi': { enabled: false, path: '@opentelemetry/hapi-instrumentation' }, | ||
koa: { enabled: false, path: '@opentelemetry/koa-instrumentation' }, | ||
dns: { enabled: false, path: '@opentelemetry/plugin-dns' }, | ||
}, | ||
}, | ||
], | ||
tracerProvider: provider, | ||
}); | ||
|
||
return trace.getTracer('meta-node-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build | ||
.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,8 @@ | ||
module.exports = { | ||
"env": { | ||
"commonjs": true, | ||
"node": true, | ||
"mocha": 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.