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

feat: MySQL Tracing Support #3088

Merged
merged 6 commits into from
Dec 4, 2020
Merged

feat: MySQL Tracing Support #3088

merged 6 commits into from
Dec 4, 2020

Conversation

kamilogorek
Copy link
Contributor

@kamilogorek kamilogorek commented Dec 1, 2020

MySQL and MariaDB tracing support.

Usage:

const Sentry = require("@sentry/node");
const Tracing = require("@sentry/tracing");
const mysql = require("mysql");

Sentry.init({
  dsn: "__PUBLIC_DSN__",
  integrations: [
    new Tracing.Integrations.Mysql(),
  ],
  tracesSampleRate: 1.0,
});

const connection = mysql.createConnection({
  user: "root",
  password: "docker",
});

connection.connect(function (err) {
  if (err) {
    console.error("error connecting: " + err.stack);
    return;
  }
  console.log("connected as id " + connection.threadId);
});

// the rest of the code, for raw-node code we need manual instrumentation, eg.

const transaction = Sentry.startTransaction({
  op: "transaction",
  name: "My Transaction",
});

Sentry.configureScope((scope) => {
  scope.setSpan(transaction);
});

connection.query("SELECT 1 + 1 AS solution", function (error, results, fields) {
  console.log(err ? err.stack : results[0].solution); // 2
  if (transaction) transaction.finish();
  connection.end();
});

Works nicely with Express too:

const Sentry = require("@sentry/node");
const Tracing = require("@sentry/tracing");
const mysql = require("mysql");
const express = require("express");

Sentry.init({
  dsn: "__PUBLIC_DSN__",
  integrations: [
    new Sentry.Integrations.Http({ tracing: true }),
    new Tracing.Integrations.Express({
      app: express.Router,
      methods: ["get"],
    }),
    new Tracing.Integrations.Mysql(),
  ],
  tracesSampleRate: 1.0,
});

const app = express();
const connection = mysql.createConnection({
  user: "root",
  password: "docker",
});

connection.connect();

app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
app.use(express.json());

app.get("/hi", function sayHi(req, res) {
  connection.query("SELECT 1 + 1 AS solution", function (error, results, fields) {
    res.send(results[0].solution);
  });
});

app.use(Sentry.Handlers.errorHandler());

app.listen(3000);

@kamilogorek kamilogorek requested a review from a team December 1, 2020 13:51
@kamilogorek kamilogorek force-pushed the node-mysql branch 3 times, most recently from 1deded0 to 0fe030b Compare December 1, 2020 14:01
@github-actions
Copy link
Contributor

github-actions bot commented Dec 1, 2020

size-limit report

Path Size
@sentry/browser - CDN Bundle (gzipped) 19.73 KB (-0.01% 🔽)
@sentry/browser - Webpack 20.6 KB (0%)
@sentry/react - Webpack 20.6 KB (0%)
@sentry/browser + @sentry/tracing - CDN Bundle (gzipped) 26.88 KB (-0.01% 🔽)

Copy link
Member

@lobsterkatie lobsterkatie left a comment

Choose a reason for hiding this comment

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

One small nit, but otherwise LGTM!

P.S. Any possibility of adding some tests?

packages/tracing/src/integrations/mysql.ts Outdated Show resolved Hide resolved
@HazAT HazAT merged commit a1934b4 into master Dec 4, 2020
@HazAT HazAT deleted the node-mysql branch December 4, 2020 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants