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

ResourceBuilder API #1533

Merged
merged 21 commits into from
Nov 17, 2020
Merged

Conversation

CodeBlanch
Copy link
Member

@CodeBlanch CodeBlanch commented Nov 13, 2020

Changes - V2.5

Switched Resources API to use a builder pattern.

  • Resource implements the specification.
  • ResourceBuilder can be used to build up a final Resource from Resources or IResourceDetectors which will be merged together.
  • Extension methods on ResourceBuilder give user's a friendly API to work with and extension authors something to extend.
  • TracerProviderSdk now uses ResourceBuilder to do its work. SetResource is now SetResourceBuilder.
  • Made Resource semantic conventions internal.

Public API Changes

    public class ResourceBuilder
    {
        private ResourceBuilder();
        public static ResourceBuilder CreateDefault();
        public static ResourceBuilder CreateEmpty();

        public ResourceBuilder AddResource(Resource resource);
        public ResourceBuilder Clear();
        public Resource Build();

        // Note: Not public, but might be in the future.
        internal ResourceBuilder AddDetector(IResourceDetector resourceDetector)
    }

    public static class ResourceBuilderExtensions
    {
        public static ResourceBuilder AddService(
            this ResourceBuilder resourceBuilder,
            string serviceName,
            string serviceNamespace = null,
            string serviceVersion = null,
            bool autoGenerateServiceInstanceId = true,
            string serviceInstanceId = null);
        public static ResourceBuilder AddTelemetrySdk(this ResourceBuilder resourceBuilder);
        public static ResourceBuilder AddAttributes(this ResourceBuilder resourceBuilder, IEnumerable<KeyValuePair<string, object>> attributes);
        public static ResourceBuilder AddEnvironmentVariableDetector(this ResourceBuilder resourceBuilder);
    }

    public static class TracerProviderBuilderExtensions
    {
        // Removed
        - public static TracerProviderBuilder SetResource(this TracerProviderBuilder tracerProviderBuilder, Resource resource);

        // Added
        + public static TracerProviderBuilder SetResourceBuilder(this TracerProviderBuilder tracerProviderBuilder, ResourceBuilder resourceBuilder);
    }

Usage

            var resourceBuilder = ResourceBuilder.CreateDefault()
                .AddEnvironmentVariableDetector()
                .AddService("MyService")
                .AddAttributes(new Dictionary<string, object>
                {
                    ["service.datacenter"] = 1,
                });

            var tracerProvider = Sdk.CreateTracerProviderBuilder()
                 .SetResourceBuilder(resourceBuilder)
                 .Build();

TODOs

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

@CodeBlanch CodeBlanch requested a review from a team November 13, 2020 04:54
@codecov
Copy link

codecov bot commented Nov 13, 2020

Codecov Report

Merging #1533 (5e08d0f) into master (65762cd) will decrease coverage by 0.00%.
The diff coverage is 91.22%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1533      +/-   ##
==========================================
- Coverage   80.98%   80.98%   -0.01%     
==========================================
  Files         236      237       +1     
  Lines        6433     6453      +20     
==========================================
+ Hits         5210     5226      +16     
- Misses       1223     1227       +4     
Impacted Files Coverage Δ
...rc/OpenTelemetry.Exporter.Jaeger/JaegerExporter.cs 84.94% <ø> (ø)
...rc/OpenTelemetry.Exporter.Zipkin/ZipkinExporter.cs 88.04% <ø> (ø)
src/OpenTelemetry/Internal/PeerServiceResolver.cs 100.00% <ø> (ø)
src/OpenTelemetry/Resources/Resource.cs 93.33% <ø> (-2.13%) ⬇️
...Telemetry/Trace/TracerProviderBuilderExtensions.cs 70.58% <0.00%> (ø)
...rc/OpenTelemetry/Trace/TracerProviderBuilderSdk.cs 87.03% <90.00%> (+6.26%) ⬆️
src/OpenTelemetry/Resources/ResourceBuilder.cs 90.47% <90.47%> (ø)
...enTelemetry/Resources/ResourceBuilderExtensions.cs 95.65% <95.65%> (ø)
...try.Exporter.OpenTelemetryProtocol/OtlpExporter.cs 58.53% <100.00%> (ø)
...ZPages/Implementation/ZPagesExporterEventSource.cs 56.25% <0.00%> (-6.25%) ⬇️
... and 3 more

@alanwest
Copy link
Member

Should the public constants be internal? We don't expose semantic attribute keys but we do resource keys.

My gut says yes. These seem similar to the SemanticConventions class that we've kept internal.

Copy link
Member

@reyang reyang left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Member

@cijothomas cijothomas left a comment

Choose a reason for hiding this comment

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

Please add entry to changelog.md as this is breaking existing APIs.

@CodeBlanch
Copy link
Member Author

CodeBlanch commented Nov 14, 2020

@cijothomas You see the two use cases in the description? How you feel about that? I thought about removing SetResource but I felt like that was still useful.

@cijothomas cijothomas added this to the 1.0.0-RC1 milestone Nov 17, 2020
@CodeBlanch CodeBlanch changed the title Attempting to improve the CreateServiceResource API ResourceBuilder API Nov 17, 2020
@cijothomas cijothomas merged commit d24a042 into open-telemetry:master Nov 17, 2020
@eddynaka eddynaka deleted the resource-create branch November 17, 2020 14:55
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.

4 participants