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(koa): migrate koa example to opentelemetry-instrumentation-koa #1118

Merged
merged 9 commits into from
Aug 21, 2022
44 changes: 0 additions & 44 deletions examples/koa/tracer.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
examples
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
"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"
"zipkin:server": "cross-env EXPORTER=zipkin ts-node src/server.ts",
"zipkin:client": "cross-env EXPORTER=zipkin ts-node src/client.ts",
"jaeger:server": "cross-env EXPORTER=jaeger ts-node src/server.ts",
"jaeger:client": "cross-env EXPORTER=jaeger ts-node src/client.ts",
"compile": "tsc -p ."
},
"repository": {
"type": "git",
Expand All @@ -33,19 +32,22 @@
"dependencies": {
"@koa/router": "^9.3.1",
"@opentelemetry/api": "^1.0.2",
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
"@opentelemetry/exporter-jaeger": "^0.25.0",
"@opentelemetry/exporter-zipkin": "^0.25.0",
"@opentelemetry/instrumentation": "^0.25.0",
"@opentelemetry/instrumentation-http": "^0.25.0",
"@opentelemetry/instrumentation-koa": "^0.23.0",
"@opentelemetry/sdk-trace-node": "^0.25.0",
"@opentelemetry/sdk-trace-base": "^0.25.0",
"@opentelemetry/exporter-jaeger": "^1.0.0",
"@opentelemetry/exporter-zipkin": "^1.0.0",
"@opentelemetry/instrumentation": "^0.27.0",
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
"@opentelemetry/instrumentation-http": "^0.27.0",
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
"@opentelemetry/instrumentation-koa": "^0.31.0",
"@opentelemetry/sdk-trace-node": "^1.1.1",
"@opentelemetry/sdk-trace-base": "^1.1.1",
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
"axios": "^0.21.1",
"koa": "^2.13.0"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib#readme",
"devDependencies": {
"cross-env": "^6.0.0",
"eslint": "^7.4.0"
"eslint": "^7.4.0",
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
"ts-node": "^10.6.0",
"typescript": "^4.6.2",
"@types/koa": "^2.13.5"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'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;
// const tracer = require('../tracer')('example-koa-client');
import { setupTracing } from "./tracer";
const tracer = setupTracing('example-express-client');
blumamir marked this conversation as resolved.
Show resolved Hide resolved
// const api = require('@opentelemetry/api');
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
import * as api from '@opentelemetry/api';
// const axios = require('axios').default;
import { default as axios } from 'axios';

function makeRequest() {
const span = tracer.startSpan('client.makeRequest()', {
Expand All @@ -16,7 +20,9 @@ function makeRequest() {
span.setStatus({ code: api.SpanStatusCode.OK });
console.log(res.statusText);
} catch (e) {
span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message });
if(e instanceof Error) {
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message });
}
}
span.end();
console.log('Sleeping 5 seconds before shutdown to ensure all records are flushed.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

const api = require('@opentelemetry/api');
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved

require('./tracer')('example-koa-server');
// require('./tracer')('example-koa-server');
import { setupTracing } from './tracer'
setupTracing('example-koa-server');

// Adding Koa router (if desired)
const router = require('@koa/router')();
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
const Koa = require('koa');
import * as Koa from "koa"


// Setup koa
const app = new Koa();
Expand All @@ -27,7 +30,7 @@ async function setUp() {
*/
const posts = ['post 0', 'post 1', 'post 2'];

function addPost(ctx) {
function addPost(ctx: Koa.Context) {
posts.push(`post ${posts.length}`);
const currentSpan = api.trace.getSpan(api.context.active());
currentSpan.addEvent('Added post');
Expand All @@ -36,7 +39,7 @@ function addPost(ctx) {
ctx.redirect('/post/3');
}

async function showNewPost(ctx) {
async function showNewPost(ctx: Koa.Context) {
const { id } = ctx.params;
console.log(`showNewPost with id: ${id}`);
const post = posts[id];
Expand All @@ -46,7 +49,7 @@ async function showNewPost(ctx) {
ctx.body = post;
}

function runTest(ctx) {
function runTest(ctx: Koa.Context) {
console.log('runTest');
const currentSpan = api.trace.getSpan(api.context.active());
const { traceId } = currentSpan.spanContext();
Expand All @@ -57,7 +60,7 @@ function runTest(ctx) {
ctx.redirect('/post/new');
}

async function noOp(ctx, next) {
async function noOp(ctx: Koa.Context, next: Koa.Next) {
console.log('Sample basic koa middleware');
const syntheticDelay = 100;
await new Promise((r) => setTimeout(r, syntheticDelay));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const { KoaInstrumentation } = require('@opentelemetry/instrumentation-koa');
haddasbronfman marked this conversation as resolved.
Show resolved Hide resolved
// import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
// const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';

import * as api from '@opentelemetry/api';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'

const EXPORTER = process.env.EXPORTER || '';

export const setupTracing = (serviceName: string) => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName
})
});

let exporter;
if (EXPORTER === 'jaeger') {
exporter = new JaegerExporter();
} else {
exporter = new ZipkinExporter();
}
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

registerInstrumentations({
instrumentations: [
new KoaInstrumentation(),
new HttpInstrumentation(),
],
tracerProvider: provider,
});

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return api.trace.getTracer(serviceName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tracer name is usually set to a package name ( as set in the name field in package.json) which in our case is koa-example

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here I took this behavior from here: https://github.com/open-telemetry/opentelemetry-js-contrib/pull/939/files, see tracer.ts file, line 50. this PR appears in the issue as a reference, so I thought this is ok they will be aligned.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is an overlooked mistake in express example PR.
This is a minor issue but would love other opinions @open-telemetry/javascript-approvers

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
"rootDir": ".",
},
"include": [
"src/**/*.ts",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"prewatch": "npm run precompile",
"version:update": "node ../../../scripts/version-update.js",
"compile": "tsc -p .",
"compile:examples": "cd examples && npm run compile",
"prepare": "npm run compile",
"watch": "tsc -w"
},
Expand Down