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

[Sdk] Added options for disabling signals #3639

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<MicrosoftAspNetCoreHttpFeaturesPkgVer>[2.1.1,6.0)</MicrosoftAspNetCoreHttpFeaturesPkgVer>
<MicrosoftCodeAnalysisAnalyzersPkgVer>[3.3.3]</MicrosoftCodeAnalysisAnalyzersPkgVer>
<MicrosoftCodeCoveragePkgVer>[17.3.0]</MicrosoftCodeCoveragePkgVer>
<MicrosoftExtensionsConfigurationEnvironmentVariablesPkgVer>[3.1.0,)</MicrosoftExtensionsConfigurationEnvironmentVariablesPkgVer>
<MicrosoftExtensionsHostingAbstractionsPkgVer>[2.1.0,)</MicrosoftExtensionsHostingAbstractionsPkgVer>
<MicrosoftExtensionsLoggingPkgVer>[3.1.0,)</MicrosoftExtensionsLoggingPkgVer>
<MicrosoftExtensionsLoggingConfigurationPkgVer>[3.1.0,)</MicrosoftExtensionsLoggingConfigurationPkgVer>
Expand Down
1 change: 1 addition & 0 deletions src/OpenTelemetry/OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="$(SystemReflectionEmitLightweightPkgVer)" Condition="'$(TargetFramework)' != 'net6.0'" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingPkgVer)" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="$(MicrosoftExtensionsLoggingConfigurationPkgVer)" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsConfigurationEnvironmentVariablesPkgVer)" />
Copy link
Member Author

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?

Copy link
Member

@alanwest alanwest Sep 8, 2022

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

</ItemGroup>

<ItemGroup>
Expand Down
68 changes: 68 additions & 0 deletions src/OpenTelemetry/SdkOptions.cs
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);
}
}
}
}
38 changes: 30 additions & 8 deletions src/OpenTelemetry/Trace/Builder/TracerProviderBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -64,7 +72,7 @@ protected TracerProviderBuilderBase()
{
var services = new ServiceCollection();

services.AddOptions();
RegisterSharedServices(services);

this.services = services;
this.ownsServices = true;
Expand Down Expand Up @@ -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();
Copy link
Member Author

Choose a reason for hiding this comment

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

@rajkumar-rangaraj Your design was returning the TracerProvider.Default for this case. I had that, but decided to add a no-op instance instead. The reason being: User's code is probably calling dispose/shutdown on the instance returned from build invocation. Seemed risky to return them something shared which they might kill accidentally?

}
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(
Expand Down Expand Up @@ -332,10 +357,7 @@ private void TryAddSingleton<T>()
{
var services = this.services;

if (services != null)
{
services.TryAddSingleton<T>();
}
services?.TryAddSingleton<T>();
}
}
}
24 changes: 24 additions & 0 deletions src/OpenTelemetry/Trace/NoopTracerProvider.cs
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
{
}
}