Skip to content

Commit 5ad7b76

Browse files
authored
Add logs for database (#343)
1 parent 1027479 commit 5ad7b76

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

src/datastore/FileStore.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Logger } from 'pino';
44
import { LoggerFactory } from '../telemetry/LoggerFactory';
55
import { ScopedTelemetry } from '../telemetry/ScopedTelemetry';
66
import { Telemetry } from '../telemetry/TelemetryDecorator';
7+
import { formatNumber } from '../utils/String';
78
import { DataStore, DataStoreFactory, StoreName } from './DataStore';
89
import { EncryptedFileStore } from './file/EncryptedFileStore';
910
import { encryptionKey } from './file/Encryption';
@@ -40,7 +41,7 @@ export class FileStoreFactory implements DataStoreFactory {
4041
2 * 60 * 1000,
4142
);
4243

43-
this.log.info(`Initialized FileDB ${Version}`);
44+
this.log.info(`Initialized FileDB ${Version} and ${formatNumber(this.totalBytes() / (1024 * 1024), 4)} MB`);
4445
}
4546

4647
get(store: StoreName): DataStore {
@@ -66,18 +67,16 @@ export class FileStoreFactory implements DataStoreFactory {
6667
this.telemetry.histogram('version', VersionNumber);
6768
this.telemetry.histogram('env.entries', this.stores.size);
6869

69-
let totalBytes = 0;
7070
for (const [name, store] of this.stores.entries()) {
7171
const stats = store.stats();
7272

73-
totalBytes += stats.totalSize;
7473
this.telemetry.histogram(`store.${name}.entries`, stats.entries);
7574
this.telemetry.histogram(`store.${name}.size.bytes`, stats.totalSize, {
7675
unit: 'By',
7776
});
7877
}
7978

80-
this.telemetry.histogram('total.size.bytes', totalBytes, {
79+
this.telemetry.histogram('total.size.bytes', this.totalBytes(), {
8180
unit: 'By',
8281
});
8382
}
@@ -96,6 +95,16 @@ export class FileStoreFactory implements DataStoreFactory {
9695
}
9796
}
9897
}
98+
99+
private totalBytes() {
100+
let totalBytes = 0;
101+
102+
for (const store of this.stores.values()) {
103+
totalBytes += store.stats().totalSize;
104+
}
105+
106+
return totalBytes;
107+
}
99108
}
100109

101110
const VersionNumber = 2;

src/datastore/LMDB.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { LoggerFactory } from '../telemetry/LoggerFactory';
55
import { ScopedTelemetry } from '../telemetry/ScopedTelemetry';
66
import { Telemetry } from '../telemetry/TelemetryDecorator';
77
import { isWindows } from '../utils/Environment';
8-
import { toString } from '../utils/String';
8+
import { formatNumber, toString } from '../utils/String';
99
import { DataStore, DataStoreFactory, StoreName } from './DataStore';
1010
import { LMDBStore } from './lmdb/LMDBStore';
1111
import { stats } from './lmdb/Stats';
@@ -69,7 +69,7 @@ export class LMDBStoreFactory implements DataStoreFactory {
6969
noSubdir: config.noSubdir,
7070
overlappingSync: config.overlappingSync,
7171
},
72-
`Initialized LMDB ${Version} with stores: ${toString(storeNames)}`,
72+
`Initialized LMDB ${Version} with stores: ${toString(storeNames)} and ${formatNumber(this.totalBytes() / (1024 * 1024), 4)} MB`,
7373
);
7474
}
7575

@@ -110,19 +110,18 @@ export class LMDBStoreFactory implements DataStoreFactory {
110110
}
111111

112112
private emitMetrics(): void {
113-
let totalBytes = 0;
113+
const totalBytes = this.totalBytes();
114+
114115
const envStat = stats(this.env);
115116
this.telemetry.histogram('version', VersionNumber);
116117
this.telemetry.histogram('env.size.bytes', envStat.totalSize, { unit: 'By' });
117118
this.telemetry.histogram('env.max.size.bytes', envStat.maxSize, {
118119
unit: 'By',
119120
});
120121
this.telemetry.histogram('env.entries', envStat.entries);
121-
totalBytes += envStat.totalSize;
122122

123123
for (const [name, store] of this.stores.entries()) {
124124
const stat = store.stats();
125-
totalBytes += stat.totalSize;
126125

127126
this.telemetry.histogram(`store.${name}.size.bytes`, stat.totalSize, {
128127
unit: 'By',
@@ -137,6 +136,17 @@ export class LMDBStoreFactory implements DataStoreFactory {
137136
unit: 'By',
138137
});
139138
}
139+
140+
private totalBytes() {
141+
let totalBytes = 0;
142+
totalBytes += stats(this.env).totalSize;
143+
144+
for (const store of this.stores.values()) {
145+
totalBytes += store.stats().totalSize;
146+
}
147+
148+
return totalBytes;
149+
}
140150
}
141151

142152
const VersionNumber = 5;

src/schema/GetSamSchemaTask.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class GetSamSchemaTask extends GetSchemaTask {
3434

3535
await dataStore.put(SamStoreKey, samSchemasData);
3636

37-
this.logger.info(`${resourceSchemas.size} SAM schemas downloaded and stored`);
37+
this.logger.info(`${resourceSchemas.size} SAM schemas downloaded and saved`);
3838
} catch (error) {
3939
this.logger.error(error, 'Failed to download SAM schemas');
4040
throw error;

src/schema/GetSchemaTask.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class GetPublicSchemaTask extends GetSchemaTask {
6666
};
6767

6868
await dataStore.put<RegionalSchemasType>(this.region, value);
69-
this.logger.info(`${schemas.length} public schemas retrieved for ${this.region}`);
69+
this.logger.info(`${schemas.length} public schemas downloaded for ${this.region} and saved`);
7070
}
7171
}
7272

src/telemetry/ScopedTelemetry.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,15 @@ import {
99
Tracer,
1010
UpDownCounter,
1111
ValueType,
12-
MetricAdvice,
1312
} from '@opentelemetry/api';
1413
import { Closeable } from '../utils/Closeable';
1514
import { typeOf } from '../utils/TypeCheck';
1615
import { TelemetryContext } from './TelemetryContext';
1716

18-
export type MetricConfig = {
19-
description?: string;
20-
unit?: string;
21-
valueType?: ValueType;
22-
advice?: MetricAdvice;
17+
export interface MetricConfig extends MetricOptions {
2318
trackObjectKey?: string;
2419
attributes?: Attributes;
25-
};
20+
}
2621

2722
export class ScopedTelemetry implements Closeable {
2823
private readonly counters = new Map<string, Counter>();

src/utils/String.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ export function byteSize(str: string) {
3434
const bytes: Uint8Array = new TextEncoder().encode(str);
3535
return bytes.length;
3636
}
37+
38+
export function formatNumber(value: number, decimals: number = 2) {
39+
return value.toFixed(decimals);
40+
}

0 commit comments

Comments
 (0)