-
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.
Showing
10 changed files
with
247 additions
and
1 deletion.
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
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
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,71 @@ | ||
/** | ||
* Copyright 2017 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 shimmer = require('shimmer'); | ||
|
||
var SUPPORTED_VERSIONS = '^6.x'; | ||
|
||
module.exports = [ | ||
{ | ||
file: 'lib/client.js', | ||
versions: SUPPORTED_VERSIONS, | ||
patch: function(Client, api) { | ||
function queryWrap(query) { | ||
return function query_trace() { | ||
var span = api.createChildSpan({ | ||
name: 'pg-query' | ||
}); | ||
var pgQuery = query.apply(this, arguments); | ||
if (!span) { | ||
return pgQuery; | ||
} | ||
if (api.enhancedDatabaseReportingEnabled()) { | ||
span.addLabel('query', pgQuery.text); | ||
if (pgQuery.values) { | ||
span.addLabel('values', pgQuery.values); | ||
} | ||
} | ||
api.wrapEmitter(pgQuery); | ||
var done = pgQuery.callback; | ||
pgQuery.callback = api.wrap(function(err, res) { | ||
if (api.enhancedDatabaseReportingEnabled()) { | ||
if (err) { | ||
span.addLabel('error', err); | ||
} | ||
if (res) { | ||
span.addLabel('row_count', res.rowCount); | ||
span.addLabel('oid', res.oid); | ||
span.addLabel('rows', res.rows); | ||
span.addLabel('fields', res.fields); | ||
} | ||
} | ||
span.endSpan(); | ||
if (done) { | ||
done(err, res); | ||
} | ||
}); | ||
return pgQuery; | ||
}; | ||
} | ||
|
||
shimmer.wrap(Client.prototype, 'query', queryWrap); | ||
}, | ||
unpatch: function(Client) { | ||
shimmer.unwrap(Client.prototype, 'query'); | ||
} | ||
} | ||
]; |
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,26 @@ | ||
/** | ||
* Copyright 2017 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'; | ||
|
||
module.exports = (process.env.CIRCLECI === 'true') ? { | ||
host: 'localhost', | ||
user: 'ubuntu', | ||
database: 'circle_test' | ||
} : { | ||
user: 'postgres', | ||
password: 'Password12!', | ||
database: 'test' | ||
}; |
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('pg'); |
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": "pg6", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"dependencies": { | ||
"pg": "^6.1.2" | ||
} | ||
} |
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,129 @@ | ||
/** | ||
* Copyright 2017 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 common = require('./common.js'); | ||
var traceLabels = require('../../src/trace-labels.js'); | ||
var assert = require('assert'); | ||
|
||
describe('test-trace-pg', function() { | ||
var traceApi; | ||
var pool; | ||
var client; | ||
var releaseClient; | ||
before(function() { | ||
traceApi = require('../..').start({ | ||
samplingRate: 0, | ||
enhancedDatabaseReporting: true | ||
}); | ||
var pg = require('./fixtures/pg6'); | ||
pool = new pg.Pool(require('../pg-config.js')); | ||
}); | ||
|
||
beforeEach(function(done) { | ||
pool.connect(function(err, c, release) { | ||
client = c; | ||
releaseClient = release; | ||
assert(!err); | ||
client.query('CREATE TABLE t (name text NOT NULL, id text NOT NULL)', [], | ||
function(err, res) { | ||
assert(!err); | ||
common.cleanTraces(traceApi); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
afterEach(function(done) { | ||
client.query('DROP TABLE t', [], function(err, res) { | ||
assert(!err); | ||
releaseClient(); | ||
common.cleanTraces(traceApi); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should perform basic operations', function(done) { | ||
common.runInTransaction(traceApi, function(endRootSpan) { | ||
client.query('INSERT INTO t (name, id) VALUES($1, $2)', | ||
['test_name', 'test_id'], function(err, res) { | ||
endRootSpan(); | ||
assert(!err); | ||
var span = common.getMatchingSpan(traceApi, function (span) { | ||
return span.name === 'pg-query'; | ||
}); | ||
assert.equal(span.labels.query, 'INSERT INTO t (name, id) VALUES($1, $2)'); | ||
assert.equal(span.labels.values, '[ \'test_name\', \'test_id\' ]'); | ||
assert.equal(span.labels.row_count, '1'); | ||
assert.equal(span.labels.oid, '0'); | ||
assert.equal(span.labels.rows, '[]'); | ||
assert.equal(span.labels.fields, '[]'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should remove trace frames from stack', function(done) { | ||
common.runInTransaction(traceApi, function(endRootSpan) { | ||
client.query('SELECT $1::int AS number', [1], function(err, res) { | ||
endRootSpan(); | ||
assert(!err); | ||
var span = common.getMatchingSpan(traceApi, function (span) { | ||
return span.name === 'pg-query'; | ||
}); | ||
var labels = span.labels; | ||
var stackTrace = JSON.parse(labels[traceLabels.STACK_TRACE_DETAILS_KEY]); | ||
// Ensure that our patch is on top of the stack | ||
assert( | ||
stackTrace.stack_frame[0].method_name.indexOf('query_trace') !== -1); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should work with events', function(done) { | ||
common.runInTransaction(traceApi, function(endRootSpan) { | ||
var query = client.query('SELECT $1::int AS number', [1]); | ||
query.on('row', function(row) { | ||
assert.strictEqual(row.number, 1); | ||
}); | ||
query.on('end', function() { | ||
endRootSpan(); | ||
var span = common.getMatchingSpan(traceApi, function (span) { | ||
return span.name === 'pg-query'; | ||
}); | ||
assert.equal(span.labels.query, 'SELECT $1::int AS number'); | ||
assert.equal(span.labels.values, '[ \'1\' ]'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should work without events or callback', function(done) { | ||
common.runInTransaction(traceApi, function(endRootSpan) { | ||
client.query('SELECT $1::int AS number', [1]); | ||
setTimeout(function() { | ||
endRootSpan(); | ||
var span = common.getMatchingSpan(traceApi, function (span) { | ||
return span.name === 'pg-query'; | ||
}); | ||
assert.equal(span.labels.query, 'SELECT $1::int AS number'); | ||
assert.equal(span.labels.values, '[ \'1\' ]'); | ||
done(); | ||
}, 50); | ||
}); | ||
}); | ||
}); |
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