Skip to content

Commit

Permalink
chore(mysql): migrate mysql examples (#1168)
Browse files Browse the repository at this point in the history
Co-authored-by: Osher Vaknin <81672378+osherv@users.noreply.github.com>
  • Loading branch information
haddasbronfman and osherv authored Sep 18, 2022
1 parent 6584fe2 commit 9913a00
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 133 deletions.
46 changes: 0 additions & 46 deletions examples/mysql/package.json

This file was deleted.

34 changes: 0 additions & 34 deletions examples/mysql/tracer.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
examples
2 changes: 1 addition & 1 deletion plugins/node/opentelemetry-instrumentation-mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,14 +50,14 @@ Go to Zipkin with your browser <http://localhost:9411/zipkin/traces/(your-trace-

```sh
# from this directory
npm run server
npm run jaeger:server
```

- Run the client

```sh
# from this directory
npm run client
npm run jaeger:client
```

#### Jaeger UI
Expand Down
File renamed without changes
File renamed without changes
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "mysql-example",
"private": true,
"version": "0.23.0",
"description": "Example of mysql integration with OpenTelemetry",
"main": "index.js",
"scripts": {
"docker:start": "docker run -p 3306:3306 --name example-mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql:5.7",
"docker:stop": "docker stop example-mysql && docker rm example-mysql",
"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",
"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.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"
}
}
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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');
}
Loading

0 comments on commit 9913a00

Please sign in to comment.