Skip to content

Commit e65fcab

Browse files
authored
Custom metadata in sync logs (#309)
* Metadata draft * Fixes to the examples * Placeholders for version compatiblity * Add service version * import statement note * Apply suggestions from code review * JS SDK versions * Add kotlin version * Add dart version * placeholder for .net
1 parent c0277ca commit e65fcab

File tree

7 files changed

+142
-3
lines changed

7 files changed

+142
-3
lines changed
-111 KB
Binary file not shown.
-224 KB
Binary file not shown.
-510 KB
Loading
-115 KB
Binary file not shown.
-115 KB
Binary file not shown.
881 KB
Loading

usage/tools/monitoring-and-alerting.mdx

Lines changed: 142 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ View time-series and aggregated usage data for your PowerSync instance(s), inclu
5151
Access usage metrics in the [PowerSync Dashboard](https://dashboard.powersync.com/). Select your project and instance and go to the **Metrics** view:
5252

5353
<Frame>
54-
<img src="/images/usage-metrics.png" />
54+
<img src="/images/resources/view-usage-metrics.png" />
5555
</Frame>
5656

5757
You have following options:
@@ -70,7 +70,7 @@ You have following options:
7070

7171
You can review logs for your PowerSync instance(s) to troubleshoot replication or sync service issues. Logs capture activity from the PowerSync Service and Replicator processes.
7272

73-
* **Service logs**: Reflect sync processes from the PowerSync Service to clients.
73+
* **Service/API logs**: Reflect sync processes from the PowerSync Service to clients.
7474

7575
* **Replicator logs**: Reflect replication activity from your source database to the PowerSync Service.
7676

@@ -100,12 +100,151 @@ You can manage logs with the following options:
100100

101101
* **Sorting**: Sort logs by newest or oldest first.
102102

103-
* **Service Logs Metadata**: Include metadata like `user_id` and `user_agent` in the logs if available.
103+
* **Metadata**: Display metadata like `user_id` and `user_agent` in the logs if available.
104104

105105
* **View Mode**: Tail logs in real-time or view them statically.
106106

107107
* **Stack Traces**: Option to show or hide stack traces for errors.
108108

109+
## Custom Metadata in Sync Logs
110+
111+
Custom metadata in sync logs allows clients to attach additional context to their PowerSync connection for improved observability and analytics. This metadata appears in the Service/API logs, making it easier to track, debug, and analyze sync behavior across your app. For example, you can tag connections with app version, feature flags, or business context.
112+
113+
### How to Use Custom Metadata
114+
115+
You can specify application metadata when calling `PowerSyncDatabase.connect()`. To update the metadata, reconnect with new metadata values.
116+
117+
<Tabs>
118+
<Tab title="JavaScript/React Native/Node.js/Capacitor">
119+
<Note>
120+
**Version compatibility**: This feature requires JavaScript/Web SDK v1.30.0+, React Native SDK v1.28.0+, Node.js SDK v0.15.0+, or Capacitor SDK v0.2.0+, and PowerSync Service v1.17.0+.
121+
</Note>
122+
123+
```javascript
124+
import { PowerSyncDatabase } from '@powersync/web'; // Update this to the appropriate SDK package
125+
126+
const powerSync = new PowerSyncDatabase({
127+
schema: AppSchema,
128+
database: {
129+
dbFilename: 'powersync.db'
130+
}
131+
});
132+
133+
// Set custom metadata when connecting
134+
powerSync.connect(connector, {
135+
appMetadata: {
136+
app_version: '1.2.3',
137+
feature_flag: 'new_sync_flow'
138+
}
139+
});
140+
```
141+
</Tab>
142+
143+
<Tab title="Dart/Flutter">
144+
<Note>
145+
**Version compatibility**: This feature requires Dart/Flutter SDK v1.17.0+ and PowerSync Service v1.17.0+.
146+
</Note>
147+
148+
```dart
149+
import 'package:powersync/powersync.dart';
150+
151+
final powerSync = PowerSyncDatabase(
152+
schema: AppSchema,
153+
path: 'powersync.db'
154+
);
155+
await powerSync.initialize();
156+
157+
// Set custom metadata when connecting
158+
const options = SyncOptions(
159+
appMetadata: {
160+
'app_version': '1.2.3',
161+
'feature_flag': 'new_sync_flow'
162+
}
163+
);
164+
powerSync.connect(
165+
connector: MyConnector(),
166+
options: options
167+
);
168+
```
169+
</Tab>
170+
171+
<Tab title="Kotlin">
172+
<Note>
173+
**Version compatibility**: This feature requires Kotlin SDK v1.10.0+ and PowerSync Service v1.17.0+.
174+
</Note>
175+
176+
```kotlin
177+
import com.powersync.DatabaseDriverFactory
178+
import com.powersync.PowerSyncDatabase
179+
180+
// Android
181+
val driverFactory = DatabaseDriverFactory(this)
182+
// iOS & Desktop
183+
// val driverFactory = DatabaseDriverFactory()
184+
185+
val powerSync = PowerSyncDatabase({
186+
factory: driverFactory,
187+
schema: AppSchema,
188+
dbFilename: "powersync.db"
189+
})
190+
191+
// Set custom metadata when connecting
192+
powerSync.connect(
193+
connector = MyConnector(),
194+
appMetadata = mapOf(
195+
"app_version" to "1.2.3",
196+
"feature_flag" to "new_sync_flow"
197+
)
198+
)
199+
```
200+
</Tab>
201+
202+
<Tab title="Swift">
203+
Support for custom metadata is not yet available in Swift, but is planned for a future release.
204+
</Tab>
205+
206+
<Tab title=".NET">
207+
Support for custom metadata is not yet available in .NET, but is planned for a future release.
208+
209+
{/* ```csharp
210+
using PowerSync.Common.Client;
211+
212+
var powerSync = new PowerSyncDatabase(new PowerSyncDatabaseOptions
213+
{
214+
Database = new SQLOpenOptions { DbFilename = "powersync.db" },
215+
Schema = AppSchema.PowerSyncSchema,
216+
});
217+
await powerSync.Init();
218+
219+
// Set custom metadata when connecting
220+
await powerSync.Connect(
221+
connector: new MyConnector(),
222+
appMetadata: new Dictionary<string, string>
223+
{
224+
{ "app_version", "1.2.3" },
225+
{ "feature_flag", "new_sync_flow" }
226+
}
227+
);
228+
``` */}
229+
</Tab>
230+
</Tabs>
231+
232+
### View Custom Metadata in Logs
233+
234+
Custom metadata appears in the **Service/API logs** section of the [PowerSync Dashboard](https://dashboard.powersync.com/). Navigate to your project and instance, then go to the **Logs** view. The metadata is included in **Sync Stream Started** and **Sync Stream Completed** log entries.
235+
236+
<Note>
237+
Make sure the **Metadata** checkbox is enabled in the logs view to see custom metadata in log entries.
238+
</Note>
239+
240+
Note the following when using custom metadata:
241+
242+
* Keep metadata values concise. `app_metadata` is limited to 20 keys, with each string value capped at 100 characters.
243+
244+
* Avoid including sensitive information in metadata as it will appear in logs.
245+
246+
* Metadata is set per connection. Reconnect with new metadata when user context or app state changes (e.g., feature flags).
247+
109248
## Issue Alerts
110249

111250
Issue alerts capture potential problems with your instance, such as connection or replication issues.

0 commit comments

Comments
 (0)