Skip to content

Commit

Permalink
chore: tests, cleanup & clarity for redis instrumentation (#621)
Browse files Browse the repository at this point in the history
refs 104481
  • Loading branch information
kirrg001 authored Sep 22, 2022
1 parent 794212a commit 0e3d93c
Show file tree
Hide file tree
Showing 6 changed files with 614 additions and 333 deletions.
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"q": "1.5.1",
"read-pkg-up": "^7.0.1",
"redis": "^3.1.2",
"redis-v0": "npm:redis@^0.12.1",
"request": "^2.88.0",
"request-promise": "^4.2.2",
"request-promise-native": "^1.0.5",
Expand Down
64 changes: 60 additions & 4 deletions packages/collector/test/tracing/database/redis/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

'use strict';

require('./mockVersion');
require('../../../..')();

const bodyParser = require('body-parser');
Expand All @@ -21,7 +22,13 @@ let connectedToRedis = false;

const agentPort = process.env.INSTANA_AGENT_PORT;

const client = redis.createClient(`//${process.env.REDIS}`);
let client;
if (process.env.REDIS_VERSION === 'latest') {
client = redis.createClient(`//${process.env.REDIS}`);
} else {
const portAndHost = process.env.REDIS.split(':');
client = redis.createClient(portAndHost[1], portAndHost[0]);
}

client.on('ready', () => {
connectedToRedis = true;
Expand Down Expand Up @@ -56,6 +63,26 @@ app.post('/values', (req, res) => {
});
});

// https://github.com/redis/node-redis/blob/v3.1.2/test/commands/get.spec.js#L75
app.get('/get-without-cb', (req, res) => {
const key = req.query.key;
client.get(key);
request(`http://127.0.0.1:${agentPort}`).then(() => {
res.sendStatus(200);
});
});

app.get('/set-without-cb', (req, res) => {
const key = req.query.key;
const value = req.query.value;

client.set(key, value);

request(`http://127.0.0.1:${agentPort}`).then(() => {
res.sendStatus(200);
});
});

app.get('/values', (req, res) => {
const key = req.query.key;
client.get(key, (err, redisRes) => {
Expand Down Expand Up @@ -100,6 +127,34 @@ app.get('/multi', (req, res) => {
});
});

app.get('/multi-sub-cb', (req, res) => {
client
.multi()
.hset('someCollection', 'key', 'value')
.hget('someCollection', 'key', 'too', 'many', function () {
// ignore
})
.exec(err => {
if (err) {
log('Multi failed', err);

request(`http://127.0.0.1:${agentPort}`).then(() => {
res.sendStatus(200);
});
} else {
res.sendStatus(500);
}
});
});

app.get('/multiNoExecCb', (req, res) => {
client.multi().hset('someCollection', 'key', 'value').hget('someCollection', 'key').exec();

request(`http://127.0.0.1:${agentPort}`).then(() => {
res.sendStatus(200);
});
});

app.get('/multiFailure', (req, res) => {
// simulating wrong get usage
client
Expand All @@ -110,14 +165,15 @@ app.get('/multiFailure', (req, res) => {
if (err) {
log('Multi failed', err);
request(`http://127.0.0.1:${agentPort}`).then(() => {
res.sendStatus(500);
res.sendStatus(200);
});
} else {
res.sendStatus(200);
res.sendStatus(500);
}
});
});

// Difference to multi: a batch can succeed when a single operation fails
app.get('/batchFailure', (req, res) => {
// simulating wrong get usage
client
Expand All @@ -126,7 +182,7 @@ app.get('/batchFailure', (req, res) => {
.hget('someCollection', 'key', 'too', 'many', 'args')
.exec(err => {
if (err) {
log('batch failed', err);
log('batch should not fail', err);
res.sendStatus(500);
} else {
request(`http://127.0.0.1:${agentPort}`).then(() => {
Expand Down
14 changes: 14 additions & 0 deletions packages/collector/test/tracing/database/redis/mockVersion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* (c) Copyright IBM Corp. 2022
*/

'use strict';

const mock = require('mock-require');

const REDIS_VERSION = process.env.REDIS_VERSION;
const REDIS_REQUIRE = process.env.REDIS_VERSION === 'latest' ? 'redis' : `redis-v${REDIS_VERSION}`;

if (REDIS_REQUIRE !== 'redis') {
mock('redis', REDIS_REQUIRE);
}
Loading

0 comments on commit 0e3d93c

Please sign in to comment.