Skip to content

Commit

Permalink
Fix double shutdown (#1017)
Browse files Browse the repository at this point in the history
* fix double shutdown

* add consistency

* nit
  • Loading branch information
reyang authored Aug 7, 2020
1 parent 1b01f3c commit e43356a
Show file tree
Hide file tree
Showing 18 changed files with 31 additions and 43 deletions.
2 changes: 1 addition & 1 deletion docs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Please sort alphabetically.
Refer to https://docs.microsoft.com/en-us/nuget/concepts/package-versioning for semver syntax.
-->
<OpenTelemetryPkgVer>[0.4.0-beta.2,1.0)</OpenTelemetryPkgVer>
<OpenTelemetryExporterConsolePkgVer>[0.4.0-beta.2,1.0)</OpenTelemetryExporterConsolePkgVer>
<OpenTelemetryPkgVer>[0.4.0-beta.2,1.0)</OpenTelemetryPkgVer>
</PropertyGroup>
</Project>
1 change: 0 additions & 1 deletion docs/trace/building-your-own-exporter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Trace;

public class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// limitations under the License.
// </copyright>

using System;
using OpenTelemetry.Trace;

internal static class TracerProviderExtensions
Expand Down
1 change: 1 addition & 0 deletions docs/trace/building-your-own-sampler/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public static void Main()
{
activity?.SetTag("foo", 1);
activity?.SetTag("bar", "Hello, World!");
activity?.SetTag("baz", new int[] { 1, 2, 3 });
}
}
}
5 changes: 2 additions & 3 deletions docs/trace/getting-started/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public static void Main()
new string[]
{
"MyCompany.MyProduct.MyLibrary",
});

tracerProvider.AddProcessor(new SimpleActivityProcessor(new ConsoleExporter(new ConsoleExporterOptions())));
})
.AddProcessor(new SimpleActivityProcessor(new ConsoleExporter(new ConsoleExporterOptions())));

using (var activity = MyActivitySource.StartActivity("SayHello"))
{
Expand Down
1 change: 0 additions & 1 deletion src/OpenTelemetry.Exporter.Console/ConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// </copyright>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using System.Linq;

using Google.Protobuf;
using Opentelemetry.Proto.Common.V1;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using OtlpCommon = Opentelemetry.Proto.Common.V1;
Expand Down
1 change: 0 additions & 1 deletion src/OpenTelemetry.Exporter.ZPages/ZPagesProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Exporter.ZPages.Implementation;
#if NET452
using OpenTelemetry.Internal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// </copyright>
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Net.Http;

namespace OpenTelemetry.Instrumentation.Http.Implementation
Expand Down
1 change: 0 additions & 1 deletion src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Threading;
using OpenTelemetry.Metrics.Export;
Expand Down
2 changes: 0 additions & 2 deletions src/OpenTelemetry/Sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using OpenTelemetry.Context;
using OpenTelemetry.Metrics;
using OpenTelemetry.Metrics.Export;
using OpenTelemetry.Resources;
Expand Down
20 changes: 16 additions & 4 deletions src/OpenTelemetry/Trace/ActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace OpenTelemetry.Trace
/// </summary>
public abstract class ActivityProcessor : IDisposable
{
private bool disposed;

/// <summary>
/// Activity start hook.
/// </summary>
Expand Down Expand Up @@ -80,14 +82,24 @@ public void Dispose()

protected virtual void Dispose(bool disposing)
{
try
if (this.disposed)
{
this.ShutdownAsync(CancellationToken.None).GetAwaiter().GetResult();
return;
}
catch (Exception ex)

if (disposing)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
try
{
this.ShutdownAsync(CancellationToken.None).GetAwaiter().GetResult();
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
}
}

this.disposed = true;
}
}
}
16 changes: 9 additions & 7 deletions src/OpenTelemetry/Trace/CompositeActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Internal;
Expand Down Expand Up @@ -55,6 +54,11 @@ public CompositeActivityProcessor(IEnumerable<ActivityProcessor> processors)

public CompositeActivityProcessor AddProcessor(ActivityProcessor processor)
{
if (processor == null)
{
throw new ArgumentNullException(nameof(processor));
}

var node = new DoublyLinkedListNode<ActivityProcessor>(processor)
{
Previous = this.tail,
Expand Down Expand Up @@ -132,22 +136,22 @@ protected override void Dispose(bool disposing)
{
cur.Value?.Dispose();
}
catch (Exception e)
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.SpanProcessorException("Dispose", e);
OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex);
}

cur = cur.Next;
}
}

base.Dispose(disposing);

this.disposed = true;
}

private class DoublyLinkedListNode<T>
{
public readonly T Value;

public DoublyLinkedListNode(T value)
{
this.Value = value;
Expand All @@ -156,8 +160,6 @@ public DoublyLinkedListNode(T value)
public DoublyLinkedListNode<T> Previous { get; set; }

public DoublyLinkedListNode<T> Next { get; set; }

public T Value { get; set; }
}
}
}
4 changes: 0 additions & 4 deletions src/OpenTelemetry/Trace/Internal/NoopActivityProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
// limitations under the License.
// </copyright>

using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace OpenTelemetry.Trace.Internal
{
internal sealed class NoopActivityProcessor : ActivityProcessor
Expand Down
3 changes: 0 additions & 3 deletions src/OpenTelemetry/Trace/TracerProviderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Resources;

namespace OpenTelemetry.Trace
{
Expand Down
10 changes: 2 additions & 8 deletions src/OpenTelemetry/Trace/TracerProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ protected override void Dispose(bool disposing)
{
foreach (var item in this.Instrumentations)
{
if (item is IDisposable disposable)
{
disposable.Dispose();
}
(item as IDisposable)?.Dispose();
}

this.Instrumentations.Clear();

if (this.ActivityProcessor is IDisposable disposableProcessor)
{
disposableProcessor.Dispose();
}
this.ActivityProcessor?.Dispose();

// Shutdown the listener last so that anything created while instrumentation cleans up will still be processed.
// Redis instrumentation, for example, flushes during dispose which creates Activity objects for any profiling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
// </copyright>

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry.Testing.Export;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Internal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// </copyright>

using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Trace;
using Xunit;

Expand Down

0 comments on commit e43356a

Please sign in to comment.