Skip to content

Commit

Permalink
Experimental support for grpc tracing
Browse files Browse the repository at this point in the history
Fixes #226
  • Loading branch information
Matt Loring committed Apr 5, 2016
1 parent fba4121 commit c657485
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var fs = require('fs');
var toInstrument = Object.create(null, {
'express': { enumerable: true, value: { file: './userspace/hook-express.js',
patches: {} } },
'grpc': { enumerable: true, value: { file: './userspace/hook-grpc.js',
patches: {} } },
'hapi': { enumerable: true, value: { file: './userspace/hook-hapi.js',
patches: {} } },
'http': { enumerable: true, value: { file: './core/hook-http.js',
Expand Down
50 changes: 50 additions & 0 deletions lib/hooks/userspace/hook-grpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed 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';

var cls = require('../../cls.js');
var shimmer = require('shimmer');
var semver = require('semver');
var agent;

var SUPPORTED_VERSIONS = '0.13.x';

function startBatchWrap(startBatch) {
return function startBatchTrace(thing, callback) {
console.log(arguments);
// TODO: maybe we only want to do this if a root context exists.
return startBatch.call(this, thing, cls.getNamespace().bind(callback));
};
}

module.exports = function(version_, agent_) {
if (!semver.satisfies(version_, SUPPORTED_VERSIONS)) {
agent_.logger.info('grpc: unsupported version ' + version_ + ' loaded');
return {};
}
return {
'src/node/src/grpc_extension.js': {
patch: function(extension) {
agent = agent_;
shimmer.wrap(extension.Call.prototype, 'startBatch', startBatchWrap);
},
unpatch: function(extension) {
shimmer.unwrap(extension.Call.prototype, 'startBatch');
}
}
};
};
15 changes: 15 additions & 0 deletions test/fixtures/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
syntax = "proto3";

package nodetest;

service Tester {
rpc Test (TestRequest) returns (TestReply) {}
}

message TestRequest {
string message = 1;
}

message TestReply {
string message = 1;
}
1 change: 1 addition & 0 deletions test/hooks/fixtures/grpc0.13/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('grpc');
8 changes: 8 additions & 0 deletions test/hooks/fixtures/grpc0.13/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "grpc0.13",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"grpc": "^0.13.1"
}
}
69 changes: 69 additions & 0 deletions test/standalone/test-grpc-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed 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';

var agent = require('../..').start({ samplingRate: 0 }).private_();

var common = require('../hooks/common.js');

var assert = require('assert');
var express = require('../hooks/fixtures/express4');
var http = require('http');
var grpc = require('../hooks/fixtures/grpc0.13');
var test_proto = grpc.load(__dirname + '/../fixtures/test.proto').nodetest;
var grcPort = 50051;
var debugCount = 0;
agent.logger.debug = function(error) {
if (error.indexOf('http') !== -1) {
debugCount++;
}
};

describe('express + grpc', function() {
it('grpc should preserve context', function(done) {
var app = express();
app.get('/', function (req, res) {
var client = new test_proto.Tester('localhost:' + grcPort,
grpc.credentials.createInsecure());
client.test({message: 'hello'}, function(err, grpcRes) {
http.get('http://www.google.com/', function(httpRes) {
httpRes.on('data', function() {});
httpRes.on('end', function() {
res.sendStatus(200);
});
});
});
});
var server = app.listen(common.serverPort, function() {
var grpcServer = new grpc.Server();
grpcServer.addProtoService(test_proto.Tester.service, {
test: function(call, cb) {
cb(null, {message: 'world'});
}
});
grpcServer.bind('localhost:' + grcPort,
grpc.ServerCredentials.createInsecure());
grpcServer.start();
http.get({port: common.serverPort}, function(res) {
grpcServer.forceShutdown();
server.close();
assert.equal(common.getTraces().length, 1);
assert.equal(debugCount, 1);
done();
});
});
});
});

0 comments on commit c657485

Please sign in to comment.