Skip to content

Commit 20f0bed

Browse files
authored
Add document size metrics (#231)
1 parent 3e82359 commit 20f0bed

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/document/DocumentManager.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import { DefaultSettings, EditorSettings } from '../settings/Settings';
55
import { LoggerFactory } from '../telemetry/LoggerFactory';
66
import { ScopedTelemetry } from '../telemetry/ScopedTelemetry';
77
import { Telemetry } from '../telemetry/TelemetryDecorator';
8+
import { Closeable } from '../utils/Closeable';
89
import { Delayer } from '../utils/Delayer';
10+
import { byteSize } from '../utils/String';
911
import { CloudFormationFileType, Document, DocumentType } from './Document';
1012
import { DocumentMetadata } from './DocumentProtocol';
1113

12-
export class DocumentManager implements SettingsConfigurable {
14+
export class DocumentManager implements SettingsConfigurable, Closeable {
1315
private readonly log = LoggerFactory.getLogger(DocumentManager);
1416

1517
@Telemetry() private readonly telemetry!: ScopedTelemetry;
@@ -19,6 +21,7 @@ export class DocumentManager implements SettingsConfigurable {
1921
private readonly documentMap = new Map<string, Document>();
2022

2123
private settingsSubscription?: SettingsSubscription;
24+
private readonly interval: NodeJS.Timeout;
2225

2326
constructor(
2427
private readonly documents: TextDocuments<TextDocument>,
@@ -27,6 +30,9 @@ export class DocumentManager implements SettingsConfigurable {
2730
},
2831
) {
2932
this.registerDocumentGauges();
33+
this.interval = setInterval(() => {
34+
this.emitDocSizeMetrics();
35+
}, 30 * 1000);
3036
}
3137

3238
configure(settingsManager: ISettingsSubscriber): void {
@@ -157,6 +163,12 @@ export class DocumentManager implements SettingsConfigurable {
157163
}
158164
}
159165

166+
private emitDocSizeMetrics() {
167+
for (const doc of this.documentMap.values()) {
168+
this.telemetry.histogram(`documents.size.bytes`, byteSize(doc.contents()), { unit: 'By' });
169+
}
170+
}
171+
160172
private countDocumentsByCfnType(cfnType: CloudFormationFileType): number {
161173
return [...this.documentMap.values()].filter((doc) => doc.cfnFileType === cfnType).length;
162174
}
@@ -168,4 +180,8 @@ export class DocumentManager implements SettingsConfigurable {
168180
private countDocumentsByExtension(extension: string): number {
169181
return [...this.documentMap.values()].filter((doc) => doc.isTemplate() && doc.extension === extension).length;
170182
}
183+
184+
close() {
185+
clearInterval(this.interval);
186+
}
171187
}

src/server/CfnInfraCore.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@ export class CfnInfraCore implements Configurables, Closeable {
7272
}
7373

7474
async close() {
75-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
76-
// @ts-ignore
77-
return await closeSafely(this.dataStoreFactory, TelemetryService.instance, LoggerFactory._instance);
75+
return await closeSafely(
76+
this.documentManager,
77+
this.dataStoreFactory,
78+
TelemetryService.instance,
79+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
80+
// @ts-ignore
81+
LoggerFactory._instance,
82+
);
7883
}
7984
}

src/telemetry/OTELInstrumentation.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ export function otelSdk(clientId: string, client?: ClientInfo) {
5959
},
6060
},
6161
} satisfies ViewOptions,
62+
{
63+
instrumentName: '*.bytes',
64+
aggregation: {
65+
type: AggregationType.EXPONENTIAL_HISTOGRAM,
66+
options: {
67+
recordMinMax: true,
68+
},
69+
},
70+
} satisfies ViewOptions,
6271
],
6372
instrumentations: [
6473
getNodeAutoInstrumentations({

0 commit comments

Comments
 (0)