Skip to content

Commit

Permalink
chore: optimise url parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
kirrg001 committed Feb 24, 2023
1 parent 9f06fa2 commit 3f37db1
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports.init = function init() {
requireHook.onModuleLoad('@elastic/elasticsearch', instrument);
};

let connectionString;
let parsedConnectionUrl;

function instrument(es, esModuleFilename) {
// v8
Expand All @@ -40,7 +40,12 @@ function instrument(es, esModuleFilename) {
const client = new OriginalClient(...arguments);

if (client.connectionPool && client.connectionPool.connections && client.connectionPool.connections.length > 0) {
connectionString = client.connectionPool.connections[0].url;
const connectionString = client.connectionPool.connections[0].url;
try {
parsedConnectionUrl = new URL(connectionString);
} catch (parseErrr) {
/* ignore */
}
}

return client;
Expand Down Expand Up @@ -215,8 +220,7 @@ function parseConnectionFromResult(span, result) {
span.data.elasticsearch.address = connectionUrl.hostname;
span.data.elasticsearch.port = connectionUrl.port;
}
} else if (connectionString) {
const parsedConnectionUrl = new URL(connectionString);
} else if (parsedConnectionUrl) {
span.data.elasticsearch.address = parsedConnectionUrl.hostname;
span.data.elasticsearch.port = parsedConnectionUrl.port;
}
Expand Down

0 comments on commit 3f37db1

Please sign in to comment.