-
Notifications
You must be signed in to change notification settings - Fork 887
Refactor exporter - step 2 #1081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ | |
|
|
||
| using System.Diagnostics; | ||
| using OpenTelemetry; | ||
| using OpenTelemetry.Trace; | ||
|
|
||
| public class Program | ||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/OpenTelemetry/Trace/BatchExportActivityProcessor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // <copyright file="BatchExportActivityProcessor.cs" company="OpenTelemetry Authors"> | ||
| // 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. | ||
| // </copyright> | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using OpenTelemetry.Internal; | ||
|
|
||
| namespace OpenTelemetry.Trace | ||
| { | ||
| /// <summary> | ||
| /// Implements processor that batches activities before calling exporter. | ||
| /// </summary> | ||
| 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; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BatchExportActivityProcessor"/> class with custom settings. | ||
| /// </summary> | ||
| /// <param name="exporter">Exporter instance.</param> | ||
| /// <param name="maxQueueSize">The maximum queue size. After the size is reached data are dropped. The default value is 2048.</param> | ||
| /// <param name="scheduledDelayMillis">The delay interval in milliseconds between two consecutive exports. The default value is 5000.</param> | ||
| /// <param name="exporterTimeoutMillis">How long the export can run before it is cancelled. The default value is 30000.</param> | ||
| /// <param name="maxExportBatchSize">The maximum batch size of every export. It must be smaller or equal to maxQueueSize. The default value is 512.</param> | ||
| 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; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void OnEnd(Activity activity) | ||
| { | ||
| // TODO | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| /// <exception cref="OperationCanceledException">If the <paramref name="cancellationToken"/> is canceled.</exception> | ||
| public override Task ForceFlushAsync(CancellationToken cancellationToken) | ||
| { | ||
| // TODO | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| /// <exception cref="OperationCanceledException">If the <paramref name="cancellationToken"/> is canceled.</exception> | ||
| public override Task ShutdownAsync(CancellationToken cancellationToken) | ||
| { | ||
| // TODO | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Releases the unmanaged resources used by this class and optionally releases the managed resources. | ||
| /// </summary> | ||
| /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param> | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // <copyright file="SimpleExportActivityProcessor.cs" company="OpenTelemetry Authors"> | ||
| // 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. | ||
| // </copyright> | ||
|
|
||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using OpenTelemetry.Internal; | ||
|
|
||
| namespace OpenTelemetry.Trace | ||
| { | ||
| /// <summary> | ||
| /// Implements simple activity processor that exports activities in OnEnd call without batching. | ||
| /// </summary> | ||
| public class SimpleExportActivityProcessor : ActivityProcessor | ||
| { | ||
| private readonly ActivityExporterSync exporter; | ||
| private bool stopped; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="SimpleExportActivityProcessor"/> class. | ||
| /// </summary> | ||
| /// <param name="exporter">Activity exporter instance.</param> | ||
| public SimpleExportActivityProcessor(ActivityExporterSync exporter) | ||
| { | ||
| this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| 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); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| 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 | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Releases the unmanaged resources used by this class and optionally releases the managed resources. | ||
| /// </summary> | ||
| /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param> | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,6 @@ | |
| // </copyright> | ||
|
|
||
| using System; | ||
| using OpenTelemetry.Trace; | ||
|
|
||
| namespace OpenTelemetry.Trace.Tests | ||
| { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for try/catch here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reyang I merged to unblock you, but probably need try/catch in a future PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to get feedback here.
The spec is saying that exporter should not throw.
I had similar struggle here, whether should I trust the other processors won't throw, or I should make the composite one bullet proof.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the higher order question is, how much do we trust / rely on the other components (whether processors/exporters that we own / in this repo, or we don't own / in other repos) to follow the spec / be a good citizen.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is my take while I was a C/C++ runtime library dev:
freeon a buffer (allocated by malloc) that is already freed, we want to fail him instead of keep silent. In this way we are actually helping that developer to discover his bug, instead of hiding it since his bug could potentially has other implications (e.g. data loss, corruption, logical flaw).However, here I'm struggling as I believe we have another design principle - a telemetry SDK, once initialized, should not punish the users by killing their applications.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is mainly from the responsibility and dev experience perspective (no perf concern here).
For example if some bad guy developed a broken exporter that throws (against the spec law) and we swallowed the exception for him, it seems like we are contributing to a crime instead of reporting to the cops.
And if a good guy is developing an exporter where he made subtle mistake, by throwing error directly we helped him to fix his bug. Otherwise he will have to look at the self diagnostic logs.
Anyways, I'm struggling here which means I don't have strong preference (and as you can see, I do have strong preference that we should catch Dispose exception and I put that catch statement 😄). So I'm happy to change either way (or even have a global flag to turn on/off such behavior) if @alanwest @CodeBlanch or anyone in the community has a strong preference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As usual, @reyang raises good points! I'm good with letting it throw. But I will say this... that doesn't necessarily guarantee anyone will pay attention! I have seen a lot of code get to prod where ops immediately starts complaining things won't shut down. Launch the app in dev, it is easily reproducable. How does this get by the devs? Because they use the "stop" button in VS which doesn't perform graceful shutdown 🙄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, definitely good points to ponder. I'm also good with letting it throw.
Though, I've generally thought of the telemetry SDK and other profiling tools as similar to logging frameworks where "do no harm" is often the general mantra. Usually (hopefully) there are other signs that something is awry - like a sudden drop of data reported to your backend, even though seemingly your app is still running "just fine".
This conversation is definitely bigger than just exporter or processors - these questions would apply to instrumentation among other things as well. It's more of general design philosophy, maybe more appropriate for a spec-level kind of guidance/adoption?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A slightly different story from the Nt vs. Zw API.
I think given that none of us have strong opinion, and it seems @alanwest @CodeBlanch both have/had the intuition that it is nicer to swallow exception, we probably should go with the direction (to swallow exceptions) for now and make sure we handle this consistently across the repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I got two 👍 I'll send a PR to swallow the exception for both the exporting processor and composite processor.