@myrotvorets/opentelemetry-plugin-knex
Version:
OpenTelemetry knex automatic instrumentation package
47 lines (46 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectionAttributes = void 0;
const incubating_1 = require("@opentelemetry/semantic-conventions/incubating");
function findAttribute(where, keys) {
for (const key of keys) {
const value = where[key];
if (typeof value === 'number' || typeof value === 'string' || typeof value === 'boolean') {
return value;
}
}
return undefined;
}
class ConnectionAttributes {
attributes = {};
constructor(connection) {
this.parseConnection(connection);
}
getAttributes() {
return this.attributes;
}
parseConnection(connection) {
this.setDbName(connection);
this.setNetAttributes(connection);
}
setDbName(connection) {
const database = findAttribute(connection, ['filename', 'db', 'database']);
// istanbul ignore else
if (database) {
this.attributes[incubating_1.ATTR_DB_NAMESPACE] = database;
}
}
setNetAttributes(connection) {
const name = findAttribute(connection, ['host', 'server', 'unixSocket', 'socketPath']);
const port = findAttribute(connection, ['port']);
// istanbul ignore if
if (port) {
this.attributes[incubating_1.ATTR_NETWORK_PEER_PORT] = port;
}
// istanbul ignore if
if (name) {
this.attributes[incubating_1.ATTR_SERVER_ADDRESS] = name;
}
}
}
exports.ConnectionAttributes = ConnectionAttributes;