Skip to content

Commit

Permalink
refactor(mwcp): trace
Browse files Browse the repository at this point in the history
  • Loading branch information
waitingsong committed Nov 21, 2024
1 parent d4b10f3 commit f4974e7
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 138 deletions.
4 changes: 2 additions & 2 deletions packages/midway-component-kmore/src/lib/caller.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class CallerService {

// #region Call stack

retrieveCallerInfo(distance = 0): CallerInfo {
const callerInfo = getCallerStack(distance + 1, false)
retrieveCallerInfo(distance = 0, retrievePosition = false): CallerInfo {
const callerInfo = getCallerStack(distance + 1, retrievePosition)
return callerInfo
}

Expand Down
152 changes: 85 additions & 67 deletions packages/midway-component-kmore/src/lib/db-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
Singleton,
} from '@midwayjs/core'
import {
type Span,
type TraceService,
AttrNames,
Attributes,
DecoratorTraceData,
Expand All @@ -16,6 +18,7 @@ import {
Trace,
TraceLog,
TraceScopeType,
getSpan,
} from '@mwcp/otel'
import { Application, Context, MConfig, getWebContext } from '@mwcp/share'
import { humanMemoryUsage } from '@waiting/shared-core'
Expand All @@ -38,7 +41,7 @@ export class DbEvent<SourceName extends string = string> {
@Inject() readonly baseDir: string

@Inject() readonly trxStatusSvc: TrxStatusService

@Inject() readonly traceService: TraceService

getDbConfigByDbId(dbId: SourceName): DbConfig | undefined {
assert(dbId)
Expand All @@ -62,6 +65,7 @@ export class DbEvent<SourceName extends string = string> {
}
}


// #region onStart

@Trace<DbEvent['onStart']>({
Expand All @@ -77,39 +81,43 @@ export class DbEvent<SourceName extends string = string> {
const name = `Kmore ${kmore.dbId} ${method}`
return name
},
scope([options]) {
before([options], decoratorContext) {
if (! eventNeedTrace(KmoreAttrNames.BuilderCompile, options.dbConfig)) { return }
const traceContext = decoratorContext.traceContext ?? this.traceService.getActiveContext()
const { kmore, event } = options
const { kmoreQueryId, queryBuilder } = event

const traceScope = this.retrieveTraceScope(kmore, kmoreQueryId, queryBuilder)
return traceScope
},
before: ([options], decoratorContext) => {
if (! eventNeedTrace(KmoreAttrNames.BuilderCompile, options.dbConfig)) { return }
const { event } = options
const { queryBuilder } = event
if (! this.trxStatusSvc.getTraceContextByScope(traceScope)) {
this.trxStatusSvc.setTraceContextByScope(traceScope, traceContext)
}
this.trxStatusSvc.setTraceContextByScope(kmoreQueryId, traceContext)

const { pagingType } = queryBuilder
const { traceSpan } = decoratorContext
const traceSpan = getSpan(traceContext)
const ret: DecoratorTraceData = {}

if (pagingType && traceSpan) {
const { traceService, traceScope, traceContext } = decoratorContext
if (pagingType === 'counter') {
assert(queryBuilder.pagingGroupKey, 'queryBuilder.pagingGroupKey is empty')
if (! this.trxStatusSvc.getTraceContextByScope(queryBuilder.pagingGroupKey)) {
this.trxStatusSvc.setTraceContextByScope(queryBuilder.pagingGroupKey, traceContext)
}

// @ts-expect-error name
const spanName = traceSpan.name as string
const spanName2 = `${spanName} AutoPaging`
if (! spanName.endsWith('AutoPaging')) {
traceSpan.updateName(spanName2)
}

const opts: StartScopeActiveSpanOptions = {
name: 'Kmore Counter',
scope: traceScope,
traceContext,
}
assert(traceService, 'traceService is empty')
const { span, traceContext: traceCtx2 } = traceService.startScopeActiveSpan(opts)
void span
ret.traceContext = traceCtx2
const { traceContext: traceCtx2 } = this.traceService.startScopeSpan(opts)
this.trxStatusSvc.setTraceContextByScope(kmoreQueryId, traceCtx2)
ret.traceContext = traceCtx2 // necessary
}
else {
const spanName2 = 'Kmore Pager'
Expand All @@ -133,27 +141,15 @@ export class DbEvent<SourceName extends string = string> {
// value: true,
// })
// }

const cb = dbConfig.eventCallbacks?.start
return cb?.(event, kmore)
}

// #region onResp

@TraceLog<DbEvent['onResp']>({
scope([options]) {
const { kmore, event } = options
// const traceScope = this.getTrxTraceScopeByQueryId(kmore, event.kmoreQueryId)
// if (traceScope) {
// return traceScope
// }
// const traceScope2 = event.queryBuilder.kmoreQueryId
const traceScope = this.retrieveTraceScope(kmore, event.kmoreQueryId, event.queryBuilder)
return traceScope
},
before: ([options]) => {
before([options]) {
if (! eventNeedTrace(KmoreAttrNames.QueryResponse, options.dbConfig)) { return }

const { respRaw } = options.event

const attrs: Attributes = {}
Expand All @@ -171,28 +167,55 @@ export class DbEvent<SourceName extends string = string> {

return { attrs, events }
},
after([options], _result, decoratorContext) {
after([options]) {
if (! eventNeedTrace(KmoreAttrNames.QueryResponse, options.dbConfig)) { return }

const { traceService, traceScope, traceSpan } = decoratorContext
assert(traceService, 'traceService is empty')
assert(traceScope, 'onResp.after() traceScope is empty')
assert(traceSpan, 'traceSpan is empty')

const ret: DecoratorTraceData = {}

const { pagingType } = options.event.queryBuilder
if (pagingType && pagingType !== 'counter') {
ret.endParentSpan = true
}

const scopeRootSpan = traceService.getActiveSpanOnlyScope(traceScope)
// const foo = traceService.retrieveParentTraceInfoBySpan(traceSpan, traceScope)
// void foo
const { kmore, event } = options
const { kmoreQueryId } = event
const traceScope = this.retrieveTraceScope(kmore, kmoreQueryId, event.queryBuilder)

switch (pagingType) {
case 'counter': {
this.trxStatusSvc.removeTraceContextByScope(kmoreQueryId)
ret.endSpanAfterTraceLog = true
break
}

if (scopeRootSpan && scopeRootSpan === traceSpan) {
ret.endSpanAfterTraceLog = true
case 'pager': {
const spans: Span[] = []
const scopeCtx = this.trxStatusSvc.getTraceContextByScope(traceScope)
if (scopeCtx) {
const span = getSpan(scopeCtx)
if (span?.isRecording()) {
spans.push(span)
}
}
const activeCtx = this.trxStatusSvc.getTraceContextByScope(event.kmoreQueryId)
if (activeCtx) {
const span = getSpan(activeCtx)
if (span?.isRecording()) {
spans.push(span)
}
}

this.trxStatusSvc.removeTraceContextByScope(kmoreQueryId)
const { pagingGroupKey } = event.queryBuilder
if (pagingGroupKey) {
this.trxStatusSvc.removeTraceContextByScope(pagingGroupKey)
}
this.trxStatusSvc.removeTraceContextByScope(traceScope)
ret.endSpanAfterTraceLog = spans
break
}

default: {
ret.endSpanAfterTraceLog = true
}
}

return ret
},
})
Expand All @@ -209,17 +232,10 @@ export class DbEvent<SourceName extends string = string> {
// #region onQuery

@TraceLog<DbEvent['onQuery']>({
scope([options]) {
const { kmore, event } = options
const traceScope = this.retrieveTraceScope(kmore, event.kmoreQueryId, event.queryBuilder)
return traceScope
},
before([options], decoratorContext) {
if (! eventNeedTrace(KmoreAttrNames.QueryQuerying, options.dbConfig)) { return }

const { traceService, traceScope, traceSpan } = decoratorContext
const { traceService, traceSpan } = decoratorContext
assert(traceService, 'traceService is empty')
assert(traceScope, 'onQuery.after() traceScope is empty')
assert(traceSpan, 'traceSpan is empty')

const { config: knexConfig } = options.dbConfig
Expand Down Expand Up @@ -262,13 +278,13 @@ export class DbEvent<SourceName extends string = string> {
// #region onError

@TraceLog<DbEvent['onError']>({
scope([options]) {
const { kmore, event } = options
const traceScope = this.retrieveTraceScope(kmore, event.kmoreQueryId, event.queryBuilder)
return traceScope
},
before([options]) {
if (! eventNeedTrace(KmoreAttrNames.QueryError, options.dbConfig)) { return }
// if (! decoratorContext.traceScope) {
// const { kmore } = options
// const { kmoreQueryId, queryBuilder } = options.event
// decoratorContext.traceScope = this.retrieveTraceScope(kmore, kmoreQueryId, queryBuilder)
// }

const {
dbId, kUid, queryUid, trxId, exData, exError,
Expand Down Expand Up @@ -296,13 +312,15 @@ export class DbEvent<SourceName extends string = string> {

const { traceService, traceScope, traceSpan } = decoratorContext
assert(traceService, 'traceService is empty')
assert(traceScope, 'onResp.after() traceScope is empty')
// assert(traceScope, 'onResp.after() traceScope is empty')
assert(traceSpan, 'traceSpan is empty')

const scopeRootSpan = traceService.getActiveSpanOnlyScope(traceScope)
if (scopeRootSpan && scopeRootSpan === traceSpan) {
const spanStatusOptions = { code: SpanStatusCode.ERROR }
return { endSpanAfterTraceLog: true, spanStatusOptions }
if (traceScope) {
const scopeRootSpan = traceService.getRootSpan(traceScope)
if (scopeRootSpan && scopeRootSpan === traceSpan) {
const spanStatusOptions = { code: SpanStatusCode.ERROR }
return { endSpanAfterTraceLog: true, spanStatusOptions }
}
}

return null
Expand All @@ -318,12 +336,7 @@ export class DbEvent<SourceName extends string = string> {
}


protected getTrxTraceScopeByQueryId(db: Kmore, queryId: symbol): TraceScopeType | undefined {
const trx = db.getTrxByQueryId(queryId)
return trx?.kmoreTrxId
}

protected retrieveTraceScope(kmore: Kmore, kmoreQueryId: symbol, builder: KmoreQueryBuilder): TraceScopeType {
retrieveTraceScope(kmore: Kmore, kmoreQueryId: symbol, builder: KmoreQueryBuilder): TraceScopeType {
const { pagingGroupKey, pagingType } = builder

const traceScope = this.getTrxTraceScopeByQueryId(kmore, kmoreQueryId)
Expand All @@ -339,6 +352,11 @@ export class DbEvent<SourceName extends string = string> {
return traceScope ?? kmoreQueryId
}

protected getTrxTraceScopeByQueryId(db: Kmore, queryId: symbol): TraceScopeType | undefined {
const trx = db.getTrxByQueryId(queryId)
return trx?.kmoreTrxId
}

}

// #region types
Expand Down
33 changes: 19 additions & 14 deletions packages/midway-component-kmore/src/lib/db-hook/db-hook.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export class DbHookBuilder<SourceName extends string = string> {
}

/* Call stack in bindBuilderPropagationData():
1 - TrxStatusService.bindBuilderPropagationData(trx - status.service.ts: 389)
2 - DbSourceManager.builderPrePropagating(db - source - manager.ts: 377)
3 - DbSourceManager.builderPreProcessor(db - source - manager.ts: 378)
1 - TrxStatusService.bindBuilderPropagationData(trx-status.service.ts: 389)
2 - DbSourceManager.builderPrePropagating(db- source-manager.ts: 377)
3 - DbSourceManager.builderPreProcessor(db-source-manager.ts: 378)
4 - <anonymous>(builder.index.js: 67)
5 - extRefTableFnProperty(builder.index.js: 64)
6 - tb_user(builder.index.js: 28)
Expand All @@ -84,7 +84,20 @@ export class DbHookBuilder<SourceName extends string = string> {
7 - TrxRepo._update (101/101r.middle-trx-auto-action.repo.ts:46) <-- call from here 6+1
8 - TrxRepo.commit (101/101r.middle-trx-auto-action.repo.ts:33)
*/
this.trxStatusSvc.bindBuilderPropagationData(kmore.dbId, builder, 6)
/*
1 - TrxStatusService.bindBuilderPropagationData(src\lib\trx-status.service.ts: 342)
2 - DbHookBuilder.builderPrePropagating(src\lib\db-hook\db-hook.builder.ts: 87)
3 - DbHookBuilder.builderPreHooks(src\lib\db-hook\db-hook.builder.ts: 44)
4 - <anonymous>(kmore\dist\lib\builder\builder.index.js: 57)
5 - createQueryBuilder(kmore\dist\lib\builder\builder.index.js: 55)
6 - tb_user(kmore\dist\lib\builder\builder.index.js: 25)
7 - TransactionalSimpleRepo.getUserOne(test\fixtures\base-app\src\transactional\30\30r.transactional - simple.repo.ts: 51)
8 - Clz.<computed>(kmore\node_modules\@midwayjs\core\dist\service\aspectService.js: 92)
9 - process.processTicksAndRejections(<node_internals> /internal/process / task_queues: 105)
10 - await(未知源: 0)
11 - user(test\fixtures\base - app\src\transactional\30\30r.transactional - simple.repo.ts: 33)
*/
this.trxStatusSvc.bindBuilderPropagationData(kmore.dbId, builder, 5)
}

protected async builderPostPropagating(options: BuilderHookOptions): Promise<void> {
Expand Down Expand Up @@ -126,20 +139,12 @@ export class DbHookBuilder<SourceName extends string = string> {
// #region builderTransactingPostHook

@TraceLog<DbHookBuilder['builderTransactingPostHook']>({
scope([options]) {
const { kmore, builder } = options
const traceScope = this.getTrxTraceScopeByQueryId(kmore, builder.kmoreQueryId)
if (traceScope) {
return traceScope
}
const traceScope2 = builder.kmoreQueryId
return traceScope2
},
after([options]) { // options.dbConfig not exists at before()
after([options]) { // options.dbConfig not exist at before()
const dbConfig = this.getDbConfigByDbId(options.kmore.dbId)
if (dbConfig && ! eventNeedTrace(KmoreAttrNames.BuilderTransacting, dbConfig)) { return }

const { builder } = options

// @ts-expect-error builder._method
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
let method: string = builder._method ?? 'unknown'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,27 @@ export function processTrxCommitAndRollback(

const { traceScope } = decoratorContext
if (traceScope) {
const scopeRootSpan2 = traceService.getActiveSpanOnlyScope(traceScope)
if (scopeRootSpan2 && scopeRootSpan2 === traceSpan) {
const scopeRootSpan2 = traceService.getRootSpan(traceScope)
if (scopeRootSpan2) {
if (scopeRootSpan2 === traceSpan) {
ret.endSpanAfterTraceLog = true
}
}
else {
ret.endSpanAfterTraceLog = true
}
return ret
}

const { scope } = trx
assert(scope, `${hook}.${stage}-${op.toUpperCase()} trx.scope is empty`)
const scopeRootSpan = traceService.getActiveSpanOnlyScope(scope)
if (scopeRootSpan && scopeRootSpan === traceSpan) {
const scopeRootSpan = traceService.getRootSpan(scope)
if (scopeRootSpan) {
if (scopeRootSpan === traceSpan) {
ret.endSpanAfterTraceLog = true
}
}
else {
ret.endSpanAfterTraceLog = true
}

Expand Down
Loading

0 comments on commit f4974e7

Please sign in to comment.