-
Notifications
You must be signed in to change notification settings - Fork 765
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
Add Sdk.SuppressInstrumentation #960
Changes from 1 commit
7a29abf
a686324
3911dc5
1f83935
af61349
ce02006
0594fdd
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 |
---|---|---|
|
@@ -17,7 +17,9 @@ | |
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,24 @@ 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"); | ||
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. This would help the exporters/processors to quickly look up the suppress_instrumentation flag without a hash lookup. |
||
|
||
public static bool SuppressInstrumentation | ||
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.
/// <summary>
/// Gets or sets a value indicating whether or not <see cref="Activity"/>Activity</see> object collection in the current context should be suppressed (dsiabled). Default value: False.
/// </summary>
/// <remarks>
/// Set <see cref="SuppressInstrumentation"/> to <see langword="true"/> when you want to turn off automatic <see cref="Activity"/> collection.
/// This is typically done to prevent infinte loops created by collection of internal operations, such as exporting spans over HTTP.
/// <code>
/// public override async Task<ExportResult> ExportAsync(IEnumerable<Activity> batchActivity, CancellationToken cancellationToken)
/// {
/// Sdk.SuppressInstrumentation = true;
/// try
/// {
/// await this.SendBatchActivityAsync(batchActivity, cancellationToken).ConfigureAwait(false);
/// return ExportResult.Success;
/// }
/// finally
/// {
/// Sdk.SuppressInstrumentation = false;
/// }
/// }
/// </code>
/// </remarks>
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.
Good catch, will add it.
Not sure, the proposed name seems too long 😅 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. What if it wasn't exposed as a bool? Something more like log scope: 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 believe this is what @reyang was proposing in the PR description. I think I do like the disposable idea. I'm currently playing with this implementation and I thing this would make things read nicer. 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. Yeah, @alanwest is helping on that part in a separate PR ❤️ |
||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get | ||
{ | ||
return SuppressInstrumentationRuntimeContextSlot.Get(); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
set | ||
{ | ||
SuppressInstrumentationRuntimeContextSlot.Set(value); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Creates MeterProvider with the configuration provided. | ||
|
@@ -60,7 +79,7 @@ public static MeterProvider CreateMeterProvider(Action<MeterBuilder> configure) | |
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); | ||
|
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.
Will this cause a cast exception if the the TryGet fails?
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 it will. I expect consumers to call this once, and cache the slot for subsequent use (similar like what I did for the
Sdk
).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.
Perhaps I should just use
(RuntimeContextSlot<T>)Slots[name]
?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 think that will throw a key not found. I'm just noting your comment
<returns>The slot previously registered, or null if not found.</returns>
Seems like the TryGetValue is a good way to go and just return null if that fails. Was that your intent?
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've updated the code and also the PR description to show my current approach/thinking.
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.
👍 ok yea I get it, thanks.
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.
Also, the
using
idea is interesting - maybe we try on for size what you have here, and refactor if we like it?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.
Yeah, I think we will have better understanding of how syntactical sugar will help exporters/processors/instrumentation when we start to add the code.