Skip to content
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

Make suppress instrumentation APIs less prone to introducing bugs #1067

2 changes: 1 addition & 1 deletion docs/trace/building-your-own-exporter/MyExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class MyExporter : ActivityExporter
public override Task<ExportResult> ExportAsync(
IEnumerable<Activity> batch, CancellationToken cancellationToken)
{
using var scope = Sdk.SuppressInstrumentation.Begin();
using var scope = Sdk.BeginSuppressInstrumentationScope();

foreach (var activity in batch)
{
Expand Down
31 changes: 29 additions & 2 deletions src/OpenTelemetry/Sdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,37 @@ namespace OpenTelemetry
/// </summary>
public static class Sdk
{
public static readonly SuppressInstrumentationScope SuppressInstrumentation = new SuppressInstrumentationScope(false);

private static readonly TimeSpan DefaultPushInterval = TimeSpan.FromSeconds(60);

/// <summary>
/// Gets a value indicating whether instrumentation is suppressed (disabled).
/// </summary>
public static bool SuppressInstrumentation => SuppressInstrumentationScope.IsSuppressed;
Copy link
Member Author

@alanwest alanwest Aug 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's more to the API now, but I think it is simpler to understand by decoupling the disposable object from the boolean check. Should we apply AggresiveInlining on these APIs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess AggresiveInlining is not going to help here :)


/// <summary>
/// Begins a new scope in which instrumentation is suppressed (disabled).
/// </summary>
/// <param name="value">Value indicating whether to suppress instrumentation.</param>
/// <returns>Object to dispose to end the scope.</returns>
/// <remarks>
/// This is typically used to prevent infinite loops created by
/// collection of internal operations, such as exporting traces over HTTP.
/// <code>
/// public override async Task&lt;ExportResult&gt; ExportAsync(
/// IEnumerable&lt;Activity&gt; batch,
/// CancellationToken cancellationToken)
/// {
/// using (Sdk.BeginSuppressInstrumentationScope())
/// {
/// // Instrumentation is suppressed (i.e., Sdk.SuppressInstrumentation == true)
/// }
///
/// // Instrumentation is not suppressed (i.e., Sdk.SuppressInstrumentation == false)
/// }
/// </code>
/// </remarks>
public static SuppressInstrumentationScope BeginSuppressInstrumentationScope(bool value = true) => SuppressInstrumentationScope.Begin(value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think return should just be IDisposable. It's not like you can do anything with the class. More closely matches log scope that way too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! Good call.


/// <summary>
/// Creates MeterProvider with the configuration provided.
/// Configuration involves MetricProcessor, Exporter and push internval.
Expand Down
34 changes: 6 additions & 28 deletions src/OpenTelemetry/SuppressInstrumentationScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,7 @@ internal SuppressInstrumentationScope(bool value = true)
Slot.Set(value);
}

public static implicit operator bool(SuppressInstrumentationScope unused) => Slot.Get();

/// <summary>
/// Begins a new scope in which instrumentation is suppressed (disabled).
/// </summary>
/// <param name="value">Value indicating whether to suppress instrumentation.</param>
/// <returns>Object to dispose to end the scope.</returns>
/// <remarks>
/// This is typically used to prevent infinite loops created by
/// collection of internal operations, such as exporting traces over HTTP.
/// <code>
/// public override async Task&lt;ExportResult&gt; ExportAsync(
/// IEnumerable&lt;Activity&gt; batch,
/// CancellationToken cancellationToken)
/// {
/// using (Sdk.SuppressInstrumentation.Begin())
/// {
/// // Instrumentation is suppressed (i.e., Sdk.SuppressInstrumentation == true)
/// }
///
/// // Instrumentation is not suppressed (i.e., Sdk.SuppressInstrumentation == false)
/// }
/// </code>
/// </remarks>
public IDisposable Begin(bool value = true)
{
return new SuppressInstrumentationScope(value);
}
internal static bool IsSuppressed => Slot.Get();

/// <inheritdoc/>
public void Dispose()
Expand All @@ -70,5 +43,10 @@ public void Dispose()
this.disposed = true;
}
}

internal static SuppressInstrumentationScope Begin(bool value = true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you feel about leaving this in?

public static IDisposable Begin(bool value = true)

Because I kind of like this way better:
using (var scope = SuppressInstrumentationScope.Begin())

But I'm OK also having the helper too 😁

There's no more danger of using (SuppressInstrumentationScope) because the static is gone right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel great about this. I actually decided to remove the helper in favor of this idea. Though, happy to revert and put the helper back if others find it useful. I personally like your idea better.

{
return new SuppressInstrumentationScope(value);
}
}
}
6 changes: 4 additions & 2 deletions test/OpenTelemetry.Tests/SuppressInstrumentationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ public class SuppressInstrumentationTest
[Fact]
public static void UsingSuppressInstrumentation()
{
using (var scope = Sdk.SuppressInstrumentation.Begin())
Assert.False(Sdk.SuppressInstrumentation);

using (var scope = Sdk.BeginSuppressInstrumentationScope())
{
Assert.True(Sdk.SuppressInstrumentation);

using (var innerScope = Sdk.SuppressInstrumentation.Begin())
using (var innerScope = Sdk.BeginSuppressInstrumentationScope())
{
innerScope.Dispose();

Expand Down