|
1 |
| -import assert from 'node:assert' |
2 |
| - |
3 | 1 | import type { Span } from '@opentelemetry/api'
|
4 |
| -import { isArrowFunction } from '@waiting/shared-core' |
5 | 2 |
|
6 | 3 | import type {
|
7 |
| - DecoratorContextBase, |
8 |
| - DecoratorExecutorParam, |
9 | 4 | DecoratorTraceDataResp,
|
10 |
| - ScopeGenerator, |
11 |
| - TraceDecoratorOptions, |
12 | 5 | TraceService,
|
13 | 6 | } 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' |
15 | 8 | import { getSpan } from './util.js'
|
16 | 9 |
|
17 | 10 |
|
@@ -55,143 +48,6 @@ function processEvents(
|
55 | 48 | traceService.addEvent(span, events)
|
56 | 49 | }
|
57 | 50 |
|
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 |
| - |
195 | 51 | export function isTraceScopeParamType(input: TraceScopeParamType | undefined): input is TraceScopeParamType {
|
196 | 52 | if (! input) {
|
197 | 53 | return false
|
|
0 commit comments