Skip to content

Commit

Permalink
Merge 2856cb4 into e0b7dde
Browse files Browse the repository at this point in the history
  • Loading branch information
obecny authored Mar 12, 2021
2 parents e0b7dde + 2856cb4 commit f563fcf
Show file tree
Hide file tree
Showing 22 changed files with 756 additions and 9 deletions.
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://git@github.com/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) => {
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

0 comments on commit f563fcf

Please sign in to comment.