Skip to content

Commit f9fc3ec

Browse files
committed
two small fixes
- parsing issue with span name - _main_ was not handled correctly by span search leading to performance issues
1 parent fb13c06 commit f9fc3ec

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

src/services/documentInfoProvider.ts

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,55 @@ export class DocumentInfoProvider implements vscode.Disposable
4848
public async searchForSpan(instrumentationInfo: InstrumentationInfo): Promise<SpanInfo|undefined>{
4949
const codeFileHint = instrumentationInfo.instrumentationName;
5050
if (codeFileHint){
51-
let codeHintFiles = codeFileHint.split(' ');
52-
let head = codeHintFiles[0];
53-
let folder = await vscode.workspace.workspaceFolders?.find(w => w.name === head);
54-
let tail= codeHintFiles;
5551

56-
if (folder) {
57-
tail = codeHintFiles.slice(1);
58-
}
52+
if (codeFileHint==='__main__'){
5953

60-
if (codeHintFiles.length>=1){
61-
const files = await vscode.workspace.findFiles(`**/${tail.join('/')}.*`);
54+
let doc = vscode.window.activeTextEditor?.document
55+
if (doc){
56+
const docInfo = await this.getDocumentInfo(doc);
57+
if (docInfo){
58+
let spanInfos = docInfo.spans.filter(span => span.name===instrumentationInfo.spanName);
59+
return spanInfos.firstOrDefault(x=>x!== undefined);
6260

63-
const spansPromises = files.map(async file =>{
64-
try{
65-
const doc = await vscode.workspace.openTextDocument(file);
66-
const docInfo = await this.getDocumentInfo(doc);
67-
if(docInfo){
68-
return docInfo.spans.filter(span => span.name===instrumentationInfo.spanName);
69-
}
70-
}
71-
catch(error){
72-
Logger.warn(`Searching for span "${instrumentationInfo.spanName}" skipped ${file.fsPath}`, error);
7361
}
74-
return [];
75-
});
7662

77-
const spnaInfos = (await Promise.all(spansPromises)).flat();
78-
if (spnaInfos.length===1){
79-
return spnaInfos[0];
63+
}
64+
65+
}
66+
else{
67+
68+
let codeHintFiles = codeFileHint.split(' ');
69+
let head = codeHintFiles[0];
70+
let folder = await vscode.workspace.workspaceFolders?.find(w => w.name === head);
71+
let tail= codeHintFiles;
72+
73+
if (folder) {
74+
tail = codeHintFiles.slice(1);
75+
}
76+
77+
if (codeHintFiles.length>=1){
78+
const files = await vscode.workspace.findFiles(`**/${tail.join('/')}.*`);
79+
80+
const spansPromises = files.map(async file =>{
81+
try{
82+
const doc = await vscode.workspace.openTextDocument(file);
83+
const docInfo = await this.getDocumentInfo(doc);
84+
if(docInfo){
85+
return docInfo.spans.filter(span => span.name===instrumentationInfo.spanName);
86+
}
87+
}
88+
catch(error){
89+
Logger.warn(`Searching for span "${instrumentationInfo.spanName}" skipped ${file.fsPath}`, error);
90+
}
91+
return [];
92+
});
93+
94+
const spnaInfos = (await Promise.all(spansPromises)).flat().firstOrDefault(x=>x!== undefined);
95+
return spnaInfos;
8096
}
8197
}
98+
99+
82100
}
83101
}
84102

src/services/languages/python/spanExtractor.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ export class PythonSpanExtractor implements ISpanExtractor {
9090
return results;
9191
}
9292

93+
94+
9395
private async extractNameTypeTrace(filePath: string) : Promise<string> {
9496

9597
const pythonFileSuffix = ".py";
@@ -107,7 +109,9 @@ export class PythonSpanExtractor implements ISpanExtractor {
107109
if (relativePath.endsWith(pythonFileSuffix)){
108110
relativePath=relativePath.substring(0,relativePath.length-pythonFileSuffix.length);
109111
}
110-
relativePath=relativePath.replace("/",".").replace("\\",".");
112+
113+
relativePath=this.replaceAll(relativePath,"/",".");
114+
relativePath=this.replaceAll(relativePath,"\\",".");
111115

112116
return relativePath;
113117
}
@@ -116,6 +120,14 @@ export class PythonSpanExtractor implements ISpanExtractor {
116120

117121
}
118122

123+
private escapeRegExp(s: string) {
124+
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
125+
}
126+
private replaceAll(str:string, find:string, replace:string) {
127+
return str.replace(new RegExp(this.escapeRegExp(find), 'g'), replace);
128+
}
129+
130+
119131
private isCallToStartSpan(token: Token) {
120132
return token.type === TokenType.method && token.text === 'start_as_current_span';
121133
}

0 commit comments

Comments
 (0)