Skip to content

Commit

Permalink
chore: add linter to example code
Browse files Browse the repository at this point in the history
  • Loading branch information
carolinee21 committed Jul 15, 2020
1 parent 1f57da9 commit befe3f6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 49 deletions.
7 changes: 5 additions & 2 deletions examples/koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"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"
"jaeger:client": "cross-env EXPORTER=jaeger node ./client.js",
"lint": "eslint . --ext .js",
"lint:fix": "eslint . --ext .js --fix"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,6 +43,7 @@
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme",
"devDependencies": {
"cross-env": "^6.0.0"
"cross-env": "^6.0.0",
"eslint": "^7.4.0"
}
}
74 changes: 36 additions & 38 deletions examples/koa/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,64 @@
// 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');
const Koa = require('koa');

// Setup koa
const app = new koa();
const app = new Koa();
const PORT = 8081;

// route definitions
router.get('/run_test', runTest)
.get('/post/new', addPost)
.get('/post/:id', showNewPost);

.get('/post/new', addPost)
.get('/post/:id', showNewPost);

async function setUp () {
app.use(no_op);
app.use(router.routes());
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"];
const posts = ['post 0', 'post 1', 'post 2'];

async function addPost(ctx, next) {
posts.push("post " + posts.length);
console.log("addPost");
const currentSpan = tracer.getCurrentSpan();
currentSpan.addEvent("Added post");
currentSpan.setAttribute("Date", new Date);
ctx.body = "Added post: " + posts[posts.length-1];
const viewNewPost = ctx.redirect(`/post/3`);
async function addPost(ctx) {
posts.push(`post ${posts.length}`);
console.log('addPost');
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, next) {
console.log("showNewPost");
const id = ctx.params.id;
const post = posts[id];
if (!post) ctx.throw(404, 'Invalid post id');
ctx.body = post;
async function showNewPost(ctx) {
console.log('showNewPost');
const { id } = ctx.params;
const post = posts[id];
if (!post) ctx.throw(404, 'Invalid post id');
ctx.body = post;
}

async function runTest (ctx, next) {
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;
const addNewPost = await ctx.redirect(`/post/new`);
async 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');
}

function no_op (ctx, next) {
console.log("Sample basic koa middleware");
next();
function no_op(ctx, next) {
console.log('Sample basic koa middleware');
next();
}

setUp().then(() => {
app.listen(PORT);
console.log(`Listening on http://localhost:${PORT}`);
app.listen(PORT);
console.log(`Listening on http://localhost:${PORT}`);
});
19 changes: 11 additions & 8 deletions examples/koa/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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/plugin-koa",
enhancedDatabaseReporting: true
path: '@opentelemetry/plugin-koa',
enhancedDatabaseReporting: true,
},
http: {
enabled: true,
Expand All @@ -21,12 +23,13 @@ module.exports = (serviceName) => {
},
});


if (process.env.EXPORTER == 'jaeger') {
provider.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter({serviceName})));
} else {
provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter({serviceName})));
}
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();
Expand Down
2 changes: 1 addition & 1 deletion plugins/node/opentelemetry-plugin-koa/.eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
build
build

0 comments on commit befe3f6

Please sign in to comment.