Add Sdk.SuppressInstrumentation (#960)

* add Sdk.SuppressInstrumentation

* simplify the code

* fix comments

* add XML comment

Co-authored-by: Mikel Blanchard <mblanchard@macrosssoftware.com>
This commit is contained in:
Reiley Yang 2020-07-31 20:42:16 -07:00 committed by GitHub
parent 8468380afa
commit aa86dfe219
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 4 deletions

View File

@ -15,6 +15,7 @@
// </copyright>
#if !NET452
using System.Runtime.CompilerServices;
using System.Threading;
namespace OpenTelemetry.Context
@ -38,12 +39,14 @@ namespace OpenTelemetry.Context
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override T Get()
{
return this.slot.Value;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Set(T value)
{
this.slot.Value = value;

View File

@ -17,6 +17,7 @@
#if NET452
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Messaging;
namespace OpenTelemetry.Context
@ -50,6 +51,7 @@ namespace OpenTelemetry.Context
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override T Get()
{
var wrapper = CallContext.LogicalGetData(this.Name) as BitArray;
@ -63,6 +65,7 @@ namespace OpenTelemetry.Context
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Set(T value)
{
var wrapper = new BitArray(0);

View File

@ -17,6 +17,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace OpenTelemetry.Context
{
@ -41,7 +42,8 @@ namespace OpenTelemetry.Context
/// </summary>
/// <param name="name">The name of the context slot.</param>
/// <typeparam name="T">The type of the underlying value.</typeparam>
public static void RegisterSlot<T>(string name)
/// <returns>The slot registered.</returns>
public static RuntimeContextSlot<T> RegisterSlot<T>(string name)
{
lock (Slots)
{
@ -52,10 +54,23 @@ namespace OpenTelemetry.Context
var type = ContextSlotType.MakeGenericType(typeof(T));
var ctor = type.GetConstructor(new Type[] { typeof(string) });
Slots[name] = ctor.Invoke(new object[] { name });
var slot = (RuntimeContextSlot<T>)ctor.Invoke(new object[] { name });
Slots[name] = slot;
return slot;
}
}
/// <summary>
/// Get a registered slot from a given name.
/// </summary>
/// <param name="name">The name of the context slot.</param>
/// <typeparam name="T">The type of the underlying value.</typeparam>
/// <returns>The slot previously registered.</returns>
public static RuntimeContextSlot<T> GetSlot<T>(string name)
{
return (RuntimeContextSlot<T>)Slots[name];
}
/*
public static void Apply(IDictionary<string, object> snapshot)
{
@ -86,6 +101,7 @@ namespace OpenTelemetry.Context
/// <param name="name">The name of the context slot.</param>
/// <param name="value">The value to be set.</param>
/// <typeparam name="T">The type of the value.</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void SetValue<T>(string name, T value)
{
var slot = (RuntimeContextSlot<T>)Slots[name];
@ -98,6 +114,7 @@ namespace OpenTelemetry.Context
/// <param name="name">The name of the context slot.</param>
/// <typeparam name="T">The type of the value.</typeparam>
/// <returns>The value retrieved from the context slot.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T GetValue<T>(string name)
{
var slot = (RuntimeContextSlot<T>)Slots[name];

View File

@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>
using System.Runtime.CompilerServices;
using System.Threading;
namespace OpenTelemetry.Context
@ -37,12 +38,14 @@ namespace OpenTelemetry.Context
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override T Get()
{
return this.slot.Value;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override void Set(T value)
{
this.slot.Value = value;

View File

@ -17,7 +17,9 @@ using System;
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.Trace;
@ -32,7 +34,53 @@ namespace OpenTelemetry
/// </summary>
public static class Sdk
{
private static TimeSpan defaultPushInterval = TimeSpan.FromSeconds(60);
private static readonly TimeSpan DefaultPushInterval = TimeSpan.FromSeconds(60);
private static readonly RuntimeContextSlot<bool> SuppressInstrumentationRuntimeContextSlot = RuntimeContext.RegisterSlot<bool>("otel.suppress_instrumentation");
/// <summary>
/// Gets or sets a value indicating whether automatic telemetry
/// collection in the current context should be suppressed (disabled).
/// Default value: False.
/// </summary>
/// <remarks>
/// Set <see cref="SuppressInstrumentation"/> to <see langword="true"/>
/// when you want to turn off automatic telemetry collection.
/// 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)
/// {
/// var currentSuppressionPolicy = Sdk.SuppressInstrumentation;
/// Sdk.SuppressInstrumentation = true;
/// try
/// {
/// await this.SendBatchActivityAsync(batch, cancellationToken).ConfigureAwait(false);
/// return ExportResult.Success;
/// }
/// finally
/// {
/// Sdk.SuppressInstrumentation = currentSuppressionPolicy;
/// }
/// }
/// </code>
/// </remarks>
public static bool SuppressInstrumentation
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return SuppressInstrumentationRuntimeContextSlot.Get();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
SuppressInstrumentationRuntimeContextSlot.Set(value);
}
}
/// <summary>
/// Creates MeterProvider with the configuration provided.
@ -60,7 +108,7 @@ namespace OpenTelemetry
meterRegistry,
metricProcessor,
metricExporter,
meterBuilder.MetricPushInterval == default(TimeSpan) ? defaultPushInterval : meterBuilder.MetricPushInterval,
meterBuilder.MetricPushInterval == default(TimeSpan) ? DefaultPushInterval : meterBuilder.MetricPushInterval,
cancellationTokenSource);
var meterProviderSdk = new MeterProviderSdk(metricProcessor, meterRegistry, controller, cancellationTokenSource);