-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Change resource.New() to use functional options; add builtin attributes for (host.*, telemetry.sdk.*) #1235
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
557c162
Add a resource.Configure() with functional options
a0c5f4d
Add a changelog
f0a7298
Add tests for builtin resources
ca7311f
Rename to WithoutBuiltin
1015745
Add new test; restore environment after tests
d547ca3
Upstream
5fd94db
Apply feedback
91b6a51
Changelog
e1f4ee3
Apply suggestions from code review
jmacd 15d2465
Upstream
345557e
Comment edits
99acc3c
Merge branch 'master' into jmacd/resource_conf
jmacd c0ad125
Upstream
5788aea
Rename New, former method NewFromAttributes
0efcca6
NewFromAttributes->NewWithAttributes
3f563e5
Upstream
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package resource | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "go.opentelemetry.io/otel/label" | ||
| opentelemetry "go.opentelemetry.io/otel/sdk" | ||
| "go.opentelemetry.io/otel/semconv" | ||
| ) | ||
|
|
||
| type ( | ||
| // TelemetrySDK is a Detector that provides information about | ||
| // the OpenTelemetry SDK used. This Detector is included as a | ||
| // builtin. If these resource attributes are not wanted, use | ||
| // the WithTelemetrySDK(nil) or WithoutBuiltin() options to | ||
| // explicitly disable them. | ||
| TelemetrySDK struct{} | ||
|
|
||
| // Host is a Detector that provides information about the host | ||
| // being run on. This Detector is included as a builtin. If | ||
| // these resource attributes are not wanted, use the | ||
| // WithHost(nil) or WithoutBuiltin() options to explicitly | ||
| // disable them. | ||
| Host struct{} | ||
|
|
||
| stringDetector struct { | ||
| K label.Key | ||
| F func() (string, error) | ||
| } | ||
| ) | ||
|
|
||
| var ( | ||
| _ Detector = TelemetrySDK{} | ||
| _ Detector = Host{} | ||
| _ Detector = stringDetector{} | ||
| ) | ||
|
|
||
| // Detect returns a *Resource that describes the OpenTelemetry SDK used. | ||
| func (TelemetrySDK) Detect(context.Context) (*Resource, error) { | ||
|
jmacd marked this conversation as resolved.
|
||
| return New( | ||
| semconv.TelemetrySDKNameKey.String("opentelemetry-go"), | ||
| semconv.TelemetrySDKLanguageKey.String("go"), | ||
| semconv.TelemetrySDKVersionKey.String(opentelemetry.Version()), | ||
| ), nil | ||
| } | ||
|
|
||
| // Detect returns a *Resource that describes the host being run on. | ||
| func (Host) Detect(ctx context.Context) (*Resource, error) { | ||
|
jmacd marked this conversation as resolved.
|
||
| return StringDetector(semconv.HostNameKey, os.Hostname).Detect(ctx) | ||
| } | ||
|
|
||
| // StringDetector returns a Detector that will produce a *Resource | ||
| // containing the string as a value corresponding to k. | ||
| func StringDetector(k label.Key, f func() (string, error)) Detector { | ||
|
jmacd marked this conversation as resolved.
|
||
| return stringDetector{K: k, F: f} | ||
| } | ||
|
|
||
| // Detect implements Detector. | ||
| func (sd stringDetector) Detect(ctx context.Context) (*Resource, error) { | ||
| value, err := sd.F() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("%s: %w", string(sd.K), err) | ||
| } | ||
| return New(sd.K.String(value)), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package resource_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "go.opentelemetry.io/otel/label" | ||
| "go.opentelemetry.io/otel/sdk/resource" | ||
| ) | ||
|
|
||
| func TestBuiltinStringDetector(t *testing.T) { | ||
| E := fmt.Errorf("no K") | ||
| res, err := resource.StringDetector(label.Key("K"), func() (string, error) { | ||
| return "", E | ||
| }).Detect(context.Background()) | ||
| require.True(t, errors.Is(err, E)) | ||
| require.NotEqual(t, E, err) | ||
| require.Nil(t, res) | ||
| } | ||
|
|
||
| func TestBuiltinStringConfig(t *testing.T) { | ||
| res, err := resource.NewConfig( | ||
| context.Background(), | ||
| resource.WithoutBuiltin(), | ||
| resource.WithAttributes(label.String("A", "B")), | ||
| resource.WithDetectors(resource.StringDetector(label.Key("K"), func() (string, error) { | ||
| return "", fmt.Errorf("K-IS-MISSING") | ||
| })), | ||
| ) | ||
| require.Error(t, err) | ||
| require.Contains(t, err.Error(), "K-IS-MISSING") | ||
| require.NotNil(t, res) | ||
|
|
||
| m := map[string]string{} | ||
| for _, kv := range res.Attributes() { | ||
| m[string(kv.Key)] = kv.Value.Emit() | ||
| } | ||
| require.EqualValues(t, map[string]string{ | ||
| "A": "B", | ||
| }, m) | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.