Skip to content

Commit 61f48cd

Browse files
authored
Expand details in database application name. (#293)
1 parent 71cf892 commit 61f48cd

File tree

26 files changed

+114
-42
lines changed

26 files changed

+114
-42
lines changed

.changeset/slow-hounds-walk.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
---
22
'@powersync/service-module-mongodb-storage': patch
3+
'@powersync/service-module-postgres-storage': patch
34
'@powersync/service-module-mongodb': patch
5+
'@powersync/service-module-postgres': patch
46
'@powersync/service-module-mysql': patch
57
'@powersync/lib-service-mongodb': patch
6-
'@powersync/service-core': patch
7-
'@powersync/service-image': patch
8+
'@powersync/service-core': minor
9+
'@powersync/service-image': minor
810
---
911

10-
Add 'powersync' as the app name for MongoDB and MySQL connections.
12+
Add 'powersync' or 'powersync-storage' as the app name for database connections.

libs/lib-mongodb/src/db/mongo.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export const MONGO_OPERATION_TIMEOUT_MS = 30_000;
2929
export const MONGO_CLEAR_OPERATION_TIMEOUT_MS = 5_000;
3030

3131
export interface MongoConnectionOptions {
32-
maxPoolSize: number;
32+
maxPoolSize?: number;
33+
powersyncVersion?: string;
3334
}
3435

3536
/**
@@ -50,7 +51,12 @@ export function createMongoClient(config: BaseMongoConfigDecoded, options?: Mong
5051
serverSelectionTimeoutMS: 30_000,
5152

5253
// Identify the client
53-
appName: 'powersync-storage',
54+
appName: options?.powersyncVersion ? `powersync-storage ${options.powersyncVersion}` : 'powersync-storage',
55+
driverInfo: {
56+
// This is merged with the node driver info.
57+
name: 'powersync-storage',
58+
version: options?.powersyncVersion
59+
},
5460

5561
lookup: normalized.lookup,
5662

libs/lib-postgres/src/db/connection/ConnectionSlot.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type ConnectionLease = {
1919
export type ConnectionSlotOptions = {
2020
config: pgwire.NormalizedConnectionConfig;
2121
notificationChannels?: string[];
22+
applicationName: string;
2223
};
2324

2425
export const MAX_CONNECTION_ATTEMPTS = 5;
@@ -46,7 +47,10 @@ export class ConnectionSlot extends framework.BaseObserver<ConnectionSlotListene
4647
}
4748

4849
protected async connect() {
49-
this.connectingPromise = pgwire.connectPgWire(this.options.config, { type: 'standard' });
50+
this.connectingPromise = pgwire.connectPgWire(this.options.config, {
51+
type: 'standard',
52+
applicationName: this.options.applicationName
53+
});
5054
const connection = await this.connectingPromise;
5155
this.connectingPromise = null;
5256
await this.iterateAsyncListeners(async (l) => l.connectionCreated?.(connection));

libs/lib-postgres/src/db/connection/DatabaseClient.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export type DatabaseClientOptions = {
1515
* Notification channels to listen to.
1616
*/
1717
notificationChannels?: string[];
18+
19+
applicationName: string;
1820
};
1921

2022
export type DatabaseClientListener = NotificationListener & {
@@ -43,12 +45,17 @@ export class DatabaseClient extends AbstractPostgresConnection<DatabaseClientLis
4345
super();
4446
this.closed = false;
4547
this.pool = pgwire.connectPgWirePool(options.config, {
46-
maxSize: options.config.max_pool_size
48+
maxSize: options.config.max_pool_size,
49+
applicationName: options.applicationName
4750
});
4851
this.connections = Array.from({ length: TRANSACTION_CONNECTION_COUNT }, (v, index) => {
4952
// Only listen to notifications on a single (the first) connection
5053
const notificationChannels = index == 0 ? options.notificationChannels : [];
51-
const slot = new ConnectionSlot({ config: options.config, notificationChannels });
54+
const slot = new ConnectionSlot({
55+
config: options.config,
56+
notificationChannels,
57+
applicationName: options.applicationName
58+
});
5259
slot.registerListener({
5360
connectionAvailable: () => this.processConnectionQueue(),
5461
connectionError: (ex) => this.handleConnectionError(ex),

modules/module-mongodb-storage/src/storage/implementation/MongoStorageProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as lib_mongo from '@powersync/lib-service-mongodb';
22
import { ErrorCode, logger, ServiceAssertionError, ServiceError } from '@powersync/lib-services-framework';
3-
import { storage } from '@powersync/service-core';
3+
import { POWERSYNC_VERSION, storage } from '@powersync/service-core';
44
import { MongoStorageConfig } from '../../types/types.js';
55
import { MongoBucketStorage } from '../MongoBucketStorage.js';
66
import { PowerSyncMongo } from './db.js';
@@ -23,6 +23,7 @@ export class MongoStorageProvider implements storage.BucketStorageProvider {
2323

2424
const decodedConfig = MongoStorageConfig.decode(storage as any);
2525
const client = lib_mongo.db.createMongoClient(decodedConfig, {
26+
powersyncVersion: POWERSYNC_VERSION,
2627
maxPoolSize: resolvedConfig.storage.max_pool_size ?? 8
2728
});
2829

modules/module-mongodb-storage/src/storage/implementation/db.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as lib_mongo from '@powersync/lib-service-mongodb';
22
import { mongo } from '@powersync/lib-service-mongodb';
3-
import { storage } from '@powersync/service-core';
3+
import { POWERSYNC_VERSION, storage } from '@powersync/service-core';
44

55
import { MongoStorageConfig } from '../../types/types.js';
66
import {
@@ -130,5 +130,11 @@ export class PowerSyncMongo {
130130
}
131131

132132
export function createPowerSyncMongo(config: MongoStorageConfig, options?: lib_mongo.MongoConnectionOptions) {
133-
return new PowerSyncMongo(lib_mongo.createMongoClient(config, options), { database: config.database });
133+
return new PowerSyncMongo(
134+
lib_mongo.createMongoClient(config, {
135+
powersyncVersion: POWERSYNC_VERSION,
136+
...options
137+
}),
138+
{ database: config.database }
139+
);
134140
}

modules/module-mongodb-storage/src/storage/implementation/util.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,13 @@ export function replicaIdToSubkey(table: bson.ObjectId, id: storage.ReplicaId):
104104
}
105105
}
106106

107-
/**
108-
* Helper function for creating a MongoDB client from consumers of this package
109-
*/
110-
const createMongoClient = (url: string, options?: mongo.MongoClientOptions) => {
111-
return new mongo.MongoClient(url, options);
112-
};
113-
114107
/**
115108
* Helper for unit tests
116109
*/
117110
export const connectMongoForTests = (url: string, isCI: boolean) => {
118111
// Short timeout for tests, to fail fast when the server is not available.
119112
// Slightly longer timeouts for CI, to avoid arbitrary test failures
120-
const client = createMongoClient(url, {
113+
const client = new mongo.MongoClient(url, {
121114
connectTimeoutMS: isCI ? 15_000 : 5_000,
122115
socketTimeoutMS: isCI ? 15_000 : 5_000,
123116
serverSelectionTimeoutMS: isCI ? 15_000 : 2_500

modules/module-mongodb/src/replication/MongoManager.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { mongo } from '@powersync/lib-service-mongodb';
22

33
import { NormalizedMongoConnectionConfig } from '../types/types.js';
4-
import { BSON_DESERIALIZE_DATA_OPTIONS } from '@powersync/service-core';
4+
import { BSON_DESERIALIZE_DATA_OPTIONS, POWERSYNC_VERSION } from '@powersync/service-core';
55

66
/**
77
* Manage a MongoDB source database connection.
@@ -30,7 +30,12 @@ export class MongoManager {
3030
serverSelectionTimeoutMS: 30_000,
3131

3232
// Identify the client
33-
appName: 'powersync',
33+
appName: `powersync ${POWERSYNC_VERSION}`,
34+
driverInfo: {
35+
// This is merged with the node driver info.
36+
name: 'powersync',
37+
version: POWERSYNC_VERSION
38+
},
3439

3540
// Avoid too many connections:
3641
// 1. It can overwhelm the source database.

modules/module-mysql/src/replication/BinLogReplicationJob.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { container, logger as defaultLogger } from '@powersync/lib-services-framework';
2-
import { replication } from '@powersync/service-core';
2+
import { POWERSYNC_VERSION, replication } from '@powersync/service-core';
33
import { BinlogConfigurationError, BinLogStream } from './BinLogStream.js';
44
import { MySQLConnectionManagerFactory } from './MySQLConnectionManagerFactory.js';
55

@@ -61,7 +61,8 @@ export class BinLogReplicationJob extends replication.AbstractReplicationJob {
6161
// https://dev.mysql.com/doc/refman/8.0/en/performance-schema-connection-attribute-tables.html
6262
// These do not appear to be supported by Zongji yet, so we only specify it here.
6363
// Query using `select * from performance_schema.session_connect_attrs`.
64-
program_name: 'powersync'
64+
program_name: 'powersync',
65+
program_version: POWERSYNC_VERSION
6566

6667
// _client_name and _client_version is specified by the driver
6768
}

modules/module-postgres-storage/src/migrations/PostgresMigrationAgent.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { fileURLToPath } from 'url';
66

77
import { normalizePostgresStorageConfig, PostgresStorageConfigDecoded } from '../types/types.js';
88

9+
import { getStorageApplicationName } from '../utils/application-name.js';
910
import { STORAGE_SCHEMA_NAME } from '../utils/db.js';
1011
import { PostgresMigrationStore } from './PostgresMigrationStore.js';
1112

@@ -25,7 +26,8 @@ export class PostgresMigrationAgent extends migrations.AbstractPowerSyncMigratio
2526

2627
this.db = new lib_postgres.DatabaseClient({
2728
config: normalizePostgresStorageConfig(config),
28-
schema: STORAGE_SCHEMA_NAME
29+
schema: STORAGE_SCHEMA_NAME,
30+
applicationName: getStorageApplicationName()
2931
});
3032
this.store = new PostgresMigrationStore({
3133
db: this.db

0 commit comments

Comments
 (0)