in WEB/Src/PerformanceCollector/PerformanceCollector/Implementation/QuickPulse/QuickPulseCollectionStateManager.cs [97:221]
public TimeSpan UpdateState(string instrumentationKey, string authApiKey)
{
if (string.IsNullOrWhiteSpace(instrumentationKey))
{
return this.timings.ServicePollingInterval;
}
if (this.firstStateUpdate)
{
this.ResetLastSuccessful();
this.firstStateUpdate = false;
}
AuthToken authToken = default;
if (this.telemetryConfiguration.CredentialEnvelope != null)
{
authToken = this.telemetryConfiguration.CredentialEnvelope.GetToken();
if (authToken == default)
{
// If a credential has been set on the configuration and we fail to get a token, do net send.
QuickPulseEventSource.Log.FailedToGetAuthToken();
return this.DetermineBackOffs();
}
}
CollectionConfigurationInfo configurationInfo;
if (this.IsCollectingData)
{
// we are currently collecting
IList<QuickPulseDataSample> dataSamplesToSubmit = this.onSubmitSamples();
if (!dataSamplesToSubmit.Any())
{
// no samples to submit, do nothing
return this.DetermineBackOffs();
}
else
{
// we have samples
if (dataSamplesToSubmit.Any(sample => sample.CollectionConfigurationAccumulator.GetRef() != 0))
{
// some samples are still being processed, wait a little to give them a chance to finish
Task.Delay(this.coolDownTimeout).GetAwaiter().GetResult();
bool allCooledDown =
dataSamplesToSubmit.All(sample => sample.CollectionConfigurationAccumulator.GetRef() == 0);
QuickPulseEventSource.Log.CollectionConfigurationSampleCooldownEvent(allCooledDown);
}
}
bool? keepCollecting = this.serviceClient.SubmitSamples(
dataSamplesToSubmit,
instrumentationKey,
this.currentConfigurationETag,
authApiKey,
authToken.Token,
out configurationInfo,
this.collectionConfigurationErrors.ToArray());
QuickPulseEventSource.Log.SampleSubmittedEvent(this.currentConfigurationETag, configurationInfo?.ETag, keepCollecting.ToString());
switch (keepCollecting)
{
case null:
// the request has failed, so we need to return the samples back to the submitter
this.onReturnFailedSamples(dataSamplesToSubmit);
break;
case true:
// the service wants us to keep collecting
this.UpdateConfiguration(configurationInfo);
break;
case false:
// the service wants us to stop collection
this.onStopCollection();
break;
}
this.lastSuccessfulSubmit = keepCollecting.HasValue ? this.timeProvider.UtcNow : this.lastSuccessfulSubmit;
this.IsCollectingData = keepCollecting ?? this.IsCollectingData;
}
else
{
// we are currently idle and pinging the service waiting for it to ask us to start collecting data
bool? startCollection = this.serviceClient.Ping(
instrumentationKey,
this.timeProvider.UtcNow,
this.currentConfigurationETag,
authApiKey,
authToken.Token,
out configurationInfo,
out TimeSpan? servicePollingIntervalHint);
this.latestServicePollingIntervalHint = servicePollingIntervalHint ?? this.latestServicePollingIntervalHint;
QuickPulseEventSource.Log.PingSentEvent(this.currentConfigurationETag, configurationInfo?.ETag, startCollection.ToString());
switch (startCollection)
{
case null:
// the request has failed
break;
case true:
// the service wants us to start collection now
this.UpdateConfiguration(configurationInfo);
this.onStartCollection();
break;
case false:
// the service wants us to remain idle and keep pinging
break;
}
this.lastSuccessfulPing = startCollection.HasValue ? this.timeProvider.UtcNow : this.lastSuccessfulPing;
this.IsCollectingData = startCollection ?? this.IsCollectingData;
}
this.onUpdatedServiceEndpoint?.Invoke(this.serviceClient.CurrentServiceUri);
return this.DetermineBackOffs();
}