Skip to content

Commit aa30f4d

Browse files
committed
refactor(otel): remove TraceDecoratorOptions['scope'] for @trace param scope and more
- TraceDecoratorOptions['scope'] - genTraceScopeFrom() - getScopeStringCache() - genTraceScope() - updateTraceScopeProperty() - getTraceScopeParamFromArgs()
1 parent 948261c commit aa30f4d

11 files changed

+11
-567
lines changed

packages/otel/src/lib/decorator.helper.base.ts

Lines changed: 1 addition & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
import assert from 'node:assert'
2-
31
import type { Span } from '@opentelemetry/api'
4-
import { isArrowFunction } from '@waiting/shared-core'
52

63
import type {
7-
DecoratorContextBase,
8-
DecoratorExecutorParam,
94
DecoratorTraceDataResp,
10-
ScopeGenerator,
11-
TraceDecoratorOptions,
125
TraceService,
136
} from './trace.service/index.trace.service.js'
14-
import type { Attributes, TraceContext, TraceScopeParamType, TraceScopeType } from './types.js'
7+
import type { Attributes, TraceContext, TraceScopeParamType } from './types.js'
158
import { getSpan } from './util.js'
169

1710

@@ -55,143 +48,6 @@ function processEvents(
5548
traceService.addEvent(span, events)
5649
}
5750

58-
/**
59-
* @deprecated
60-
* @note
61-
* - input options.traceScope will be updated by generated traceScope
62-
* - the first object arg of methodArgs will be appended key `traceScope` and set value
63-
*/
64-
export function genTraceScopeFrom(options: DecoratorExecutorParam): TraceScopeType | undefined {
65-
if (options.traceScope && isTraceScopeParamType(options.traceScope)) {
66-
return options.traceScope
67-
}
68-
69-
const decoratorContextBase: DecoratorContextBase = {
70-
webApp: options.webApp,
71-
webContext: options.webContext,
72-
traceService: options.traceService,
73-
traceScope: options.traceScope,
74-
/** Caller Class name */
75-
instanceName: options.instanceName,
76-
/** Caller method name */
77-
methodName: options.methodName,
78-
// instance: options.instance,
79-
}
80-
81-
const scopeFromArg = getTraceScopeParamFromArgs(options.methodArgs as GetTraceScopeFromArgsOptions[])
82-
let ret = genTraceScope({
83-
scope: scopeFromArg,
84-
methodArgs: options.methodArgs,
85-
decoratorContext: decoratorContextBase,
86-
instance: options.instance,
87-
})
88-
if (! ret) {
89-
const scope: TraceScopeParamType | undefined = options.mergedDecoratorParam?.scope
90-
ret = genTraceScope({
91-
scope,
92-
methodArgs: options.methodArgs,
93-
decoratorContext: decoratorContextBase,
94-
instance: options.instance,
95-
})
96-
}
97-
return ret
98-
}
99-
100-
const traceScopeStringCache = new Map<string, symbol>()
101-
/**
102-
* @deprecated
103-
*/
104-
export function getScopeStringCache(key: string): symbol {
105-
let sym = traceScopeStringCache.get(key)
106-
if (! sym) {
107-
sym = Symbol(key)
108-
setScopeStringCache(key, sym)
109-
}
110-
return sym
111-
}
112-
function setScopeStringCache(key: string, sym: symbol): void {
113-
/* c8 ignore next 3 */
114-
if (traceScopeStringCache.size > 10000) {
115-
console.warn('traceScopeStringCache.size > 10000, should clear it')
116-
}
117-
traceScopeStringCache.set(key, sym)
118-
}
119-
120-
export interface GenTraceScopeOptions {
121-
scope: TraceDecoratorOptions['scope']
122-
methodArgs: unknown[]
123-
decoratorContext: DecoratorContextBase
124-
instance: DecoratorExecutorParam['instance']
125-
}
126-
export function genTraceScope(options: GenTraceScopeOptions): TraceScopeType | undefined {
127-
const { scope } = options
128-
129-
let ret: TraceScopeType | undefined
130-
switch (typeof scope) {
131-
case 'string': {
132-
// Symbol.for() invalid as key of WeakMap
133-
ret = getScopeStringCache(scope)
134-
break
135-
}
136-
137-
case 'object': {
138-
ret = scope
139-
break
140-
}
141-
142-
case 'undefined':
143-
return
144-
145-
case 'function': {
146-
assert(options.instance, 'instance is required')
147-
const funcBind = (isArrowFunction(scope as ScopeGenerator) ? scope : scope.bind(options.instance)) as ScopeGenerator
148-
ret = funcBind(options.methodArgs, options.decoratorContext)
149-
assert(typeof ret === 'object' || typeof ret === 'symbol', 'scope function must return an object or a symbol')
150-
break
151-
}
152-
153-
case 'symbol': {
154-
ret = scope
155-
break
156-
}
157-
158-
/* c8 ignore next 2 */
159-
default:
160-
throw new Error('scope must be a string, an object, a symbol, or a function')
161-
}
162-
163-
return ret
164-
}
165-
166-
// function updateTraceScopeProperty(input: TraceScopeType): void {
167-
// assert(typeof input === 'object', 'input must be an object')
168-
// if (input.isTraceScope) { return }
169-
// Object.defineProperty(input, 'isTraceScope', { value: true })
170-
// }
171-
172-
type GetTraceScopeFromArgsOptions = TraceScopeParamType | { traceScope?: TraceScopeParamType, [key: string]: unknown }
173-
174-
export function getTraceScopeParamFromArgs(
175-
args: GetTraceScopeFromArgsOptions[] | undefined,
176-
): TraceScopeParamType | undefined {
177-
178-
const key = 'traceScope'
179-
180-
if (! args || ! Array.isArray(args)) { return }
181-
for (const arg of args) {
182-
183-
if (! arg || typeof arg !== 'object') { continue }
184-
185-
// @ts-ignore
186-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
187-
const val = arg[key]
188-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
189-
if (val && isTraceScopeParamType(val)) {
190-
return val
191-
}
192-
}
193-
}
194-
19551
export function isTraceScopeParamType(input: TraceScopeParamType | undefined): input is TraceScopeParamType {
19652
if (! input) {
19753
return false

packages/otel/src/lib/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,5 @@ export {
5555

5656

5757
export { genDecoratorExecutorOptions } from './trace.helper.js'
58-
export {
59-
genTraceScope, getScopeStringCache,
60-
} from './decorator.helper.base.js'
61-
6258
export { AttrNames } from './attrnames.types.js'
6359

packages/otel/src/lib/trace.service/trace.service.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import {
4040
type SpanStatusOptions,
4141
AttrNames,
4242
ConfigKey,
43-
TraceScopeParamType,
4443
TraceScopeType,
4544
middlewareEnableCacheKey,
4645
} from '../types.js'
@@ -268,21 +267,6 @@ export interface TraceDecoratorOptions<
268267
*/
269268
spanNameDelimiter: string | undefined
270269

271-
/**
272-
* 生成唯一标识符,用于确定同一方法的跨度, 避免异步方法并发调用时调用链关系混乱
273-
* Generate the unique key for spans determination of the same method,
274-
* avoid the confusion of call chain relationship when async methods are called concurrently
275-
* @default undefined, runtime value rule (priority from high to low):
276-
* - passed value in options.traceScope
277-
* - generated automatically retrieved from object arg of the method args, that containing key `traceScope`,
278-
* - webContext (traceService.ctx)
279-
* - run before the `before()` method
280-
* @caution symbol must be non-registered symbols, it means Symbol(string) is valid, and Symbol.for(string) is invalid
281-
* @note `TraceInit()` not supported
282-
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap
283-
*/
284-
scope: ScopeGenerator<MThis, MParamType> | TraceScopeParamType | undefined
285-
286270
before: MethodTypeUnknown<
287271
[MParamType, DecoratorContext<MThis>], // input args
288272
DecoratorTraceDataResp | DecoratorTraceDataRespAsync, // output data

packages/otel/test/api-test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export const apiMethod = {
4444
scope: 'scope',
4545
simple_scope: 'simple_scope',
4646
trace_auto_scope: 'trace_auto_scope',
47-
scope_priority: 'scope_priority',
4847

4948
after_throw: 'after_throw',
5049
}

packages/otel/test/decorator/114.docorator-scope-priority.test.ts

Lines changed: 0 additions & 129 deletions
This file was deleted.

packages/otel/test/fixtures/base-app/src/trace.decorator/111c.decorator-scope.controller.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ import {
44
Inject,
55
} from '@midwayjs/core'
66
import { MConfig } from '@mwcp/share'
7-
import { sleep } from '@waiting/shared-core'
87

98
import { apiBase, apiMethod } from '../types/api-test.js'
109
import { Trace, TraceService } from '../types/index.js'
1110
import { Config, ConfigKey } from '../types/lib-types.js'
1211

1312

14-
const scope1 = Symbol('scope1')
15-
1613
@Controller(apiBase.decorator_data)
1714
export class DecoratorScopeComponentController {
1815

@@ -30,19 +27,15 @@ export class DecoratorScopeComponentController {
3027

3128
// #region private methods
3229

33-
@Trace<DecoratorScopeComponentController['_simple1']>({
34-
scope: scope1,
35-
})
30+
@Trace<DecoratorScopeComponentController['_simple1']>()
3631
private async _simple1(): Promise<string> {
3732
await this._simple1a()
3833
return 'ok'
3934
}
4035

4136
// #region private methods sub
4237

43-
@Trace<DecoratorScopeComponentController['_simple1a']>({
44-
scope: () => scope1,
45-
})
38+
@Trace<DecoratorScopeComponentController['_simple1a']>()
4639
private async _simple1a(): Promise<string> {
4740
return 'ok'
4841
}

0 commit comments

Comments
 (0)