| 13 | const logger = loggerService.withContext('SpanCacheService') |
| 14 | |
| 15 | class SpanCacheService implements TraceCache { |
| 16 | private topicMap: Map<string, string> = new Map<string, string>() |
| 17 | private fileDir: string |
| 18 | private cache: Map<string, SpanEntity> = new Map<string, SpanEntity>() |
| 19 | pri |
| 20 | |
| 21 | constructor() { |
| 22 | this.fileDir = path.join(os.homedir(), HOME_CHERRY_DIR, 'trace') |
| 23 | } |
| 24 | |
| 25 | createSpan: (span: ReadableSpan) => void = (span: ReadableSpan) => { |
| 26 | if (!configManager.getEnableDeveloperMode()) { |
| 27 | return |
| 28 | } |
| 29 | const spanEntity = convertSpanToSpanEntity(span) |
| 30 | spanEntity.topicId = this.topicMap.get(spanEntity.traceId) |
| 31 | this.cache.set(span.spanContext().spanId, spanEntity) |
| 32 | this._updateModelName(spanEntity) |
| 33 | } |
| 34 | |
| 35 | endSpan: (span: ReadableSpan) => void = (span: ReadableSpan) => { |
| 36 | if (!configManager.getEnableDeveloperMode()) { |
| 37 | return |
| 38 | } |
| 39 | const spanId = span.spanContext().spanId |
| 40 | const spanEntity = this.cache.get(spanId) |
| 41 | if (!spanEntity) { |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | spanEntity.topicId = this.topicMap.get(spanEntity.traceId) |
| 46 | spanEntity.endTime = span.endTime ? span.endTime[0] * 1e3 + Math.floor(span.endTime[1] / 1e6) : null |
| 47 | spanEntity.status = SpanStatusCode[span.status.code] |
| 48 | spanEntity.attributes = span.attributes ? ({ ...span.attributes } as Attributes) : {} |
| 49 | spanEntity.events = span.events |
| 50 | spanEntity.links = span.links |
| 51 | this._updateModelName(spanEntity) |
| 52 | } |
| 53 | |
| 54 | clear: () => void = () => { |
| 55 | this.cache.clear() |
| 56 | } |
| 57 | |
| 58 | async cleanTopic(topicId: string, traceId?: string, modelName?: string) { |
| 59 | const spans = Array.from(this.cache.values().filter((e) => e.topicId === topicId)) |
| 60 | spans.map((e) => e.id).forEach((id) => this.cache.delete(id)) |
| 61 | |
| 62 | await this._checkFolder(path.join(this.fileDir, topicId)) |
| 63 | |
| 64 | if (modelName) { |
| 65 | await this.cleanHistoryTrace(topicId, traceId || '', modelName) |
| 66 | await this.saveSpans(topicId) |
| 67 | } else if (traceId) { |
| 68 | await fs.rm(path.join(this.fileDir, topicId, traceId)) |
| 69 | } else { |
| 70 | const files = await fs.readdir(path.join(this.fileDir, topicId)) |
| 71 | for (const file of files) { |
| 72 | await fs.rm(path.join(this.fileDir, topicId, file)) |
nothing calls this directly
no test coverage detected