Skip to content

Conversation

@CodeBlanch
Copy link
Member

@CodeBlanch CodeBlanch commented Feb 2, 2023

Fixes #4139
Relates to #4103

Changes

  • Puts the ResourceBuilder.AddDetector factory pattern overload back into the public API.

History

This API was originally put in to enable retrieving services from IServiceProvider while building resources. It is similar to our AddProcessor, SetSampler, AddInstrumentation, & AddReader overloads exposing the factory pattern. During public API review of 1.4 we made this internal because we weren't sure if anyone would use it. But it looks like users are interested (see: #4139).

TODOs

  • Appropriate CHANGELOG.md updated for non-trivial changes
  • Changes in public API reviewed

@CodeBlanch CodeBlanch requested a review from a team February 2, 2023 18:46
@codecov
Copy link

codecov bot commented Feb 2, 2023

Codecov Report

Merging #4141 (6a7e7fd) into main (c0f927f) will increase coverage by 0.04%.
The diff coverage is n/a.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4141      +/-   ##
==========================================
+ Coverage   85.54%   85.58%   +0.04%     
==========================================
  Files         293      293              
  Lines       11371    11371              
==========================================
+ Hits         9727     9732       +5     
+ Misses       1644     1639       -5     
Impacted Files Coverage Δ
src/OpenTelemetry/Resources/ResourceBuilder.cs 89.79% <ø> (ø)
...tation/OpenTelemetryProtocolExporterEventSource.cs 85.00% <0.00%> (-15.00%) ⬇️
...porter.OpenTelemetryProtocol/OtlpMetricExporter.cs 72.72% <0.00%> (-13.64%) ⬇️
...ter.ZPages/Implementation/ZPagesActivityTracker.cs 97.14% <0.00%> (-2.86%) ⬇️
...ZPages/Implementation/ZPagesExporterEventSource.cs 62.50% <0.00%> (+6.25%) ⬆️
...emetry.Api/Internal/OpenTelemetryApiEventSource.cs 82.35% <0.00%> (+8.82%) ⬆️
src/OpenTelemetry/Logs/Pool/LogRecordSharedPool.cs 100.00% <0.00%> (+21.05%) ⬆️

Comment on lines -146 to +145
internal ResourceBuilder AddDetector(Func<IServiceProvider?, IResourceDetector> resourceDetectorFactory)
public ResourceBuilder AddDetector(Func<IServiceProvider?, IResourceDetector> resourceDetectorFactory)
Copy link
Member

Choose a reason for hiding this comment

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

Spoke with @CodeBlanch offline a bit about this... This method seems kinda wonky in that it differs from our provider builders (tracer, meter, etc). I floated the idea of something like

        public static ResourceBuilder AddDetector<T>(this ResourceBuilder resourceBuilder)
            where T : IResourceDetector
        {
            return resourceBuilder.AddDetector(sp => sp.GetRequiredService<T>());
        }

Though this is wonky for other reasons. It differs from methods like AddProcessor/Instrumentation/Sampler/etc on our provider builders in that this will not automatically add a detector of type T to the service collection.

Also, @CodeBlanch raised a point that ResourceBuilder is hard because it has a public constructor, so something like the following would never work

new ResourceBuilder().AddDetector<MyDetector>()

All that said, my hope is we can do something better here that feels more consistent with the provider builders. My vote is to hold off on this change until after 1.4. A workaround has been provided, albeit not great, but gives us a chance to consider alternatives.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good to me I'll close this and we can work on it for a future release.

@CodeBlanch CodeBlanch closed this Feb 6, 2023
@CodeBlanch CodeBlanch deleted the resourcedetector-factory branch February 6, 2023 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't inject resource detectors after upgrading to 1.4.0-rc.3

4 participants