-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental support for grpc tracing
Fixes #226
- Loading branch information
Matt Loring
committed
Apr 5, 2016
1 parent
fba4121
commit c657485
Showing
6 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('grpc'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); |