diff --git a/examples/mysql/package.json b/examples/mysql/package.json deleted file mode 100644 index 10c4f80c1..000000000 --- a/examples/mysql/package.json +++ /dev/null @@ -1,46 +0,0 @@ -{ - "name": "mysql-example", - "private": true, - "version": "0.23.0", - "description": "Example of mysql 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" - }, - "repository": { - "type": "git", - "url": "git+ssh://git@github.com/open-telemetry/opentelemetry-js.git" - }, - "keywords": [ - "opentelemetry", - "mysql", - "tracing" - ], - "engines": { - "node": ">=8" - }, - "author": "OpenTelemetry Authors", - "license": "Apache-2.0", - "bugs": { - "url": "https://github.com/open-telemetry/opentelemetry-js/issues" - }, - "dependencies": { - "@opentelemetry/api": "^1.0.2", - "@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-mysql": "^0.23.0", - "@opentelemetry/sdk-trace-node": "^0.25.0", - "@opentelemetry/sdk-trace-base": "^0.25.0", - "mysql": "*" - }, - "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", - "devDependencies": { - "@types/mysql": "*", - "cross-env": "^6.0.0" - } -} diff --git a/examples/mysql/tracer.js b/examples/mysql/tracer.js deleted file mode 100644 index 79147d0a5..000000000 --- a/examples/mysql/tracer.js +++ /dev/null @@ -1,34 +0,0 @@ -'use strict'; - -const opentelemetry = require('@opentelemetry/api'); -const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); -const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base'); -const { JaegerExporter } = require('@opentelemetry/exporter-jaeger'); -const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin'); -const { registerInstrumentations } = require('@opentelemetry/instrumentation'); -const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); -const { MySQLInstrumentation } = require('@opentelemetry/instrumentation-mysql'); - -module.exports = (serviceName) => { - const provider = new NodeTracerProvider(); - - provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter({ - serviceName, - }))); - provider.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter({ - serviceName, - }))); - - // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings - provider.register(); - - registerInstrumentations({ - instrumentations: [ - new HttpInstrumentation(), - new MySQLInstrumentation(), - ], - tracerProvider: provider, - }); - - return opentelemetry.trace.getTracer('mysql-example'); -}; diff --git a/plugins/node/opentelemetry-instrumentation-mysql/.eslintignore b/plugins/node/opentelemetry-instrumentation-mysql/.eslintignore index 378eac25d..514cc95d4 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql/.eslintignore +++ b/plugins/node/opentelemetry-instrumentation-mysql/.eslintignore @@ -1 +1,2 @@ build +examples diff --git a/plugins/node/opentelemetry-instrumentation-mysql/README.md b/plugins/node/opentelemetry-instrumentation-mysql/README.md index f7f338a1a..94074d044 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql/README.md +++ b/plugins/node/opentelemetry-instrumentation-mysql/README.md @@ -40,7 +40,7 @@ registerInstrumentations({ }) ``` -See [examples/mysql](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/examples/mysql) for a short example. +See [examples](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-mysql/examples) for a short example. ## Useful links diff --git a/examples/mysql/README.md b/plugins/node/opentelemetry-instrumentation-mysql/examples/README.md similarity index 95% rename from examples/mysql/README.md rename to plugins/node/opentelemetry-instrumentation-mysql/examples/README.md index 6b8348d34..7b3180e37 100644 --- a/examples/mysql/README.md +++ b/plugins/node/opentelemetry-instrumentation-mysql/examples/README.md @@ -27,14 +27,14 @@ Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/ ```sh # from this directory - npm run server + npm run zipkin:server ``` - Run the client ```sh # from this directory - npm run client + npm run zipkin:client ``` #### Zipkin UI @@ -50,14 +50,14 @@ Go to Zipkin with your browser =8" + }, + "author": "OpenTelemetry Authors", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/open-telemetry/opentelemetry-js/issues" + }, + "dependencies": { + "@opentelemetry/api": "^1.0.0", + "@opentelemetry/exporter-jaeger": "^1.0.0", + "@opentelemetry/exporter-zipkin": "^1.0.0", + "@opentelemetry/instrumentation": "^0.32.0", + "@opentelemetry/instrumentation-http": "^0.32.0", + "@opentelemetry/instrumentation-mysql": "^0.31.0", + "@opentelemetry/sdk-trace-base": "^1.0.0", + "@opentelemetry/sdk-trace-node": "^1.0.0", + "mysql": "^2.18.1" + }, + "homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", + "devDependencies": { + "@types/mysql": "^2.15.21", + "cross-env": "^6.0.0", + "ts-node": "^10.6.0", + "typescript": "4.3.5" + } +} diff --git a/examples/mysql/client.js b/plugins/node/opentelemetry-instrumentation-mysql/examples/src/client.ts similarity index 88% rename from examples/mysql/client.js rename to plugins/node/opentelemetry-instrumentation-mysql/examples/src/client.ts index f67025c51..bdbb43649 100644 --- a/examples/mysql/client.js +++ b/plugins/node/opentelemetry-instrumentation-mysql/examples/src/client.ts @@ -1,9 +1,9 @@ 'use strict'; -const api = require('@opentelemetry/api'); -const tracer = require('./tracer')('example-mysql-http-client'); -// eslint-disable-next-line import/order -const http = require('http'); +import * as api from '@opentelemetry/api'; +import { setupTracing } from "./tracer"; +const tracer = setupTracing('example-mysql-client'); +import * as http from 'http'; /** A function which makes requests and handles response. */ function makeRequest() { @@ -22,7 +22,7 @@ function makeRequest() { port: 8080, path: '/connection/query', }, (response) => { - const body = []; + const body: any[] = []; response.on('data', (chunk) => body.push(chunk)); response.on('end', () => { responses += 1; @@ -38,7 +38,7 @@ function makeRequest() { port: 8080, path: '/pool/query', }, (response) => { - const body = []; + const body: any[] = []; response.on('data', (chunk) => body.push(chunk)); response.on('end', () => { responses += 1; @@ -54,7 +54,7 @@ function makeRequest() { port: 8080, path: '/cluster/query', }, (response) => { - const body = []; + const body: any[] = []; response.on('data', (chunk) => body.push(chunk)); response.on('end', () => { responses += 1; diff --git a/examples/mysql/server.js b/plugins/node/opentelemetry-instrumentation-mysql/examples/src/server.ts similarity index 50% rename from examples/mysql/server.js rename to plugins/node/opentelemetry-instrumentation-mysql/examples/src/server.ts index 1599327a4..36d1083a8 100644 --- a/examples/mysql/server.js +++ b/plugins/node/opentelemetry-instrumentation-mysql/examples/src/server.ts @@ -1,23 +1,24 @@ 'use strict'; // eslint-disable-next-line import/order -const tracer = require('./tracer')('example-mysql-http-server'); -const api = require('@opentelemetry/api'); -const mysql = require('mysql'); -const http = require('http'); +import { setupTracing } from "./tracer"; +setupTracing('example-mysql-server'); +import * as api from '@opentelemetry/api'; +import * as mysql from 'mysql' +import * as http from 'http'; +import { MysqlError } from "mysql"; +import { PoolConnection } from "mysql"; const pool = mysql.createPool({ host: 'localhost', user: 'root', password: 'secret', - database: 'my_db', }); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'secret', - database: 'my_db', }); const cluster = mysql.createPoolCluster(); @@ -26,34 +27,34 @@ cluster.add({ host: 'localhost', user: 'root', password: 'secret', - database: 'my_db', }); /** Starts a HTTP server that receives requests on sample server port. */ -function startServer(port) { +function startServer(port: number | undefined) { // Creates a server const server = http.createServer(handleRequest); // Starts the server - server.listen(port, (err) => { - if (err) { - throw err; - } + server.listen(port, () => { console.log(`Node HTTP listening on ${port}`); }); } /** A function which handles requests and send response. */ -function handleRequest(request, response) { - const currentSpan = tracer.getCurrentSpan(); +function handleRequest(request: any, response: any) { + const currentSpan = api.trace.getSpan(api.context.active()) // display traceid in the terminal - const { traceId } = currentSpan.spanContext(); + const traceId = currentSpan?.spanContext(); console.log(`traceid: ${traceId}`); console.log(`Jaeger URL: http://localhost:16686/trace/${traceId}`); console.log(`Zipkin URL: http://localhost:9411/zipkin/traces/${traceId}`); try { const body = []; - request.on('error', (err) => console.log(err)); - request.on('data', (chunk) => body.push(chunk)); + request.on('error', + (err: any) => console.log(err) + ); + request.on('data', + (chunk: any) => body.push(chunk) + ); request.on('end', () => { if (request.url === '/connection/query') { handleConnectionQuery(response); @@ -72,22 +73,27 @@ function handleRequest(request, response) { startServer(8080); -function handlePoolQuery(response) { +function handlePoolQuery(response: any) { const query = 'SELECT 1 + 1 as pool_solution'; - pool.getConnection((connErr, conn, _fields) => { - conn.query(query, (err, results) => { - tracer.getCurrentSpan().addEvent('results'); - if (err) { - console.log('Error code:', err.code); - response.end(err.message); - } else { - response.end(`${query}: ${results[0].pool_solution}`); - } - }); + pool.getConnection((connErr: MysqlError, conn: PoolConnection) => { + if (connErr) { + console.log('Error connection: ', connErr.message); + response.end(connErr.message); + } else { + conn.query(query, (err, results) => { + api.trace.getSpan(api.context.active())?.addEvent('results'); + if (err) { + console.log('Error code:', err.code); + response.end(err.message); + } else { + response.end(`${query}: ${results[0].pool_solution}`); + } + }); + } }); } -function handleConnectionQuery(response) { +function handleConnectionQuery(response: any) { const query = 'SELECT 1 + 1 as solution'; connection.query(query, (err, results, _fields) => { if (err) { @@ -99,21 +105,26 @@ function handleConnectionQuery(response) { }); } -function handleClusterQuery(response) { +function handleClusterQuery(response: any) { const query = 'SELECT 1 + 1 as cluster_solution'; cluster.getConnection((connErr, conn) => { - conn.query(query, (err, results, _fields) => { - api.trace.getSpan(api.context.active()).addEvent('results'); - if (err) { - console.log('Error code:', err.code); - response.end(err.message); - } else { - response.end(`${query}: ${results[0].cluster_solution}`); - } - }); + if (connErr) { + console.log('Error connection: ', connErr.message); + response.end(connErr.message); + } else { + conn.query(query, (err, results, _fields) => { + api.trace.getSpan(api.context.active())?.addEvent('results'); + if (err) { + console.log('Error code:', err.code); + response.end(err.message); + } else { + response.end(`${query}: ${results[0].cluster_solution}`); + } + }); + } }); } -function handleNotFound(response) { +function handleNotFound(response: any) { response.end('not found'); } diff --git a/plugins/node/opentelemetry-instrumentation-mysql/examples/src/tracer.ts b/plugins/node/opentelemetry-instrumentation-mysql/examples/src/tracer.ts new file mode 100644 index 000000000..7f44c5b21 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mysql/examples/src/tracer.ts @@ -0,0 +1,40 @@ +'use strict'; + +import opentelemetry from '@opentelemetry/api'; +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 { registerInstrumentations } from '@opentelemetry/instrumentation'; +import { HttpInstrumentation } from '@opentelemetry/instrumentation-http'; +import { MySQLInstrumentation } from '@opentelemetry/instrumentation-mysql'; +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, + }),}); + + if (EXPORTER.toLowerCase().startsWith('z')) { + provider.addSpanProcessor(new SimpleSpanProcessor(new ZipkinExporter())); + } else { + provider.addSpanProcessor(new SimpleSpanProcessor(new JaegerExporter())); + } + + // Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings + provider.register(); + + registerInstrumentations({ + instrumentations: [ + new HttpInstrumentation(), + new MySQLInstrumentation(), + ], + tracerProvider: provider, + }); + + return opentelemetry.trace.getTracer('mysql-example'); +}; diff --git a/plugins/node/opentelemetry-instrumentation-mysql/examples/tsconfig.json b/plugins/node/opentelemetry-instrumentation-mysql/examples/tsconfig.json new file mode 100644 index 000000000..5a768b7e2 --- /dev/null +++ b/plugins/node/opentelemetry-instrumentation-mysql/examples/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "noEmit": true, + "rootDir": ".", + }, + "include": [ + "src/**/*.ts", + ] +} diff --git a/plugins/node/opentelemetry-instrumentation-mysql/package.json b/plugins/node/opentelemetry-instrumentation-mysql/package.json index 80fb32b54..7f20c7c3c 100644 --- a/plugins/node/opentelemetry-instrumentation-mysql/package.json +++ b/plugins/node/opentelemetry-instrumentation-mysql/package.json @@ -15,7 +15,8 @@ "prepare": "npm run compile", "tdd": "npm run test -- --watch-extensions ts --watch", "test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'", - "version:update": "node ../../../scripts/version-update.js" + "version:update": "node ../../../scripts/version-update.js", + "compile:examples": "cd examples && npm run compile" }, "keywords": [ "instrumentation",