-
Notifications
You must be signed in to change notification settings - Fork 773
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 SuppressInstrumentation an IDisposable #988
Changes from 6 commits
f663f4e
7f18daf
363d56b
ed6f6bf
447cc2b
f0cc2c8
79cf89b
3ca9878
9993df6
46adcfe
f1745bd
ace48c8
21cb077
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,74 @@ | ||||||
// <copyright file="SuppressInstrumentationScope.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 OpenTelemetry.Context; | ||||||
|
||||||
namespace OpenTelemetry | ||||||
{ | ||||||
public sealed class SuppressInstrumentationScope : IDisposable | ||||||
{ | ||||||
private static readonly RuntimeContextSlot<bool> SuppressInstrumentationRuntimeContextSlot = RuntimeContext.RegisterSlot<bool>("otel.suppress_instrumentation"); | ||||||
|
||||||
private readonly bool previousValue; | ||||||
private bool disposed; | ||||||
|
||||||
internal SuppressInstrumentationScope(bool value = true) | ||||||
{ | ||||||
this.previousValue = SuppressInstrumentationRuntimeContextSlot.Get(); | ||||||
SuppressInstrumentationRuntimeContextSlot.Set(value); | ||||||
} | ||||||
|
||||||
public static implicit operator bool(SuppressInstrumentationScope x) => SuppressInstrumentationRuntimeContextSlot.Get(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or other name that explains the intention. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😄 agree There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like |
||||||
|
||||||
/// <summary> | ||||||
/// Begins a new scope in which automatic telemetry is suppressed (disabled). | ||||||
reyang marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/// </summary> | ||||||
/// <param name="value">Value indicating whether to suppress automatic telemetry.</param> | ||||||
reyang marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/// <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<ExportResult> ExportAsync( | ||||||
/// IEnumerable<Activity> 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); | ||||||
} | ||||||
|
||||||
/// <inheritdoc/> | ||||||
public void Dispose() | ||||||
{ | ||||||
if (!this.disposed) | ||||||
{ | ||||||
SuppressInstrumentationRuntimeContextSlot.Set(this.previousValue); | ||||||
this.disposed = true; | ||||||
} | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// <copyright file="SuppressInstrumentationTest.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 Xunit; | ||
|
||
namespace OpenTelemetry.Tests | ||
{ | ||
public class SuppressInstrumentationTest | ||
{ | ||
[Fact] | ||
public static void UsingSuppressInstrumentation() | ||
{ | ||
using (var scope = Sdk.SuppressInstrumentation.Begin()) | ||
{ | ||
Assert.True(Sdk.SuppressInstrumentation); | ||
|
||
using (var innerScope = Sdk.SuppressInstrumentation.Begin()) | ||
{ | ||
innerScope.Dispose(); | ||
|
||
Assert.True(Sdk.SuppressInstrumentation); | ||
|
||
scope.Dispose(); | ||
} | ||
|
||
Assert.False(Sdk.SuppressInstrumentation); | ||
} | ||
|
||
Assert.False(Sdk.SuppressInstrumentation); | ||
} | ||
} | ||
} |
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.
Given this is inside a class
SuppressInstrumentationScope
, we can probably simplify the name toContextSlot
or evenSlot
.