Skip to content

Flush Enhancements #1743

@rajkumar-rangaraj

Description

@rajkumar-rangaraj

Issues with flush

Telemetry items are not send immediately, items are batched and flushed by SDK at fixed intervals (default is 30 secs) or whenever buffer is full (default is 500 items). If an application exits right after calling Track methods, telemetry may not be sent. To avoid telemetry loss during application shutdown, recommendation is to call Flush before app exit and introduce some delay for the Flush call to complete. The exact amount of delay isn't predictable and depends on factors like how many items or Transmission instances are in memory, how many are on disk, how many are being transmitted to the back end, and whether the channel is in the middle of exponential back-off scenarios. Flush method doesn't return success or failure. Flush functionality differs with two built-in channels and following is the difference.

Sl.No InMemoryChannel ServerTelemetryChannel
1. Flush method is synchronous Flush method is not synchronous
2. Flush returns once telemetry is sent Flush returns once telemetry is queued to Sender/Buffer/Storage queue
3. OnAppExit: No sleep/delay is required after calling Flush 3. OnAppExit: Delay is required after Flush so that telemetries in the queue can be sent by background tasks

Note: Ideally, Flush method should be used in the shutdown activity of an application.

Related Issues: #407 #482

Code Samples

InMemoryChannel

// Onboarded with https://www.nuget.org/packages/Microsoft.ApplicationInsights/
class Program
{
    static void Main(string[] args)
    {
        TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
        configuration.InstrumentationKey = "IKEY";
        var telemetryClient = new TelemetryClient(configuration);
        telemetryClient.TrackTrace("Hello World!");

        // before exit, flush the remaining data
        telemetryClient.Flush();
    }
}

ServerTelemetryChannel

// Onboarded with https://www.nuget.org/packages/Microsoft.ApplicationInsights.WindowsServer/
class Program
{
    static void Main(string[] args)
    {
        TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
        configuration.InstrumentationKey = "IKEY";
        var telemetryClient = new TelemetryClient(configuration);
        telemetryClient.TrackTrace("Hello World!");

        // before exit, flush the remaining data
        telemetryClient.Flush();

        // flush is not synchronous so wait for sometime
        Task.Delay(5000).Wait();
    }
}

Proposal

Add asynchronous Flush method with following capabilities

  • Wait until all telemetry items are sent to server
  • During network outages or high telemetry volumes, move telemetry items from sender, buffer to local disk
  • Allow caller to decide when to cancel the Flush method through CancellationToken
  • Indicate flush is success or failure. Return success indicating that telemetry items are sent either to server or local storage.
Task<bool> FlushAsync(CancellationToken cancellationToken);

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions