-
Notifications
You must be signed in to change notification settings - Fork 763
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
[Sdk] Added options for disabling signals #3639
Changes from 2 commits
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,68 @@ | ||
// <copyright file="SdkOptions.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> | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace OpenTelemetry | ||
{ | ||
public sealed class SdkOptions | ||
{ | ||
private static Action<SdkOptions>? globalConfigureCallbacks; | ||
|
||
public bool TracingEnabled { get; set; } = true; | ||
|
||
public bool MetricsEnabled { get; set; } = true; | ||
|
||
public bool LoggingEnabled { get; set; } = true; | ||
|
||
public static void RegisterConfigureCallback(Action<SdkOptions> configure) | ||
{ | ||
globalConfigureCallbacks += configure ?? throw new ArgumentNullException(nameof(configure)); | ||
} | ||
|
||
internal sealed class ConfigureSdkOptions : IConfigureOptions<SdkOptions> | ||
{ | ||
private readonly IConfiguration configuration; | ||
|
||
public ConfigureSdkOptions(IConfiguration configuration) | ||
{ | ||
Debug.Assert(configuration != null, "configuration was null"); | ||
|
||
this.configuration = configuration; | ||
} | ||
|
||
public void Configure(SdkOptions options) | ||
{ | ||
Debug.Assert(options != null, "options was null"); | ||
|
||
var globalDisableFlagValue = this.configuration.GetValue("OTEL_SDK_DISABLED", false); | ||
if (globalDisableFlagValue) | ||
{ | ||
options.TracingEnabled = false; | ||
options.MetricsEnabled = false; | ||
options.LoggingEnabled = false; | ||
} | ||
|
||
globalConfigureCallbacks?.Invoke(options); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
using System; | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
|
@@ -51,8 +52,15 @@ internal TracerProviderBuilderBase(IServiceCollection services) | |
{ | ||
Guard.ThrowIfNull(services); | ||
|
||
services.AddOptions(); | ||
services.TryAddSingleton<TracerProvider>(sp => new TracerProviderSdk(sp, ownsServiceProvider: false)); | ||
RegisterSharedServices(services); | ||
|
||
services.TryAddSingleton<TracerProvider>(sp => | ||
{ | ||
var sdkOptions = sp.GetRequiredService<IOptions<SdkOptions>>().Value; | ||
return !sdkOptions.TracingEnabled | ||
? new NoopTracerProvider() | ||
: new TracerProviderSdk(sp, ownsServiceProvider: false); | ||
}); | ||
|
||
this.services = services; | ||
this.ownsServices = false; | ||
|
@@ -64,7 +72,7 @@ protected TracerProviderBuilderBase() | |
{ | ||
var services = new ServiceCollection(); | ||
|
||
services.AddOptions(); | ||
RegisterSharedServices(services); | ||
|
||
this.services = services; | ||
this.ownsServices = true; | ||
|
@@ -264,7 +272,24 @@ protected TracerProvider Build() | |
|
||
var serviceProvider = services.BuildServiceProvider(); | ||
|
||
return new TracerProviderSdk(serviceProvider, ownsServiceProvider: true); | ||
var sdkOptions = serviceProvider.GetRequiredService<IOptions<SdkOptions>>().Value; | ||
if (!sdkOptions.TracingEnabled) | ||
{ | ||
serviceProvider.Dispose(); | ||
|
||
return new NoopTracerProvider(); | ||
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. @rajkumar-rangaraj Your design was returning the |
||
} | ||
else | ||
{ | ||
return new TracerProviderSdk(serviceProvider, ownsServiceProvider: true); | ||
} | ||
} | ||
|
||
private static void RegisterSharedServices(IServiceCollection services) | ||
{ | ||
services.AddOptions(); | ||
services.TryAddSingleton<IConfiguration>(sp => new ConfigurationBuilder().AddEnvironmentVariables().Build()); | ||
services.TryAddSingleton<IConfigureOptions<SdkOptions>, SdkOptions.ConfigureSdkOptions>(); | ||
} | ||
|
||
private static BaseProcessor<Activity> BuildExportProcessor( | ||
|
@@ -332,10 +357,7 @@ private void TryAddSingleton<T>() | |
{ | ||
var services = this.services; | ||
|
||
if (services != null) | ||
{ | ||
services.TryAddSingleton<T>(); | ||
} | ||
services?.TryAddSingleton<T>(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// <copyright file="NoopTracerProvider.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> | ||
|
||
#nullable enable | ||
|
||
namespace OpenTelemetry.Trace | ||
{ | ||
internal sealed class NoopTracerProvider : TracerProvider | ||
{ | ||
} | ||
} |
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.
Don't love adding this dependency, but we do make heavy use of environment variables so it didn't seem totally unreasonable?
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'm not personally super concerned, but I have heard concern from end users regarding dependency bloat...
We could experiment with breaking out SDK configuration into its own package. This is kind of similar to what Java has with their auto-config module https://github.com/open-telemetry/opentelemetry-java/tree/main/sdk-extensions/autoconfigure