-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added segment synthesis for db client otel spans to db trace (#…
- Loading branch information
Showing
12 changed files
with
413 additions
and
153 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
const logger = require('../../logger').child({ component: 'elasticsearch_query_parser' }) | ||
const { isNotEmpty } = require('../../util/objects') | ||
|
||
/** | ||
* Parses the parameters sent to elasticsearch for collection, | ||
* method, and query | ||
* | ||
* @param {object} params Query object received by the datashim. | ||
* Required properties: path {string}, method {string}. | ||
* Optional properties: querystring {string}, body {object}, and | ||
* bulkBody {object} | ||
* @returns {object} consisting of collection {string}, operation {string}, | ||
* and query {string} | ||
*/ | ||
function queryParser(params) { | ||
params = JSON.parse(params) | ||
const { collection, operation } = parsePath(params.path, params.method) | ||
|
||
// the substance of the query may be in querystring or in body. | ||
let queryParam = {} | ||
if (isNotEmpty(params.querystring)) { | ||
queryParam = params.querystring | ||
} | ||
// let body or bulkBody override querystring, as some requests have both | ||
if (isNotEmpty(params.body)) { | ||
queryParam = params.body | ||
} else if (Array.isArray(params.bulkBody) && params.bulkBody.length) { | ||
queryParam = params.bulkBody | ||
} | ||
// The helper interface provides a simpler API: | ||
|
||
const query = JSON.stringify(queryParam) | ||
|
||
return { | ||
collection, | ||
operation, | ||
query | ||
} | ||
} | ||
|
||
/** | ||
* Convenience function for parsing the params.path sent to the queryParser | ||
* for normalized collection and operation | ||
* | ||
* @param {string} pathString params.path supplied to the query parser | ||
* @param {string} method http method called by @elastic/elasticsearch | ||
* @returns {object} consisting of collection {string} and operation {string} | ||
*/ | ||
function parsePath(pathString, method) { | ||
let collection | ||
let operation | ||
const defaultCollection = 'any' | ||
const actions = { | ||
GET: 'get', | ||
PUT: 'create', | ||
POST: 'create', | ||
DELETE: 'delete', | ||
HEAD: 'exists' | ||
} | ||
const suffix = actions[method] | ||
|
||
try { | ||
const path = pathString.split('/') | ||
if (method === 'PUT' && path.length === 2) { | ||
collection = path?.[1] || defaultCollection | ||
operation = `index.create` | ||
return { collection, operation } | ||
} | ||
path.forEach((segment, idx) => { | ||
const prev = idx - 1 | ||
let opname | ||
if (segment === '_search') { | ||
collection = path?.[prev] || defaultCollection | ||
operation = `search` | ||
} else if (segment[0] === '_') { | ||
opname = segment.substring(1) | ||
collection = path?.[prev] || defaultCollection | ||
operation = `${opname}.${suffix}` | ||
} | ||
}) | ||
if (!operation && !collection) { | ||
// likely creating an index--no underscore segments | ||
collection = path?.[1] || defaultCollection | ||
operation = `index.${suffix}` | ||
} | ||
} catch (e) { | ||
logger.warn('Failed to parse path for operation and collection. Using defaults') | ||
logger.warn(e) | ||
collection = defaultCollection | ||
operation = 'unknown' | ||
} | ||
|
||
return { collection, operation } | ||
} | ||
|
||
module.exports = { queryParser, parsePath } |
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,29 @@ | ||
/* | ||
* Copyright 2024 New Relic Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict' | ||
|
||
/** | ||
* parser used to grab the collection and operation | ||
* from a running query | ||
* | ||
* @param {object} operation mongodb operation | ||
* @returns {object} { operation, collection } parsed operation and collection | ||
*/ | ||
function queryParser(operation) { | ||
let collection = this.collectionName || 'unknown' | ||
|
||
// cursor methods have collection on namespace.collection | ||
if (this?.namespace?.collection) { | ||
collection = this.namespace.collection | ||
// (un)ordered bulk operations have collection on different key | ||
} else if (this?.s?.collection?.collectionName) { | ||
collection = this.s.collection.collectionName | ||
} | ||
|
||
return { operation, collection } | ||
} | ||
|
||
module.exports = queryParser |
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
Oops, something went wrong.