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

Adding TracerProviderSdkOptions and use factories for all options #1901

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions src/OpenTelemetry/.publicApi/net452/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
OpenTelemetry.Trace.ParentBasedSampler.ParentBasedSampler(OpenTelemetry.Trace.Sampler rootSampler, OpenTelemetry.Trace.Sampler remoteParentSampled = null, OpenTelemetry.Trace.Sampler remoteParentNotSampled = null, OpenTelemetry.Trace.Sampler localParentSampled = null, OpenTelemetry.Trace.Sampler localParentNotSampled = null) -> void
OpenTelemetry.Trace.TracerProviderBuilderSdk
OpenTelemetry.Trace.TracerProviderBuilderSdk.TracerProviderBuilderSdk() -> void
OpenTelemetry.Trace.TracerProviderSdkOptions
OpenTelemetry.Trace.TracerProviderSdkOptions.InstrumentationFactories.get -> System.Collections.Generic.ICollection<OpenTelemetry.Trace.TracerProviderSdkOptions.InstrumentationFactory>
OpenTelemetry.Trace.TracerProviderSdkOptions.InstrumentationFactory
OpenTelemetry.Trace.TracerProviderSdkOptions.InstrumentationFactory.InstrumentationFactory() -> void
OpenTelemetry.Trace.TracerProviderSdkOptions.InstrumentationFactory.InstrumentationFactory(string name, string version, System.Func<object> factory) -> void
OpenTelemetry.Trace.TracerProviderSdkOptions.LegacyActivityOperationNames.get -> System.Collections.Generic.IDictionary<string, bool>
OpenTelemetry.Trace.TracerProviderSdkOptions.ProcessorFactories.get -> System.Collections.Generic.ICollection<System.Func<OpenTelemetry.BaseProcessor<System.Diagnostics.Activity>>>
OpenTelemetry.Trace.TracerProviderSdkOptions.ResourceFactory.get -> System.Func<OpenTelemetry.Resources.Resource>
OpenTelemetry.Trace.TracerProviderSdkOptions.ResourceFactory.set -> void
OpenTelemetry.Trace.TracerProviderSdkOptions.SamplerFactory.get -> System.Func<OpenTelemetry.Trace.Sampler>
OpenTelemetry.Trace.TracerProviderSdkOptions.SamplerFactory.set -> void
OpenTelemetry.Trace.TracerProviderSdkOptions.SetErrorStatusOnException.get -> bool
OpenTelemetry.Trace.TracerProviderSdkOptions.SetErrorStatusOnException.set -> void
OpenTelemetry.Trace.TracerProviderSdkOptions.Sources.get -> System.Collections.Generic.ICollection<string>
OpenTelemetry.Trace.TracerProviderSdkOptions.TracerProviderSdkOptions() -> void
override OpenTelemetry.Trace.TracerProviderBuilderSdk.AddInstrumentation<TInstrumentation>(System.Func<TInstrumentation> instrumentationFactory) -> OpenTelemetry.Trace.TracerProviderBuilder
override OpenTelemetry.Trace.TracerProviderBuilderSdk.AddSource(params string[] names) -> OpenTelemetry.Trace.TracerProviderBuilder
readonly OpenTelemetry.Trace.TracerProviderBuilderSdk.options -> OpenTelemetry.Trace.TracerProviderSdkOptions
readonly OpenTelemetry.Trace.TracerProviderSdkOptions.InstrumentationFactory.Factory -> System.Func<object>
readonly OpenTelemetry.Trace.TracerProviderSdkOptions.InstrumentationFactory.Name -> string
readonly OpenTelemetry.Trace.TracerProviderSdkOptions.InstrumentationFactory.Version -> string
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddLegacySource(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, string operationName) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddProcessor(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, System.Func<OpenTelemetry.BaseProcessor<System.Diagnostics.Activity>> processorFactory) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetErrorStatusOnException(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, bool enabled = true) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetResourceBuilder(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, System.Func<OpenTelemetry.Resources.Resource> resourceFactory) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.SetSampler(this OpenTelemetry.Trace.TracerProviderBuilder tracerProviderBuilder, System.Func<OpenTelemetry.Trace.Sampler> samplerFactory) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderExtensions.ForceFlush(this OpenTelemetry.Trace.TracerProvider provider, int timeoutMilliseconds = -1) -> bool
57 changes: 53 additions & 4 deletions src/OpenTelemetry/Trace/TracerProviderBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,23 @@ public static TracerProviderBuilder SetSampler(this TracerProviderBuilder tracer
{
if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk)
{
tracerProviderBuilderSdk.SetSampler(sampler);
tracerProviderBuilderSdk.SetSampler(() => sampler);
}

return tracerProviderBuilder;
}

/// <summary>
/// Sets sampler.
/// </summary>
/// <param name="tracerProviderBuilder">TracerProviderBuilder instance.</param>
/// <param name="samplerFactory">Sampler instance.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
public static TracerProviderBuilder SetSampler(this TracerProviderBuilder tracerProviderBuilder, Func<Sampler> samplerFactory)
{
if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk)
{
tracerProviderBuilderSdk.SetSampler(samplerFactory);
}

return tracerProviderBuilder;
Expand All @@ -66,7 +82,24 @@ public static TracerProviderBuilder SetResourceBuilder(this TracerProviderBuilde
{
if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk)
{
tracerProviderBuilderSdk.SetResourceBuilder(resourceBuilder);
tracerProviderBuilderSdk.SetResource(() => resourceBuilder.Build());
}

return tracerProviderBuilder;
}

/// <summary>
/// Sets the <see cref="ResourceBuilder"/> from which the Resource associated with
/// this provider is built from. Overwrites currently set ResourceBuilder.
/// </summary>
/// <param name="tracerProviderBuilder">TracerProviderBuilder instance.</param>
/// <param name="resourceFactory"><see cref="ResourceBuilder"/> from which Resource will be built.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
public static TracerProviderBuilder SetResourceBuilder(this TracerProviderBuilder tracerProviderBuilder, Func<Resource> resourceFactory)
{
if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk)
{
tracerProviderBuilderSdk.SetResource(resourceFactory);
}

return tracerProviderBuilder;
Expand All @@ -76,13 +109,29 @@ public static TracerProviderBuilder SetResourceBuilder(this TracerProviderBuilde
/// Adds processor to the provider.
/// </summary>
/// <param name="tracerProviderBuilder">TracerProviderBuilder instance.</param>
/// <param name="processor">Activity processor to add.</param>
/// <param name="processor"><see cref="BaseProcessor{Activity}"/> to add.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
public static TracerProviderBuilder AddProcessor(this TracerProviderBuilder tracerProviderBuilder, BaseProcessor<Activity> processor)
{
if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk)
{
tracerProviderBuilderSdk.AddProcessor(processor);
tracerProviderBuilderSdk.AddProcessor(() => processor);
}

return tracerProviderBuilder;
}

/// <summary>
/// Adds processor to the provider.
/// </summary>
/// <param name="tracerProviderBuilder">TracerProviderBuilder instance.</param>
/// <param name="processorFactory">Function that builds a <see cref="BaseProcessor{Activity}"/> instance to add.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
public static TracerProviderBuilder AddProcessor(this TracerProviderBuilder tracerProviderBuilder, Func<BaseProcessor<Activity>> processorFactory)
{
if (tracerProviderBuilder is TracerProviderBuilderSdk tracerProviderBuilderSdk)
{
tracerProviderBuilderSdk.AddProcessor(processorFactory);
}

return tracerProviderBuilder;
Expand Down
106 changes: 29 additions & 77 deletions src/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// </copyright>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Resources;

Expand All @@ -24,18 +23,9 @@ namespace OpenTelemetry.Trace
/// <summary>
/// Build TracerProvider with Resource, Sampler, Processors and Instrumentation.
/// </summary>
internal class TracerProviderBuilderSdk : TracerProviderBuilder
public class TracerProviderBuilderSdk : TracerProviderBuilder
{
private readonly List<InstrumentationFactory> instrumentationFactories = new List<InstrumentationFactory>();
private readonly List<BaseProcessor<Activity>> processors = new List<BaseProcessor<Activity>>();
private readonly List<string> sources = new List<string>();
private readonly Dictionary<string, bool> legacyActivityOperationNames = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
private ResourceBuilder resourceBuilder = ResourceBuilder.CreateDefault();
private Sampler sampler = new ParentBasedSampler(new AlwaysOnSampler());

internal TracerProviderBuilderSdk()
{
}
protected readonly TracerProviderSdkOptions options = new TracerProviderSdkOptions();

/// <summary>
/// Adds an instrumentation to the provider.
Expand All @@ -52,8 +42,8 @@ public override TracerProviderBuilder AddInstrumentation<TInstrumentation>(
throw new ArgumentNullException(nameof(instrumentationFactory));
}

this.instrumentationFactories.Add(
new InstrumentationFactory(
this.options.InstrumentationFactories.Add(
new TracerProviderSdkOptions.InstrumentationFactory(
typeof(TInstrumentation).Name,
"semver:" + typeof(TInstrumentation).Assembly.GetName().Version,
instrumentationFactory));
Expand Down Expand Up @@ -82,7 +72,7 @@ public override TracerProviderBuilder AddSource(params string[] names)

// TODO: We need to fix the listening model.
// Today it ignores version.
this.sources.Add(name);
this.options.Sources.Add(name);
}

return this;
Expand All @@ -96,75 +86,57 @@ public override TracerProviderBuilder AddSource(params string[] names)
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder SetErrorStatusOnException(bool enabled)
{
ExceptionProcessor existingExceptionProcessor = null;

if (this.processors.Count > 0)
{
existingExceptionProcessor = this.processors[0] as ExceptionProcessor;
}

if (enabled)
{
if (existingExceptionProcessor == null)
{
try
{
this.processors.Insert(0, new ExceptionProcessor());
}
catch (Exception ex)
{
throw new NotSupportedException("SetErrorStatusOnException is not supported on this platform.", ex);
}
}
}
else
{
if (existingExceptionProcessor != null)
{
this.processors.RemoveAt(0);
existingExceptionProcessor.Dispose();
}
}
this.options.SetErrorStatusOnException = enabled;

return this;
}

/// <summary>
/// Sets sampler.
/// </summary>
/// <param name="sampler">Sampler instance.</param>
/// <param name="samplerFactory">Function that builds a <see cref="Sampler"/> instance.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder SetSampler(Sampler sampler)
internal TracerProviderBuilder SetSampler(Func<Sampler> samplerFactory)
{
this.sampler = sampler ?? throw new ArgumentNullException(nameof(sampler));
if (samplerFactory == null)
{
throw new ArgumentNullException(nameof(samplerFactory));
}

this.options.SamplerFactory = samplerFactory;
return this;
}

/// <summary>
/// Sets the <see cref="ResourceBuilder"/> from which the Resource associated with
/// this provider is built from. Overwrites currently set ResourceBuilder.
/// </summary>
/// <param name="resourceBuilder"><see cref="ResourceBuilder"/> from which Resource will be built.</param>
/// <param name="resourceFactory">Function that builds a <see cref="Resource"/> instance.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder SetResourceBuilder(ResourceBuilder resourceBuilder)
internal TracerProviderBuilder SetResource(Func<Resource> resourceFactory)
{
this.resourceBuilder = resourceBuilder ?? throw new ArgumentNullException(nameof(resourceBuilder));
if (resourceFactory == null)
{
throw new ArgumentNullException(nameof(resourceFactory));
}

this.options.ResourceFactory = resourceFactory;
return this;
}

/// <summary>
/// Adds processor to the provider.
/// </summary>
/// <param name="processor">Activity processor to add.</param>
/// <param name="processorFactory">Function that builds a <see cref="BaseProcessor{Activity}"/> instance to add.</param>
/// <returns>Returns <see cref="TracerProviderBuilder"/> for chaining.</returns>
internal TracerProviderBuilder AddProcessor(BaseProcessor<Activity> processor)
internal TracerProviderBuilder AddProcessor(Func<BaseProcessor<Activity>> processorFactory)
{
if (processor == null)
if (processorFactory == null)
{
throw new ArgumentNullException(nameof(processor));
throw new ArgumentNullException(nameof(processorFactory));
}

this.processors.Add(processor);
this.options.ProcessorFactories.Add(processorFactory);

return this;
}
Expand All @@ -184,34 +156,14 @@ internal TracerProviderBuilder AddLegacySource(string operationName)
throw new ArgumentException($"{nameof(operationName)} contains null or whitespace string.");
}

this.legacyActivityOperationNames[operationName] = true;
this.options.LegacyActivityOperationNames[operationName] = true;

return this;
}

internal TracerProvider Build()
{
return new TracerProviderSdk(
this.resourceBuilder.Build(),
this.sources,
this.instrumentationFactories,
this.sampler,
this.processors,
this.legacyActivityOperationNames);
}

internal readonly struct InstrumentationFactory
{
public readonly string Name;
public readonly string Version;
public readonly Func<object> Factory;

internal InstrumentationFactory(string name, string version, Func<object> factory)
{
this.Name = name;
this.Version = version;
this.Factory = factory;
}
return new TracerProviderSdk(this.options);
}
}
}
Loading