Don't allocate a stopwatch if it isn't going to be used. (#2608)

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
Mikel Blanchard 2021-11-15 10:17:17 -08:00 committed by GitHub
parent a2a8061ccf
commit 770a367b39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 12 deletions

View File

@ -130,7 +130,9 @@ namespace OpenTelemetry
var triggers = new WaitHandle[] { this.dataExportedNotification, this.shutdownTrigger };
var sw = Stopwatch.StartNew();
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
// There is a chance that the export thread finished processing all the data from the queue,
// and signaled before we enter wait here, use polling to prevent being blocked indefinitely.
@ -138,7 +140,7 @@ namespace OpenTelemetry
while (true)
{
if (timeoutMilliseconds == Timeout.Infinite)
if (sw == null)
{
try
{

View File

@ -83,11 +83,13 @@ namespace OpenTelemetry
protected override bool OnForceFlush(int timeoutMilliseconds)
{
var result = true;
var sw = Stopwatch.StartNew();
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
for (var cur = this.head; cur != null; cur = cur.Next)
{
if (timeoutMilliseconds == Timeout.Infinite)
if (sw == null)
{
result = cur.Value.ForceFlush(Timeout.Infinite) && result;
}
@ -107,11 +109,13 @@ namespace OpenTelemetry
protected override bool OnShutdown(int timeoutMilliseconds)
{
var result = true;
var sw = Stopwatch.StartNew();
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
for (var cur = this.head; cur != null; cur = cur.Next)
{
if (timeoutMilliseconds == Timeout.Infinite)
if (sw == null)
{
result = cur.Value.Shutdown(Timeout.Infinite) && result;
}

View File

@ -75,11 +75,13 @@ namespace OpenTelemetry.Metrics
protected override bool OnCollect(int timeoutMilliseconds = Timeout.Infinite)
{
var result = true;
var sw = Stopwatch.StartNew();
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
for (var cur = this.head; cur != null; cur = cur.Next)
{
if (timeoutMilliseconds == Timeout.Infinite)
if (sw == null)
{
result = cur.Value.Collect(Timeout.Infinite) && result;
}
@ -99,11 +101,13 @@ namespace OpenTelemetry.Metrics
protected override bool OnShutdown(int timeoutMilliseconds)
{
var result = true;
var sw = Stopwatch.StartNew();
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
for (var cur = this.head; cur != null; cur = cur.Next)
{
if (timeoutMilliseconds == Timeout.Infinite)
if (sw == null)
{
result = cur.Value.Shutdown(Timeout.Infinite) && result;
}

View File

@ -207,12 +207,14 @@ namespace OpenTelemetry.Metrics
/// </remarks>
protected virtual bool OnCollect(int timeoutMilliseconds)
{
var sw = Stopwatch.StartNew();
var sw = timeoutMilliseconds == Timeout.Infinite
? null
: Stopwatch.StartNew();
var collectMetric = this.ParentProvider.GetMetricCollect();
var metrics = collectMetric();
if (timeoutMilliseconds == Timeout.Infinite)
if (sw == null)
{
return this.ProcessMetrics(metrics, Timeout.Infinite);
}