[sdk] Make ResourceBuilder.AddDetector factory pattern public (attempt 2) #4261
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4139
Fixes #4228
Relates to #4103
Relates to #4230
Changes
Public API Changes
namespace OpenTelemetry.Resources { public class ResourceBuilder { + public ResourceBuilder AddDetector(Func<IServiceProvider, IResourceDetector> resourceDetectorFactory) {} } }Details
The original API that was removed/hidden from 1.4:
public ResourceBuilder AddDetector(Func<IServiceProvider?, IResourceDetector> resourceDetectorFactory) {}The API being added here:
public ResourceBuilder AddDetector(Func<IServiceProvider, IResourceDetector> resourceDetectorFactory) {}The difference is the nullable behavior of the
IServiceProviderparameter.With the new API, doing this...
new ResourceBuilder().AddDector(sp => sp.GetRequiredService<MyResourceDetector>()).Build()...will lead to a
NotSupportedExceptionbecause theResourceBuilderis not associated to either anIServiceCollectionor anIServiceProvider.Using either of these APIs...
builder.SetResourceBuilder(new ResourceBuilder().AddDector(sp => sp.GetRequiredService<MyResourceDetector>()))builder.ConfigureResourceBuilder(builder => builder.AddDector(sp => sp.GetRequiredService<MyResourceDetector>()))...everything will work fine across traces, metrics, and logs.
The idea here is the common use cases will work as expected without the complexity of having to deal with the
nullcase.I really wanted to also add
AddDetector<T>()to be consistent with our other APIs but there isn't a good way for that to work in logs currently and I felt it being consistent for all signals was a good thing. Hopefully we can bring that in for ~1.6.TODOs
CHANGELOG.mdupdated for non-trivial changes