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

Change resource.New() to use functional options; add builtin attributes for (host.*, telemetry.sdk.*) #1235

Merged
merged 16 commits into from
Oct 31, 2020

Conversation

jmacd
Copy link
Contributor

@jmacd jmacd commented Oct 9, 2020

This adds resource.NewConfig(ctx, ...) with functional options:

  • WithAttributes(...) for directly adding key:values
  • WithDetectors(...) for directly adding resource detectors
  • WithTelemetrySDK(...) to override the default telemetry.sdk.* resources
  • WithHost(...) to override the default host.* resources
  • WithFromEnv(...) to override the default environment configuration
  • WithoutBuiltin() to disable all builtin resources

The intention is to make it easy to configure a *Resource with the standard resources. This adds standard resource detectors for the telemetry SDK and host name.

Resolves #1076.

@jmacd jmacd added area:metrics Part of OpenTelemetry Metrics area:trace Part of OpenTelemetry tracing labels Oct 9, 2020
@jmacd
Copy link
Contributor Author

jmacd commented Oct 9, 2020

Resolves #1076

@codecov
Copy link

codecov bot commented Oct 9, 2020

Codecov Report

Merging #1235 into master will increase coverage by 0.2%.
The diff coverage is 95.1%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #1235     +/-   ##
========================================
+ Coverage    76.8%   77.1%   +0.2%     
========================================
  Files         122     123      +1     
  Lines        5953    5985     +32     
========================================
+ Hits         4574    4616     +42     
+ Misses       1131    1121     -10     
  Partials      248     248             
Impacted Files Coverage Δ
sdk/resource/config.go 91.3% <91.3%> (ø)
sdk/metric/controller/pull/pull.go 100.0% <100.0%> (ø)
sdk/metric/controller/push/push.go 96.2% <100.0%> (ø)
sdk/metric/sdk.go 79.3% <100.0%> (-0.3%) ⬇️
sdk/resource/auto.go 100.0% <100.0%> (+100.0%) ⬆️
sdk/resource/builtin.go 100.0% <100.0%> (ø)
sdk/resource/env.go 100.0% <100.0%> (ø)
sdk/resource/resource.go 57.5% <100.0%> (ø)
codes/codes.go 89.6% <0.0%> (ø)
sdk/opentelemetry.go 100.0% <0.0%> (ø)
... and 3 more

@jmacd jmacd changed the title Add a resource.Configure() with functional options for easy builtin resources Add a resource.Configure() with functional options for easy builtin (host.*, telemetry.sdk.*) resources Oct 9, 2020
@jmacd jmacd changed the title Add a resource.Configure() with functional options for easy builtin (host.*, telemetry.sdk.*) resources Add a resource.Configure() with functional options for (host.*, telemetry.sdk.*) resources Oct 9, 2020
sdk/resource/builtin.go Outdated Show resolved Hide resolved
sdk/resource/builtin.go Outdated Show resolved Hide resolved
sdk/resource/config_test.go Outdated Show resolved Hide resolved
sdk/resource/config.go Outdated Show resolved Hide resolved
sdk/resource/config.go Outdated Show resolved Hide resolved
sdk/resource/config.go Outdated Show resolved Hide resolved
sdk/metric/sdk.go Show resolved Hide resolved
@jmacd
Copy link
Contributor Author

jmacd commented Oct 15, 2020

FYI I will address the feedback in this PR today.

@jmacd jmacd changed the title Add a resource.Configure() with functional options for (host.*, telemetry.sdk.*) resources Add a resource.NewConfig() with functional options for (host.*, telemetry.sdk.*) resources Oct 16, 2020
Copy link
Contributor

@MrAlias MrAlias left a comment

Choose a reason for hiding this comment

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

We can probably merge this as is. Adding the exported type/method docs would be nice, but could be added in a follow on.

sdk/resource/builtin.go Outdated Show resolved Hide resolved
sdk/resource/builtin.go Show resolved Hide resolved
sdk/resource/builtin.go Show resolved Hide resolved
sdk/resource/builtin.go Show resolved Hide resolved
sdk/resource/config.go Outdated Show resolved Hide resolved
sdk/resource/env_test.go Show resolved Hide resolved
@jmacd
Copy link
Contributor Author

jmacd commented Oct 20, 2020

I will document this PR today. Sorry @MrAlias, and thanks for keeping me honest.

jmacd and others added 2 commits October 21, 2020 01:05
❤️

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
@jmacd
Copy link
Contributor Author

jmacd commented Oct 22, 2020

@open-telemetry/go-approvers how would you feel about removing the current resource.New(...label.KeyValue) *Resource and renaming this PR's NewConfig() as the replacement API, i.e., resource.New(context.Context, ...Option) (*Resource, error)? I think this will encourage the use of the detector pattern.

@MrAlias
Copy link
Contributor

MrAlias commented Oct 23, 2020

@open-telemetry/go-approvers how would you feel about removing the current resource.New(...label.KeyValue) *Resource and renaming this PR's NewConfig() as the replacement API, i.e., resource.New(context.Context, ...Option) (*Resource, error)? I think this will encourage the use of the detector pattern.

I am in favor of this 👍

@XSAM
Copy link
Member

XSAM commented Oct 27, 2020

how would you feel about removing the current resource.New(...label.KeyValue) *Resource and renaming this PR's NewConfig() as the replacement API, i.e., resource.New(context.Context, ...Option) (*Resource, error)? I think this will encourage the use of the detector pattern.

I'm not sure about this. New(kvs ...label.KeyValue) *Resource seems quite simple to me. Also, does any chance that resource.New might return an error?

@jmacd
Copy link
Contributor Author

jmacd commented Oct 27, 2020

I'm not sure about this.

I had originally used Configure() as the name, trying to make it clear that Configure() had some logic (possible errors) whereas New() just builds a resource (no errors). I hope that most end users will call only a single method, is the thing added in this PR, and the current New() method will mostly be used by detector implementations. Whatever we do, we should choose the right name from the majority of users' perspective.

@Aneurysm9
Copy link
Member

@open-telemetry/go-approvers how would you feel about removing the current resource.New(...label.KeyValue) *Resource and renaming this PR's NewConfig() as the replacement API, i.e., resource.New(context.Context, ...Option) (*Resource, error)? I think this will encourage the use of the detector pattern.

I think that I like this approach. The addition of resource.WithAttributes() seems like a pretty small overhead for a more flexible API that may also encourage the use of detectors.

@jmacd
Copy link
Contributor Author

jmacd commented Oct 28, 2020

I'd like this to make progress. The current ideas for this signature:

  • NewConfig
  • Configure
  • New (where existing New is renamed something else).

Another proposal is to change the signature of Detect() to support the options in this PR. Most users, then, would only call Detect().

@Aneurysm9
Copy link
Member

I think my order of preference would be:

  • New (with existing New renamed or dropped)
  • Detect
  • NewConfig
  • Configure

with the caveat that I for some reason actively dislike the last two. They just don't feel right compared with the other options.

@jmacd jmacd changed the title Add a resource.NewConfig() with functional options for (host.*, telemetry.sdk.*) resources Change resource.New() to use functional options; add builtin attributes for (host.*, telemetry.sdk.*) Oct 29, 2020
@jmacd
Copy link
Contributor Author

jmacd commented Oct 29, 2020

I applied your suggestion @Aneurysm9, renamed the new method to New() and renamed the old method to NewFromAttributes().

@MrAlias
Copy link
Contributor

MrAlias commented Oct 29, 2020

@XSAM we talked about this issue in the SIG meeting today and wanted to make sure we have consensus on this change going forward. We definitely value your opinion and want to understand if you are in strong opposition to these changes and if we can find an agreeable solution.

@XSAM
Copy link
Member

XSAM commented Oct 30, 2020

@MrAlias I misunderstand what @jmacd is proposed. After reviewing the implementation, I believe this is the right decision.

@jmacd
Copy link
Contributor Author

jmacd commented Oct 30, 2020

The only last thought I have here is maybe the new method should be NewWithAttributes() instead of NewFromAttributes(), since it's an abbreviation for New(..., WithAttributes(...), WithoutBuiltin()) that discards a nil error.

@MrAlias
Copy link
Contributor

MrAlias commented Oct 30, 2020

The only last thought I have here is maybe the new method should be NewWithAttributes() instead of NewFromAttributes(), since it's an abbreviation for New(..., WithAttributes(...), WithoutBuiltin()) that discards a nil error.

Yeah, that makes sense.

@MrAlias
Copy link
Contributor

MrAlias commented Oct 30, 2020

I think this is ready to merge. @jmacd @Aneurysm9 thoughts?

@jmacd
Copy link
Contributor Author

jmacd commented Oct 30, 2020

I think this is ready! 😀

@MrAlias MrAlias merged commit 187adeb into open-telemetry:master Oct 31, 2020
@hstan hstan mentioned this pull request Nov 2, 2020
@MrAlias MrAlias mentioned this pull request Nov 20, 2020
AzfaarQureshi pushed a commit to open-o11y/opentelemetry-go that referenced this pull request Dec 3, 2020
…es for (host.*, telemetry.sdk.*) (open-telemetry#1235)

* Add a resource.Configure() with functional options

* Add a changelog

* Add tests for builtin resources

* Rename to WithoutBuiltin

* Add new test; restore environment after tests

* Apply feedback

* Apply suggestions from code review

:heart:

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Comment edits

* Rename New, former method NewFromAttributes

* NewFromAttributes->NewWithAttributes

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
@jmacd jmacd deleted the jmacd/resource_conf branch June 18, 2021 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metrics Part of OpenTelemetry Metrics area:trace Part of OpenTelemetry tracing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SDK should populate telemetry.sdk.* resource attributes
4 participants