From a4237cf104482332bea006acf0c6610dfee71e61 Mon Sep 17 00:00:00 2001 From: Reiley Yang Date: Fri, 14 Aug 2020 15:52:15 -0700 Subject: [PATCH] Refactor exporter - step 2 (#1081) * skeleton of export processors * remove unused using statements * add blank line --- .../building-your-own-processor/Program.cs | 1 - examples/Console/TestConsoleExporter.cs | 2 - .../Implementation/JaegerThriftClient.cs | 2 +- .../Trace/ActivityExporterSync.cs | 2 - .../Trace/BatchExportActivityProcessor.cs | 125 ++++++++++++++++++ .../Trace/SimpleExportActivityProcessor.cs | 93 +++++++++++++ .../Resources/ResourceTest.cs | 1 - .../Resources/ResourcesTests.cs | 1 - .../Trace/OpenTelemetrySdkTest.cs | 1 - .../Propagation/CompositePropagatorTest.cs | 1 - .../Trace/Propagation/TestPropagator.cs | 1 - .../Trace/Propagation/TraceContextTest.cs | 1 - .../Trace/Propagation/TracestateUtilsTests.cs | 1 - .../Trace/SpanAttributesTest.cs | 1 - test/OpenTelemetry.Tests/Trace/TestSampler.cs | 1 - 15 files changed, 219 insertions(+), 15 deletions(-) create mode 100644 src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs create mode 100644 src/OpenTelemetry/Trace/SimpleExportActivityProcessor.cs diff --git a/docs/trace/building-your-own-processor/Program.cs b/docs/trace/building-your-own-processor/Program.cs index 942a6d07ce..5ffb9c91c9 100644 --- a/docs/trace/building-your-own-processor/Program.cs +++ b/docs/trace/building-your-own-processor/Program.cs @@ -16,7 +16,6 @@ using System.Diagnostics; using OpenTelemetry; -using OpenTelemetry.Trace; public class Program { diff --git a/examples/Console/TestConsoleExporter.cs b/examples/Console/TestConsoleExporter.cs index 1419cc3221..748907e64d 100644 --- a/examples/Console/TestConsoleExporter.cs +++ b/examples/Console/TestConsoleExporter.cs @@ -16,8 +16,6 @@ using System; using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; using OpenTelemetry; using OpenTelemetry.Resources; using OpenTelemetry.Trace; diff --git a/src/OpenTelemetry.Exporter.Jaeger/Implementation/JaegerThriftClient.cs b/src/OpenTelemetry.Exporter.Jaeger/Implementation/JaegerThriftClient.cs index 98f9612997..a014ba36bb 100644 --- a/src/OpenTelemetry.Exporter.Jaeger/Implementation/JaegerThriftClient.cs +++ b/src/OpenTelemetry.Exporter.Jaeger/Implementation/JaegerThriftClient.cs @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. // -using System; + using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; diff --git a/src/OpenTelemetry/Trace/ActivityExporterSync.cs b/src/OpenTelemetry/Trace/ActivityExporterSync.cs index 4ab0a6947c..f2bcb97c91 100644 --- a/src/OpenTelemetry/Trace/ActivityExporterSync.cs +++ b/src/OpenTelemetry/Trace/ActivityExporterSync.cs @@ -17,8 +17,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; namespace OpenTelemetry.Trace { diff --git a/src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs b/src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs new file mode 100644 index 0000000000..10482cdf03 --- /dev/null +++ b/src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs @@ -0,0 +1,125 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; +using OpenTelemetry.Internal; + +namespace OpenTelemetry.Trace +{ + /// + /// Implements processor that batches activities before calling exporter. + /// + public class BatchExportActivityProcessor : ActivityProcessor + { + private readonly ActivityExporterSync exporter; + private readonly int maxQueueSize; + private readonly TimeSpan scheduledDelay; + private readonly TimeSpan exporterTimeout; + private readonly int maxExportBatchSize; + private bool disposed; + + /// + /// Initializes a new instance of the class with custom settings. + /// + /// Exporter instance. + /// The maximum queue size. After the size is reached data are dropped. The default value is 2048. + /// The delay interval in milliseconds between two consecutive exports. The default value is 5000. + /// How long the export can run before it is cancelled. The default value is 30000. + /// The maximum batch size of every export. It must be smaller or equal to maxQueueSize. The default value is 512. + public BatchExportActivityProcessor( + ActivityExporterSync exporter, + int maxQueueSize = 2048, + int scheduledDelayMillis = 5000, + int exporterTimeoutMillis = 30000, + int maxExportBatchSize = 512) + { + if (maxQueueSize <= 0) + { + throw new ArgumentOutOfRangeException(nameof(maxQueueSize)); + } + + if (maxExportBatchSize <= 0 || maxExportBatchSize > maxQueueSize) + { + throw new ArgumentOutOfRangeException(nameof(maxExportBatchSize)); + } + + if (scheduledDelayMillis <= 0) + { + throw new ArgumentOutOfRangeException(nameof(scheduledDelayMillis)); + } + + if (exporterTimeoutMillis < 0) + { + throw new ArgumentOutOfRangeException(nameof(exporterTimeoutMillis)); + } + + this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter)); + this.maxQueueSize = maxQueueSize; + this.scheduledDelay = TimeSpan.FromMilliseconds(scheduledDelayMillis); + this.exporterTimeout = TimeSpan.FromMilliseconds(exporterTimeoutMillis); + this.maxExportBatchSize = maxExportBatchSize; + } + + /// + public override void OnEnd(Activity activity) + { + // TODO + throw new NotImplementedException(); + } + + /// + /// If the is canceled. + public override Task ForceFlushAsync(CancellationToken cancellationToken) + { + // TODO + throw new NotImplementedException(); + } + + /// + /// If the is canceled. + public override Task ShutdownAsync(CancellationToken cancellationToken) + { + // TODO + throw new NotImplementedException(); + } + + /// + /// Releases the unmanaged resources used by this class and optionally releases the managed resources. + /// + /// to release both managed and unmanaged resources; to release only unmanaged resources. + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + + if (disposing && !this.disposed) + { + try + { + this.exporter.Dispose(); + } + catch (Exception ex) + { + OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex); + } + + this.disposed = true; + } + } + } +} diff --git a/src/OpenTelemetry/Trace/SimpleExportActivityProcessor.cs b/src/OpenTelemetry/Trace/SimpleExportActivityProcessor.cs new file mode 100644 index 0000000000..ae103d41cf --- /dev/null +++ b/src/OpenTelemetry/Trace/SimpleExportActivityProcessor.cs @@ -0,0 +1,93 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; +using System.Diagnostics; +using System.Threading; +using System.Threading.Tasks; +using OpenTelemetry.Internal; + +namespace OpenTelemetry.Trace +{ + /// + /// Implements simple activity processor that exports activities in OnEnd call without batching. + /// + public class SimpleExportActivityProcessor : ActivityProcessor + { + private readonly ActivityExporterSync exporter; + private bool stopped; + + /// + /// Initializes a new instance of the class. + /// + /// Activity exporter instance. + public SimpleExportActivityProcessor(ActivityExporterSync exporter) + { + this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter)); + } + + /// + public override void OnEnd(Activity activity) + { + try + { + // TODO: avoid heap allocation + _ = this.exporter.Export(new[] { activity }); + } + catch (Exception ex) + { + OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.OnEnd), ex); + } + } + + /// + public override Task ShutdownAsync(CancellationToken cancellationToken) + { + if (!this.stopped) + { + this.exporter.Shutdown(); + this.stopped = true; + } + +#if NET452 + return Task.FromResult(0); +#else + return Task.CompletedTask; +#endif + } + + /// + /// Releases the unmanaged resources used by this class and optionally releases the managed resources. + /// + /// to release both managed and unmanaged resources; to release only unmanaged resources. + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + + if (disposing) + { + try + { + this.exporter.Dispose(); + } + catch (Exception ex) + { + OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Dispose), ex); + } + } + } + } +} diff --git a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs index d8d4c32ce1..9a06560a02 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourceTest.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourceTest.cs @@ -16,7 +16,6 @@ using System; using System.Collections.Generic; using System.Linq; -using OpenTelemetry.Resources; using Xunit; namespace OpenTelemetry.Resources.Tests diff --git a/test/OpenTelemetry.Tests/Resources/ResourcesTests.cs b/test/OpenTelemetry.Tests/Resources/ResourcesTests.cs index 79e4cb217a..6077ccbe5b 100644 --- a/test/OpenTelemetry.Tests/Resources/ResourcesTests.cs +++ b/test/OpenTelemetry.Tests/Resources/ResourcesTests.cs @@ -17,7 +17,6 @@ using System; using System.Collections.Generic; using System.Linq; -using OpenTelemetry.Resources; using Xunit; namespace OpenTelemetry.Resources.Tests diff --git a/test/OpenTelemetry.Tests/Trace/OpenTelemetrySdkTest.cs b/test/OpenTelemetry.Tests/Trace/OpenTelemetrySdkTest.cs index 1660c09162..6035e6a4e4 100644 --- a/test/OpenTelemetry.Tests/Trace/OpenTelemetrySdkTest.cs +++ b/test/OpenTelemetry.Tests/Trace/OpenTelemetrySdkTest.cs @@ -16,7 +16,6 @@ using System.Diagnostics; using OpenTelemetry.Resources; -using OpenTelemetry.Trace; using Xunit; namespace OpenTelemetry.Trace.Tests diff --git a/test/OpenTelemetry.Tests/Trace/Propagation/CompositePropagatorTest.cs b/test/OpenTelemetry.Tests/Trace/Propagation/CompositePropagatorTest.cs index 85eed8df49..0890f5173c 100644 --- a/test/OpenTelemetry.Tests/Trace/Propagation/CompositePropagatorTest.cs +++ b/test/OpenTelemetry.Tests/Trace/Propagation/CompositePropagatorTest.cs @@ -18,7 +18,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using OpenTelemetry.Context.Propagation; using Xunit; namespace OpenTelemetry.Context.Propagation.Tests diff --git a/test/OpenTelemetry.Tests/Trace/Propagation/TestPropagator.cs b/test/OpenTelemetry.Tests/Trace/Propagation/TestPropagator.cs index 43eba23756..0078b103b6 100644 --- a/test/OpenTelemetry.Tests/Trace/Propagation/TestPropagator.cs +++ b/test/OpenTelemetry.Tests/Trace/Propagation/TestPropagator.cs @@ -18,7 +18,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; -using OpenTelemetry.Context.Propagation; namespace OpenTelemetry.Context.Propagation.Tests { diff --git a/test/OpenTelemetry.Tests/Trace/Propagation/TraceContextTest.cs b/test/OpenTelemetry.Tests/Trace/Propagation/TraceContextTest.cs index 2de64ec67a..d5bac5315f 100644 --- a/test/OpenTelemetry.Tests/Trace/Propagation/TraceContextTest.cs +++ b/test/OpenTelemetry.Tests/Trace/Propagation/TraceContextTest.cs @@ -16,7 +16,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; -using OpenTelemetry.Context.Propagation; using Xunit; namespace OpenTelemetry.Context.Propagation.Tests diff --git a/test/OpenTelemetry.Tests/Trace/Propagation/TracestateUtilsTests.cs b/test/OpenTelemetry.Tests/Trace/Propagation/TracestateUtilsTests.cs index 89665c9f75..5f71dddb36 100644 --- a/test/OpenTelemetry.Tests/Trace/Propagation/TracestateUtilsTests.cs +++ b/test/OpenTelemetry.Tests/Trace/Propagation/TracestateUtilsTests.cs @@ -16,7 +16,6 @@ using System.Collections.Generic; using System.Linq; -using OpenTelemetry.Context.Propagation; using Xunit; namespace OpenTelemetry.Context.Propagation.Tests diff --git a/test/OpenTelemetry.Tests/Trace/SpanAttributesTest.cs b/test/OpenTelemetry.Tests/Trace/SpanAttributesTest.cs index 9c43ff90db..76276730b6 100644 --- a/test/OpenTelemetry.Tests/Trace/SpanAttributesTest.cs +++ b/test/OpenTelemetry.Tests/Trace/SpanAttributesTest.cs @@ -15,7 +15,6 @@ // using System; -using System.Collections.Generic; using Xunit; namespace OpenTelemetry.Trace.Tests diff --git a/test/OpenTelemetry.Tests/Trace/TestSampler.cs b/test/OpenTelemetry.Tests/Trace/TestSampler.cs index 1a30c320e4..c4182cc7d3 100644 --- a/test/OpenTelemetry.Tests/Trace/TestSampler.cs +++ b/test/OpenTelemetry.Tests/Trace/TestSampler.cs @@ -15,7 +15,6 @@ // using System; -using OpenTelemetry.Trace; namespace OpenTelemetry.Trace.Tests {