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

Commit 6266ecd

Browse files
committed
add suport socket.io
1 parent 9bfe84a commit 6266ecd

File tree

5 files changed

+96
-2
lines changed

5 files changed

+96
-2
lines changed

modules/nodejs-agent/lib/plugins/plugin-manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
module.exports = PluginManager;
2121
const logger = require("../logger");
22-
const OFFICER_SUPPORTED_MODULE = ["mysql", "http", "egg-core", "egg", "amqp"];
22+
const OFFICER_SUPPORTED_MODULE = ["mysql", "http", "egg-core", "egg", "amqp","socket.io"];
2323

2424
/**
2525
*
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the SkyAPM under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
"use strict";
18+
19+
const ContextCarrier = require("skyapm-nodejs/lib/trace/context-carrier");
20+
const layerDefine = require("skyapm-nodejs/lib/trace/span-layer");
21+
const componentDefine = require("skyapm-nodejs/lib/trace/component-define");
22+
var parser = require('socket.io-parser');
23+
/**
24+
*
25+
* @param {socketioModule} socketioModule
26+
* @param {instrumentation} instrumentation
27+
* @param {contextManager} contextManager
28+
* @return {*}
29+
* @author Quanjie.Deng
30+
*/
31+
module.exports = function(socketioModule, instrumentation, contextManager) {
32+
instrumentation.enhanceMethod(socketioModule, "ondecoded", wrapOndecoded);
33+
return socketioModule;
34+
35+
function wrapOndecoded(original) {
36+
return function(packet) {
37+
let contextCarrier = new ContextCarrier();
38+
if(packet.data.hasOwnProperty("headers")){
39+
contextCarrier.fetchBy(function(key) {
40+
if (packet.data.headers.hasOwnProperty(key)) {
41+
return packet.data.headers[key];
42+
}
43+
return undefined;
44+
});
45+
}
46+
47+
let opName = packet.nsp+packet.data[0];
48+
let span = contextManager.createEntrySpan(opName, contextCarrier);
49+
span.component(componentDefine.Components.SOCKETIO);
50+
span.spanLayer(layerDefine.Layers.HTTP);
51+
let result = original.apply(this,arguments);
52+
contextManager.finishSpan(span);
53+
return result;
54+
};
55+
}
56+
57+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the SkyAPM under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
"use strict";
19+
20+
const Plugin = require("skyapm-nodejs/lib/plugins/plugin");
21+
22+
module.exports = new Plugin("socket.io-plugin", "socket.io", [{
23+
_name: "socket.io",
24+
_description: "Enhance all version of socket.io module",
25+
_enhanceModules: ["client"],
26+
canEnhance: function(version, enhanceFile) {
27+
if (this._enhanceModules.indexOf(enhanceFile) > -1) {
28+
return true;
29+
}
30+
return false;
31+
},
32+
getInterceptor: function(enhanceFile) {
33+
return require("./" + enhanceFile);
34+
},
35+
}]);

modules/nodejs-agent/lib/trace/component-define.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ let Components = function() {
4040
this.MYSQL = new OfficeComponent(5, "MYSQL");
4141
this.EGG = new OfficeComponent(4003, "Egg");
4242
this.AMQP = new OfficeComponent(4004, "AMQP");
43+
this.SOCKETIO = new OfficeComponent(4005, "SOCKET.IO");
4344
};
4445

4546
Components.instance = null;

modules/nodejs-agent/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"on-finished": "^2.3.0",
4444
"randomatic": "^3.1.1",
4545
"resolve": "^1.8.1",
46-
"semver": "^5.5.1"
46+
"semver": "^5.5.1",
47+
"socket.io-parser": "~3.4.0"
4748
},
4849
"devDependencies": {
4950
"cz-conventional-changelog": "^2.1.0",

0 commit comments

Comments
 (0)