Skip to content

Commit

Permalink
refactor: add private methods for span creation
Browse files Browse the repository at this point in the history
  • Loading branch information
stfsy committed Apr 1, 2024
1 parent 81f901a commit c70cfd2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
17 changes: 12 additions & 5 deletions lib/one-to-few-ref-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = class extends Base {
* @returns {boolean}
*/
async exists(resourceIds) {
return withActiveSpan(`${name}#exists-one-to-few-ref-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('exists-one-to-few-ref-by-id', resourceIds, async () => {
const collection = await this._getCollection()
const parent = await collection.findOne({
id: resourceIds.at(0),
Expand All @@ -90,6 +90,13 @@ module.exports = class extends Base {
})
}

/**
* @private
*/
async _withActiveSpan(spanName, resourceIds, callback) {
return withActiveSpan(`${name}#${spanName}`, { 'peer.service': 'resource-client', resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, callback)
}

/**
* Returns all references.
*
Expand All @@ -98,7 +105,7 @@ module.exports = class extends Base {
* @returns {Array.<Object>}
*/
async getAll(resourceIds) {
return withActiveSpan(`${name}#get-all-one-to-few-refs-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('get-all-one-to-few-refs-by-id', resourceIds, async () => {
const collection = await this._getCollection()
const parent = await collection.findOne({
id: resourceIds.at(0)
Expand All @@ -122,7 +129,7 @@ module.exports = class extends Base {
* @returns {Promise.<ObjectId>}
*/
async create(resourceIds, ref, options) {
return withActiveSpan(`${name}#create-one-to-few-ref-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('create-one-to-few-ref-by-id', resourceIds, async () => {
const exists = await this.exists(resourceIds)

if (exists) {
Expand Down Expand Up @@ -156,7 +163,7 @@ module.exports = class extends Base {
* @returns {Promise.<void>}
*/
async findReferences(resourceIds, references) {
return withActiveSpan(`${name}#find-one-to-few-refs-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('find-one-to-few-refs-by-id', resourceIds, async () => {
const collection = await this._getCollection()

const stages = [
Expand All @@ -178,7 +185,7 @@ module.exports = class extends Base {
* @returns {Promise.<void>}
*/
async delete(resourceIds, options) {
return withActiveSpan(`${name}#delete-one-to-few-ref-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('delete-one-to-few-ref-by-id', resourceIds, async () => {
const exists = await this.exists(resourceIds)
if (!exists) {
throw new Error(`${resourceIds} does not exist.`)
Expand Down
17 changes: 12 additions & 5 deletions lib/one-to-few-resource-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ module.exports = class extends Base {
* @returns {Promise.<Object>}
*/
async get(resourceIds, { withMetadata = false, projection } = {}) {
return withActiveSpan(`${name}#get-one-to-few-resource-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('get-one-to-few-resource-by-id', resourceIds, async () => {
const collection = await this._getCollection()
const aggregationStages = [
EQUALS('id', resourceIds.at(0)),
Expand All @@ -120,6 +120,13 @@ module.exports = class extends Base {
})
}

/**
* @private
*/
async _withActiveSpan(spanName, resourceIds, callback) {
return withActiveSpan(`${name}#${spanName}`, { 'peer.service': 'resource-client', resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, callback)
}

/**
* Returns all resources.
*
Expand All @@ -129,7 +136,7 @@ module.exports = class extends Base {
* @returns {Promise.<Array.<Object>>}
*/
async getAll(resourceIds, { withMetadata = false, projection } = {}) {
return withActiveSpan(`${name}#get-all-one-to-few-resources-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('get-all-one-to-few-resources-by-id', resourceIds, async () => {
const collection = await this._getCollection()
const aggregationStages = [
EQUALS('id', resourceIds.at(0)),
Expand Down Expand Up @@ -159,7 +166,7 @@ module.exports = class extends Base {
* @returns {ObjectId}
*/
async create(resourceIds, resource) {
return withActiveSpan(`${name}#create-one-to-few-resource-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('create-one-to-few-resource-by-id', resourceIds, async () => {
const exists = await this.exists(resourceIds)
if (exists) {
throw new Error(`${resourceIds} already exists. ${JSON.stringify(exists)}`)
Expand Down Expand Up @@ -197,7 +204,7 @@ module.exports = class extends Base {
* @returns {void}
*/
async update(resourceIds, update) {
return withActiveSpan(`${name}#update-one-to-few-resource-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('update-one-to-few-resource-by-id', resourceIds, async () => {
const beforeResource = await this.get(resourceIds)
if (!beforeResource) {
throw new Error(`${resourceIds} does not exist.`)
Expand Down Expand Up @@ -249,7 +256,7 @@ module.exports = class extends Base {
* @returns {void}
*/
async delete(resourceIds) {
return withActiveSpan(`${name}#delete-one-to-few-resource-by-id`, { resourceIds, resourceName: this._resourceName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('delete-one-to-few-resource-by-id', resourceIds, async () => {
const resource = await this.get(resourceIds)
if (!resource) {
throw new Error(`${resourceIds} does not exist.`)
Expand Down
20 changes: 15 additions & 5 deletions lib/one-to-many-resource-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,19 @@ module.exports = class extends Base {
* @returns {boolean}
*/
async exists(resourceIds) {
return withActiveSpan(`${name}#exists-one-to-many-resource-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('exists-one-to-many-resource-by-id', resourceIds, async () => {
const resource = await this.get(resourceIds)
return resource != null
})
}

/**
* @private
*/
async _withActiveSpan(spanName, resourceIds, callback) {
return withActiveSpan(`${name}#${spanName}`, { 'peer.service': 'resource-client', resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, callback)
}

/**
* Returns a resource by ids.
*
Expand All @@ -146,7 +153,7 @@ module.exports = class extends Base {
* @returns {Object}
*/
async get(resourceIds, options) {
return withActiveSpan(`${name}#get-one-to-many-resource-by-id`, { 'peer.service': 'resource-client', resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async (span) => {
return this._withActiveSpan('get-one-to-many-resource-by-id', resourceIds, async (span) => {
if (!Array.isArray(resourceIds) || !(resourceIds.length == this._resourcePath.length + 1)) {
const errorMessage = `Given resourceIds ${resourceIds} and resourcePath ${this._resourcePath} dont match lengths. For this operation you need to provide resourceIds with length ${this._resourcePath.length + 1}`
span.addEvent(errorMessage, {
Expand All @@ -169,15 +176,15 @@ module.exports = class extends Base {
*/

/**
* Returns a resource by ids.
* Find a resource by via options.match query.
*
* @name get
* @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id}
* @param {FindOptions} options
* @returns {Object}
*/
async find(resourceIds, options) {
return withActiveSpan(`${name}#find-one-to-many-resource-by-id`, { 'peer.service': 'resource-client', resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async (span) => {
return this._withActiveSpan('find-one-to-many-resource-by-id', resourceIds, async (span) => {
if (!Array.isArray(resourceIds) || !(resourceIds.length == this._resourcePath.length)) {
const errorMessage = `Given resourceIds ${resourceIds} and resourcePath ${this._resourcePath} dont match lengths. For this operations you need to provide a resourceId array with length ${this._resourcePath.length}.`
span.addEvent(errorMessage, {
Expand Down Expand Up @@ -271,7 +278,7 @@ module.exports = class extends Base {
* @param {GetOptions} options
*/
async getAll(resourceIds, { withMetadata = false, addDocumentPath, projection } = {}) {
return withActiveSpan(`${name}#get-all-one-to-many-resources-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async (span) => {
return this._withActiveSpan('get-all-one-to-many-resources-by-id', resourceIds, async (span) => {
if (!Array.isArray(resourceIds) || !(resourceIds.length == this._resourcePath.length)) {
const errorMessage = `Given resourceIds ${resourceIds} and resourcePath ${this._resourcePath} dont match lengths. For this operations you need to provide a resourceId array with length ${this._resourcePath.length}.`
span.addEvent(errorMessage, {
Expand Down Expand Up @@ -321,6 +328,7 @@ module.exports = class extends Base {
* @returns
*/
async create(resourceIds, resource) {
return this._withActiveSpan('create-one-to-many-resource-by-id', resourceIds, async (span) => {
if (!Array.isArray(resourceIds) || !(resourceIds.length == this._resourcePath.length + 1)) {
const errorMessage = `Given resourceIds ${resourceIds} and resourcePath ${this._resourcePath} dont match lengths. For this operations you need to provide a resourceId array with length ${this._resourcePath.length + 1}.`
span.addEvent(errorMessage, {
Expand Down Expand Up @@ -356,6 +364,7 @@ module.exports = class extends Base {
* @returns
*/
async update(resourceIds, update) {
return this._withActiveSpan('update-one-to-many-resource-by-id', resourceIds, async (span) => {
if (!Array.isArray(resourceIds) || !(resourceIds.length == this._resourcePath.length + 1)) {
const errorMessage = `Given resourceIds ${resourceIds} and resourcePath ${this._resourcePath} dont match lengths. For this operation you need to provide resourceIds with length ${this._resourcePath.length + 1}`
span.addEvent(errorMessage, {
Expand Down Expand Up @@ -391,6 +400,7 @@ module.exports = class extends Base {
* @returns
*/
async delete(resourceIds) {
return this._withActiveSpan('delete-one-to-many-resource-by-id', resourceIds, async (span) => {
if (!Array.isArray(resourceIds) || !(resourceIds.length == this._resourcePath.length + 1)) {
const errorMessage = `Given resourceIds ${resourceIds} and resourcePath ${this._resourcePath} dont match lengths. For this operation you need to provide resourceIds with length ${this._resourcePath.length + 1}`
span.addEvent(errorMessage, {
Expand Down
29 changes: 18 additions & 11 deletions lib/simple-resource-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ module.exports = class {
* @returns {import('mongodb').Db}
*/
async _getDb(givenClient) {
return withActiveSpan(`${name}#get-db-instance`, { resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('get-db-instance', null, async () => {
const client = givenClient ?? await this._getConnectedClient()
return client.db(this._databaseName)
})
Expand All @@ -154,7 +154,7 @@ module.exports = class {
* @returns {Promise.<Collection>}
*/
async _getCollection(collectionName, givenClient) {
return withActiveSpan(`${name}#get-db-collection`, { collectionName, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('get-db-collection', null, async () => {
if (collectionName && typeof collectionName !== 'string') {
givenClient = collectionName
collectionName = null
Expand All @@ -164,6 +164,13 @@ module.exports = class {
})
}

/**
* @private
*/
async _withActiveSpan(spanName, resourceIds, callback) {
return withActiveSpan(`${name}#${spanName}`, { 'peer.service': 'resource-client', resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, callback)
}

/**
* @callback WithSessionCallback
* @param {import('mongodb').ClientSession}
Expand Down Expand Up @@ -242,7 +249,7 @@ module.exports = class {
* @returns {Object}
*/
async __getResource(resourceIds, { withMetadata = false, projection } = {}) {
return withActiveSpan(`${name}#get-simple-resource-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('get-simple-resource-by-id', resourceIds, async () => {
const collection = await this._getCollection()
const aggregationStages = [
EQUALS('id', this._toStringIfArray(resourceIds)),
Expand Down Expand Up @@ -272,7 +279,7 @@ module.exports = class {
* @returns {Array.<Object>}
*/
async getAll({ withMetadata = false, projection } = {}) {
return withActiveSpan(`${name}#get-all-simple-resources`, { resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('get-all-simple-resources', { resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
const collection = await this._getCollection()
const aggregationStages = []

Expand Down Expand Up @@ -318,7 +325,7 @@ module.exports = class {
* @returns {Promise.<ChildrenAndResourcePaths>}
*/
async getAllChildren(resourceIds, childPath, { withMetadata = false, projection, match } = {}) {
return withActiveSpan(`${name}#get-all-simple-resource-children`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('get-all-simple-resource-children', resourceIds, async () => {
if (childPath.startsWith('/')) {
childPath = childPath.substring(1)
}
Expand Down Expand Up @@ -359,7 +366,7 @@ module.exports = class {
* @returns {Array.<Object>}
*/
async find(aggregations = []) {
return withActiveSpan(`${name}#find-simple-resources`, { resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('find-simple-resources', { resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
const collection = await this._getCollection()

const cursor = collection.aggregate(aggregations)
Expand All @@ -375,7 +382,7 @@ module.exports = class {
* @returns {boolean}
*/
async exists(resourceIds) {
return withActiveSpan(`${name}#exists-simple-resource-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('exists-simple-resource-by-id', resourceIds, async () => {
const resource = await this.get(resourceIds)
return resource != undefined
})
Expand All @@ -390,7 +397,7 @@ module.exports = class {
* @returns {boolean}
*/
async _checkResourceExistsSimple(resourceIds) {
return withActiveSpan(`${name}#exists-simple-resource-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('exists-simple-resource-by-id', resourceIds, async () => {
const resource = await this.__getResource(resourceIds)
return resource != undefined
})
Expand All @@ -406,7 +413,7 @@ module.exports = class {
* @returns
*/
async create(resourceIds, resource, options) {
return withActiveSpan(`${name}#create-simple-resource-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('create-simple-resource-by-id', resourceIds, async () => {
const exists = await this._checkResourceExistsSimple(resourceIds)
if (exists) {
throw new Error(`${resourceIds} already exists. ${JSON.stringify(exists)}`)
Expand Down Expand Up @@ -452,7 +459,7 @@ module.exports = class {
* @returns
*/
async update(resourceIds, update) {
return withActiveSpan(`${name}#update-simple-resource-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('update-simple-resource-by-id', resourceIds, async () => {
return this._withTransaction(async (session, client) => {
const resource = await this.get(resourceIds)
if (!resource) {
Expand Down Expand Up @@ -540,7 +547,7 @@ module.exports = class {
* @returns
*/
async delete(resourceIds) {
return withActiveSpan(`${name}#delete-simple-resource-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => {
return this._withActiveSpan('delete-simple-resource-by-id', resourceIds, async () => {
return this._withTransaction(async (session, client) => {
const resource = await this.get(resourceIds)
if (!resource) {
Expand Down

0 comments on commit c70cfd2

Please sign in to comment.