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

fix grpc parsing error causing an exception from the plugin #750

Merged
merged 2 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 0 deletions packages/datadog-plugin-grpc/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module.exports = {
},

addMetadataTags (span, metadata, filter, type) {
if (!metadata) return

const values = filter(metadata.getMap())

for (const key in values) {
Expand Down
40 changes: 37 additions & 3 deletions packages/datadog-plugin-grpc/test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ describe('Plugin', () => {
let port
let server
let tracer
let loader

function buildClient (service) {
function buildClient (service, ClientService) {
service = Object.assign({
getBidi: () => {},
getServerStream: () => {},
getClientStream: () => {},
getUnary: () => {}
}, service)

const loader = require('../../../versions/@grpc/proto-loader').get()
loader = require('../../../versions/@grpc/proto-loader').get()

const definition = loader.loadSync(`${__dirname}/test.proto`)
const TestService = grpc.loadPackageDefinition(definition).test.TestService

Expand All @@ -32,7 +34,9 @@ describe('Plugin', () => {
server.addService(TestService.service, service)
server.start()

return new TestService(`localhost:${port}`, grpc.credentials.createInsecure())
ClientService = ClientService || TestService

return new ClientService(`localhost:${port}`, grpc.credentials.createInsecure())
}

describe('grpc/client', () => {
Expand Down Expand Up @@ -260,6 +264,36 @@ describe('Plugin', () => {
client.getUnary({ first: 'foobar' }, () => {})
})

it('should handle protocol errors', done => {
const definition = loader.loadSync(`${__dirname}/invalid.proto`)
const test = grpc.loadPackageDefinition(definition).test
const client = buildClient({
getUnary: (_, callback) => callback(null)
}, test.TestService)

agent
.use(traces => {
expect(traces[0][0]).to.have.property('error', 1)
expect(traces[0][0].meta).to.include({
'error.msg': '13 INTERNAL: Failed to parse server response',
'error.type': 'Error',
'grpc.method.name': 'getUnary',
'grpc.method.service': 'TestService',
'grpc.method.package': 'test',
'grpc.method.path': '/test.TestService/getUnary',
'grpc.method.kind': kinds.unary,
'grpc.status.code': '13',
'span.kind': 'client',
'component': 'grpc'
})
expect(traces[0][0].meta).to.have.property('error.stack')
})
.then(done)
.catch(done)

client.getUnary({ first: 'foobar' }, () => {})
})

it('should handle a missing callback', done => {
const client = buildClient({
getUnary: (_, callback) => callback()
Expand Down
19 changes: 19 additions & 0 deletions packages/datadog-plugin-grpc/test/invalid.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax = "proto3";

package test;

service TestService {
rpc get_Bidi (stream Request) returns (stream Response) {} // test rename support
rpc getServerStream (Request) returns (stream Response) {}
rpc getClientStream (stream Request) returns (Response) {}
rpc getUnary (Request) returns (Response) {}
}

message Request {
string first = 1;
int32 second = 2;
}

message Response {
required string first = 1;
}
11 changes: 5 additions & 6 deletions packages/datadog-plugin-grpc/test/test.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ syntax = "proto3";
package test;

service TestService {
rpc get_Bidi (stream Request) returns (stream Empty) {} // test rename support
rpc getServerStream (Request) returns (stream Empty) {}
rpc getClientStream (stream Request) returns (Empty) {}
rpc getUnary (Request) returns (Empty) {}
rpc get_Bidi (stream Request) returns (stream Response) {} // test rename support
rpc getServerStream (Request) returns (stream Response) {}
rpc getClientStream (stream Request) returns (Response) {}
rpc getUnary (Request) returns (Response) {}
}

message Request {
string first = 1;
int32 second = 2;
}

message Empty {
}
message Response {}