Skip to content

Commit 67bf36e

Browse files
konraddysputjasoncdavis0
andauthored
Feature/database improvements (#36)
* Database improvements * Invalid meta file info * Updated for 3.0.2 * Version update * Store temporary json in memory Co-authored-by: jasoncdavis0 <[email protected]>
1 parent d27c96a commit 67bf36e

19 files changed

+347
-155
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Backtrace Unity Release Notes
22

3+
## Version 3.0.2
4+
- `BacktraceDatabase` now provides a new `Send` method. This method will try to send all objects from the database respecting the client side deduplication and retry setting. This can be used as an alternative to the `Flush` method which will try to send all objects from the database ignoring any client side deduplication and retry settings.
5+
- `BacktraceClient` has been optimized to only serialize data as needed.
6+
- `BacktraceDatabase` `AutoSend` function has been optimized for performance improvements.
7+
- `BacktraceClient` by default will generate configuration file with client rate limit equal to 50.
8+
- Fixed invalid meta file.
9+
310
## Version 3.0.1
411
- The `BacktraceDatabase` class will now create database directory before final database validation. Previously, when directory didn't exist, BacktraceDatabase was disabled.
512
- The `BacktraceDatabase` field now allows users to pass interpolated string in Database options. Developer can use `${Application.dataPath}` or `${Application.persistentDataPath}` to set path to database.

Editor/BacktraceConfigurationLabels.cs.meta

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/BacktraceClient.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ private void SendReport(BacktraceReport report, Action<BacktraceResult> sendCall
337337
private IEnumerator CollectDataAndSend(BacktraceReport report, Action<BacktraceResult> sendCallback = null)
338338
{
339339
BacktraceData data = SetupBacktraceData(report);
340+
yield return new WaitForEndOfFrame();
340341
if (BeforeSend != null)
341342
{
342343
data = BeforeSend.Invoke(data);
@@ -346,6 +347,11 @@ private IEnumerator CollectDataAndSend(BacktraceReport report, Action<BacktraceR
346347
}
347348
}
348349
BacktraceDatabaseRecord record = null;
350+
351+
// avoid serializing data twice
352+
// if record is here we should try to send json data that are available in record
353+
// otherwise we can still use BacktraceData.ToJson().
354+
string json = string.Empty;
349355
if (Database != null)
350356
{
351357
yield return new WaitForEndOfFrame();
@@ -359,11 +365,21 @@ record = Database.Add(data);
359365
{
360366
yield break;
361367
}
368+
json = record.BacktraceDataJson();
362369
}
363-
370+
}
371+
if (string.IsNullOrEmpty(json))
372+
{
373+
json = data.ToJson();
374+
}
375+
//backward compatibility
376+
if (RequestHandler != null)
377+
{
378+
yield return RequestHandler.Invoke(BacktraceApi.ServerUrl, data);
379+
yield break;
364380
}
365381

366-
StartCoroutine(BacktraceApi.Send(data, (BacktraceResult result) =>
382+
StartCoroutine(BacktraceApi.Send(json, data.Attachments, data.Deduplication, (BacktraceResult result) =>
367383
{
368384
if (record != null)
369385
{

Runtime/BacktraceDatabase.cs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public DeduplicationStrategy DeduplicationStrategy
106106
/// </summary>
107107
public void Reload()
108108
{
109-
109+
110110
// validate configuration
111111
if (Configuration == null)
112112
{
@@ -169,6 +169,11 @@ private void Update()
169169
return;
170170
}
171171
LastFrameTime = Time.time;
172+
if (!DatabaseSettings.AutoSendMode)
173+
{
174+
return;
175+
}
176+
172177
if (Time.time - _lastConnection > DatabaseSettings.RetryInterval)
173178
{
174179
_lastConnection = Time.time;
@@ -189,15 +194,15 @@ private void Start()
189194
{
190195
return;
191196
}
192-
if (DatabaseSettings.AutoSendMode)
193-
{
194-
_lastConnection = Time.time;
195-
}
196197
// load reports from hard drive
197198
LoadReports();
198199
// remove orphaned files
199200
RemoveOrphaned();
200-
SendData(BacktraceDatabaseContext.FirstOrDefault());
201+
if (DatabaseSettings.AutoSendMode)
202+
{
203+
_lastConnection = Time.time;
204+
SendData(BacktraceDatabaseContext.FirstOrDefault());
205+
}
201206
}
202207

203208
/// <summary>
@@ -238,7 +243,7 @@ public void Clear()
238243
/// <summary>
239244
/// Add new report to BacktraceDatabase
240245
/// </summary>
241-
public BacktraceDatabaseRecord Add(BacktraceData data)
246+
public BacktraceDatabaseRecord Add(BacktraceData data, bool @lock = true)
242247
{
243248
if (data == null || !Enable)
244249
{
@@ -251,7 +256,12 @@ public BacktraceDatabaseRecord Add(BacktraceData data)
251256
{
252257
return null;
253258
}
254-
return BacktraceDatabaseContext.Add(data);
259+
var record = BacktraceDatabaseContext.Add(data);
260+
if(!@lock)
261+
{
262+
record.Dispose();
263+
}
264+
return record;
255265
}
256266

257267
/// <summary>
@@ -310,6 +320,19 @@ public void Flush()
310320
FlushRecord(BacktraceDatabaseContext.FirstOrDefault());
311321
}
312322

323+
/// <summary>
324+
/// Try to send all data from database
325+
/// </summary>
326+
public void Send()
327+
{
328+
if(!Enable || !BacktraceDatabaseContext.Any())
329+
{
330+
return;
331+
}
332+
333+
SendData(BacktraceDatabaseContext.FirstOrDefault());
334+
}
335+
313336
private void FlushRecord(BacktraceDatabaseRecord record)
314337
{
315338
if (record == null)
@@ -408,7 +431,7 @@ protected virtual bool InitializeDatabasePaths()
408431
{
409432
return false;
410433
}
411-
434+
412435
var databaseDirExists = Directory.Exists(DatabasePath);
413436

414437
// handle situation when Backtrace plugin should create database directory

Runtime/Interfaces/IBacktraceAPI.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ namespace Backtrace.Unity.Interfaces
1010
/// </summary>
1111
public interface IBacktraceApi
1212
{
13+
/// <summary>
14+
/// Server url
15+
/// </summary>
16+
string ServerUrl { get; }
1317
/// <summary>
1418
/// Send a Backtrace report to Backtrace API
1519
/// </summary>

Runtime/Interfaces/IBacktraceDatabase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public interface IBacktraceDatabase
8080
/// Add Backtrace data to database
8181
/// </summary>
8282
/// <param name="data">Backtrace data</param>
83+
/// <param name="lock">Lock report - default true</param>
8384
/// <returns>Backtrace record</returns>
84-
BacktraceDatabaseRecord Add(BacktraceData data);
85+
BacktraceDatabaseRecord Add(BacktraceData data, bool @lock = true);
8586
}
8687
}

Runtime/Model/BacktraceConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public class BacktraceConfiguration : ScriptableObject
2323
/// <summary>
2424
/// Maximum number reports per minute
2525
/// </summary>
26-
[Tooltip("Reports per minute: Limits the number of reports the client will send per minutes. If set to 0, there is no limit. If set to a higher value and the value is reached, the client will not send any reports until the next minute.")]
27-
public int ReportPerMin;
26+
[Tooltip("Reports per minute: Limits the number of reports the client will send per minutes. If set to 0, there is no limit. If set to a higher value and the value is reached, the client will not send any reports until the next minute. Default: 50")]
27+
public int ReportPerMin = 50;
2828

2929
/// <summary>
3030
/// Determine if client should catch unhandled exceptions

Runtime/Model/BacktraceData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class BacktraceData
4545
/// <summary>
4646
/// Version of the C# library
4747
/// </summary>
48-
public const string AgentVersion = "3.0.1";
48+
public const string AgentVersion = "3.0.2";
4949

5050
/// <summary>
5151
/// Application thread details

Runtime/Model/BacktraceDatabaseConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class BacktraceDatabaseConfiguration : BacktraceClientConfiguration
1515
public string DatabasePath;
1616

1717
/// <summary>
18-
/// Resend report when http client throw exception
18+
/// When toggled on, the database will send automatically reports to Backtrace server based on the Retry Settings below. When toggled off, the developer will need to use the Flush method to attempt to send and clear. Recommend that this is toggled on.
1919
/// </summary>
2020
public bool AutoSendMode = true;
2121

Runtime/Model/Database/BacktraceDatabaseRecord.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public class BacktraceDatabaseRecord : IDisposable
5858
/// </summary>
5959
public List<string> Attachments { get; private set; }
6060

61+
private string _diagnosticDataJson;
62+
6163
public bool Duplicated
6264
{
6365
get
@@ -84,6 +86,11 @@ public int Count
8486

8587
public string BacktraceDataJson()
8688
{
89+
if (!string.IsNullOrEmpty(_diagnosticDataJson))
90+
{
91+
return _diagnosticDataJson;
92+
}
93+
8794
if (Record != null)
8895
{
8996
return Record.ToJson();
@@ -166,8 +173,8 @@ public bool Save()
166173
{
167174
try
168175
{
169-
var diagnosticDataJson = Record.ToJson();
170-
DiagnosticDataPath = Save(diagnosticDataJson, string.Format("{0}-attachment", Id));
176+
_diagnosticDataJson = Record.ToJson();
177+
DiagnosticDataPath = Save(_diagnosticDataJson, string.Format("{0}-attachment", Id));
171178

172179
if (Attachments != null && Attachments.Any())
173180
{
@@ -342,6 +349,7 @@ protected virtual void Dispose(bool disposing)
342349
{
343350
Locked = false;
344351
Record = null;
352+
_diagnosticDataJson = string.Empty;
345353
}
346354
}
347355

0 commit comments

Comments
 (0)