Skip to content
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

chore: creating meta package for default auto instrumentations for node #379

Merged
merged 12 commits into from
Mar 15, 2021
Merged
3 changes: 2 additions & 1 deletion .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"karma.conf.js",
"src/platform/browser/*.ts",
"test/index-webpack.ts",
"webpack/*.js"
"webpack/*.js",
".eslintrc.js"
],
"all": true
}
27 changes: 27 additions & 0 deletions examples/meta-node/client.js
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();
47 changes: 47 additions & 0 deletions examples/meta-node/package.json
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"
}
}
57 changes: 57 additions & 0 deletions examples/meta-node/server.js
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}`);
});
58 changes: 58 additions & 0 deletions examples/meta-node/tracer.js
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) => {
obecny marked this conversation as resolved.
Show resolved Hide resolved
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');
};
2 changes: 2 additions & 0 deletions metapackages/auto-instrumentations-node/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
.eslintrc.js
8 changes: 8 additions & 0 deletions metapackages/auto-instrumentations-node/.eslintrc.js
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')
}
4 changes: 4 additions & 0 deletions metapackages/auto-instrumentations-node/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/bin
/coverage
/doc
/test
Loading