Merge pull request #12635 from Budibase/tweak-db-instrumentation

Give db operation spans better names.
This commit is contained in:
Sam Rose 2023-12-19 17:52:29 +00:00 committed by GitHub
commit 8072d9c2a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 22 deletions

View File

@ -37,7 +37,7 @@ export function DatabaseWithConnection(
opts?: DatabaseOpts opts?: DatabaseOpts
) { ) {
const db = new DatabaseImpl(dbName, opts, connection) const db = new DatabaseImpl(dbName, opts, connection)
return new DDInstrumentedDatabase(db, "couchdb") return new DDInstrumentedDatabase(db)
} }
export class DatabaseImpl implements Database { export class DatabaseImpl implements Database {

View File

@ -3,7 +3,7 @@ import { CouchFindOptions, Database, DatabaseOpts } from "@budibase/types"
import { DDInstrumentedDatabase } from "./instrumentation" import { DDInstrumentedDatabase } from "./instrumentation"
export function getDB(dbName: string, opts?: DatabaseOpts): Database { export function getDB(dbName: string, opts?: DatabaseOpts): Database {
return new DDInstrumentedDatabase(new DatabaseImpl(dbName, opts), "couchdb") return new DDInstrumentedDatabase(new DatabaseImpl(dbName, opts))
} }
// we have to use a callback for this so that we can close // we have to use a callback for this so that we can close

View File

@ -18,31 +18,28 @@ import tracer from "dd-trace"
import { Writable } from "stream" import { Writable } from "stream"
export class DDInstrumentedDatabase implements Database { export class DDInstrumentedDatabase implements Database {
constructor( constructor(private readonly db: Database) {}
private readonly db: Database,
private readonly resource: string
) {}
get name(): string { get name(): string {
return this.db.name return this.db.name
} }
exists(): Promise<boolean> { exists(): Promise<boolean> {
return tracer.trace("exists", { resource: this.resource }, span => { return tracer.trace("db.exists", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.exists() return this.db.exists()
}) })
} }
checkSetup(): Promise<DocumentScope<any>> { checkSetup(): Promise<DocumentScope<any>> {
return tracer.trace("checkSetup", { resource: this.resource }, span => { return tracer.trace("db.checkSetup", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.checkSetup() return this.db.checkSetup()
}) })
} }
get<T extends Document>(id?: string | undefined): Promise<T> { get<T extends Document>(id?: string | undefined): Promise<T> {
return tracer.trace("get", { resource: this.resource }, span => { return tracer.trace("db.get", span => {
span?.addTags({ db_name: this.name, doc_id: id }) span?.addTags({ db_name: this.name, doc_id: id })
return this.db.get(id) return this.db.get(id)
}) })
@ -52,7 +49,7 @@ export class DDInstrumentedDatabase implements Database {
ids: string[], ids: string[],
opts?: { allowMissing?: boolean | undefined } | undefined opts?: { allowMissing?: boolean | undefined } | undefined
): Promise<T[]> { ): Promise<T[]> {
return tracer.trace("getMultiple", { resource: this.resource }, span => { return tracer.trace("db.getMultiple", span => {
span?.addTags({ span?.addTags({
db_name: this.name, db_name: this.name,
num_docs: ids.length, num_docs: ids.length,
@ -66,7 +63,7 @@ export class DDInstrumentedDatabase implements Database {
id: string | Document, id: string | Document,
rev?: string | undefined rev?: string | undefined
): Promise<DocumentDestroyResponse> { ): Promise<DocumentDestroyResponse> {
return tracer.trace("remove", { resource: this.resource }, span => { return tracer.trace("db.remove", span => {
span?.addTags({ db_name: this.name, doc_id: id }) span?.addTags({ db_name: this.name, doc_id: id })
return this.db.remove(id, rev) return this.db.remove(id, rev)
}) })
@ -76,14 +73,14 @@ export class DDInstrumentedDatabase implements Database {
document: AnyDocument, document: AnyDocument,
opts?: DatabasePutOpts | undefined opts?: DatabasePutOpts | undefined
): Promise<DocumentInsertResponse> { ): Promise<DocumentInsertResponse> {
return tracer.trace("put", { resource: this.resource }, span => { return tracer.trace("db.put", span => {
span?.addTags({ db_name: this.name, doc_id: document._id }) span?.addTags({ db_name: this.name, doc_id: document._id })
return this.db.put(document, opts) return this.db.put(document, opts)
}) })
} }
bulkDocs(documents: AnyDocument[]): Promise<DocumentBulkResponse[]> { bulkDocs(documents: AnyDocument[]): Promise<DocumentBulkResponse[]> {
return tracer.trace("bulkDocs", { resource: this.resource }, span => { return tracer.trace("db.bulkDocs", span => {
span?.addTags({ db_name: this.name, num_docs: documents.length }) span?.addTags({ db_name: this.name, num_docs: documents.length })
return this.db.bulkDocs(documents) return this.db.bulkDocs(documents)
}) })
@ -92,7 +89,7 @@ export class DDInstrumentedDatabase implements Database {
allDocs<T extends Document>( allDocs<T extends Document>(
params: DatabaseQueryOpts params: DatabaseQueryOpts
): Promise<AllDocsResponse<T>> { ): Promise<AllDocsResponse<T>> {
return tracer.trace("allDocs", { resource: this.resource }, span => { return tracer.trace("db.allDocs", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.allDocs(params) return this.db.allDocs(params)
}) })
@ -102,56 +99,56 @@ export class DDInstrumentedDatabase implements Database {
viewName: string, viewName: string,
params: DatabaseQueryOpts params: DatabaseQueryOpts
): Promise<AllDocsResponse<T>> { ): Promise<AllDocsResponse<T>> {
return tracer.trace("query", { resource: this.resource }, span => { return tracer.trace("db.query", span => {
span?.addTags({ db_name: this.name, view_name: viewName }) span?.addTags({ db_name: this.name, view_name: viewName })
return this.db.query(viewName, params) return this.db.query(viewName, params)
}) })
} }
destroy(): Promise<void | OkResponse> { destroy(): Promise<void | OkResponse> {
return tracer.trace("destroy", { resource: this.resource }, span => { return tracer.trace("db.destroy", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.destroy() return this.db.destroy()
}) })
} }
compact(): Promise<void | OkResponse> { compact(): Promise<void | OkResponse> {
return tracer.trace("compact", { resource: this.resource }, span => { return tracer.trace("db.compact", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.compact() return this.db.compact()
}) })
} }
dump(stream: Writable, opts?: DatabaseDumpOpts | undefined): Promise<any> { dump(stream: Writable, opts?: DatabaseDumpOpts | undefined): Promise<any> {
return tracer.trace("dump", { resource: this.resource }, span => { return tracer.trace("db.dump", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.dump(stream, opts) return this.db.dump(stream, opts)
}) })
} }
load(...args: any[]): Promise<any> { load(...args: any[]): Promise<any> {
return tracer.trace("load", { resource: this.resource }, span => { return tracer.trace("db.load", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.load(...args) return this.db.load(...args)
}) })
} }
createIndex(...args: any[]): Promise<any> { createIndex(...args: any[]): Promise<any> {
return tracer.trace("createIndex", { resource: this.resource }, span => { return tracer.trace("db.createIndex", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.createIndex(...args) return this.db.createIndex(...args)
}) })
} }
deleteIndex(...args: any[]): Promise<any> { deleteIndex(...args: any[]): Promise<any> {
return tracer.trace("deleteIndex", { resource: this.resource }, span => { return tracer.trace("db.deleteIndex", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.deleteIndex(...args) return this.db.deleteIndex(...args)
}) })
} }
getIndexes(...args: any[]): Promise<any> { getIndexes(...args: any[]): Promise<any> {
return tracer.trace("getIndexes", { resource: this.resource }, span => { return tracer.trace("db.getIndexes", span => {
span?.addTags({ db_name: this.name }) span?.addTags({ db_name: this.name })
return this.db.getIndexes(...args) return this.db.getIndexes(...args)
}) })