-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Resources in the SDK (#552)
* Add support for Resources in the SDK Add `Config` types for the push `Controller` and the `SDK`. Included with this are helper functions to configure the `ErrorHandler` and `Resource`. Add a `Resource` to the Meter `Descriptor`. The choice to add the `Resource` here (instead of say a `Record` or the `Instrument` itself) was motivated by the definition of the `Descriptor` as the way to uniquely describe a metric instrument. Update the push `Controller` and default `SDK` to pass down their configured `Resource` from instantiation to the metric instruments. * Update New SDK constructor documentation * Change NewDescriptor constructor to take opts Add DescriptorConfig and DescriptorOption to configure the metric Descriptor with the description, unit, keys, and resource. Update all function calls to NewDescriptor to use new function signature. * Apply suggestions from code review Co-Authored-By: Rahul Patel <[email protected]> * Update and add copyright notices * Update push controller creator func Pass the configured ErrorHandler for the controller to the SDK. * Update Resource integration with the SDK Add back the Resource field to the Descriptor that was moved in the last merge with master. Add a resource.Provider interface. Have the default SDK implement the new resource.Provider interface and integrate the new interface into the newSync/newAsync workflows. Now, if the SDK has a Resource defined it will be passed to all Descriptors created for the instruments it creates. * Remove nil check for metric SDK config * Fix and add test for API Options Add an `Equal` method to the Resource so it can be compared with github.com/google/go-cmp/cmp. Add additional test of the API Option unit tests to ensure WithResource correctly sets a new resource. * Move the resource.Provider interface to the API package Move the interface to where it is used. Fix spelling. * Remove errant line * Remove nil checks for the push controller config * Fix check SDK implements Resourcer * Apply suggestions from code review Co-Authored-By: Rahul Patel <[email protected]> Co-authored-by: Rahul Patel <[email protected]>
- Loading branch information
Showing
11 changed files
with
319 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,44 @@ | ||
package metric | ||
|
||
import "go.opentelemetry.io/otel/sdk/resource" | ||
|
||
// Config contains configuration for an SDK. | ||
type Config struct { | ||
// ErrorHandler is the function called when the SDK encounters an error. | ||
// | ||
// This option can be overridden after instantiation of the SDK | ||
// with the `SetErrorHandler` method. | ||
ErrorHandler ErrorHandler | ||
|
||
// Resource is the OpenTelemetry resource associated with all Meters | ||
// created by the SDK. | ||
Resource resource.Resource | ||
} | ||
|
||
// Option is the interface that applies the value to a configuration option. | ||
type Option interface { | ||
// Apply sets the Option value of a Config. | ||
Apply(*Config) | ||
} | ||
|
||
// WithErrorHandler sets the ErrorHandler configuration option of a Config. | ||
func WithErrorHandler(fn ErrorHandler) Option { | ||
return errorHandlerOption(fn) | ||
} | ||
|
||
type errorHandlerOption ErrorHandler | ||
|
||
func (o errorHandlerOption) Apply(config *Config) { | ||
config.ErrorHandler = ErrorHandler(o) | ||
} | ||
|
||
// WithResource sets the Resource configuration option of a Config. | ||
func WithResource(r resource.Resource) Option { | ||
return resourceOption(r) | ||
} | ||
|
||
type resourceOption resource.Resource | ||
|
||
func (o resourceOption) Apply(config *Config) { | ||
config.Resource = resource.Resource(o) | ||
} |
This file contains 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,47 @@ | ||
package metric | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.opentelemetry.io/otel/api/core" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
) | ||
|
||
func TestWithErrorHandler(t *testing.T) { | ||
errH, reg := func() (ErrorHandler, *error) { | ||
e := fmt.Errorf("default invalid") | ||
reg := &e | ||
return func(err error) { | ||
*reg = err | ||
}, reg | ||
}() | ||
|
||
c := &Config{} | ||
WithErrorHandler(errH).Apply(c) | ||
err1 := fmt.Errorf("error 1") | ||
c.ErrorHandler(err1) | ||
assert.EqualError(t, *reg, err1.Error()) | ||
|
||
// Ensure overwriting works. | ||
c = &Config{ErrorHandler: DefaultErrorHandler} | ||
WithErrorHandler(errH).Apply(c) | ||
err2 := fmt.Errorf("error 2") | ||
c.ErrorHandler(err2) | ||
assert.EqualError(t, *reg, err2.Error()) | ||
} | ||
|
||
func TestWithResource(t *testing.T) { | ||
r := resource.New(core.Key("A").String("a")) | ||
|
||
c := &Config{} | ||
WithResource(*r).Apply(c) | ||
assert.Equal(t, *r, c.Resource) | ||
|
||
// Ensure overwriting works. | ||
c = &Config{Resource: resource.Resource{}} | ||
WithResource(*r).Apply(c) | ||
assert.Equal(t, *r, c.Resource) | ||
} |
This file contains 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,47 @@ | ||
package push | ||
|
||
import ( | ||
sdk "go.opentelemetry.io/otel/sdk/metric" | ||
"go.opentelemetry.io/otel/sdk/resource" | ||
) | ||
|
||
// Config contains configuration for a push Controller. | ||
type Config struct { | ||
// ErrorHandler is the function called when the Controller encounters an error. | ||
// | ||
// This option can be overridden after instantiation of the Controller | ||
// with the `SetErrorHandler` method. | ||
ErrorHandler sdk.ErrorHandler | ||
|
||
// Resource is the OpenTelemetry resource associated with all Meters | ||
// created by the Controller. | ||
Resource resource.Resource | ||
} | ||
|
||
// Option is the interface that applies the value to a configuration option. | ||
type Option interface { | ||
// Apply sets the Option value of a Config. | ||
Apply(*Config) | ||
} | ||
|
||
// WithErrorHandler sets the ErrorHandler configuration option of a Config. | ||
func WithErrorHandler(fn sdk.ErrorHandler) Option { | ||
return errorHandlerOption(fn) | ||
} | ||
|
||
type errorHandlerOption sdk.ErrorHandler | ||
|
||
func (o errorHandlerOption) Apply(config *Config) { | ||
config.ErrorHandler = sdk.ErrorHandler(o) | ||
} | ||
|
||
// WithResource sets the Resource configuration option of a Config. | ||
func WithResource(r resource.Resource) Option { | ||
return resourceOption(r) | ||
} | ||
|
||
type resourceOption resource.Resource | ||
|
||
func (o resourceOption) Apply(config *Config) { | ||
config.Resource = resource.Resource(o) | ||
} |
Oops, something went wrong.