Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Support for the node_redis framework #73

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/nodejs-agent/lib/plugins/plugin-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

module.exports = PluginManager;
const logger = require("../logger");
const OFFICER_SUPPORTED_MODULE = ["mysql", "http", "egg-core", "egg"];
const OFFICER_SUPPORTED_MODULE = ["redis", "mysql", "http", "egg-core", "egg"];

/**
*
Expand Down
36 changes: 36 additions & 0 deletions modules/nodejs-agent/lib/plugins/redis/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the SkyAPM 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.
*/

"use strict";

const Plugin = require("../plugin");

module.exports = new Plugin("redis-plugin", "redis", [{
_name: "node",
a526672351 marked this conversation as resolved.
Show resolved Hide resolved
_description: "Only enhancements to node_redis",
_enhanceModules: ["redis"],
canEnhance: function(version, enhanceFile) {
if (this._enhanceModules.indexOf(enhanceFile) > -1) {
return true;
}
return false;
},
getInterceptor: function(enhanceFile) {
return require("./" + this._name + "/" + enhanceFile);
},
}]);

80 changes: 80 additions & 0 deletions modules/nodejs-agent/lib/plugins/redis/node/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Licensed to the SkyAPM 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.
*/

"use strict";

const spanLayer = require("../../../trace/span-layer");
const componentDefine = require("../../../trace/component-define");
const Tags = require("../../../trace/tags");

/**
* @param {originModule} originModule
* @param {instrumentation} instrumentation
* @param {contextManager} contextManager
* @return {*}
* @author huang qicong
*/
module.exports = function(originModule, instrumentation, contextManager) {
instrumentation.enhanceMethod(originModule, "createClient", wrapCreateClient);

/**
* @this OriginObject
* @param {original} original
* @return {function(): *}
*/
function wrapCreateClient(original) {
return function() {
let client = original.apply(this, arguments);
enhanceCommandsMethod(client, instrumentation, contextManager);
return client;
};
}

return originModule;
};

/**
* @param {obj} obj
* @param {instrumentation} instrumentation
* @param {contextManager} contextManager
* @return {wrappedMethod}
*/
function enhanceCommandsMethod(obj, instrumentation, contextManager) {
let connection = obj;
return instrumentation.enhanceMethod(obj, "internal_send_command", commandInterceptor);

/**
*
* @param {original} original
* @return {function(*=, *=, *=): *}
*/
function commandInterceptor(original) {
// eslint-disable-next-line camelcase
return function(command_obj) {
let span = contextManager.createExitSpan("Redis/command", connection.address);
span.component(componentDefine.Components.REDIS);
span.spanLayer(spanLayer.Layers.CACHE);
Tags.DB_TYPE.tag(span, "Redis");
Tags.DB_INSTANCE.tag(span, connection.selected_db);
Tags.DB_STATEMENT.tag(span, command_obj.command);
contextManager.finishSpan(span);
Copy link
Member

Choose a reason for hiding this comment

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

Why do you finish span in this time? The cost time of Redis span is incorrect if you finish the span in this time. The cost of real Redis span is the cost of executing Redis command.

Copy link
Member

@kezhenxu94 kezhenxu94 May 25, 2020

Choose a reason for hiding this comment

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

Why do you finish span in this time? The cost time of Redis span is incorrect if you finish the span in this time. The cost of real Redis span is the cost of executing Redis command.

Hi @ascrutae , seems this comment is addressed, is there anything else that needs to be updated? Let me continue this if there is any

// eslint-disable-next-line camelcase
const result = original.apply(this, [command_obj]);
return result;
};
}
}
1 change: 1 addition & 0 deletions modules/nodejs-agent/lib/trace/component-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ OfficeComponent.prototype.getName = function() {
let Components = function() {
this.HTTP = new OfficeComponent(2, "HTTP");
this.MYSQL = new OfficeComponent(5, "MYSQL");
this.REDIS = new OfficeComponent(7, "REDIS");
this.EGG = new OfficeComponent(4003, "Egg");
};

Expand Down