@stratumn/instrumentation-pg
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -17,2 +17,6 @@ import { BasePlugin } from '@opencensus/core'; | ||
private decorateSpan; | ||
/** Ends a span when the query is successful */ | ||
private endSpanOnSuccess; | ||
/** Ends a span when the query fails */ | ||
private endSpanOnFailure; | ||
/** Creates spans for Command operations. */ | ||
@@ -19,0 +23,0 @@ private getPatchQuery; |
@@ -42,3 +42,3 @@ "use strict"; | ||
/** Adds attributes to the span. */ | ||
decorateSpan(span, client) { | ||
decorateSpan(span, client, queryText) { | ||
span.addAttribute('db.host', client.host); | ||
@@ -48,3 +48,15 @@ span.addAttribute('db.name', client.database); | ||
span.addAttribute('db.domain', client.domain); | ||
span.addAttribute('db.queryText', queryText); | ||
} | ||
/** Ends a span when the query is successful */ | ||
endSpanOnSuccess(span, res) { | ||
span.addAttribute('db.rowCount', res.rowCount); | ||
span.end(); | ||
} | ||
/** Ends a span when the query fails */ | ||
endSpanOnFailure(span) { | ||
// Set span status to error | ||
span.status = 2; | ||
span.end(); | ||
} | ||
/** Creates spans for Command operations. */ | ||
@@ -54,11 +66,43 @@ getPatchQuery() { | ||
const decorateSpan = this.decorateSpan; | ||
const endSpanOnFailure = this.endSpanOnFailure; | ||
const endSpanOnSuccess = this.endSpanOnSuccess; | ||
const logWarning = this.logger.warn.bind(this.logger); | ||
return (original) => { | ||
return function () { | ||
let spanName = 'pg::query'; | ||
if (typeof arguments[0] === 'string') { | ||
// Add first word of sql statement to make trace more readable | ||
const command = arguments[0].trim().split(' ')[0]; | ||
spanName += ` ${command.toUpperCase()}`; | ||
// get info we want to add to the span from the arguments | ||
let queryText; | ||
let spanNameSuffix; | ||
const firstArg = arguments[0]; | ||
if (typeof firstArg === 'string') { | ||
queryText = firstArg; | ||
} | ||
const span = tracer.startChildSpan(spanName); | ||
else if (firstArg && typeof firstArg.text === 'string') { | ||
queryText = firstArg.text; | ||
} | ||
// start span and add base attributes | ||
spanNameSuffix = queryText ? queryText.trim().split(' ')[0] : ''; | ||
const span = tracer.startChildSpan(`pg::query ${spanNameSuffix}`); | ||
if (!span) { | ||
logWarning(new Error('span is not defined')); | ||
return original.apply(this, arguments); | ||
} | ||
decorateSpan(span, this, queryText); | ||
// check the last arg to see if we need to handle a promise or callback | ||
const lastArg = arguments[arguments.length - 1]; | ||
if (typeof lastArg === 'function') { | ||
// modify the callback to make sure we end the span | ||
arguments[arguments.length - 1] = (err, res) => { | ||
if (span) { | ||
if (err) { | ||
endSpanOnFailure(span); | ||
} | ||
else { | ||
endSpanOnSuccess(span, res); | ||
} | ||
} | ||
lastArg(err, res); | ||
}; | ||
return original.apply(this, arguments); | ||
} | ||
// make sure the promise ends the span whether it resolves or rejects | ||
return original | ||
@@ -68,5 +112,3 @@ .apply(this, arguments) | ||
if (span) { | ||
decorateSpan(span, this); | ||
span.addAttribute('db.rowCount', res.rowCount); | ||
span.end(); | ||
endSpanOnSuccess(span, res); | ||
} | ||
@@ -77,6 +119,3 @@ return res; | ||
if (span) { | ||
decorateSpan(span, this); | ||
// Set span status to error | ||
span.status = 2; | ||
span.end(); | ||
endSpanOnFailure(span); | ||
} | ||
@@ -83,0 +122,0 @@ throw err; |
{ | ||
"name": "@stratumn/instrumentation-pg", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Opencensus plugin for pg", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
12518
157