Skip to content

Commit 770a367

Browse files
Don't allocate a stopwatch if it isn't going to be used. (#2608)
Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
1 parent a2a8061 commit 770a367

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

src/OpenTelemetry/BatchExportProcessor.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,17 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
130130

131131
var triggers = new WaitHandle[] { this.dataExportedNotification, this.shutdownTrigger };
132132

133-
var sw = Stopwatch.StartNew();
133+
var sw = timeoutMilliseconds == Timeout.Infinite
134+
? null
135+
: Stopwatch.StartNew();
134136

135137
// There is a chance that the export thread finished processing all the data from the queue,
136138
// and signaled before we enter wait here, use polling to prevent being blocked indefinitely.
137139
const int pollingMilliseconds = 1000;
138140

139141
while (true)
140142
{
141-
if (timeoutMilliseconds == Timeout.Infinite)
143+
if (sw == null)
142144
{
143145
try
144146
{

src/OpenTelemetry/CompositeProcessor.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@ public override void OnStart(T data)
8383
protected override bool OnForceFlush(int timeoutMilliseconds)
8484
{
8585
var result = true;
86-
var sw = Stopwatch.StartNew();
86+
var sw = timeoutMilliseconds == Timeout.Infinite
87+
? null
88+
: Stopwatch.StartNew();
8789

8890
for (var cur = this.head; cur != null; cur = cur.Next)
8991
{
90-
if (timeoutMilliseconds == Timeout.Infinite)
92+
if (sw == null)
9193
{
9294
result = cur.Value.ForceFlush(Timeout.Infinite) && result;
9395
}
@@ -107,11 +109,13 @@ protected override bool OnForceFlush(int timeoutMilliseconds)
107109
protected override bool OnShutdown(int timeoutMilliseconds)
108110
{
109111
var result = true;
110-
var sw = Stopwatch.StartNew();
112+
var sw = timeoutMilliseconds == Timeout.Infinite
113+
? null
114+
: Stopwatch.StartNew();
111115

112116
for (var cur = this.head; cur != null; cur = cur.Next)
113117
{
114-
if (timeoutMilliseconds == Timeout.Infinite)
118+
if (sw == null)
115119
{
116120
result = cur.Value.Shutdown(Timeout.Infinite) && result;
117121
}

src/OpenTelemetry/Metrics/CompositeMetricReader.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ protected override bool ProcessMetrics(in Batch<Metric> metrics, int timeoutMill
7575
protected override bool OnCollect(int timeoutMilliseconds = Timeout.Infinite)
7676
{
7777
var result = true;
78-
var sw = Stopwatch.StartNew();
78+
var sw = timeoutMilliseconds == Timeout.Infinite
79+
? null
80+
: Stopwatch.StartNew();
7981

8082
for (var cur = this.head; cur != null; cur = cur.Next)
8183
{
82-
if (timeoutMilliseconds == Timeout.Infinite)
84+
if (sw == null)
8385
{
8486
result = cur.Value.Collect(Timeout.Infinite) && result;
8587
}
@@ -99,11 +101,13 @@ protected override bool OnCollect(int timeoutMilliseconds = Timeout.Infinite)
99101
protected override bool OnShutdown(int timeoutMilliseconds)
100102
{
101103
var result = true;
102-
var sw = Stopwatch.StartNew();
104+
var sw = timeoutMilliseconds == Timeout.Infinite
105+
? null
106+
: Stopwatch.StartNew();
103107

104108
for (var cur = this.head; cur != null; cur = cur.Next)
105109
{
106-
if (timeoutMilliseconds == Timeout.Infinite)
110+
if (sw == null)
107111
{
108112
result = cur.Value.Shutdown(Timeout.Infinite) && result;
109113
}

src/OpenTelemetry/Metrics/MetricReader.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,14 @@ internal virtual void SetParentProvider(BaseProvider parentProvider)
207207
/// </remarks>
208208
protected virtual bool OnCollect(int timeoutMilliseconds)
209209
{
210-
var sw = Stopwatch.StartNew();
210+
var sw = timeoutMilliseconds == Timeout.Infinite
211+
? null
212+
: Stopwatch.StartNew();
211213

212214
var collectMetric = this.ParentProvider.GetMetricCollect();
213215
var metrics = collectMetric();
214216

215-
if (timeoutMilliseconds == Timeout.Infinite)
217+
if (sw == null)
216218
{
217219
return this.ProcessMetrics(metrics, Timeout.Infinite);
218220
}

0 commit comments

Comments
 (0)