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

Add ioredis plugin #53

Merged
merged 16 commits into from
May 24, 2021
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,13 @@ Library | Plugin Name
| [`Express`](https://expressjs.com) | `express` |
| [`Axios`](https://github.com/axios/axios) | `axios` |
| [`MySQL`](https://github.com/mysqljs/mysql) | `mysql` |
| [`MySQL`](https://github.com/sidorares/node-mysql2) | `mysql2` |
| [`PostgreSQL`](https://github.com/brianc/node-postgres) | `pg` |
| [`pg-cursor`](https://github.com/brianc/node-postgres) | `pg-cursor` |
| [`MongoDB`](https://github.com/mongodb/node-mongodb-native) | `mongodb` |
| [`Mongoose`](https://github.com/Automattic/mongoose) | `mongoose` |
| [`RabbitMQ`](https://github.com/squaremo/amqp.node) | `amqplib` |
| [`Redis`](https://github.com/luin/ioredis) | `ioredis` |

### Compatible Libraries

Expand Down
95 changes: 95 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"devDependencies": {
"@types/express": "^4.17.9",
"@types/google-protobuf": "^3.7.2",
"@types/ioredis": "^4.26.4",
"@types/jest": "^26.0.15",
"@types/node": "^14.0.11",
"@types/semver": "^7.2.0",
Expand All @@ -49,6 +50,7 @@
"express": "^4.17.1",
"grpc-tools": "^1.10.0",
"grpc_tools_node_protoc_ts": "^4.0.0",
"ioredis": "^4.27.2",
"jest": "^26.6.3",
"mongodb": "^3.6.4",
"mongoose": "^5.12.2",
Expand Down
62 changes: 62 additions & 0 deletions src/plugins/IORedisPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import PluginInstaller from '../core/PluginInstaller';
import SwPlugin, { wrapPromise } from '../core/SwPlugin';
import { SpanLayer } from '../proto/language-agent/Tracing_pb';
import Tag from '../Tag';
import { Component } from '../trace/Component';
import ContextManager from '../trace/context/ContextManager';

class IORedisPlugin implements SwPlugin {
readonly module = 'ioredis';
readonly versions = '*';

install(installer: PluginInstaller): void {
const Redis = installer.require('ioredis');

this.interceptOperation(Redis, 'sendCommand');
}

interceptOperation(Cls: any, operation: string): void {
const _original = Cls.prototype[operation];

if (!_original)
return;

Cls.prototype[operation] = function (...args: any[]) {
const command = args[0];
const host = `${this.options.host}:${this.options.port}`;
const span = ContextManager.current.newExitSpan(`redis/${command?.name}`, Component.REDIS);

span.start();
span.component = Component.REDIS;
span.layer = SpanLayer.CACHE;
span.peer = host;
span.tag(Tag.dbType('Redis'));
span.tag(Tag.dbInstance(`${this.condition.select}`));

const ret = wrapPromise(span, _original.apply(this, args));
span.async();
return ret;
}
}
}

export default new IORedisPlugin();
3 changes: 2 additions & 1 deletion src/trace/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Component {
static readonly UNKNOWN = new Component(0);
static readonly HTTP = new Component(2);
static readonly MYSQL = new Component(5);
static readonly REDIS = new Component(7);
static readonly MONGODB = new Component(9);
static readonly POSTGRESQL = new Component(22);
static readonly HTTP_SERVER = new Component(49);
Expand All @@ -31,5 +32,5 @@ export class Component {
static readonly AXIOS = new Component(4005);
static readonly MONGOOSE = new Component(4006);

constructor(public readonly id: number) {}
constructor(public readonly id: number) { }
}
38 changes: 38 additions & 0 deletions tests/plugins/ioredis/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*!
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import * as http from 'http';
import agent from '../../../src';

agent.start({
serviceName: 'client',
maxBufferSize: 1000,
})

const server = http.createServer((req, res) => {
http
.request(`http://${process.env.SERVER || 'localhost:5000'}${req.url}`, (r) => {
let data = '';
r.on('data', (chunk) => (data += chunk));
r.on('end', () => res.end(data));
})
.end();
});

server.listen(5001, () => console.info('Listening on port 5001...'));
84 changes: 84 additions & 0 deletions tests/plugins/ioredis/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

version: "2.1"

services:
collector:
extends:
file: ../common/base-compose.yml
service: collector
networks:
- traveling-light

redis:
container_name: redis
ports:
- 6379:6379
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/6379"]
interval: 5s
timeout: 60s
retries: 120
image: "redis:latest"
networks:
- traveling-light

server:
extends:
file: ../common/base-compose.yml
service: agent
ports:
- 5000:5000
environment:
REDIS_HOST: redis
volumes:
- .:/app/tests/plugins/ioredis
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/5000"]
interval: 5s
timeout: 60s
retries: 120
entrypoint:
["bash", "-c", "npx ts-node /app/tests/plugins/ioredis/server.ts"]
depends_on:
collector:
condition: service_healthy
redis:
condition: service_healthy

client:
extends:
file: ../common/base-compose.yml
service: agent
ports:
- 5001:5001
environment:
SERVER: server:5000
healthcheck:
test: ["CMD", "bash", "-c", "cat < /dev/null > /dev/tcp/127.0.0.1/5001"]
interval: 5s
timeout: 60s
retries: 120
entrypoint:
["bash", "-c", "npx ts-node /app/tests/plugins/ioredis/client.ts"]
depends_on:
server:
condition: service_healthy

networks:
traveling-light:
Loading